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.
 
 
 
 

118 lines
3.8 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.active_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. @json_payloads = statuses.map { |s| [s.id, Oj.dump(event: :delete, payload: s.id.to_s)] }.to_h
  17. @activity_xml = {}
  18. # Ensure that rendered XML reflects destroyed state
  19. statuses.each do |status|
  20. status.mark_for_mass_destruction!
  21. status.destroy
  22. end
  23. # Batch by source account
  24. statuses.group_by(&:account_id).each_value do |account_statuses|
  25. account = account_statuses.first.account
  26. unpush_from_home_timelines(account, account_statuses)
  27. unpush_from_list_timelines(account, account_statuses)
  28. batch_stream_entries(account, account_statuses) if account.local?
  29. end
  30. # Cannot be batched
  31. statuses.each do |status|
  32. unpush_from_public_timelines(status)
  33. batch_salmon_slaps(status) if status.local?
  34. end
  35. Pubsubhubbub::RawDistributionWorker.push_bulk(@stream_entry_batches) { |batch| batch }
  36. NotificationWorker.push_bulk(@salmon_batches) { |batch| batch }
  37. end
  38. private
  39. def batch_stream_entries(account, statuses)
  40. statuses.each do |status|
  41. @stream_entry_batches << [build_xml(status.stream_entry), account.id]
  42. end
  43. end
  44. def unpush_from_home_timelines(account, statuses)
  45. recipients = account.followers_for_local_distribution.to_a
  46. recipients << account if account.local?
  47. recipients.each do |follower|
  48. statuses.each do |status|
  49. FeedManager.instance.unpush_from_home(follower, status)
  50. end
  51. end
  52. end
  53. def unpush_from_list_timelines(account, statuses)
  54. account.lists_for_local_distribution.select(:id, :account_id).each do |list|
  55. statuses.each do |status|
  56. FeedManager.instance.unpush_from_list(list, status)
  57. end
  58. end
  59. end
  60. def unpush_from_public_timelines(status)
  61. return unless status.public_visibility?
  62. payload = @json_payloads[status.id]
  63. redis.pipelined do
  64. redis.publish('timeline:public', payload)
  65. redis.publish('timeline:public:local', payload) if status.local?
  66. if status.media_attachments.any?
  67. redis.publish('timeline:public:media', payload)
  68. redis.publish('timeline:public:local:media', payload) if status.local?
  69. end
  70. @tags[status.id].each do |hashtag|
  71. redis.publish("timeline:hashtag:#{hashtag}", payload)
  72. redis.publish("timeline:hashtag:#{hashtag}:local", payload) if status.local?
  73. end
  74. end
  75. end
  76. def batch_salmon_slaps(status)
  77. return if @mentions[status.id].empty?
  78. recipients = @mentions[status.id].map(&:account).reject(&:local?).select(&:ostatus?).uniq(&:domain).map(&:id)
  79. recipients.each do |recipient_id|
  80. @salmon_batches << [build_xml(status.stream_entry), status.account_id, recipient_id]
  81. end
  82. end
  83. def redis
  84. Redis.current
  85. end
  86. def build_xml(stream_entry)
  87. return @activity_xml[stream_entry.id] if @activity_xml.key?(stream_entry.id)
  88. @activity_xml[stream_entry.id] = stream_entry_to_xml(stream_entry)
  89. end
  90. end