vote_service.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # frozen_string_literal: true
  2. class VoteService < BaseService
  3. include Authorization
  4. include Payloadable
  5. def call(account, poll, choices)
  6. authorize_with account, poll, :vote?
  7. @account = account
  8. @poll = poll
  9. @choices = choices
  10. @votes = []
  11. ApplicationRecord.transaction do
  12. @choices.each do |choice|
  13. @votes << @poll.votes.create!(account: @account, choice: choice)
  14. end
  15. end
  16. ActivityTracker.increment('activity:interactions')
  17. if @poll.account.local?
  18. distribute_poll!
  19. else
  20. deliver_votes!
  21. queue_final_poll_check!
  22. end
  23. end
  24. private
  25. def distribute_poll!
  26. return if @poll.hide_totals?
  27. ActivityPub::DistributePollUpdateWorker.perform_in(3.minutes, @poll.status.id)
  28. end
  29. def queue_final_poll_check!
  30. return unless @poll.expires?
  31. PollExpirationNotifyWorker.perform_at(@poll.expires_at + 5.minutes, @poll.id)
  32. end
  33. def deliver_votes!
  34. @votes.each do |vote|
  35. ActivityPub::DeliveryWorker.perform_async(
  36. build_json(vote),
  37. @account.id,
  38. @poll.account.inbox_url
  39. )
  40. end
  41. end
  42. def build_json(vote)
  43. Oj.dump(serialize_payload(vote, ActivityPub::VoteSerializer))
  44. end
  45. end