about summary refs log tree commit diff
path: root/app/models/announcement.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2020-01-26 20:07:26 +0100
committerGitHub <noreply@github.com>2020-01-26 20:07:26 +0100
commitb9d74d407673a6dbdc87c3310618b22c85358c85 (patch)
treeace3034da9a8d8973ccbbcf3532dee2f597a5364 /app/models/announcement.rb
parent408b3e2b9328f54d471deba346a182f7c8856676 (diff)
Add streaming API updates for announcements being modified or deleted (#12963)
Change `all_day` to be a visual client-side cue only

Publish immediately if `scheduled_at` is in the past

Add `published_at` and `updated_at` to announcements JSON
Diffstat (limited to 'app/models/announcement.rb')
-rw-r--r--app/models/announcement.rb21
1 files changed, 7 insertions, 14 deletions
diff --git a/app/models/announcement.rb b/app/models/announcement.rb
index 4da9f94d6..c5cf08f7c 100644
--- a/app/models/announcement.rb
+++ b/app/models/announcement.rb
@@ -16,8 +16,6 @@
 #
 
 class Announcement < ApplicationRecord
-  after_commit :queue_publish, on: :create
-
   scope :unpublished, -> { where(published: false) }
   scope :published, -> { where(published: true) }
   scope :without_muted, ->(account) { joins("LEFT OUTER JOIN announcement_mutes ON announcement_mutes.announcement_id = announcements.id AND announcement_mutes.account_id = #{account.id}").where('announcement_mutes.id IS NULL') }
@@ -31,8 +29,11 @@ class Announcement < ApplicationRecord
   validates :ends_at, presence: true, if: -> { starts_at.present? }
 
   before_validation :set_all_day
-  before_validation :set_starts_at, on: :create
-  before_validation :set_ends_at, on: :create
+  before_validation :set_published, on: :create
+
+  def published_at
+    scheduled_at || created_at
+  end
 
   def time_range?
     starts_at.present? && ends_at.present?
@@ -71,15 +72,7 @@ class Announcement < ApplicationRecord
     self.all_day = false if starts_at.blank? || ends_at.blank?
   end
 
-  def set_starts_at
-    self.starts_at = starts_at.change(hour: 0, min: 0, sec: 0) if all_day? && starts_at.present?
-  end
-
-  def set_ends_at
-    self.ends_at = ends_at.change(hour: 23, min: 59, sec: 59) if all_day? && ends_at.present?
-  end
-
-  def queue_publish
-    PublishScheduledAnnouncementWorker.perform_async(id) if scheduled_at.blank?
+  def set_published
+    self.published = true if scheduled_at.blank? || scheduled_at.past?
   end
 end