batched_remove_status_service.rb 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # frozen_string_literal: true
  2. class BatchedRemoveStatusService < BaseService
  3. include Redisable
  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 [Enumerable<Status>] statuses A preferably batched array of statuses
  10. # @param [Hash] options
  11. # @option [Boolean] :skip_side_effects
  12. def call(statuses, **options)
  13. statuses = Status.where(id: statuses.map(&:id)).includes(:account).flat_map { |status| [status] + status.reblogs.includes(:account).to_a }
  14. @mentions = statuses.each_with_object({}) { |s, h| h[s.id] = s.active_mentions.includes(:account).to_a }
  15. @tags = statuses.each_with_object({}) { |s, h| h[s.id] = s.tags.pluck(:name) }
  16. @json_payloads = statuses.each_with_object({}) { |s, h| h[s.id] = Oj.dump(event: :delete, payload: s.id.to_s) }
  17. # Ensure that rendered XML reflects destroyed state
  18. statuses.each do |status|
  19. status.mark_for_mass_destruction!
  20. status.destroy
  21. end
  22. return if options[:skip_side_effects]
  23. # Batch by source account
  24. statuses.group_by(&:account_id).each_value do |account_statuses|
  25. account = account_statuses.first.account
  26. next unless account
  27. unpush_from_home_timelines(account, account_statuses)
  28. unpush_from_list_timelines(account, account_statuses)
  29. end
  30. # Cannot be batched
  31. statuses.each do |status|
  32. unpush_from_public_timelines(status)
  33. end
  34. end
  35. private
  36. def unpush_from_home_timelines(account, statuses)
  37. recipients = account.followers_for_local_distribution.to_a
  38. recipients << account if account.local?
  39. recipients.each do |follower|
  40. statuses.each do |status|
  41. FeedManager.instance.unpush_from_home(follower, status)
  42. end
  43. end
  44. end
  45. def unpush_from_list_timelines(account, statuses)
  46. account.lists_for_local_distribution.select(:id, :account_id).each do |list|
  47. statuses.each do |status|
  48. FeedManager.instance.unpush_from_list(list, status)
  49. end
  50. end
  51. end
  52. def unpush_from_public_timelines(status)
  53. return unless status.public_visibility?
  54. payload = @json_payloads[status.id]
  55. redis.pipelined do
  56. redis.publish('timeline:public', payload)
  57. if status.local?
  58. redis.publish('timeline:public:local', payload)
  59. else
  60. redis.publish('timeline:public:remote', payload)
  61. end
  62. if status.media_attachments.any?
  63. redis.publish('timeline:public:media', payload)
  64. if status.local?
  65. redis.publish('timeline:public:local:media', payload)
  66. else
  67. redis.publish('timeline:public:remote:media', payload)
  68. end
  69. end
  70. @tags[status.id].each do |hashtag|
  71. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}", payload)
  72. redis.publish("timeline:hashtag:#{hashtag.mb_chars.downcase}:local", payload) if status.local?
  73. end
  74. end
  75. end
  76. end