batched_remove_status_service.rb 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 [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. redis.publish('timeline:public:local', payload) if status.local?
  58. if status.media_attachments.any?
  59. redis.publish('timeline:public:media', payload)
  60. redis.publish('timeline:public:local:media', payload) if status.local?
  61. end
  62. @tags[status.id].each do |hashtag|
  63. redis.publish("timeline:hashtag:#{hashtag}", payload)
  64. redis.publish("timeline:hashtag:#{hashtag}:local", payload) if status.local?
  65. end
  66. end
  67. end
  68. end