feed_manager.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. # frozen_string_literal: true
  2. require 'singleton'
  3. class FeedManager
  4. include Singleton
  5. MAX_ITEMS = 400
  6. # Must be <= MAX_ITEMS or the tracking sets will grow forever
  7. REBLOG_FALLOFF = 40
  8. def key(type, id, subtype = nil)
  9. return "feed:#{type}:#{id}" unless subtype
  10. "feed:#{type}:#{id}:#{subtype}"
  11. end
  12. def filter?(timeline_type, status, receiver_id)
  13. if timeline_type == :home
  14. filter_from_home?(status, receiver_id)
  15. elsif timeline_type == :mentions
  16. filter_from_mentions?(status, receiver_id)
  17. else
  18. false
  19. end
  20. end
  21. def push_to_home(account, status)
  22. return false unless add_to_feed(:home, account.id, status, account.user&.aggregates_reblogs?)
  23. trim(:home, account.id)
  24. PushUpdateWorker.perform_async(account.id, status.id, "timeline:#{account.id}") if push_update_required?("timeline:#{account.id}")
  25. true
  26. end
  27. def unpush_from_home(account, status)
  28. return false unless remove_from_feed(:home, account.id, status)
  29. Redis.current.publish("timeline:#{account.id}", Oj.dump(event: :delete, payload: status.id.to_s))
  30. true
  31. end
  32. def push_to_list(list, status)
  33. if status.reply? && status.in_reply_to_account_id != status.account_id
  34. should_filter = status.in_reply_to_account_id != list.account_id
  35. should_filter &&= !ListAccount.where(list_id: list.id, account_id: status.in_reply_to_account_id).exists?
  36. return false if should_filter
  37. end
  38. return false unless add_to_feed(:list, list.id, status, list.account.user&.aggregates_reblogs?)
  39. trim(:list, list.id)
  40. PushUpdateWorker.perform_async(list.account_id, status.id, "timeline:list:#{list.id}") if push_update_required?("timeline:list:#{list.id}")
  41. true
  42. end
  43. def unpush_from_list(list, status)
  44. return false unless remove_from_feed(:list, list.id, status)
  45. Redis.current.publish("timeline:list:#{list.id}", Oj.dump(event: :delete, payload: status.id.to_s))
  46. true
  47. end
  48. def trim(type, account_id)
  49. timeline_key = key(type, account_id)
  50. reblog_key = key(type, account_id, 'reblogs')
  51. # Remove any items past the MAX_ITEMS'th entry in our feed
  52. redis.zremrangebyrank(timeline_key, '0', (-(FeedManager::MAX_ITEMS + 1)).to_s)
  53. # Get the score of the REBLOG_FALLOFF'th item in our feed, and stop
  54. # tracking anything after it for deduplication purposes.
  55. falloff_rank = FeedManager::REBLOG_FALLOFF - 1
  56. falloff_range = redis.zrevrange(timeline_key, falloff_rank, falloff_rank, with_scores: true)
  57. falloff_score = falloff_range&.first&.last&.to_i || 0
  58. # Get any reblogs we might have to clean up after.
  59. redis.zrangebyscore(reblog_key, 0, falloff_score).each do |reblogged_id|
  60. # Remove it from the set of reblogs we're tracking *first* to avoid races.
  61. redis.zrem(reblog_key, reblogged_id)
  62. # Just drop any set we might have created to track additional reblogs.
  63. # This means that if this reblog is deleted, we won't automatically insert
  64. # another reblog, but also that any new reblog can be inserted into the
  65. # feed.
  66. redis.del(key(type, account_id, "reblogs:#{reblogged_id}"))
  67. end
  68. end
  69. def merge_into_timeline(from_account, into_account)
  70. timeline_key = key(:home, into_account.id)
  71. query = from_account.statuses.limit(FeedManager::MAX_ITEMS / 4)
  72. if redis.zcard(timeline_key) >= FeedManager::MAX_ITEMS / 4
  73. oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true)&.first&.last&.to_i || 0
  74. query = query.where('id > ?', oldest_home_score)
  75. end
  76. query.each do |status|
  77. next if status.direct_visibility? || status.limited_visibility? || filter?(:home, status, into_account)
  78. add_to_feed(:home, into_account.id, status, into_account.user&.aggregates_reblogs?)
  79. end
  80. trim(:home, into_account.id)
  81. end
  82. def unmerge_from_timeline(from_account, into_account)
  83. timeline_key = key(:home, into_account.id)
  84. oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true)&.first&.last&.to_i || 0
  85. from_account.statuses.select('id, reblog_of_id').where('id > ?', oldest_home_score).reorder(nil).find_each do |status|
  86. remove_from_feed(:home, into_account.id, status)
  87. end
  88. end
  89. def clear_from_timeline(account, target_account)
  90. timeline_key = key(:home, account.id)
  91. timeline_status_ids = redis.zrange(timeline_key, 0, -1)
  92. target_statuses = Status.where(id: timeline_status_ids, account: target_account)
  93. target_statuses.each do |status|
  94. unpush_from_home(account, status)
  95. end
  96. end
  97. def populate_feed(account)
  98. added = 0
  99. limit = FeedManager::MAX_ITEMS / 2
  100. max_id = nil
  101. loop do
  102. statuses = Status.as_home_timeline(account)
  103. .paginate_by_max_id(limit, max_id)
  104. break if statuses.empty?
  105. statuses.each do |status|
  106. next if filter_from_home?(status, account)
  107. added += 1 if add_to_feed(:home, account.id, status, account.user&.aggregates_reblogs?)
  108. end
  109. break unless added.zero?
  110. max_id = statuses.last.id
  111. end
  112. end
  113. private
  114. def redis
  115. Redis.current
  116. end
  117. def push_update_required?(timeline_id)
  118. redis.exists("subscribed:#{timeline_id}")
  119. end
  120. def blocks_or_mutes?(receiver_id, account_ids, context)
  121. Block.where(account_id: receiver_id, target_account_id: account_ids).any? ||
  122. (context == :home ? Mute.where(account_id: receiver_id, target_account_id: account_ids).any? : Mute.where(account_id: receiver_id, target_account_id: account_ids, hide_notifications: true).any?)
  123. end
  124. def filter_from_home?(status, receiver_id)
  125. return false if receiver_id == status.account_id
  126. return true if status.reply? && (status.in_reply_to_id.nil? || status.in_reply_to_account_id.nil?)
  127. return true if phrase_filtered?(status, receiver_id, :home)
  128. check_for_blocks = status.active_mentions.pluck(:account_id)
  129. check_for_blocks.concat([status.account_id])
  130. if status.reblog?
  131. check_for_blocks.concat([status.reblog.account_id])
  132. check_for_blocks.concat(status.reblog.active_mentions.pluck(:account_id))
  133. end
  134. return true if blocks_or_mutes?(receiver_id, check_for_blocks, :home)
  135. if status.reply? && !status.in_reply_to_account_id.nil? # Filter out if it's a reply
  136. 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
  137. should_filter &&= receiver_id != status.in_reply_to_account_id # and it's not a reply to me
  138. should_filter &&= status.account_id != status.in_reply_to_account_id # and it's not a self-reply
  139. return should_filter
  140. elsif status.reblog? # Filter out a reblog
  141. should_filter = Follow.where(account_id: receiver_id, target_account_id: status.account_id, show_reblogs: false).exists? # if the reblogger's reblogs are suppressed
  142. 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
  143. should_filter ||= AccountDomainBlock.where(account_id: receiver_id, domain: status.reblog.account.domain).exists? # or the author's domain is blocked
  144. return should_filter
  145. end
  146. false
  147. end
  148. def filter_from_mentions?(status, receiver_id)
  149. return true if receiver_id == status.account_id
  150. return true if phrase_filtered?(status, receiver_id, :notifications)
  151. # This filter is called from NotifyService, but already after the sender of
  152. # the notification has been checked for mute/block. Therefore, it's not
  153. # necessary to check the author of the toot for mute/block again
  154. check_for_blocks = status.active_mentions.pluck(:account_id)
  155. check_for_blocks.concat([status.in_reply_to_account]) if status.reply? && !status.in_reply_to_account_id.nil?
  156. should_filter = blocks_or_mutes?(receiver_id, check_for_blocks, :mentions) # Filter if it's from someone I blocked, in reply to someone I blocked, or mentioning someone I blocked (or muted)
  157. 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
  158. should_filter
  159. end
  160. def phrase_filtered?(status, receiver_id, context)
  161. active_filters = Rails.cache.fetch("filters:#{receiver_id}") { CustomFilter.where(account_id: receiver_id).active_irreversible.to_a }.to_a
  162. active_filters.select! { |filter| filter.context.include?(context.to_s) && !filter.expired? }
  163. active_filters.map! do |filter|
  164. if filter.whole_word
  165. sb = filter.phrase =~ /\A[[:word:]]/ ? '\b' : ''
  166. eb = filter.phrase =~ /[[:word:]]\z/ ? '\b' : ''
  167. /(?mix:#{sb}#{Regexp.escape(filter.phrase)}#{eb})/
  168. else
  169. /#{Regexp.escape(filter.phrase)}/i
  170. end
  171. end
  172. return false if active_filters.empty?
  173. combined_regex = active_filters.reduce { |memo, obj| Regexp.union(memo, obj) }
  174. status = status.reblog if status.reblog?
  175. !combined_regex.match(Formatter.instance.plaintext(status)).nil? ||
  176. (status.spoiler_text.present? && !combined_regex.match(status.spoiler_text).nil?)
  177. end
  178. # Adds a status to an account's feed, returning true if a status was
  179. # added, and false if it was not added to the feed. Note that this is
  180. # an internal helper: callers must call trim or push updates if
  181. # either action is appropriate.
  182. def add_to_feed(timeline_type, account_id, status, aggregate_reblogs = true)
  183. timeline_key = key(timeline_type, account_id)
  184. reblog_key = key(timeline_type, account_id, 'reblogs')
  185. if status.reblog? && (aggregate_reblogs.nil? || aggregate_reblogs)
  186. # If the original status or a reblog of it is within
  187. # REBLOG_FALLOFF statuses from the top, do not re-insert it into
  188. # the feed
  189. rank = redis.zrevrank(timeline_key, status.reblog_of_id)
  190. return false if !rank.nil? && rank < FeedManager::REBLOG_FALLOFF
  191. reblog_rank = redis.zrevrank(reblog_key, status.reblog_of_id)
  192. if reblog_rank.nil?
  193. # This is not something we've already seen reblogged, so we
  194. # can just add it to the feed (and note that we're
  195. # reblogging it).
  196. redis.zadd(timeline_key, status.id, status.id)
  197. redis.zadd(reblog_key, status.id, status.reblog_of_id)
  198. else
  199. # Another reblog of the same status was already in the
  200. # REBLOG_FALLOFF most recent statuses, so we note that this
  201. # is an "extra" reblog, by storing it in reblog_set_key.
  202. reblog_set_key = key(timeline_type, account_id, "reblogs:#{status.reblog_of_id}")
  203. redis.sadd(reblog_set_key, status.id)
  204. return false
  205. end
  206. else
  207. # A reblog may reach earlier than the original status because of the
  208. # delay of the worker deliverying the original status, the late addition
  209. # by merging timelines, and other reasons.
  210. # If such a reblog already exists, just do not re-insert it into the feed.
  211. rank = redis.zrevrank(reblog_key, status.id)
  212. return false unless rank.nil?
  213. redis.zadd(timeline_key, status.id, status.id)
  214. end
  215. true
  216. end
  217. # Removes an individual status from a feed, correctly handling cases
  218. # with reblogs, and returning true if a status was removed. As with
  219. # `add_to_feed`, this does not trigger push updates, so callers must
  220. # do so if appropriate.
  221. def remove_from_feed(timeline_type, account_id, status)
  222. timeline_key = key(timeline_type, account_id)
  223. if status.reblog?
  224. # 1. If the reblogging status is not in the feed, stop.
  225. status_rank = redis.zrevrank(timeline_key, status.id)
  226. return false if status_rank.nil?
  227. # 2. Remove reblog from set of this status's reblogs.
  228. reblog_set_key = key(timeline_type, account_id, "reblogs:#{status.reblog_of_id}")
  229. redis.srem(reblog_set_key, status.id)
  230. # 3. Re-insert another reblog or original into the feed if one
  231. # remains in the set. We could pick a random element, but this
  232. # set should generally be small, and it seems ideal to show the
  233. # oldest potential such reblog.
  234. other_reblog = redis.smembers(reblog_set_key).map(&:to_i).min
  235. redis.zadd(timeline_key, other_reblog, other_reblog) if other_reblog
  236. # 4. Remove the reblogging status from the feed (as normal)
  237. # (outside conditional)
  238. else
  239. # If the original is getting deleted, no use for reblog references
  240. redis.del(key(timeline_type, account_id, "reblogs:#{status.id}"))
  241. end
  242. redis.zrem(timeline_key, status.id)
  243. end
  244. end