UX: events are now managed through md markup

UX: events are now managed through md markup

diff --git a/app/controllers/discourse_post_event/events_controller.rb b/app/controllers/discourse_post_event/events_controller.rb
index 6671e69..fd6ef0c 100644
--- a/app/controllers/discourse_post_event/events_controller.rb
+++ b/app/controllers/discourse_post_event/events_controller.rb
@@ -55,21 +55,7 @@ module DiscoursePostEvent
         event = Event.find(params[:id])
         guardian.ensure_can_edit!(event.post)
         guardian.ensure_can_act_on_event!(event)
-        event.enforce_utc!(event_params)
-
-        case event_params[:status].to_i
-        when Event.statuses[:private]
-          raw_invitees = Array(event_params[:raw_invitees])
-          event.update!(event_params.merge(raw_invitees: raw_invitees))
-          event.enforce_raw_invitees!
-        when Event.statuses[:public]
-          event.update!(event_params.merge(raw_invitees: []))
-        when Event.statuses[:standalone]
-          event.update!(event_params.merge(raw_invitees: []))
-          event.invitees.destroy_all
-        end
-
-        event.publish_update!
+        event.update_with_params!(event_params)
         serializer = EventSerializer.new(event, scope: guardian)
         render_json_dump(serializer)
       end
diff --git a/app/models/discourse_post_event/event.rb b/app/models/discourse_post_event/event.rb
index 0d21753..eee307c 100644
--- a/app/models/discourse_post_event/event.rb
+++ b/app/models/discourse_post_event/event.rb
@@ -42,8 +42,10 @@ module DiscoursePostEvent
 
     validates :starts_at, presence: true
 
+    MIN_NAME_LENGTH = 5
+    MAX_NAME_LENGTH = 30
     validates :name,
-      length: { in: 5..30 },
+      length: { in: MIN_NAME_LENGTH..MAX_NAME_LENGTH },
       unless: -> (event) { event.name.blank? }
 
     validate :raw_invitees_length
@@ -148,11 +150,11 @@ module DiscoursePostEvent
     end
 
     def enforce_utc!(params)
-      if params['starts_at'].present?
-        params['starts_at'] = Time.parse(params['starts_at']).utc
+      if params[:starts_at].present?
+        params[:starts_at] = Time.parse(params[:starts_at]).utc
       end
-      if params['ends_at'].present?
-        params['ends_at'] = Time.parse(params['ends_at']).utc
+      if params[:ends_at].present?
+        params[:ends_at] = Time.parse(params[:ends_at]).utc
       end
     end
 
@@ -171,5 +173,40 @@ module DiscoursePostEvent
     def is_expired?
       Time.now > (self.ends_at || self.starts_at || Time.now)
     end
+
+    def self.update_from_raw(post)
+      events = DiscoursePostEvent::EventParser.extract_events(post.raw)
+      if events.present?
+        event_params = events.first
+        event = post.event || Event.new(id: post.id)
+        params = {
+          name: event_params[:name] || event.name,
+          starts_at: event_params[:start] || event.starts_at,
+          ends_at: event_params[:end] || event.ends_at,
+          status: event_params[:status].present? ? Event.statuses[event_params[:status].to_sym] : event.status,
+          raw_invitees: event_params[:allowedGroups] ? event_params[:allowedGroups].split(',') : nil
+        }
+        event.enforce_utc!(params)
+        event.update_with_params!(params)
+      elsif post.event
+        post.event.destroy
+      end
+    end
+
+    def update_with_params!(params)
+      case params[:status].to_i
+      when Event.statuses[:private]
+        raw_invitees = Array(params[:raw_invitees])
+        self.update!(params.merge(raw_invitees: raw_invitees))
+        self.enforce_raw_invitees!
+      when Event.statuses[:public]
+        self.update!(params.merge(raw_invitees: []))
+      when Event.statuses[:standalone]
+        self.update!(params.merge(raw_invitees: []))
+        self.invitees.destroy_all
+      end
+
+      self.publish_update!
+    end
   end
 end
diff --git a/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6 b/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6
index 0cc6c17..43643bf 100644
--- a/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6
+++ b/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6
@@ -1,3 +1,4 @@
+import TextLib from "discourse/lib/text";
 import Group from "discourse/models/group";
 import ModalFunctionality from "discourse/mixins/modal-functionality";
 import Controller from "@ember/controller";
@@ -58,14 +59,32 @@ export default Controller.extend(ModalFunctionality, {
   @action
   destroyPostEvent() {
     bootbox.confirm(
-      I18n.t("event.ui_builder.confirm_delete"),
+      I18n.t("discourse_post_event.builder_modal.confirm_delete"),
       I18n.t("no_value"),
       I18n.t("yes_value"),
       confirmed => {
         if (confirmed) {
-          this.model.eventModel
-            .destroyRecord()
-            .then(() => this.send("closeModal"));
+          return this.store
+            .find("post", this.model.eventModel.id)
+            .then(post => {
+              const raw = post.raw;
+              const newRaw = this._removeRawEvent(raw);
+
+              if (newRaw) {
+                const props = {
+                  raw: newRaw,
+                  edit_reason: I18n.t("discourse_post_event.destroy_event")
+                };
+
+                return TextLib.cookAsync(newRaw).then(cooked => {
+                  props.cooked = cooked.string;
+                  return post
+                    .save(props)
+                    .catch(e => this.flash(extractError(e), "error"))
+                    .then(result => result && this.send("closeModal"));
+                });
+              }
+            });
         }
       }
     );
@@ -73,17 +92,93 @@ export default Controller.extend(ModalFunctionality, {
 
   @action
   createEvent() {
-    this.model.eventModel
-      .save()
-      .then(() => this.send("closeModal"))
-      .catch(e => this.flash(extractError(e), "error"));
+    if (!this.startsAt) {
+      this.send("closeModal");
+      return;
+    }
+
+    const eventParams = this._buildEventParams();
+    const markdownParams = [];
+    Object.keys(eventParams).forEach(key => {
+      markdownParams.push(`${key}="${eventParams[key]}"`);
+    });
+
+    this.toolbarEvent.addText(
+      `[wrap=event ${markdownParams.join(" ")}]\n[/wrap]`
+    );
+    this.send("closeModal");
   },
 
   @action
   updateEvent() {
-    this.model.eventModel
-      .save()
-      .then(() => this.send("closeModal"))
-      .catch(e => this.flash(extractError(e), "error"));
+    const eventParams = this._buildEventParams();
+    return this.store.find("post", this.model.eventModel.id).then(post => {
+      const raw = post.raw;
+      const newRaw = this._replaceRawEvent(eventParams, raw);
+
+      if (newRaw) {
+        const props = {
+          raw: newRaw,
+          edit_reason: I18n.t("discourse_post_event.update_event")
+        };
+
+        return TextLib.cookAsync(newRaw).then(cooked => {
+          props.cooked = cooked.string;
+          return post
+            .save(props)
+            .catch(e => this.flash(extractError(e), "error"))
+            .then(result => result && this.send("closeModal"));
+        });
+      }
+    });
+  },
+
+  _buildEventParams() {
+    const eventParams = {
+      start: this.startsAt,
+      status: this.model.eventModel.status,
+      name: this.model.eventModel.name
+    };
+
+    if (this.endsAt) {
+      eventParams.end = this.endsAt;
+    }
+
+    if (this.model.eventModel.status === "private") {
+      eventParams.allowedGroups = (
+        this.model.eventModel.raw_invitees || []
+      ).join(",");
+    }
+
+    return eventParams;
+  },
+
+  _removeRawEvent(raw) {
+    const eventRegex = new RegExp(
+      `\\[wrap=event\\s(.*?)\\]\\n\\[\\/wrap\\]`,
+      "m"
+    );
+
+    return raw.replace(eventRegex, "");
+  },
+
+  _replaceRawEvent(eventparams, raw) {
+    const eventRegex = new RegExp(`\\[wrap=event\\s(.*?)\\]`, "m");
+    const eventMatches = raw.match(eventRegex);
+

[... diff too long, it was truncated ...]

GitHub sha: d12d45d2

eslint: error 'siteSettings' is assigned a value but never used no-unused-vars

it has been fixed. :slight_smile:

1 Like