You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB

  1. # frozen_string_literal: true
  2. class PollExpirationNotifyWorker
  3. include Sidekiq::Worker
  4. sidekiq_options lock: :until_executed
  5. def perform(poll_id)
  6. @poll = Poll.find(poll_id)
  7. return if does_not_expire?
  8. requeue! && return if not_due_yet?
  9. notify_remote_voters_and_owner! if @poll.local?
  10. notify_local_voters!
  11. rescue ActiveRecord::RecordNotFound
  12. true
  13. end
  14. def self.remove_from_scheduled(poll_id)
  15. queue = Sidekiq::ScheduledSet.new
  16. queue.select { |scheduled| scheduled.klass == name && scheduled.args[0] == poll_id }.map(&:delete)
  17. end
  18. private
  19. def does_not_expire?
  20. @poll.expires_at.nil?
  21. end
  22. def not_due_yet?
  23. @poll.expires_at.present? && !@poll.expired?
  24. end
  25. def requeue!
  26. PollExpirationNotifyWorker.perform_at(@poll.expires_at + 5.minutes, @poll.id)
  27. end
  28. def notify_remote_voters_and_owner!
  29. ActivityPub::DistributePollUpdateWorker.perform_async(@poll.status.id)
  30. LocalNotificationWorker.perform_async(@poll.account_id, @poll.id, 'Poll', 'poll')
  31. end
  32. def notify_local_voters!
  33. @poll.voters.merge(Account.local).select(:id).find_in_batches do |accounts|
  34. LocalNotificationWorker.push_bulk(accounts) do |account|
  35. [account.id, @poll.id, 'Poll', 'poll']
  36. end
  37. end
  38. end
  39. end