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.

113 lines
2.4 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::Activity
  3. include JsonLdHelper
  4. def initialize(json, account)
  5. @json = json
  6. @account = account
  7. @object = @json['object']
  8. end
  9. def perform
  10. raise NotImplementedError
  11. end
  12. class << self
  13. def factory(json, account)
  14. @json = json
  15. klass&.new(json, account)
  16. end
  17. private
  18. def klass
  19. case @json['type']
  20. when 'Create'
  21. ActivityPub::Activity::Create
  22. when 'Announce'
  23. ActivityPub::Activity::Announce
  24. when 'Delete'
  25. ActivityPub::Activity::Delete
  26. when 'Follow'
  27. ActivityPub::Activity::Follow
  28. when 'Like'
  29. ActivityPub::Activity::Like
  30. when 'Block'
  31. ActivityPub::Activity::Block
  32. when 'Update'
  33. ActivityPub::Activity::Update
  34. when 'Undo'
  35. ActivityPub::Activity::Undo
  36. when 'Accept'
  37. ActivityPub::Activity::Accept
  38. when 'Reject'
  39. ActivityPub::Activity::Reject
  40. end
  41. end
  42. end
  43. protected
  44. def status_from_uri(uri)
  45. ActivityPub::TagManager.instance.uri_to_resource(uri, Status)
  46. end
  47. def account_from_uri(uri)
  48. ActivityPub::TagManager.instance.uri_to_resource(uri, Account)
  49. end
  50. def object_uri
  51. @object_uri ||= @object.is_a?(String) ? @object : @object['id']
  52. end
  53. def redis
  54. Redis.current
  55. end
  56. def distribute(status)
  57. notify_about_reblog(status) if reblog_of_local_account?(status)
  58. notify_about_mentions(status)
  59. crawl_links(status)
  60. distribute_to_followers(status)
  61. end
  62. def reblog_of_local_account?(status)
  63. status.reblog? && status.reblog.account.local?
  64. end
  65. def notify_about_reblog(status)
  66. NotifyService.new.call(status.reblog.account, status)
  67. end
  68. def notify_about_mentions(status)
  69. status.mentions.includes(:account).each do |mention|
  70. next unless mention.account.local? && audience_includes?(mention.account)
  71. NotifyService.new.call(mention.account, mention)
  72. end
  73. end
  74. def crawl_links(status)
  75. return if status.spoiler_text?
  76. LinkCrawlWorker.perform_async(status.id)
  77. end
  78. def distribute_to_followers(status)
  79. ::DistributionWorker.perform_async(status.id)
  80. end
  81. def delete_arrived_first?(uri)
  82. key = "delete_upon_arrival:#{@account.id}:#{uri}"
  83. if redis.exists(key)
  84. redis.del(key)
  85. true
  86. else
  87. false
  88. end
  89. end
  90. def delete_later!(uri)
  91. redis.setex("delete_upon_arrival:#{@account.id}:#{uri}", 6.hours.seconds, uri)
  92. end
  93. end