about summary refs log tree commit diff
path: root/app/models/announcement.rb
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2020-01-27 15:46:50 +0100
committerThibaut Girka <thib@sitedethib.com>2020-01-27 15:46:50 +0100
commitc56a504d116d62fe669d15a270133d7c78dd61f1 (patch)
tree7400f3182f2a4ebd1606c37a1a5cad4cc95e443b /app/models/announcement.rb
parent8924743349ec5ce37cd949445e071c14968ec2ec (diff)
parentc2dfd5e4e24a70fbfa02678fde4cfc6f6750deb4 (diff)
Merge branch 'master' into glitch-soc/merge-upstream
Conflicts:
- `app/serializers/rest/account_serializer.rb`:
  Upstream added code too close to glitch-soc-specific followers-hiding code.
  Ported upstream changes.
Diffstat (limited to 'app/models/announcement.rb')
-rw-r--r--app/models/announcement.rb29
1 files changed, 15 insertions, 14 deletions
diff --git a/app/models/announcement.rb b/app/models/announcement.rb
index 4da9f94d6..d99502f44 100644
--- a/app/models/announcement.rb
+++ b/app/models/announcement.rb
@@ -13,15 +13,14 @@
 #  ends_at      :datetime
 #  created_at   :datetime         not null
 #  updated_at   :datetime         not null
+#  published_at :datetime
 #
 
 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') }
-  scope :chronological, -> { order(Arel.sql('COALESCE(announcements.starts_at, announcements.scheduled_at, announcements.created_at) ASC')) }
+  scope :chronological, -> { order(Arel.sql('COALESCE(announcements.starts_at, announcements.scheduled_at, announcements.published_at, announcements.created_at) ASC')) }
 
   has_many :announcement_mutes, dependent: :destroy
   has_many :announcement_reactions, dependent: :destroy
@@ -31,8 +30,15 @@ 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 publish!
+    update!(published: true, published_at: Time.now.utc, scheduled_at: nil)
+  end
+
+  def unpublish!
+    update!(published: false, scheduled_at: nil)
+  end
 
   def time_range?
     starts_at.present? && ends_at.present?
@@ -71,15 +77,10 @@ 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 set_published
+    return unless scheduled_at.blank? || scheduled_at.past?
 
-  def queue_publish
-    PublishScheduledAnnouncementWorker.perform_async(id) if scheduled_at.blank?
+    self.published = true
+    self.published_at = Time.now.utc
   end
 end