2017-08-12 23:44:41 +01: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 08:48:59 +00:00
|
|
|
[payload, @account.id, inbox_url]
|
2017-08-12 23:44:41 +01:00
|
|
|
end
|
2018-07-13 01:16:06 +01:00
|
|
|
|
|
|
|
relay! if relayable?
|
2017-08-12 23:44:41 +01:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def skip_distribution?
|
2018-10-17 16:13:04 +01:00
|
|
|
@status.direct_visibility? || @status.limited_visibility?
|
2017-08-12 23:44:41 +01:00
|
|
|
end
|
|
|
|
|
2018-07-13 01:16:06 +01:00
|
|
|
def relayable?
|
|
|
|
@status.public_visibility?
|
|
|
|
end
|
|
|
|
|
2017-08-12 23:44:41 +01:00
|
|
|
def inboxes
|
|
|
|
@inboxes ||= @account.followers.inboxes
|
|
|
|
end
|
|
|
|
|
2017-08-26 12:47:38 +01:00
|
|
|
def signed_payload
|
2018-12-30 08:48:59 +00:00
|
|
|
Oj.dump(ActivityPub::LinkedDataSignature.new(unsigned_payload).sign!(@account))
|
2017-08-26 12:47:38 +01:00
|
|
|
end
|
|
|
|
|
2018-12-30 08:48:59 +00:00
|
|
|
def unsigned_payload
|
|
|
|
ActiveModelSerializers::SerializableResource.new(
|
2017-08-12 23:44:41 +01:00
|
|
|
@status,
|
|
|
|
serializer: ActivityPub::ActivitySerializer,
|
|
|
|
adapter: ActivityPub::Adapter
|
2017-08-26 12:47:38 +01:00
|
|
|
).as_json
|
2017-08-12 23:44:41 +01:00
|
|
|
end
|
2018-07-13 01:16:06 +01:00
|
|
|
|
2018-12-30 08:48:59 +00:00
|
|
|
def payload
|
|
|
|
@payload ||= @status.distributable? ? signed_payload : Oj.dump(unsigned_payload)
|
|
|
|
end
|
|
|
|
|
2018-07-13 01:16:06 +01:00
|
|
|
def relay!
|
|
|
|
ActivityPub::DeliveryWorker.push_bulk(Relay.enabled.pluck(:inbox_url)) do |inbox_url|
|
2018-12-30 08:48:59 +00:00
|
|
|
[payload, @account.id, inbox_url]
|
2018-07-13 01:16:06 +01:00
|
|
|
end
|
|
|
|
end
|
2017-08-12 23:44:41 +01:00
|
|
|
end
|