batched_remove_status_service.rb 3.7 KB

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