]> cat aescling's git repositories - mastodon.git/commitdiff
Fix expired announcements being re-published (#12964)
authorEugen Rochko <eugen@zeonfederated.com>
Sun, 26 Jan 2020 21:43:18 +0000 (22:43 +0100)
committerGitHub <noreply@github.com>
Sun, 26 Jan 2020 21:43:18 +0000 (22:43 +0100)
app/models/announcement.rb
app/workers/publish_scheduled_announcement_worker.rb
app/workers/scheduler/scheduled_statuses_scheduler.rb
db/migrate/20200126203551_add_published_at_to_announcements.rb [new file with mode: 0644]
db/schema.rb

index c5cf08f7c721ff6a0f48a3f331734df388c2bb12..670b24f01de72277c10a8cfa3099eef8e9433ed5 100644 (file)
 #  ends_at      :datetime
 #  created_at   :datetime         not null
 #  updated_at   :datetime         not null
+#  published_at :datetime
 #
 
 class Announcement < ApplicationRecord
   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,10 +32,6 @@ class Announcement < ApplicationRecord
   before_validation :set_all_day
   before_validation :set_published, on: :create
 
-  def published_at
-    scheduled_at || created_at
-  end
-
   def time_range?
     starts_at.present? && ends_at.present?
   end
@@ -73,6 +70,9 @@ class Announcement < ApplicationRecord
   end
 
   def set_published
-    self.published = true if scheduled_at.blank? || scheduled_at.past?
+    return unless scheduled_at.blank? || scheduled_at.past?
+
+    self.published = true
+    self.published_at = Time.now.utc
   end
 end
index 6b5898bf5741208571a2f54446e8633d1fd105dd..4fc6bef2fdcb50e8b609a1bf1cae8e067955d6d1 100644 (file)
@@ -6,7 +6,8 @@ class PublishScheduledAnnouncementWorker
 
   def perform(announcement_id)
     announcement = Announcement.find(announcement_id)
-    announcement.update(published: true)
+
+    announcement.update(published: true, published_at: Time.now.utc, scheduled_at: nil) unless announcement.published?
 
     payload = InlineRenderer.render(announcement, nil, :announcement)
     payload = Oj.dump(event: :announcement, payload: payload)
index 4262f1d01b6b30cda3b0915ed22470e2481e138d..9cfe949dec52c85031e28368ac8e4bb4dba39ac9 100644 (file)
@@ -34,7 +34,7 @@ class Scheduler::ScheduledStatusesScheduler
   end
 
   def unpublish_expired_announcements!
-    expired_announcements.in_batches.update_all(published: false)
+    expired_announcements.in_batches.update_all(published: false, scheduled_at: nil)
   end
 
   def expired_announcements
diff --git a/db/migrate/20200126203551_add_published_at_to_announcements.rb b/db/migrate/20200126203551_add_published_at_to_announcements.rb
new file mode 100644 (file)
index 0000000..d99f956
--- /dev/null
@@ -0,0 +1,5 @@
+class AddPublishedAtToAnnouncements < ActiveRecord::Migration[5.2]
+  def change
+    add_column :announcements, :published_at, :datetime
+  end
+end
index d3a2c05b313873c63ecc29607920a0e0355a64d3..b09ee0e763691a611c6cba872e063aeac43afcdf 100644 (file)
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2020_01_19_112504) do
+ActiveRecord::Schema.define(version: 2020_01_26_203551) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -228,6 +228,7 @@ ActiveRecord::Schema.define(version: 2020_01_19_112504) do
     t.datetime "ends_at"
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
+    t.datetime "published_at"
   end
 
   create_table "backups", force: :cascade do |t|