activity.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # frozen_string_literal: true
  2. class ActivityPub::Activity
  3. include JsonLdHelper
  4. include Redisable
  5. SUPPORTED_TYPES = %w(Note Question).freeze
  6. CONVERTED_TYPES = %w(Image Audio Video Article Page Event).freeze
  7. def initialize(json, account, **options)
  8. @json = json
  9. @account = account
  10. @object = @json['object']
  11. @options = options
  12. end
  13. def perform
  14. raise NotImplementedError
  15. end
  16. class << self
  17. def factory(json, account, **options)
  18. @json = json
  19. klass&.new(json, account, **options)
  20. end
  21. private
  22. def klass
  23. case @json['type']
  24. when 'Create'
  25. ActivityPub::Activity::Create
  26. when 'Announce'
  27. ActivityPub::Activity::Announce
  28. when 'Delete'
  29. ActivityPub::Activity::Delete
  30. when 'Follow'
  31. ActivityPub::Activity::Follow
  32. when 'Like'
  33. ActivityPub::Activity::Like
  34. when 'Block'
  35. ActivityPub::Activity::Block
  36. when 'Update'
  37. ActivityPub::Activity::Update
  38. when 'Undo'
  39. ActivityPub::Activity::Undo
  40. when 'Accept'
  41. ActivityPub::Activity::Accept
  42. when 'Reject'
  43. ActivityPub::Activity::Reject
  44. when 'Flag'
  45. ActivityPub::Activity::Flag
  46. when 'Add'
  47. ActivityPub::Activity::Add
  48. when 'Remove'
  49. ActivityPub::Activity::Remove
  50. when 'Move'
  51. ActivityPub::Activity::Move
  52. end
  53. end
  54. end
  55. protected
  56. def status_from_uri(uri)
  57. ActivityPub::TagManager.instance.uri_to_resource(uri, Status)
  58. end
  59. def account_from_uri(uri)
  60. ActivityPub::TagManager.instance.uri_to_resource(uri, Account)
  61. end
  62. def object_uri
  63. @object_uri ||= begin
  64. str = value_or_id(@object)
  65. if str&.start_with?('bear:')
  66. Addressable::URI.parse(str).query_values['u']
  67. else
  68. str
  69. end
  70. end
  71. end
  72. def unsupported_object_type?
  73. @object.is_a?(String) || !(supported_object_type? || converted_object_type?)
  74. end
  75. def supported_object_type?
  76. equals_or_includes_any?(@object['type'], SUPPORTED_TYPES)
  77. end
  78. def converted_object_type?
  79. equals_or_includes_any?(@object['type'], CONVERTED_TYPES)
  80. end
  81. def distribute(status)
  82. crawl_links(status)
  83. notify_about_reblog(status) if reblog_of_local_account?(status) && !reblog_by_following_group_account?(status)
  84. notify_about_mentions(status)
  85. # Only continue if the status is supposed to have arrived in real-time.
  86. # Note that if @options[:override_timestamps] isn't set, the status
  87. # may have a lower snowflake id than other existing statuses, potentially
  88. # "hiding" it from paginated API calls
  89. return unless @options[:override_timestamps] || status.within_realtime_window?
  90. distribute_to_followers(status)
  91. end
  92. def reblog_of_local_account?(status)
  93. status.reblog? && status.reblog.account.local?
  94. end
  95. def reblog_by_following_group_account?(status)
  96. status.reblog? && status.account.group? && status.reblog.account.following?(status.account)
  97. end
  98. def notify_about_reblog(status)
  99. NotifyService.new.call(status.reblog.account, :reblog, status)
  100. end
  101. def notify_about_mentions(status)
  102. status.active_mentions.includes(:account).each do |mention|
  103. next unless mention.account.local? && audience_includes?(mention.account)
  104. NotifyService.new.call(mention.account, :mention, mention)
  105. end
  106. end
  107. def crawl_links(status)
  108. return if status.spoiler_text?
  109. # Spread out crawling randomly to avoid DDoSing the link
  110. LinkCrawlWorker.perform_in(rand(1..59).seconds, status.id)
  111. end
  112. def distribute_to_followers(status)
  113. ::DistributionWorker.perform_async(status.id)
  114. end
  115. def delete_arrived_first?(uri)
  116. redis.exists?("delete_upon_arrival:#{@account.id}:#{uri}")
  117. end
  118. def delete_later!(uri)
  119. redis.setex("delete_upon_arrival:#{@account.id}:#{uri}", 6.hours.seconds, uri)
  120. end
  121. def status_from_object
  122. # If the status is already known, return it
  123. status = status_from_uri(object_uri)
  124. return status unless status.nil?
  125. # If the boosted toot is embedded and it is a self-boost, handle it like a Create
  126. unless unsupported_object_type?
  127. actor_id = value_or_id(first_of_value(@object['attributedTo']))
  128. if actor_id == @account.uri
  129. return ActivityPub::Activity.factory({ 'type' => 'Create', 'actor' => actor_id, 'object' => @object }, @account).perform
  130. end
  131. end
  132. fetch_remote_original_status
  133. end
  134. def dereference_object!
  135. return unless @object.is_a?(String)
  136. dereferencer = ActivityPub::Dereferencer.new(@object, permitted_origin: @account.uri, signature_account: signed_fetch_account)
  137. @object = dereferencer.object unless dereferencer.object.nil?
  138. end
  139. def signed_fetch_account
  140. return Account.find(@options[:delivered_to_account_id]) if @options[:delivered_to_account_id].present?
  141. first_mentioned_local_account || first_local_follower
  142. end
  143. def first_mentioned_local_account
  144. audience = (as_array(@json['to']) + as_array(@json['cc'])).map { |x| value_or_id(x) }.uniq
  145. local_usernames = audience.select { |uri| ActivityPub::TagManager.instance.local_uri?(uri) }
  146. .map { |uri| ActivityPub::TagManager.instance.uri_to_local_id(uri, :username) }
  147. return if local_usernames.empty?
  148. Account.local.where(username: local_usernames).first
  149. end
  150. def first_local_follower
  151. @account.followers.local.first
  152. end
  153. def follow_request_from_object
  154. @follow_request ||= FollowRequest.find_by(target_account: @account, uri: object_uri) unless object_uri.nil?
  155. end
  156. def follow_from_object
  157. @follow ||= ::Follow.find_by(target_account: @account, uri: object_uri) unless object_uri.nil?
  158. end
  159. def fetch_remote_original_status
  160. if object_uri.start_with?('http')
  161. return if ActivityPub::TagManager.instance.local_uri?(object_uri)
  162. ActivityPub::FetchRemoteStatusService.new.call(object_uri, id: true, on_behalf_of: @account.followers.local.first)
  163. elsif @object['url'].present?
  164. ::FetchRemoteStatusService.new.call(@object['url'])
  165. end
  166. end
  167. def lock_or_return(key, expire_after = 7.days.seconds)
  168. yield if redis.set(key, true, nx: true, ex: expire_after)
  169. ensure
  170. redis.del(key)
  171. end
  172. def fetch?
  173. !@options[:delivery]
  174. end
  175. def followed_by_local_accounts?
  176. @account.passive_relationships.exists? || @options[:relayed_through_account]&.passive_relationships&.exists?
  177. end
  178. def requested_through_relay?
  179. @options[:relayed_through_account] && Relay.find_by(inbox_url: @options[:relayed_through_account].inbox_url)&.enabled?
  180. end
  181. def reject_payload!
  182. Rails.logger.info("Rejected #{@json['type']} activity #{@json['id']} from #{@account.uri}#{@options[:relayed_through_account] && "via #{@options[:relayed_through_account].uri}"}")
  183. nil
  184. end
  185. end