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.

187 lines
5.0 KiB

  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 Video Article Page).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 ||= value_or_id(@object)
  64. end
  65. def unsupported_object_type?
  66. @object.is_a?(String) || !(supported_object_type? || converted_object_type?)
  67. end
  68. def supported_object_type?
  69. equals_or_includes_any?(@object['type'], SUPPORTED_TYPES)
  70. end
  71. def converted_object_type?
  72. equals_or_includes_any?(@object['type'], CONVERTED_TYPES)
  73. end
  74. def distribute(status)
  75. crawl_links(status)
  76. notify_about_reblog(status) if reblog_of_local_account?(status)
  77. notify_about_mentions(status)
  78. # Only continue if the status is supposed to have arrived in real-time.
  79. # Note that if @options[:override_timestamps] isn't set, the status
  80. # may have a lower snowflake id than other existing statuses, potentially
  81. # "hiding" it from paginated API calls
  82. return unless @options[:override_timestamps] || status.within_realtime_window?
  83. distribute_to_followers(status)
  84. end
  85. def reblog_of_local_account?(status)
  86. status.reblog? && status.reblog.account.local?
  87. end
  88. def notify_about_reblog(status)
  89. NotifyService.new.call(status.reblog.account, status)
  90. end
  91. def notify_about_mentions(status)
  92. status.active_mentions.includes(:account).each do |mention|
  93. next unless mention.account.local? && audience_includes?(mention.account)
  94. NotifyService.new.call(mention.account, mention)
  95. end
  96. end
  97. def crawl_links(status)
  98. return if status.spoiler_text?
  99. # Spread out crawling randomly to avoid DDoSing the link
  100. LinkCrawlWorker.perform_in(rand(1..59).seconds, status.id)
  101. end
  102. def distribute_to_followers(status)
  103. ::DistributionWorker.perform_async(status.id)
  104. end
  105. def delete_arrived_first?(uri)
  106. redis.exists("delete_upon_arrival:#{@account.id}:#{uri}")
  107. end
  108. def delete_later!(uri)
  109. redis.setex("delete_upon_arrival:#{@account.id}:#{uri}", 6.hours.seconds, uri)
  110. end
  111. def status_from_object
  112. # If the status is already known, return it
  113. status = status_from_uri(object_uri)
  114. return status unless status.nil?
  115. # If the boosted toot is embedded and it is a self-boost, handle it like a Create
  116. unless unsupported_object_type?
  117. actor_id = value_or_id(first_of_value(@object['attributedTo'])) || @account.uri
  118. if actor_id == @account.uri
  119. return ActivityPub::Activity.factory({ 'type' => 'Create', 'actor' => actor_id, 'object' => @object }, @account).perform
  120. end
  121. end
  122. fetch_remote_original_status
  123. end
  124. def fetch_remote_original_status
  125. if object_uri.start_with?('http')
  126. return if ActivityPub::TagManager.instance.local_uri?(object_uri)
  127. ActivityPub::FetchRemoteStatusService.new.call(object_uri, id: true, on_behalf_of: @account.followers.local.first)
  128. elsif @object['url'].present?
  129. ::FetchRemoteStatusService.new.call(@object['url'])
  130. end
  131. end
  132. def lock_or_return(key, expire_after = 7.days.seconds)
  133. yield if redis.set(key, true, nx: true, ex: expire_after)
  134. ensure
  135. redis.del(key)
  136. end
  137. def fetch?
  138. !@options[:delivery]
  139. end
  140. def followed_by_local_accounts?
  141. @account.passive_relationships.exists?
  142. end
  143. def requested_through_relay?
  144. @options[:relayed_through_account] && Relay.find_by(inbox_url: @options[:relayed_through_account].inbox_url)&.enabled?
  145. end
  146. def reject_payload!
  147. Rails.logger.info("Rejected #{@json['type']} activity #{@json['id']} from #{@account.uri}#{@options[:relayed_through_account] && "via #{@options[:relayed_through_account].uri}"}")
  148. nil
  149. end
  150. end