FIX: custom fields are now part of markdown

FIX: custom fields are now part of markdown

diff --git a/app/controllers/discourse_post_event/events_controller.rb b/app/controllers/discourse_post_event/events_controller.rb
index 08ca1a2..d86dac5 100644
--- a/app/controllers/discourse_post_event/events_controller.rb
+++ b/app/controllers/discourse_post_event/events_controller.rb
@@ -50,21 +50,6 @@ module DiscoursePostEvent
       render json: success_json
     end
 
-    # update is only used for custom fields
-    # everything else is managed by cooking the post
-    def update
-      DistributedMutex.synchronize("discourse-post-event[event-update]") do
-        event_params[:custom_fields] = (event_params[:custom_fields] || {}).reject { |_, value| value.blank? }
-
-        event = Event.find(params[:id])
-        guardian.ensure_can_edit!(event.post)
-        guardian.ensure_can_act_on_discourse_post_event!(event)
-        event.update!(event_params)
-        serializer = EventSerializer.new(event, scope: guardian)
-        render_json_dump(serializer)
-      end
-    end
-
     def csv_bulk_invite
       require 'csv'
 
@@ -125,14 +110,6 @@ module DiscoursePostEvent
 
     private
 
-    def event_params
-      allowed_custom_fields = SiteSetting.discourse_post_event_allowed_custom_fields.split('|')
-
-      params
-        .require(:event)
-        .permit(custom_fields: allowed_custom_fields)
-    end
-
     def ics_request?
       request.format.symbol == :ics
     end
diff --git a/app/models/discourse_post_event/event.rb b/app/models/discourse_post_event/event.rb
index a62ae9b..c64e7cf 100644
--- a/app/models/discourse_post_event/event.rb
+++ b/app/models/discourse_post_event/event.rb
@@ -270,21 +270,18 @@ module DiscoursePostEvent
           ends_at: event_params[:end],
           url: event_params[:url],
           recurrence: event_params[:recurrence],
-          status:
-            if event_params[:status].present?
-              Event.statuses[event_params[:status].to_sym]
-            else
-              event.status
-            end,
+          status: event_params[:status].present? ? Event.statuses[event_params[:status].to_sym] : event.status,
           reminders: event_params[:reminders],
-          raw_invitees:
-            if event_params[:"allowed-groups"]
-              event_params[:"allowed-groups"].split(',')
-            else
-              nil
-            end
+          raw_invitees: event_params[:"allowed-groups"] ? event_params[:"allowed-groups"].split(',') : nil
         }
 
+        params[:custom_fields] = {}
+        SiteSetting.discourse_post_event_allowed_custom_fields.split("|").each do |setting|
+          if event_params[setting.to_sym].present?
+            params[:custom_fields][setting] = event_params[setting.to_sym]
+          end
+        end
+
         event.update_with_params!(params)
       elsif post.event
         post.event.destroy!
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 1e41538..6fd6580 100644
--- a/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6
+++ b/assets/javascripts/discourse/controllers/discourse-post-event-builder.js.es6
@@ -5,7 +5,6 @@ import Controller from "@ember/controller";
 import { set, action, computed } from "@ember/object";
 import { equal, gte } from "@ember/object/computed";
 import { extractError } from "discourse/lib/ajax-error";
-import { Promise } from "rsvp";
 
 import { buildParams, replaceRaw } from "../../lib/raw-event-helper";
 
@@ -163,7 +162,8 @@ export default Controller.extend(ModalFunctionality, {
     const eventParams = buildParams(
       this.startsAt,
       this.endsAt,
-      this.model.eventModel
+      this.model.eventModel,
+      this.siteSettings
     );
     const markdownParams = [];
     Object.keys(eventParams).forEach(key => {
@@ -178,41 +178,30 @@ export default Controller.extend(ModalFunctionality, {
   @action
   updateEvent() {
     return this.store.find("post", this.model.eventModel.id).then(post => {
-      const promises = [];
-
-      // custom_fields are not stored on the raw and are updated separately
-      const data = this.model.eventModel.getProperties("custom_fields");
-      promises.push(this.model.eventModel.update(data));
-
-      const updateRawPromise = new Promise(resolve => {
-        const raw = post.raw;
-        const eventParams = buildParams(
-          this.startsAt,
-          this.endsAt,
-          this.model.eventModel
-        );
-        const newRaw = replaceRaw(eventParams, raw);
-
-        if (newRaw) {
-          const props = {
-            raw: newRaw,
-            edit_reason: I18n.t("discourse_post_event.edit_reason")
-          };
-
-          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"))
-              .finally(resolve);
-          });
-        } else {
-          resolve();
-        }
-      });
+      const raw = post.raw;
+      const eventParams = buildParams(
+        this.startsAt,
+        this.endsAt,
+        this.model.eventModel,
+        this.siteSettings
+      );
 
-      return Promise.all(promises.concat(updateRawPromise));
+      const newRaw = replaceRaw(eventParams, raw);
+
+      if (newRaw) {
+        const props = {
+          raw: newRaw,
+          edit_reason: I18n.t("discourse_post_event.edit_reason")
+        };
+
+        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"));
+        });
+      }
     });
   },
 
diff --git a/assets/javascripts/discourse/templates/modal/discourse-post-event-builder.hbs b/assets/javascripts/discourse/templates/modal/discourse-post-event-builder.hbs
index 2f09342..69ecfd2 100644
--- a/assets/javascripts/discourse/templates/modal/discourse-post-event-builder.hbs
+++ b/assets/javascripts/discourse/templates/modal/discourse-post-event-builder.hbs
@@ -138,7 +138,6 @@
         }}
       {{/event-field}}
 
-      {{#if model.eventModel.custom_fields}}
       {{#if allowedCustomFields.length}}
         {{#event-field label="discourse_post_event.builder_modal.custom_fields.label"}}
           <p class="event-field-description">{{i18n "discourse_post_event.builder_modal.custom_fields.description"}}</p>
@@ -153,7 +152,6 @@
           {{/each}}
         {{/event-field}}
       {{/if}}
-      {{/if}}
     </form>
   {{/conditional-loading-section}}
 {{/d-modal-body}}
diff --git a/assets/javascripts/discourse/widgets/discourse-post-event.js.es6 b/assets/javascripts/discourse/widgets/discourse-post-event.js.es6
index 7550614..c6edfe2 100644
--- a/assets/javascripts/discourse/widgets/discourse-post-event.js.es6
+++ b/assets/javascripts/discourse/widgets/discourse-post-event.js.es6
@@ -72,7 +72,8 @@ export default createWidget("discourse-post-event", {
               moment().isBefore(startsAt)
                 ? moment().add(1, "minute")
                 : moment(),
-              eventModel
+              eventModel,
+              this.siteSettings
             );
             const newRaw = replaceRaw(eventParams, raw);
 
diff --git a/assets/javascripts/initializers/add-event-ui-builder.js.es6 b/assets/javascripts/initializers/add-event-ui-builder.js.es6
index 80dc707..ef5c0fb 100644
--- a/assets/javascripts/initializers/add-event-ui-builder.js.es6
+++ b/assets/javascripts/initializers/add-event-ui-builder.js.es6
@@ -36,6 +36,7 @@ function initializeEventBuilder(api) {
           "discourse-post-event-event"
         );
         eventModel.set("status", "public");
+        eventModel.set("custom_fields", {});
 

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

GitHub sha: ae6f7c9d

1 Like