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.

124 lines
3.5 KiB

  1. # frozen_string_literal: true
  2. class RemoveStatusService < BaseService
  3. include StreamEntryRenderer
  4. def call(status)
  5. @payload = Oj.dump(event: :delete, payload: status.id)
  6. @status = status
  7. @account = status.account
  8. @tags = status.tags.pluck(:name).to_a
  9. @mentions = status.mentions.includes(:account).to_a
  10. @reblogs = status.reblogs.to_a
  11. @stream_entry = status.stream_entry
  12. remove_from_self if status.account.local?
  13. remove_from_followers
  14. remove_reblogs
  15. remove_from_hashtags
  16. remove_from_public
  17. @status.destroy!
  18. return unless @account.local?
  19. @stream_entry = @stream_entry.reload
  20. remove_from_remote_followers
  21. remove_from_remote_affected
  22. end
  23. private
  24. def remove_from_self
  25. unpush(:home, @account, @status)
  26. end
  27. def remove_from_followers
  28. @account.followers.local.find_each do |follower|
  29. unpush(:home, follower, @status)
  30. end
  31. end
  32. def remove_from_remote_affected
  33. # People who got mentioned in the status, or who
  34. # reblogged it from someone else might not follow
  35. # the author and wouldn't normally receive the
  36. # delete notification - so here, we explicitly
  37. # send it to them
  38. target_accounts = (@mentions.map(&:account).reject(&:local?) + @reblogs.map(&:account).reject(&:local?)).uniq(&:id)
  39. # Ostatus
  40. NotificationWorker.push_bulk(target_accounts.select(&:ostatus?).uniq(&:domain)) do |target_account|
  41. [salmon_xml, @account.id, target_account.id]
  42. end
  43. # ActivityPub
  44. ActivityPub::DeliveryWorker.push_bulk(target_accounts.select(&:activitypub?).uniq(&:inbox_url)) do |inbox_url|
  45. [signed_activity_json, @account.id, inbox_url]
  46. end
  47. end
  48. def remove_from_remote_followers
  49. # OStatus
  50. Pubsubhubbub::DistributionWorker.perform_async(@stream_entry.id)
  51. # ActivityPub
  52. ActivityPub::DeliveryWorker.push_bulk(@account.followers.inboxes) do |inbox_url|
  53. [signed_activity_json, @account.id, inbox_url]
  54. end
  55. end
  56. def salmon_xml
  57. @salmon_xml ||= stream_entry_to_xml(@stream_entry)
  58. end
  59. def signed_activity_json
  60. @signed_activity_json ||= Oj.dump(ActivityPub::LinkedDataSignature.new(activity_json).sign!(@account))
  61. end
  62. def activity_json
  63. @activity_json ||= ActiveModelSerializers::SerializableResource.new(
  64. @status,
  65. serializer: ActivityPub::DeleteSerializer,
  66. adapter: ActivityPub::Adapter
  67. ).as_json
  68. end
  69. def remove_reblogs
  70. # We delete reblogs of the status before the original status,
  71. # because once original status is gone, reblogs will disappear
  72. # without us being able to do all the fancy stuff
  73. @reblogs.each do |reblog|
  74. RemoveStatusService.new.call(reblog)
  75. end
  76. end
  77. def unpush(type, receiver, status)
  78. if status.reblog? && !redis.zscore(FeedManager.instance.key(type, receiver.id), status.reblog_of_id).nil?
  79. redis.zadd(FeedManager.instance.key(type, receiver.id), status.reblog_of_id, status.reblog_of_id)
  80. else
  81. redis.zremrangebyscore(FeedManager.instance.key(type, receiver.id), status.id, status.id)
  82. end
  83. Redis.current.publish("timeline:#{receiver.id}", @payload)
  84. end
  85. def remove_from_hashtags
  86. @tags.each do |hashtag|
  87. Redis.current.publish("timeline:hashtag:#{hashtag}", @payload)
  88. Redis.current.publish("timeline:hashtag:#{hashtag}:local", @payload) if @status.local?
  89. end
  90. end
  91. def remove_from_public
  92. Redis.current.publish('timeline:public', @payload)
  93. Redis.current.publish('timeline:public:local', @payload) if @status.local?
  94. end
  95. def redis
  96. Redis.current
  97. end
  98. end