2017-08-13 00:44:41 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::DistributionWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
|
|
|
sidekiq_options queue: 'push'
|
|
|
|
|
|
|
|
def perform(status_id)
|
|
|
|
@status = Status.find(status_id)
|
|
|
|
@account = @status.account
|
|
|
|
|
|
|
|
return if skip_distribution?
|
|
|
|
|
|
|
|
ActivityPub::DeliveryWorker.push_bulk(inboxes) do |inbox_url|
|
2018-12-30 09:48:59 +01:00
|
|
|
[payload, @account.id, inbox_url]
|
2017-08-13 00:44:41 +02:00
|
|
|
end
|
2018-07-13 02:16:06 +02:00
|
|
|
|
|
|
|
relay! if relayable?
|
2017-08-13 00:44:41 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def skip_distribution?
|
2018-10-17 17:13:04 +02:00
|
|
|
@status.direct_visibility? || @status.limited_visibility?
|
2017-08-13 00:44:41 +02:00
|
|
|
end
|
|
|
|
|
2018-07-13 02:16:06 +02:00
|
|
|
def relayable?
|
|
|
|
@status.public_visibility?
|
|
|
|
end
|
|
|
|
|
2017-08-13 00:44:41 +02:00
|
|
|
def inboxes
|
2018-12-30 19:00:04 +01:00
|
|
|
# Deliver the status to all followers.
|
|
|
|
# If the status is a reply to another local status, also forward it to that
|
|
|
|
# status' authors' followers.
|
|
|
|
@inboxes ||= if @status.reply? && @status.thread.account.local? && @status.distributable?
|
|
|
|
@account.followers.or(@status.thread.account.followers).inboxes
|
|
|
|
else
|
|
|
|
@account.followers.inboxes
|
|
|
|
end
|
2017-08-13 00:44:41 +02:00
|
|
|
end
|
|
|
|
|
2017-08-26 13:47:38 +02:00
|
|
|
def signed_payload
|
2018-12-30 09:48:59 +01:00
|
|
|
Oj.dump(ActivityPub::LinkedDataSignature.new(unsigned_payload).sign!(@account))
|
2017-08-26 13:47:38 +02:00
|
|
|
end
|
|
|
|
|
2018-12-30 09:48:59 +01:00
|
|
|
def unsigned_payload
|
|
|
|
ActiveModelSerializers::SerializableResource.new(
|
2017-08-13 00:44:41 +02:00
|
|
|
@status,
|
|
|
|
serializer: ActivityPub::ActivitySerializer,
|
|
|
|
adapter: ActivityPub::Adapter
|
2017-08-26 13:47:38 +02:00
|
|
|
).as_json
|
2017-08-13 00:44:41 +02:00
|
|
|
end
|
2018-07-13 02:16:06 +02:00
|
|
|
|
2018-12-30 09:48:59 +01:00
|
|
|
def payload
|
|
|
|
@payload ||= @status.distributable? ? signed_payload : Oj.dump(unsigned_payload)
|
|
|
|
end
|
|
|
|
|
2018-07-13 02:16:06 +02:00
|
|
|
def relay!
|
|
|
|
ActivityPub::DeliveryWorker.push_bulk(Relay.enabled.pluck(:inbox_url)) do |inbox_url|
|
2018-12-30 09:48:59 +01:00
|
|
|
[payload, @account.id, inbox_url]
|
2018-07-13 02:16:06 +02:00
|
|
|
end
|
|
|
|
end
|
2017-08-13 00:44:41 +02:00
|
|
|
end
|