闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.4 KiB

  1. # frozen_string_literal: true
  2. class Scheduler::FeedCleanupScheduler
  3. include Sidekiq::Worker
  4. include Redisable
  5. sidekiq_options lock: :until_executed, retry: 0
  6. def perform
  7. clean_home_feeds!
  8. clean_list_feeds!
  9. end
  10. private
  11. def clean_home_feeds!
  12. clean_feeds!(inactive_account_ids, :home)
  13. end
  14. def clean_list_feeds!
  15. clean_feeds!(inactive_list_ids, :list)
  16. end
  17. def clean_feeds!(ids, type)
  18. reblogged_id_sets = {}
  19. redis.pipelined do
  20. ids.each do |feed_id|
  21. redis.del(feed_manager.key(type, feed_id))
  22. reblog_key = feed_manager.key(type, feed_id, 'reblogs')
  23. # We collect a future for this: we don't block while getting
  24. # it, but we can iterate over it later.
  25. reblogged_id_sets[feed_id] = redis.zrange(reblog_key, 0, -1)
  26. redis.del(reblog_key)
  27. end
  28. end
  29. # Remove all of the reblog tracking keys we just removed the
  30. # references to.
  31. redis.pipelined do
  32. reblogged_id_sets.each do |feed_id, future|
  33. future.value.each do |reblogged_id|
  34. reblog_set_key = feed_manager.key(type, feed_id, "reblogs:#{reblogged_id}")
  35. redis.del(reblog_set_key)
  36. end
  37. end
  38. end
  39. end
  40. def inactive_account_ids
  41. @inactive_account_ids ||= User.confirmed.inactive.pluck(:account_id)
  42. end
  43. def inactive_list_ids
  44. List.where(account_id: inactive_account_ids).pluck(:id)
  45. end
  46. def feed_manager
  47. FeedManager.instance
  48. end
  49. end