announce.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Announce < ActivityPub::Activity
  3. def perform
  4. return reject_payload! if delete_arrived_first?(@json['id']) || !related_to_local_activity?
  5. with_lock("announce:#{value_or_id(@object)}") do
  6. original_status = status_from_object
  7. return reject_payload! if original_status.nil? || !announceable?(original_status)
  8. return if requested_through_relay?
  9. @status = Status.find_by(account: @account, reblog: original_status)
  10. return @status unless @status.nil?
  11. @status = Status.create!(
  12. account: @account,
  13. reblog: original_status,
  14. uri: @json['id'],
  15. created_at: @json['published'],
  16. override_timestamps: @options[:override_timestamps],
  17. visibility: visibility_from_audience
  18. )
  19. Trends.register!(@status)
  20. distribute
  21. end
  22. @status
  23. end
  24. private
  25. def distribute
  26. # Notify the author of the original status if that status is local
  27. LocalNotificationWorker.perform_async(@status.reblog.account_id, @status.id, 'Status', 'reblog') if reblog_of_local_account?(@status) && !reblog_by_following_group_account?(@status)
  28. # Distribute into home and list feeds
  29. ::DistributionWorker.perform_async(@status.id) if @options[:override_timestamps] || @status.within_realtime_window?
  30. end
  31. def reblog_of_local_account?(status)
  32. status.reblog? && status.reblog.account.local?
  33. end
  34. def reblog_by_following_group_account?(status)
  35. status.reblog? && status.account.group? && status.reblog.account.following?(status.account)
  36. end
  37. def audience_to
  38. as_array(@json['to']).map { |x| value_or_id(x) }
  39. end
  40. def audience_cc
  41. as_array(@json['cc']).map { |x| value_or_id(x) }
  42. end
  43. def visibility_from_audience
  44. if audience_to.any? { |to| ActivityPub::TagManager.instance.public_collection?(to) }
  45. :public
  46. elsif audience_cc.any? { |cc| ActivityPub::TagManager.instance.public_collection?(cc) }
  47. :unlisted
  48. elsif audience_to.include?(@account.followers_url)
  49. :private
  50. else
  51. :direct
  52. end
  53. end
  54. def announceable?(status)
  55. status.account_id == @account.id || status.distributable?
  56. end
  57. def related_to_local_activity?
  58. followed_by_local_accounts? || requested_through_relay? || reblog_of_local_status?
  59. end
  60. def requested_through_relay?
  61. super || Relay.find_by(inbox_url: @account.inbox_url)&.enabled?
  62. end
  63. def reblog_of_local_status?
  64. status_from_uri(object_uri)&.account&.local?
  65. end
  66. end