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.
 
 
 
 

146 lines
4.7 KiB

  1. # frozen_string_literal: true
  2. class BatchedRemoveStatusService < BaseService
  3. include StreamEntryRenderer
  4. # Delete given statuses and reblogs of them
  5. # Dispatch PuSH updates of the deleted statuses, but only local ones
  6. # Dispatch Salmon deletes, unique per domain, of the deleted statuses, but only local ones
  7. # Remove statuses from home feeds
  8. # Push delete events to streaming API for home feeds and public feeds
  9. # @param [Status] statuses A preferably batched array of statuses
  10. def call(statuses)
  11. statuses = Status.where(id: statuses.map(&:id)).includes(:account, :stream_entry).flat_map { |status| [status] + status.reblogs.includes(:account, :stream_entry).to_a }
  12. @mentions = statuses.map { |s| [s.id, s.mentions.includes(:account).to_a] }.to_h
  13. @tags = statuses.map { |s| [s.id, s.tags.pluck(:name)] }.to_h
  14. @stream_entry_batches = []
  15. @salmon_batches = []
  16. @activity_json_batches = []
  17. @json_payloads = statuses.map { |s| [s.id, Oj.dump(event: :delete, payload: s.id.to_s)] }.to_h
  18. @activity_json = {}
  19. @activity_xml = {}
  20. # Ensure that rendered XML reflects destroyed state
  21. statuses.each(&:destroy)
  22. # Batch by source account
  23. statuses.group_by(&:account_id).each_value do |account_statuses|
  24. account = account_statuses.first.account
  25. unpush_from_home_timelines(account, account_statuses)
  26. unpush_from_list_timelines(account, account_statuses)
  27. if account.local?
  28. batch_stream_entries(account, account_statuses)
  29. batch_activity_json(account, account_statuses)
  30. end
  31. end
  32. # Cannot be batched
  33. statuses.each do |status|
  34. unpush_from_public_timelines(status)
  35. batch_salmon_slaps(status) if status.local?
  36. end
  37. Pubsubhubbub::RawDistributionWorker.push_bulk(@stream_entry_batches) { |batch| batch }
  38. NotificationWorker.push_bulk(@salmon_batches) { |batch| batch }
  39. ActivityPub::DeliveryWorker.push_bulk(@activity_json_batches) { |batch| batch }
  40. end
  41. private
  42. def batch_stream_entries(account, statuses)
  43. statuses.each do |status|
  44. @stream_entry_batches << [build_xml(status.stream_entry), account.id]
  45. end
  46. end
  47. def batch_activity_json(account, statuses)
  48. account.followers.inboxes.each do |inbox_url|
  49. statuses.each do |status|
  50. @activity_json_batches << [build_json(status), account.id, inbox_url]
  51. end
  52. end
  53. statuses.each do |status|
  54. other_recipients = (status.mentions + status.reblogs).map(&:account).reject(&:local?).select(&:activitypub?).uniq(&:id)
  55. other_recipients.each do |target_account|
  56. @activity_json_batches << [build_json(status), account.id, target_account.inbox_url]
  57. end
  58. end
  59. end
  60. def unpush_from_home_timelines(account, statuses)
  61. recipients = account.followers.local.to_a
  62. recipients << account if account.local?
  63. recipients.each do |follower|
  64. statuses.each do |status|
  65. FeedManager.instance.unpush_from_home(follower, status)
  66. end
  67. end
  68. end
  69. def unpush_from_list_timelines(account, statuses)
  70. account.lists.select(:id, :account_id).each do |list|
  71. statuses.each do |status|
  72. FeedManager.instance.unpush_from_list(list, status)
  73. end
  74. end
  75. end
  76. def unpush_from_public_timelines(status)
  77. return unless status.public_visibility?
  78. payload = @json_payloads[status.id]
  79. redis.pipelined do
  80. redis.publish('timeline:public', payload)
  81. redis.publish('timeline:public:local', payload) if status.local?
  82. @tags[status.id].each do |hashtag|
  83. redis.publish("timeline:hashtag:#{hashtag}", payload)
  84. redis.publish("timeline:hashtag:#{hashtag}:local", payload) if status.local?
  85. end
  86. end
  87. end
  88. def batch_salmon_slaps(status)
  89. return if @mentions[status.id].empty?
  90. recipients = @mentions[status.id].map(&:account).reject(&:local?).select(&:ostatus?).uniq(&:domain).map(&:id)
  91. recipients.each do |recipient_id|
  92. @salmon_batches << [build_xml(status.stream_entry), status.account_id, recipient_id]
  93. end
  94. end
  95. def redis
  96. Redis.current
  97. end
  98. def build_json(status)
  99. return @activity_json[status.id] if @activity_json.key?(status.id)
  100. @activity_json[status.id] = sign_json(status, ActiveModelSerializers::SerializableResource.new(
  101. status,
  102. serializer: status.reblog? ? ActivityPub::UndoAnnounceSerializer : ActivityPub::DeleteSerializer,
  103. adapter: ActivityPub::Adapter
  104. ).as_json)
  105. end
  106. def build_xml(stream_entry)
  107. return @activity_xml[stream_entry.id] if @activity_xml.key?(stream_entry.id)
  108. @activity_xml[stream_entry.id] = stream_entry_to_xml(stream_entry)
  109. end
  110. def sign_json(status, json)
  111. Oj.dump(ActivityPub::LinkedDataSignature.new(json).sign!(status.account))
  112. end
  113. end