feed_manager.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # frozen_string_literal: true
  2. require 'singleton'
  3. class FeedManager
  4. include Singleton
  5. MAX_ITEMS = 400
  6. def key(type, id)
  7. "feed:#{type}:#{id}"
  8. end
  9. def filter?(timeline_type, status, receiver_id)
  10. if timeline_type == :home
  11. filter_from_home?(status, receiver_id)
  12. elsif timeline_type == :mentions
  13. filter_from_mentions?(status, receiver_id)
  14. else
  15. false
  16. end
  17. end
  18. def push(timeline_type, account, status)
  19. timeline_key = key(timeline_type, account.id)
  20. if status.reblog?
  21. # If the original status is within 40 statuses from top, do not re-insert it into the feed
  22. rank = redis.zrevrank(timeline_key, status.reblog_of_id)
  23. return if !rank.nil? && rank < 40
  24. redis.zadd(timeline_key, status.id, status.reblog_of_id)
  25. else
  26. redis.zadd(timeline_key, status.id, status.id)
  27. trim(timeline_type, account.id)
  28. end
  29. PushUpdateWorker.perform_async(account.id, status.id) if push_update_required?(timeline_type, account.id)
  30. end
  31. def trim(type, account_id)
  32. redis.zremrangebyrank(key(type, account_id), '0', (-(FeedManager::MAX_ITEMS + 1)).to_s)
  33. end
  34. def push_update_required?(timeline_type, account_id)
  35. timeline_type != :home || redis.get("subscribed:timeline:#{account_id}").present?
  36. end
  37. def merge_into_timeline(from_account, into_account)
  38. timeline_key = key(:home, into_account.id)
  39. query = from_account.statuses.limit(FeedManager::MAX_ITEMS / 4)
  40. if redis.zcard(timeline_key) >= FeedManager::MAX_ITEMS / 4
  41. oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true)&.first&.last&.to_i || 0
  42. query = query.where('id > ?', oldest_home_score)
  43. end
  44. redis.pipelined do
  45. query.each do |status|
  46. next if status.direct_visibility? || filter?(:home, status, into_account)
  47. redis.zadd(timeline_key, status.id, status.id)
  48. end
  49. end
  50. trim(:home, into_account.id)
  51. end
  52. def unmerge_from_timeline(from_account, into_account)
  53. timeline_key = key(:home, into_account.id)
  54. oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true)&.first&.last&.to_i || 0
  55. from_account.statuses.select('id').where('id > ?', oldest_home_score).reorder(nil).find_in_batches do |statuses|
  56. redis.pipelined do
  57. statuses.each do |status|
  58. redis.zrem(timeline_key, status.id)
  59. redis.zremrangebyscore(timeline_key, status.id, status.id)
  60. end
  61. end
  62. end
  63. end
  64. def clear_from_timeline(account, target_account)
  65. timeline_key = key(:home, account.id)
  66. timeline_status_ids = redis.zrange(timeline_key, 0, -1)
  67. target_status_ids = Status.where(id: timeline_status_ids, account: target_account).ids
  68. redis.zrem(timeline_key, target_status_ids) if target_status_ids.present?
  69. end
  70. private
  71. def redis
  72. Redis.current
  73. end
  74. def filter_from_home?(status, receiver_id)
  75. return true if status.reply? && (status.in_reply_to_id.nil? || status.in_reply_to_account_id.nil?)
  76. check_for_mutes = [status.account_id]
  77. check_for_mutes.concat([status.reblog.account_id]) if status.reblog?
  78. return true if Mute.where(account_id: receiver_id, target_account_id: check_for_mutes).any?
  79. check_for_blocks = status.mentions.pluck(:account_id)
  80. check_for_blocks.concat([status.reblog.account_id]) if status.reblog?
  81. return true if Block.where(account_id: receiver_id, target_account_id: check_for_blocks).any?
  82. if status.reply? && !status.in_reply_to_account_id.nil? # Filter out if it's a reply
  83. should_filter = !Follow.where(account_id: receiver_id, target_account_id: status.in_reply_to_account_id).exists? # and I'm not following the person it's a reply to
  84. should_filter &&= receiver_id != status.in_reply_to_account_id # and it's not a reply to me
  85. should_filter &&= status.account_id != status.in_reply_to_account_id # and it's not a self-reply
  86. return should_filter
  87. elsif status.reblog? # Filter out a reblog
  88. should_filter = Block.where(account_id: status.reblog.account_id, target_account_id: receiver_id).exists? # or if the author of the reblogged status is blocking me
  89. should_filter ||= AccountDomainBlock.where(account_id: receiver_id, domain: status.reblog.account.domain).exists? # or the author's domain is blocked
  90. return should_filter
  91. end
  92. false
  93. end
  94. def filter_from_mentions?(status, receiver_id)
  95. return true if receiver_id == status.account_id
  96. check_for_blocks = [status.account_id]
  97. check_for_blocks.concat(status.mentions.pluck(:account_id))
  98. check_for_blocks.concat([status.in_reply_to_account]) if status.reply? && !status.in_reply_to_account_id.nil?
  99. should_filter = Block.where(account_id: receiver_id, target_account_id: check_for_blocks).any? # Filter if it's from someone I blocked, in reply to someone I blocked, or mentioning someone I blocked
  100. should_filter ||= (status.account.silenced? && !Follow.where(account_id: receiver_id, target_account_id: status.account_id).exists?) # of if the account is silenced and I'm not following them
  101. should_filter
  102. end
  103. end