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.

112 lines
2.5 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. end
  42. end
  43. end
  44. protected
  45. def status_from_uri(uri)
  46. ActivityPub::TagManager.instance.uri_to_resource(uri, Status)
  47. end
  48. def account_from_uri(uri)
  49. ActivityPub::TagManager.instance.uri_to_resource(uri, Account)
  50. end
  51. def object_uri
  52. @object_uri ||= value_or_id(@object)
  53. end
  54. def redis
  55. Redis.current
  56. end
  57. def distribute(status)
  58. crawl_links(status)
  59. # Only continue if the status is supposed to have
  60. # arrived in real-time
  61. return unless @options[:override_timestamps]
  62. notify_about_reblog(status) if reblog_of_local_account?(status)
  63. notify_about_mentions(status)
  64. distribute_to_followers(status)
  65. end
  66. def reblog_of_local_account?(status)
  67. status.reblog? && status.reblog.account.local?
  68. end
  69. def notify_about_reblog(status)
  70. NotifyService.new.call(status.reblog.account, status)
  71. end
  72. def notify_about_mentions(status)
  73. status.mentions.includes(:account).each do |mention|
  74. next unless mention.account.local? && audience_includes?(mention.account)
  75. NotifyService.new.call(mention.account, mention)
  76. end
  77. end
  78. def crawl_links(status)
  79. return if status.spoiler_text?
  80. LinkCrawlWorker.perform_async(status.id)
  81. end
  82. def distribute_to_followers(status)
  83. ::DistributionWorker.perform_async(status.id)
  84. end
  85. def delete_arrived_first?(uri)
  86. redis.exists("delete_upon_arrival:#{@account.id}:#{uri}")
  87. end
  88. def delete_later!(uri)
  89. redis.setex("delete_upon_arrival:#{@account.id}:#{uri}", 6.hours.seconds, uri)
  90. end
  91. end