batched_remove_status_service.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # frozen_string_literal: true
  2. class BatchedRemoveStatusService < BaseService
  3. include Redisable
  4. # Delete multiple statuses and reblogs of them as efficiently as possible
  5. # @param [Enumerable<Status>] statuses An array of statuses
  6. # @param [Hash] options
  7. # @option [Boolean] :skip_side_effects Do not modify feeds and send updates to streaming API
  8. def call(statuses, **options)
  9. ActiveRecord::Associations::Preloader.new.preload(statuses, options[:skip_side_effects] ? :reblogs : [:account, :tags, reblogs: :account])
  10. statuses_and_reblogs = statuses.flat_map { |status| [status] + status.reblogs }
  11. # The conversations for direct visibility statuses also need
  12. # to be manually updated. This part is not efficient but we
  13. # rely on direct visibility statuses being relatively rare.
  14. statuses_with_account_conversations = statuses.select(&:direct_visibility?)
  15. ActiveRecord::Associations::Preloader.new.preload(statuses_with_account_conversations, [mentions: :account])
  16. statuses_with_account_conversations.each do |status|
  17. status.send(:unlink_from_conversations)
  18. end
  19. # We do not batch all deletes into one to avoid having a long-running
  20. # transaction lock the database, but we use the delete method instead
  21. # of destroy to avoid all callbacks. We rely on foreign keys to
  22. # cascade the delete faster without loading the associations.
  23. statuses_and_reblogs.each_slice(50) { |slice| Status.where(id: slice.map(&:id)).delete_all }
  24. # Since we skipped all callbacks, we also need to manually
  25. # deindex the statuses
  26. Chewy.strategy.current.update(StatusesIndex, statuses_and_reblogs) if Chewy.enabled?
  27. return if options[:skip_side_effects]
  28. # Batch by source account
  29. statuses_and_reblogs.group_by(&:account_id).each_value do |account_statuses|
  30. account = account_statuses.first.account
  31. next unless account
  32. unpush_from_home_timelines(account, account_statuses)
  33. unpush_from_list_timelines(account, account_statuses)
  34. end
  35. # Cannot be batched
  36. @status_id_cutoff = Mastodon::Snowflake.id_at(2.weeks.ago)
  37. redis.pipelined do
  38. statuses.each do |status|
  39. unpush_from_public_timelines(status)
  40. end
  41. end
  42. end
  43. private
  44. def unpush_from_home_timelines(account, statuses)
  45. account.followers_for_local_distribution.includes(:user).reorder(nil).find_each do |follower|
  46. statuses.each do |status|
  47. FeedManager.instance.unpush_from_home(follower, status)
  48. end
  49. end
  50. end
  51. def unpush_from_list_timelines(account, statuses)
  52. account.lists_for_local_distribution.select(:id, :account_id).includes(account: :user).reorder(nil).find_each do |list|
  53. statuses.each do |status|
  54. FeedManager.instance.unpush_from_list(list, status)
  55. end
  56. end
  57. end
  58. def unpush_from_public_timelines(status)
  59. return unless status.public_visibility? && status.id > @status_id_cutoff
  60. payload = Oj.dump(event: :delete, payload: status.id.to_s)
  61. redis.publish('timeline:public', payload)
  62. redis.publish(status.local? ? 'timeline:public:local' : 'timeline:public:remote', payload)
  63. if status.media_attachments.any?
  64. redis.publish('timeline:public:media', payload)
  65. redis.publish(status.local? ? 'timeline:public:local:media' : 'timeline:public:remote:media', payload)
  66. end
  67. status.tags.map { |tag| tag.name.mb_chars.downcase }.each do |hashtag|
  68. redis.publish("timeline:hashtag:#{hashtag}", payload)
  69. redis.publish("timeline:hashtag:#{hashtag}:local", payload) if status.local?
  70. end
  71. end
  72. end