forwarder.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # frozen_string_literal: true
  2. class ActivityPub::Forwarder
  3. def initialize(account, original_json, status)
  4. @json = original_json
  5. @account = account
  6. @status = status
  7. end
  8. def forwardable?
  9. @json['signature'].present? && @status.distributable?
  10. end
  11. def forward!
  12. ActivityPub::LowPriorityDeliveryWorker.push_bulk(inboxes) do |inbox_url|
  13. [payload, signature_account_id, inbox_url]
  14. end
  15. end
  16. private
  17. def payload
  18. @payload ||= Oj.dump(@json)
  19. end
  20. def reblogged_by_account_ids
  21. @reblogged_by_account_ids ||= @status.reblogs.includes(:account).references(:account).merge(Account.local).pluck(:account_id)
  22. end
  23. def signature_account_id
  24. @signature_account_id ||= begin
  25. if in_reply_to_local?
  26. in_reply_to.account_id
  27. else
  28. reblogged_by_account_ids.first
  29. end
  30. end
  31. end
  32. def inboxes
  33. @inboxes ||= begin
  34. arr = inboxes_for_followers_of_reblogged_by_accounts
  35. arr += inboxes_for_followers_of_replied_to_account if in_reply_to_local?
  36. arr -= [@account.preferred_inbox_url]
  37. arr.uniq!
  38. arr
  39. end
  40. end
  41. def inboxes_for_followers_of_reblogged_by_accounts
  42. Account.where(id: ::Follow.where(target_account_id: reblogged_by_account_ids).select(:account_id)).inboxes
  43. end
  44. def inboxes_for_followers_of_replied_to_account
  45. in_reply_to.account.followers.inboxes
  46. end
  47. def in_reply_to
  48. @status.thread
  49. end
  50. def in_reply_to_local?
  51. @status.thread&.account&.local?
  52. end
  53. end