The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

131 lines
3.7 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.to_s)
  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_from_lists
  15. remove_from_affected
  16. remove_reblogs
  17. remove_from_hashtags
  18. remove_from_public
  19. @status.destroy!
  20. return unless @account.local?
  21. remove_from_remote_followers
  22. remove_from_remote_affected
  23. end
  24. private
  25. def remove_from_self
  26. FeedManager.instance.unpush_from_home(@account, @status)
  27. end
  28. def remove_from_followers
  29. @account.followers.local.find_each do |follower|
  30. FeedManager.instance.unpush_from_home(follower, @status)
  31. end
  32. end
  33. def remove_from_lists
  34. @account.lists.select(:id, :account_id).find_each do |list|
  35. FeedManager.instance.unpush_from_list(list, @status)
  36. end
  37. end
  38. def remove_from_affected
  39. @mentions.map(&:account).select(&:local?).each do |account|
  40. Redis.current.publish("timeline:#{account.id}", @payload)
  41. end
  42. end
  43. def remove_from_remote_affected
  44. # People who got mentioned in the status, or who
  45. # reblogged it from someone else might not follow
  46. # the author and wouldn't normally receive the
  47. # delete notification - so here, we explicitly
  48. # send it to them
  49. target_accounts = (@mentions.map(&:account).reject(&:local?) + @reblogs.map(&:account).reject(&:local?)).uniq(&:id)
  50. # Ostatus
  51. NotificationWorker.push_bulk(target_accounts.select(&:ostatus?).uniq(&:domain)) do |target_account|
  52. [salmon_xml, @account.id, target_account.id]
  53. end
  54. # ActivityPub
  55. ActivityPub::DeliveryWorker.push_bulk(target_accounts.select(&:activitypub?).uniq(&:inbox_url)) do |target_account|
  56. [signed_activity_json, @account.id, target_account.inbox_url]
  57. end
  58. end
  59. def remove_from_remote_followers
  60. # OStatus
  61. Pubsubhubbub::RawDistributionWorker.perform_async(salmon_xml, @account.id)
  62. # ActivityPub
  63. ActivityPub::DeliveryWorker.push_bulk(@account.followers.inboxes) do |inbox_url|
  64. [signed_activity_json, @account.id, inbox_url]
  65. end
  66. end
  67. def salmon_xml
  68. @salmon_xml ||= stream_entry_to_xml(@stream_entry)
  69. end
  70. def signed_activity_json
  71. @signed_activity_json ||= Oj.dump(ActivityPub::LinkedDataSignature.new(activity_json).sign!(@account))
  72. end
  73. def activity_json
  74. @activity_json ||= ActiveModelSerializers::SerializableResource.new(
  75. @status,
  76. serializer: @status.reblog? ? ActivityPub::UndoAnnounceSerializer : ActivityPub::DeleteSerializer,
  77. adapter: ActivityPub::Adapter
  78. ).as_json
  79. end
  80. def remove_reblogs
  81. # We delete reblogs of the status before the original status,
  82. # because once original status is gone, reblogs will disappear
  83. # without us being able to do all the fancy stuff
  84. @reblogs.each do |reblog|
  85. RemoveStatusService.new.call(reblog)
  86. end
  87. end
  88. def remove_from_hashtags
  89. return unless @status.public_visibility?
  90. @tags.each do |hashtag|
  91. Redis.current.publish("timeline:hashtag:#{hashtag}", @payload)
  92. Redis.current.publish("timeline:hashtag:#{hashtag}:local", @payload) if @status.local?
  93. end
  94. end
  95. def remove_from_public
  96. return unless @status.public_visibility?
  97. Redis.current.publish('timeline:public', @payload)
  98. Redis.current.publish('timeline:public:local', @payload) if @status.local?
  99. end
  100. def redis
  101. Redis.current
  102. end
  103. end