batched_remove_status_service.rb 3.5 KB

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