You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.1 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::DistributionWorker
  3. include Sidekiq::Worker
  4. sidekiq_options queue: 'push'
  5. def perform(status_id)
  6. @status = Status.find(status_id)
  7. @account = @status.account
  8. return if skip_distribution?
  9. ActivityPub::DeliveryWorker.push_bulk(inboxes) do |inbox_url|
  10. [signed_payload, @account.id, inbox_url]
  11. end
  12. relay! if relayable?
  13. rescue ActiveRecord::RecordNotFound
  14. true
  15. end
  16. private
  17. def skip_distribution?
  18. @status.direct_visibility?
  19. end
  20. def relayable?
  21. @status.public_visibility?
  22. end
  23. def inboxes
  24. @inboxes ||= @account.followers.inboxes
  25. end
  26. def signed_payload
  27. @signed_payload ||= Oj.dump(ActivityPub::LinkedDataSignature.new(payload).sign!(@account))
  28. end
  29. def payload
  30. @payload ||= ActiveModelSerializers::SerializableResource.new(
  31. @status,
  32. serializer: ActivityPub::ActivitySerializer,
  33. adapter: ActivityPub::Adapter
  34. ).as_json
  35. end
  36. def relay!
  37. ActivityPub::DeliveryWorker.push_bulk(Relay.enabled.pluck(:inbox_url)) do |inbox_url|
  38. [signed_payload, @account.id, inbox_url]
  39. end
  40. end
  41. end