activity.rb 2.6 KB

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