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