FEATURE: display event's starts_at on topic title

FEATURE: display event’s starts_at on topic title

diff --git a/app/models/discourse_post_event/event.rb b/app/models/discourse_post_event/event.rb
index c99f9aa..9953c1b 100644
--- a/app/models/discourse_post_event/event.rb
+++ b/app/models/discourse_post_event/event.rb
@@ -176,6 +176,7 @@ module DiscoursePostEvent
 
     def self.update_from_raw(post)
       events = DiscoursePostEvent::EventParser.extract_events(post)
+
       if events.present?
         event_params = events.first
         event = post.event || Event.new(id: post.id)
@@ -189,7 +190,7 @@ module DiscoursePostEvent
         event.enforce_utc!(params)
         event.update_with_params!(params)
       elsif post.event
-        post.event.destroy
+        post.event.destroy!
       end
     end
 
diff --git a/assets/javascripts/initializers/decorate-topic-title.js.es6 b/assets/javascripts/initializers/decorate-topic-title.js.es6
index e2c0af7..b5b0861 100644
--- a/assets/javascripts/initializers/decorate-topic-title.js.es6
+++ b/assets/javascripts/initializers/decorate-topic-title.js.es6
@@ -1,25 +1,15 @@
 import { withPluginApi } from "discourse/lib/plugin-api";
 import guessDateFormat from "discourse/plugins/discourse-calendar/lib/guess-best-date-format";
-import cleanTitle from "discourse/plugins/discourse-calendar/lib/clean-title";
 
 function initializeDecorateTopicTitle(api) {
   api.decorateTopicTitle((topic, node, topicTitleType) => {
-    const startsAt = topic.post_event_starts_at;
+    const startsAt = topic.event_starts_at;
     if (startsAt) {
-      const cleanedTitle = cleanTitle(node.innerText, startsAt);
-
-      if (cleanedTitle) {
-        if (topicTitleType === "topic-list-item-title") {
-          const date = moment.utc(startsAt);
-          node.innerHTML = `${node.innerText.replace(
-            cleanedTitle,
-            ""
-          )}<span class="event-date">${date
-            .tz(moment.tz.guess())
-            .format(guessDateFormat(date))}</span>`;
-        } else if (topicTitleType === "topic-title") {
-          node.innerText = node.innerText.replace(cleanedTitle, "");
-        }
+      if (topicTitleType === "topic-list-item-title") {
+        const date = moment.utc(startsAt);
+        node.innerHTML = `${node.innerText}<span class="event-date">${date
+          .tz(moment.tz.guess())
+          .format(guessDateFormat(date))}</span>`;
       }
     }
   });
diff --git a/assets/javascripts/lib/guess-best-date-format.js.es6 b/assets/javascripts/lib/guess-best-date-format.js.es6
index e085a66..e1c7e98 100644
--- a/assets/javascripts/lib/guess-best-date-format.js.es6
+++ b/assets/javascripts/lib/guess-best-date-format.js.es6
@@ -8,7 +8,7 @@ export function isNotFullDayEvent(startsAt, endsAt) {
 
 export default function guessDateFormat(startsAt, endsAt) {
   let format;
-  if (isNotFullDayEvent(startsAt, endsAt)) {
+  if (!isNotFullDayEvent(startsAt, endsAt)) {
     format = "LL";
   } else {
     format = "LLL";
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index d8d7dee..42fe4d7 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2,7 +2,8 @@ en:
   site_settings:
     calendar_enabled: "Enable the discourse-calendar plugin. This will add support for a [calendar][/calendar] tag in the first post of a topic."
     discourse_post_event_enabled: "[experimental] Enables to attach an event to a post. Note: also needs `calendar enabled` to be enabled."
-    displayed_invitees_limit: "Limit the numbers of invitees displayed on an event."
+    displayed_invitees_limit: "Limits the numbers of invitees displayed on an event."
+    display_post_event_date_on_topic_title: "Displays the date of the event after the topic title."
     discourse_post_event_allowed_on_groups: "Groups that are allowed to create events."
     holiday_calendar_topic_id: "Topic ID of staffs holiday / absence calendar."
     delete_expired_event_posts_after: "Posts with expired events will be automatically deleted after (n) hours. Set to -1 to disable deletion."
diff --git a/config/settings.yml b/config/settings.yml
index 09a3781..8802067 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -57,3 +57,6 @@ discourse_post_event:
     default: 10
     client: false
     max: 25
+  display_post_event_date_on_topic_title:
+    default: true
+    client: trye
diff --git a/plugin.rb b/plugin.rb
index d5d1f99..fc7fa1a 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -169,73 +169,73 @@ after_initialize do
     SiteSetting.discourse_post_event_enabled
   end
 
-  DiscourseEvent.on(:post_process_cooked) do |doc, post|
+  on(:post_process_cooked) do |doc, post|
     DiscoursePostEvent::Event.update_from_raw(post)
   end
 
-  DiscourseEvent.on(:post_destroyed) do |post|
+  on(:post_destroyed) do |post|
     if SiteSetting.discourse_post_event_enabled && post.event
       post.event.update!(deleted_at: Time.now)
     end
   end
 
-  DiscourseEvent.on(:post_recovered) do |post|
+  on(:post_recovered) do |post|
     if SiteSetting.discourse_post_event_enabled && post.event
       post.event.update!(deleted_at: nil)
     end
   end
 
-  DiscourseEvent.on(:post_edited) do |post, topic_changed|
-    if SiteSetting.discourse_post_event_enabled && post.event && post.is_first_post? && post.topic && topic_changed && post.topic != Archetype.private_message
-      time_range = extract_time_range(post.topic, post.user)
-
-      case time_range
-      when TimeSniffer::Interval
-        post.event.update!(
-          starts_at: time_range.from.to_time.utc,
-          ends_at: time_range.to.to_time.utc,
-        )
-      when TimeSniffer::Event
-        post.event.update!(
-          starts_at: time_range.at.to_time.utc
-        )
-      end
-
-      post.event.publish_update!
-    end
-  end
-
-  def extract_time_range(topic, user)
-    TimeSniffer.new(
-      topic.title,
-      at: topic.created_at,
-      timezone: user.user_option.timezone || 'UTC',
-      date_order: :sane,
-      matchers: [:tomorrow, :date, :time],
-    ).sniff
-  end
-
-  DiscourseEvent.on(:topic_created) do |topic, args, user|
-    if SiteSetting.discourse_post_event_enabled && topic.archetype != Archetype.private_message
-      time_range = extract_time_range(topic, user)
-
-      case time_range
-      when TimeSniffer::Interval
-        DiscoursePostEvent::Event.create!(
-          id: topic.first_post.id,
-          starts_at: time_range.from.to_time.utc,
-          ends_at: time_range.to.to_time.utc,
-          status: DiscoursePostEvent::Event.statuses[:standalone]
-        )
-      when TimeSniffer::Event
-        DiscoursePostEvent::Event.create!(
-          id: topic.first_post.id,
-          starts_at: time_range.at.to_time.utc,
-          status: DiscoursePostEvent::Event.statuses[:standalone]
-        )
-      end
-    end
-  end
+  # DiscourseEvent.on(:post_edited) do |post, topic_changed|
+  #   if SiteSetting.discourse_post_event_enabled && post.event && post.is_first_post? && post.topic && topic_changed && post.topic != Archetype.private_message
+  #     time_range = extract_time_range(post.topic, post.user)
+  #
+  #     case time_range
+  #     when TimeSniffer::Interval
+  #       post.event.update!(
+  #         starts_at: time_range.from.to_time.utc,
+  #         ends_at: time_range.to.to_time.utc,
+  #       )
+  #     when TimeSniffer::Event
+  #       post.event.update!(
+  #         starts_at: time_range.at.to_time.utc
+  #       )
+  #     end
+  #
+  #     post.event.publish_update!
+  #   end
+  # end
+  #
+  # def extract_time_range(topic, user)
+  #   TimeSniffer.new(
+  #     topic.title,
+  #     at: topic.created_at,
+  #     timezone: user.user_option.timezone || 'UTC',
+  #     date_order: :sane,
+  #     matchers: [:tomorrow, :date, :time],
+  #   ).sniff
+  # end
+  #
+  # DiscourseEvent.on(:topic_created) do |topic, args, user|
+  #   if SiteSetting.discourse_post_event_enabled && topic.archetype != Archetype.private_message
+  #     time_range = extract_time_range(topic, user)
+  #

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

GitHub sha: c4db9f25

Do we want to keep this in the plugin code base? If we remove stuff Git should have it available.

I expect to bring this back fairly quickly, but will remove it if I don’t.

1 Like