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.

130 lines
3.3 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::Activity
  3. include JsonLdHelper
  4. def initialize(json, account, **options)
  5. @json = json
  6. @account = account
  7. @object = @json['object']
  8. @options = options
  9. end
  10. def perform
  11. raise NotImplementedError
  12. end
  13. class << self
  14. def factory(json, account, **options)
  15. @json = json
  16. klass&.new(json, account, options)
  17. end
  18. private
  19. def klass
  20. case @json['type']
  21. when 'Create'
  22. ActivityPub::Activity::Create
  23. when 'Announce'
  24. ActivityPub::Activity::Announce
  25. when 'Delete'
  26. ActivityPub::Activity::Delete
  27. when 'Follow'
  28. ActivityPub::Activity::Follow
  29. when 'Like'
  30. ActivityPub::Activity::Like
  31. when 'Block'
  32. ActivityPub::Activity::Block
  33. when 'Update'
  34. ActivityPub::Activity::Update
  35. when 'Undo'
  36. ActivityPub::Activity::Undo
  37. when 'Accept'
  38. ActivityPub::Activity::Accept
  39. when 'Reject'
  40. ActivityPub::Activity::Reject
  41. when 'Flag'
  42. ActivityPub::Activity::Flag
  43. when 'Add'
  44. ActivityPub::Activity::Add
  45. when 'Remove'
  46. ActivityPub::Activity::Remove
  47. end
  48. end
  49. end
  50. protected
  51. def status_from_uri(uri)
  52. ActivityPub::TagManager.instance.uri_to_resource(uri, Status)
  53. end
  54. def account_from_uri(uri)
  55. ActivityPub::TagManager.instance.uri_to_resource(uri, Account)
  56. end
  57. def object_uri
  58. @object_uri ||= value_or_id(@object)
  59. end
  60. def redis
  61. Redis.current
  62. end
  63. def distribute(status)
  64. crawl_links(status)
  65. notify_about_reblog(status) if reblog_of_local_account?(status)
  66. notify_about_mentions(status)
  67. # Only continue if the status is supposed to have arrived in real-time.
  68. # Note that if @options[:override_timestamps] isn't set, the status
  69. # may have a lower snowflake id than other existing statuses, potentially
  70. # "hiding" it from paginated API calls
  71. return unless @options[:override_timestamps] || status.within_realtime_window?
  72. distribute_to_followers(status)
  73. end
  74. def reblog_of_local_account?(status)
  75. status.reblog? && status.reblog.account.local?
  76. end
  77. def notify_about_reblog(status)
  78. NotifyService.new.call(status.reblog.account, status)
  79. end
  80. def notify_about_mentions(status)
  81. status.mentions.includes(:account).each do |mention|
  82. next unless mention.account.local? && audience_includes?(mention.account)
  83. NotifyService.new.call(mention.account, mention)
  84. end
  85. end
  86. def crawl_links(status)
  87. return if status.spoiler_text?
  88. LinkCrawlWorker.perform_async(status.id)
  89. end
  90. def distribute_to_followers(status)
  91. ::DistributionWorker.perform_async(status.id)
  92. end
  93. def delete_arrived_first?(uri)
  94. redis.exists("delete_upon_arrival:#{@account.id}:#{uri}")
  95. end
  96. def delete_later!(uri)
  97. redis.setex("delete_upon_arrival:#{@account.id}:#{uri}", 6.hours.seconds, uri)
  98. end
  99. def fetch_remote_original_status
  100. if object_uri.start_with?('http')
  101. return if ActivityPub::TagManager.instance.local_uri?(object_uri)
  102. ActivityPub::FetchRemoteStatusService.new.call(object_uri, id: true, on_behalf_of: @account.followers.local.first)
  103. elsif @object['url'].present?
  104. ::FetchRemoteStatusService.new.call(@object['url'])
  105. end
  106. end
  107. end