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.

65 lines
1.6 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. [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? || @status.limited_visibility?
  19. end
  20. def relayable?
  21. @status.public_visibility?
  22. end
  23. def inboxes
  24. # Deliver the status to all followers.
  25. # If the status is a reply to another local status, also forward it to that
  26. # status' authors' followers.
  27. @inboxes ||= if @status.reply? && @status.thread.account.local? && @status.distributable?
  28. @account.followers.or(@status.thread.account.followers).inboxes
  29. else
  30. @account.followers.inboxes
  31. end
  32. end
  33. def signed_payload
  34. Oj.dump(ActivityPub::LinkedDataSignature.new(unsigned_payload).sign!(@account))
  35. end
  36. def unsigned_payload
  37. ActiveModelSerializers::SerializableResource.new(
  38. @status,
  39. serializer: ActivityPub::ActivitySerializer,
  40. adapter: ActivityPub::Adapter
  41. ).as_json
  42. end
  43. def payload
  44. @payload ||= @status.distributable? ? signed_payload : Oj.dump(unsigned_payload)
  45. end
  46. def relay!
  47. ActivityPub::DeliveryWorker.push_bulk(Relay.enabled.pluck(:inbox_url)) do |inbox_url|
  48. [payload, @account.id, inbox_url]
  49. end
  50. end
  51. end