logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

activity.rb (2768B)


  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
  68. # arrived in real-time
  69. return unless @options[:override_timestamps] || status.within_realtime_window?
  70. distribute_to_followers(status)
  71. end
  72. def reblog_of_local_account?(status)
  73. status.reblog? && status.reblog.account.local?
  74. end
  75. def notify_about_reblog(status)
  76. NotifyService.new.call(status.reblog.account, status)
  77. end
  78. def notify_about_mentions(status)
  79. status.mentions.includes(:account).each do |mention|
  80. next unless mention.account.local? && audience_includes?(mention.account)
  81. NotifyService.new.call(mention.account, mention)
  82. end
  83. end
  84. def crawl_links(status)
  85. return if status.spoiler_text?
  86. LinkCrawlWorker.perform_async(status.id)
  87. end
  88. def distribute_to_followers(status)
  89. ::DistributionWorker.perform_async(status.id)
  90. end
  91. def delete_arrived_first?(uri)
  92. redis.exists("delete_upon_arrival:#{@account.id}:#{uri}")
  93. end
  94. def delete_later!(uri)
  95. redis.setex("delete_upon_arrival:#{@account.id}:#{uri}", 6.hours.seconds, uri)
  96. end
  97. end