2019-03-03 21:18:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class VoteService < BaseService
|
|
|
|
include Authorization
|
2019-06-04 21:11:18 +00:00
|
|
|
include Payloadable
|
2022-04-28 15:47:34 +00:00
|
|
|
include Redisable
|
2022-05-12 22:02:35 +00:00
|
|
|
include Lockable
|
2019-03-03 21:18:23 +00:00
|
|
|
|
|
|
|
def call(account, poll, choices)
|
2022-05-26 20:06:10 +00:00
|
|
|
return if choices.empty?
|
|
|
|
|
2019-03-03 21:18:23 +00:00
|
|
|
authorize_with account, poll, :vote?
|
|
|
|
|
|
|
|
@account = account
|
|
|
|
@poll = poll
|
|
|
|
@choices = choices
|
|
|
|
@votes = []
|
|
|
|
|
2019-09-29 20:58:01 +00:00
|
|
|
already_voted = true
|
|
|
|
|
2022-05-12 22:02:35 +00:00
|
|
|
with_lock("vote:#{@poll.id}:#{@account.id}") do
|
|
|
|
already_voted = @poll.votes.where(account: @account).exists?
|
2019-09-29 20:58:01 +00:00
|
|
|
|
2022-05-12 22:02:35 +00:00
|
|
|
ApplicationRecord.transaction do
|
|
|
|
@choices.each do |choice|
|
|
|
|
@votes << @poll.votes.create!(account: @account, choice: Integer(choice))
|
2019-09-29 20:58:01 +00:00
|
|
|
end
|
2019-03-03 21:18:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-29 20:58:01 +00:00
|
|
|
increment_voters_count! unless already_voted
|
|
|
|
|
2019-03-23 13:07:17 +00:00
|
|
|
ActivityTracker.increment('activity:interactions')
|
|
|
|
|
2019-03-10 23:49:31 +00:00
|
|
|
if @poll.account.local?
|
2019-03-12 21:58:59 +00:00
|
|
|
distribute_poll!
|
2019-03-10 23:49:31 +00:00
|
|
|
else
|
2019-03-12 21:58:59 +00:00
|
|
|
deliver_votes!
|
|
|
|
queue_final_poll_check!
|
2019-03-03 21:18:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-03-12 21:58:59 +00:00
|
|
|
def distribute_poll!
|
|
|
|
return if @poll.hide_totals?
|
|
|
|
ActivityPub::DistributePollUpdateWorker.perform_in(3.minutes, @poll.status.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def queue_final_poll_check!
|
|
|
|
return unless @poll.expires?
|
|
|
|
PollExpirationNotifyWorker.perform_at(@poll.expires_at + 5.minutes, @poll.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def deliver_votes!
|
|
|
|
@votes.each do |vote|
|
|
|
|
ActivityPub::DeliveryWorker.perform_async(
|
|
|
|
build_json(vote),
|
|
|
|
@account.id,
|
|
|
|
@poll.account.inbox_url
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-03 21:18:23 +00:00
|
|
|
def build_json(vote)
|
2019-06-04 21:11:18 +00:00
|
|
|
Oj.dump(serialize_payload(vote, ActivityPub::VoteSerializer))
|
2019-03-03 21:18:23 +00:00
|
|
|
end
|
2019-09-29 20:58:01 +00:00
|
|
|
|
|
|
|
def increment_voters_count!
|
|
|
|
unless @poll.voters_count.nil?
|
|
|
|
@poll.voters_count = @poll.voters_count + 1
|
|
|
|
@poll.save
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::StaleObjectError
|
|
|
|
@poll.reload
|
|
|
|
retry
|
|
|
|
end
|
2019-03-03 21:18:23 +00:00
|
|
|
end
|