batched_remove_status_service.rb 3.5 KB

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