From 409a29befd3d0c45483c10537600df59aeb89309 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 6 Sep 2024 16:16:09 -0400 Subject: [PATCH] Use Announcement scopes in scheduled status scheduler --- app/models/announcement.rb | 4 ++++ app/workers/scheduler/scheduled_statuses_scheduler.rb | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/models/announcement.rb b/app/models/announcement.rb index 54923ed0819..a86770fd974 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -22,6 +22,10 @@ class Announcement < ApplicationRecord scope :published, -> { where(published: true) } scope :chronological, -> { order(coalesced_chronology_timestamps.asc) } scope :reverse_chronological, -> { order(coalesced_chronology_timestamps.desc) } + scope :ending_before, ->(date) { ending.where(ends_at: ..date) } + scope :ending, -> { where.not(ends_at: nil) } + scope :scheduled_before, ->(date) { scheduled.where(scheduled_at: ..date) } + scope :scheduled, -> { where.not(scheduled_at: nil) } has_many :announcement_mutes, dependent: :destroy has_many :announcement_reactions, dependent: :destroy diff --git a/app/workers/scheduler/scheduled_statuses_scheduler.rb b/app/workers/scheduler/scheduled_statuses_scheduler.rb index 4e251780de5..f5175a857d5 100644 --- a/app/workers/scheduler/scheduled_statuses_scheduler.rb +++ b/app/workers/scheduler/scheduled_statuses_scheduler.rb @@ -30,7 +30,7 @@ class Scheduler::ScheduledStatusesScheduler end def due_announcements - Announcement.unpublished.where('scheduled_at IS NOT NULL AND scheduled_at <= ?', Time.now.utc + PostStatusService::MIN_SCHEDULE_OFFSET) + Announcement.unpublished.scheduled_before(publication_scheduled_at) end def unpublish_expired_announcements! @@ -38,6 +38,10 @@ class Scheduler::ScheduledStatusesScheduler end def expired_announcements - Announcement.published.where('ends_at IS NOT NULL AND ends_at <= ?', Time.now.utc) + Announcement.published.ending_before(Time.now.utc) + end + + def publication_scheduled_at + Time.now.utc + PostStatusService::MIN_SCHEDULE_OFFSET end end