feed_manager.rb 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. # frozen_string_literal: true
  2. require 'singleton'
  3. class FeedManager
  4. include Singleton
  5. include Redisable
  6. # Maximum number of items stored in a single feed
  7. MAX_ITEMS = 800
  8. # Number of items in the feed since last reblog of status
  9. # before the new reblog will be inserted. Must be <= MAX_ITEMS
  10. # or the tracking sets will grow forever
  11. REBLOG_FALLOFF = 40
  12. # Execute block for every active account
  13. # @yield [Account]
  14. # @return [void]
  15. def with_active_accounts(&block)
  16. Account.joins(:user).merge(User.signed_in_recently).find_each(&block)
  17. end
  18. # Redis key of a feed
  19. # @param [Symbol] type
  20. # @param [Integer] id
  21. # @param [Symbol] subtype
  22. # @return [String]
  23. def key(type, id, subtype = nil)
  24. return "feed:#{type}:#{id}" unless subtype
  25. "feed:#{type}:#{id}:#{subtype}"
  26. end
  27. # Check if the status should not be added to a feed
  28. # @param [Symbol] timeline_type
  29. # @param [Status] status
  30. # @param [Account|List] receiver
  31. # @return [Boolean]
  32. def filter?(timeline_type, status, receiver)
  33. case timeline_type
  34. when :home
  35. filter_from_home?(status, receiver.id, build_crutches(receiver.id, [status]), :home)
  36. when :list
  37. filter_from_list?(status, receiver) || filter_from_home?(status, receiver.account_id, build_crutches(receiver.account_id, [status]), :list)
  38. when :mentions
  39. filter_from_mentions?(status, receiver.id)
  40. when :tags
  41. filter_from_tags?(status, receiver.id, build_crutches(receiver.id, [status]))
  42. else
  43. false
  44. end
  45. end
  46. # Add a status to a home feed and send a streaming API update
  47. # @param [Account] account
  48. # @param [Status] status
  49. # @param [Boolean] update
  50. # @return [Boolean]
  51. def push_to_home(account, status, update: false)
  52. return false unless add_to_feed(:home, account.id, status, aggregate_reblogs: account.user&.aggregates_reblogs?)
  53. trim(:home, account.id)
  54. PushUpdateWorker.perform_async(account.id, status.id, "timeline:#{account.id}", { 'update' => update }) if push_update_required?("timeline:#{account.id}")
  55. true
  56. end
  57. # Remove a status from a home feed and send a streaming API update
  58. # @param [Account] account
  59. # @param [Status] status
  60. # @param [Boolean] update
  61. # @return [Boolean]
  62. def unpush_from_home(account, status, update: false)
  63. return false unless remove_from_feed(:home, account.id, status, aggregate_reblogs: account.user&.aggregates_reblogs?)
  64. redis.publish("timeline:#{account.id}", Oj.dump(event: :delete, payload: status.id.to_s)) unless update
  65. true
  66. end
  67. # Add a status to a list feed and send a streaming API update
  68. # @param [List] list
  69. # @param [Status] status
  70. # @param [Boolean] update
  71. # @return [Boolean]
  72. def push_to_list(list, status, update: false)
  73. return false if filter_from_list?(status, list) || !add_to_feed(:list, list.id, status, aggregate_reblogs: list.account.user&.aggregates_reblogs?)
  74. trim(:list, list.id)
  75. PushUpdateWorker.perform_async(list.account_id, status.id, "timeline:list:#{list.id}", { 'update' => update }) if push_update_required?("timeline:list:#{list.id}")
  76. true
  77. end
  78. # Remove a status from a list feed and send a streaming API update
  79. # @param [List] list
  80. # @param [Status] status
  81. # @param [Boolean] update
  82. # @return [Boolean]
  83. def unpush_from_list(list, status, update: false)
  84. return false unless remove_from_feed(:list, list.id, status, aggregate_reblogs: list.account.user&.aggregates_reblogs?)
  85. redis.publish("timeline:list:#{list.id}", Oj.dump(event: :delete, payload: status.id.to_s)) unless update
  86. true
  87. end
  88. # Fill a home feed with an account's statuses
  89. # @param [Account] from_account
  90. # @param [Account] into_account
  91. # @return [void]
  92. def merge_into_home(from_account, into_account)
  93. timeline_key = key(:home, into_account.id)
  94. aggregate = into_account.user&.aggregates_reblogs?
  95. query = from_account.statuses.list_eligible_visibility.includes(:preloadable_poll, :media_attachments, reblog: :account).limit(FeedManager::MAX_ITEMS / 4)
  96. if redis.zcard(timeline_key) >= FeedManager::MAX_ITEMS / 4
  97. oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true).first.last.to_i
  98. query = query.where('id > ?', oldest_home_score)
  99. end
  100. statuses = query.to_a
  101. crutches = build_crutches(into_account.id, statuses)
  102. statuses.each do |status|
  103. next if filter_from_home?(status, into_account.id, crutches)
  104. add_to_feed(:home, into_account.id, status, aggregate_reblogs: aggregate)
  105. end
  106. trim(:home, into_account.id)
  107. end
  108. # Fill a list feed with an account's statuses
  109. # @param [Account] from_account
  110. # @param [List] list
  111. # @return [void]
  112. def merge_into_list(from_account, list)
  113. timeline_key = key(:list, list.id)
  114. aggregate = list.account.user&.aggregates_reblogs?
  115. query = from_account.statuses.list_eligible_visibility.includes(:preloadable_poll, :media_attachments, reblog: :account).limit(FeedManager::MAX_ITEMS / 4)
  116. if redis.zcard(timeline_key) >= FeedManager::MAX_ITEMS / 4
  117. oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true).first.last.to_i
  118. query = query.where('id > ?', oldest_home_score)
  119. end
  120. statuses = query.to_a
  121. crutches = build_crutches(list.account_id, statuses)
  122. statuses.each do |status|
  123. next if filter_from_home?(status, list.account_id, crutches) || filter_from_list?(status, list)
  124. add_to_feed(:list, list.id, status, aggregate_reblogs: aggregate)
  125. end
  126. trim(:list, list.id)
  127. end
  128. # Remove an account's statuses from a home feed
  129. # @param [Account] from_account
  130. # @param [Account] into_account
  131. # @return [void]
  132. def unmerge_from_home(from_account, into_account)
  133. timeline_key = key(:home, into_account.id)
  134. timeline_status_ids = redis.zrange(timeline_key, 0, -1)
  135. from_account.statuses.select('id, reblog_of_id').where(id: timeline_status_ids).reorder(nil).find_each do |status|
  136. remove_from_feed(:home, into_account.id, status, aggregate_reblogs: into_account.user&.aggregates_reblogs?)
  137. end
  138. end
  139. # Remove an account's statuses from a list feed
  140. # @param [Account] from_account
  141. # @param [List] list
  142. # @return [void]
  143. def unmerge_from_list(from_account, list)
  144. timeline_key = key(:list, list.id)
  145. timeline_status_ids = redis.zrange(timeline_key, 0, -1)
  146. from_account.statuses.select('id, reblog_of_id').where(id: timeline_status_ids).reorder(nil).find_each do |status|
  147. remove_from_feed(:list, list.id, status, aggregate_reblogs: list.account.user&.aggregates_reblogs?)
  148. end
  149. end
  150. # Remove a tag's statuses from a home feed
  151. # @param [Tag] from_tag
  152. # @param [Account] into_account
  153. # @return [void]
  154. def unmerge_tag_from_home(from_tag, into_account)
  155. timeline_key = key(:home, into_account.id)
  156. timeline_status_ids = redis.zrange(timeline_key, 0, -1)
  157. # This is a bit tricky because we need posts tagged with this hashtag that are not
  158. # also tagged with another followed hashtag or from a followed user
  159. scope = from_tag.statuses
  160. .where(id: timeline_status_ids)
  161. .where.not(account: into_account)
  162. .where.not(account: into_account.following)
  163. .tagged_with_none(TagFollow.where(account: into_account).pluck(:tag_id))
  164. scope.select('id, reblog_of_id').reorder(nil).find_each do |status|
  165. remove_from_feed(:home, into_account.id, status, aggregate_reblogs: into_account.user&.aggregates_reblogs?)
  166. end
  167. end
  168. # Clear all statuses from or mentioning target_account from a home feed
  169. # @param [Account] account
  170. # @param [Account] target_account
  171. # @return [void]
  172. def clear_from_home(account, target_account)
  173. timeline_key = key(:home, account.id)
  174. timeline_status_ids = redis.zrange(timeline_key, 0, -1)
  175. statuses = Status.where(id: timeline_status_ids).select(:id, :reblog_of_id, :account_id).to_a
  176. reblogged_ids = Status.where(id: statuses.filter_map(&:reblog_of_id), account: target_account).pluck(:id)
  177. with_mentions_ids = Mention.active.where(status_id: statuses.flat_map { |s| [s.id, s.reblog_of_id] }.compact, account: target_account).pluck(:status_id)
  178. target_statuses = statuses.select do |status|
  179. status.account_id == target_account.id || reblogged_ids.include?(status.reblog_of_id) || with_mentions_ids.include?(status.id) || with_mentions_ids.include?(status.reblog_of_id)
  180. end
  181. target_statuses.each do |status|
  182. unpush_from_home(account, status)
  183. end
  184. end
  185. # Clear all statuses from or mentioning target_account from a list feed
  186. # @param [List] list
  187. # @param [Account] target_account
  188. # @return [void]
  189. def clear_from_list(list, target_account)
  190. timeline_key = key(:list, list.id)
  191. timeline_status_ids = redis.zrange(timeline_key, 0, -1)
  192. statuses = Status.where(id: timeline_status_ids).select(:id, :reblog_of_id, :account_id).to_a
  193. reblogged_ids = Status.where(id: statuses.filter_map(&:reblog_of_id), account: target_account).pluck(:id)
  194. with_mentions_ids = Mention.active.where(status_id: statuses.flat_map { |s| [s.id, s.reblog_of_id] }.compact, account: target_account).pluck(:status_id)
  195. target_statuses = statuses.select do |status|
  196. status.account_id == target_account.id || reblogged_ids.include?(status.reblog_of_id) || with_mentions_ids.include?(status.id) || with_mentions_ids.include?(status.reblog_of_id)
  197. end
  198. target_statuses.each do |status|
  199. unpush_from_list(list, status)
  200. end
  201. end
  202. # Clear all statuses from or mentioning target_account from an account's lists
  203. # @param [Account] account
  204. # @param [Account] target_account
  205. # @return [void]
  206. def clear_from_lists(account, target_account)
  207. List.where(account: account).find_each do |list|
  208. clear_from_list(list, target_account)
  209. end
  210. end
  211. # Populate home feed of account from scratch
  212. # @param [Account] account
  213. # @return [void]
  214. def populate_home(account)
  215. limit = FeedManager::MAX_ITEMS / 2
  216. aggregate = account.user&.aggregates_reblogs?
  217. timeline_key = key(:home, account.id)
  218. account.statuses.limit(limit).each do |status|
  219. add_to_feed(:home, account.id, status, aggregate_reblogs: aggregate)
  220. end
  221. account.following.includes(:account_stat).reorder(nil).find_each do |target_account|
  222. if redis.zcard(timeline_key) >= limit
  223. oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true).first.last.to_i
  224. last_status_score = Mastodon::Snowflake.id_at(target_account.last_status_at)
  225. # If the feed is full and this account has not posted more recently
  226. # than the last item on the feed, then we can skip the whole account
  227. # because none of its statuses would stay on the feed anyway
  228. next if last_status_score < oldest_home_score
  229. end
  230. statuses = target_account.statuses.list_eligible_visibility.includes(:preloadable_poll, :media_attachments, :account, reblog: :account).limit(limit)
  231. crutches = build_crutches(account.id, statuses)
  232. statuses.each do |status|
  233. next if filter_from_home?(status, account.id, crutches)
  234. add_to_feed(:home, account.id, status, aggregate_reblogs: aggregate)
  235. end
  236. trim(:home, account.id)
  237. end
  238. end
  239. # Completely clear multiple feeds at once
  240. # @param [Symbol] type
  241. # @param [Array<Integer>] ids
  242. # @return [void]
  243. def clean_feeds!(type, ids)
  244. reblogged_id_sets = {}
  245. redis.pipelined do |pipeline|
  246. ids.each do |feed_id|
  247. reblog_key = key(type, feed_id, 'reblogs')
  248. # We collect a future for this: we don't block while getting
  249. # it, but we can iterate over it later.
  250. reblogged_id_sets[feed_id] = pipeline.zrange(reblog_key, 0, -1)
  251. pipeline.del(key(type, feed_id), reblog_key)
  252. end
  253. end
  254. # Remove all of the reblog tracking keys we just removed the
  255. # references to.
  256. keys_to_delete = reblogged_id_sets.flat_map do |feed_id, future|
  257. future.value.map do |reblogged_id|
  258. key(type, feed_id, "reblogs:#{reblogged_id}")
  259. end
  260. end
  261. redis.del(keys_to_delete) unless keys_to_delete.empty?
  262. nil
  263. end
  264. private
  265. # Trim a feed to maximum size by removing older items
  266. # @param [Symbol] type
  267. # @param [Integer] timeline_id
  268. # @return [void]
  269. def trim(type, timeline_id)
  270. timeline_key = key(type, timeline_id)
  271. reblog_key = key(type, timeline_id, 'reblogs')
  272. # Remove any items past the MAX_ITEMS'th entry in our feed
  273. redis.zremrangebyrank(timeline_key, 0, -(FeedManager::MAX_ITEMS + 1))
  274. # Get the score of the REBLOG_FALLOFF'th item in our feed, and stop
  275. # tracking anything after it for deduplication purposes.
  276. falloff_rank = FeedManager::REBLOG_FALLOFF
  277. falloff_range = redis.zrevrange(timeline_key, falloff_rank, falloff_rank, with_scores: true)
  278. falloff_score = falloff_range&.first&.last&.to_i
  279. return if falloff_score.nil?
  280. # Get any reblogs we might have to clean up after.
  281. redis.zrangebyscore(reblog_key, 0, falloff_score).each do |reblogged_id|
  282. # Remove it from the set of reblogs we're tracking *first* to avoid races.
  283. redis.zrem(reblog_key, reblogged_id)
  284. # Just drop any set we might have created to track additional reblogs.
  285. # This means that if this reblog is deleted, we won't automatically insert
  286. # another reblog, but also that any new reblog can be inserted into the
  287. # feed.
  288. redis.del(key(type, timeline_id, "reblogs:#{reblogged_id}"))
  289. end
  290. end
  291. # Check if there is a streaming API client connected
  292. # for the given feed
  293. # @param [String] timeline_key
  294. # @return [Boolean]
  295. def push_update_required?(timeline_key)
  296. redis.exists?("subscribed:#{timeline_key}")
  297. end
  298. # Check if the account is blocking or muting any of the given accounts
  299. # @param [Integer] receiver_id
  300. # @param [Array<Integer>] account_ids
  301. # @param [Symbol] context
  302. def blocks_or_mutes?(receiver_id, account_ids, context)
  303. Block.where(account_id: receiver_id, target_account_id: account_ids).any? ||
  304. (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?)
  305. end
  306. # Check if status should not be added to the home feed
  307. # @param [Status] status
  308. # @param [Integer] receiver_id
  309. # @param [Hash] crutches
  310. # @return [Boolean]
  311. def filter_from_home?(status, receiver_id, crutches, timeline_type = :home)
  312. return false if receiver_id == status.account_id
  313. return true if status.reply? && (status.in_reply_to_id.nil? || status.in_reply_to_account_id.nil?)
  314. return true if timeline_type != :list && crutches[:exclusive_list_users][status.account_id].present?
  315. return true if crutches[:languages][status.account_id].present? && status.language.present? && !crutches[:languages][status.account_id].include?(status.language)
  316. check_for_blocks = crutches[:active_mentions][status.id] || []
  317. check_for_blocks.push(status.account_id)
  318. if status.reblog?
  319. check_for_blocks.push(status.reblog.account_id)
  320. check_for_blocks.concat(crutches[:active_mentions][status.reblog_of_id] || [])
  321. end
  322. return true if check_for_blocks.any? { |target_account_id| crutches[:blocking][target_account_id] || crutches[:muting][target_account_id] }
  323. return true if crutches[:blocked_by][status.account_id]
  324. if status.reply? && !status.in_reply_to_account_id.nil? # Filter out if it's a reply
  325. should_filter = !crutches[:following][status.in_reply_to_account_id] # and I'm not following the person it's a reply to
  326. should_filter &&= receiver_id != status.in_reply_to_account_id # and it's not a reply to me
  327. should_filter &&= status.account_id != status.in_reply_to_account_id # and it's not a self-reply
  328. return !!should_filter
  329. elsif status.reblog? # Filter out a reblog
  330. should_filter = crutches[:hiding_reblogs][status.account_id] # if the reblogger's reblogs are suppressed
  331. should_filter ||= crutches[:blocked_by][status.reblog.account_id] # or if the author of the reblogged status is blocking me
  332. should_filter ||= crutches[:domain_blocking][status.reblog.account.domain] # or the author's domain is blocked
  333. return !!should_filter
  334. end
  335. false
  336. end
  337. # Check if status should not be added to the mentions feed
  338. # @see NotifyService
  339. # @param [Status] status
  340. # @param [Integer] receiver_id
  341. # @return [Boolean]
  342. def filter_from_mentions?(status, receiver_id)
  343. return true if receiver_id == status.account_id
  344. # This filter is called from NotifyService, but already after the sender of
  345. # the notification has been checked for mute/block. Therefore, it's not
  346. # necessary to check the author of the toot for mute/block again
  347. check_for_blocks = status.active_mentions.pluck(:account_id)
  348. check_for_blocks.push(status.in_reply_to_account) if status.reply? && !status.in_reply_to_account_id.nil?
  349. 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)
  350. should_filter ||= status.account.silenced? && !Follow.exists?(account_id: receiver_id, target_account_id: status.account_id) # Filter if the account is silenced and I'm not following them
  351. should_filter
  352. end
  353. # Check if status should not be added to the list feed
  354. # @param [Status] status
  355. # @param [List] list
  356. # @return [Boolean]
  357. def filter_from_list?(status, list)
  358. if status.reply? && status.in_reply_to_account_id != status.account_id # Status is a reply to account other than status account
  359. should_filter = status.in_reply_to_account_id != list.account_id # Status replies to account id other than list account
  360. should_filter &&= !list.show_followed? # List show_followed? is false
  361. should_filter &&= !(list.show_list? && ListAccount.exists?(list_id: list.id, account_id: status.in_reply_to_account_id)) # If show_list? true, check for a ListAccount with list and reply to account
  362. return !!should_filter
  363. end
  364. false
  365. end
  366. # Check if a status should not be added to the home feed when it comes
  367. # from a followed hashtag
  368. # @param [Status] status
  369. # @param [Integer] receiver_id
  370. # @param [Hash] crutches
  371. # @return [Boolean]
  372. def filter_from_tags?(status, receiver_id, crutches)
  373. receiver_id == status.account_id || # Receiver is status account?
  374. ((crutches[:active_mentions][status.id] || []) + [status.account_id]) # For mentioned accounts or status account:
  375. .any? { |target_account_id| crutches[:blocking][target_account_id] || crutches[:muting][target_account_id] } || # - Target account is muted or blocked?
  376. crutches[:blocked_by][status.account_id] || # Blocked by status account?
  377. crutches[:domain_blocking][status.account.domain] # Blocking domain of status account?
  378. end
  379. # Adds a status to an account's feed, returning true if a status was
  380. # added, and false if it was not added to the feed. Note that this is
  381. # an internal helper: callers must call trim or push updates if
  382. # either action is appropriate.
  383. # @param [Symbol] timeline_type
  384. # @param [Integer] account_id
  385. # @param [Status] status
  386. # @param [Boolean] aggregate_reblogs
  387. # @return [Boolean]
  388. def add_to_feed(timeline_type, account_id, status, aggregate_reblogs: true)
  389. timeline_key = key(timeline_type, account_id)
  390. reblog_key = key(timeline_type, account_id, 'reblogs')
  391. if status.reblog? && (aggregate_reblogs.nil? || aggregate_reblogs)
  392. # If the original status or a reblog of it is within
  393. # REBLOG_FALLOFF statuses from the top, do not re-insert it into
  394. # the feed
  395. rank = redis.zrevrank(timeline_key, status.reblog_of_id)
  396. return false if !rank.nil? && rank < FeedManager::REBLOG_FALLOFF
  397. # The ordered set at `reblog_key` holds statuses which have a reblog
  398. # in the top `REBLOG_FALLOFF` statuses of the timeline
  399. if redis.zadd(reblog_key, status.id, status.reblog_of_id, nx: true)
  400. # This is not something we've already seen reblogged, so we
  401. # can just add it to the feed (and note that we're reblogging it).
  402. redis.zadd(timeline_key, status.id, status.id)
  403. else
  404. # Another reblog of the same status was already in the
  405. # REBLOG_FALLOFF most recent statuses, so we note that this
  406. # is an "extra" reblog, by storing it in reblog_set_key.
  407. reblog_set_key = key(timeline_type, account_id, "reblogs:#{status.reblog_of_id}")
  408. redis.sadd(reblog_set_key, status.id)
  409. return false
  410. end
  411. else
  412. # A reblog may reach earlier than the original status because of the
  413. # delay of the worker delivering the original status, the late addition
  414. # by merging timelines, and other reasons.
  415. # If such a reblog already exists, just do not re-insert it into the feed.
  416. return false unless redis.zscore(reblog_key, status.id).nil?
  417. redis.zadd(timeline_key, status.id, status.id)
  418. end
  419. true
  420. end
  421. # Removes an individual status from a feed, correctly handling cases
  422. # with reblogs, and returning true if a status was removed. As with
  423. # `add_to_feed`, this does not trigger push updates, so callers must
  424. # do so if appropriate.
  425. # @param [Symbol] timeline_type
  426. # @param [Integer] account_id
  427. # @param [Status] status
  428. # @param [Boolean] aggregate_reblogs
  429. # @return [Boolean]
  430. def remove_from_feed(timeline_type, account_id, status, aggregate_reblogs: true)
  431. timeline_key = key(timeline_type, account_id)
  432. reblog_key = key(timeline_type, account_id, 'reblogs')
  433. if status.reblog? && (aggregate_reblogs.nil? || aggregate_reblogs)
  434. # 1. If the reblogging status is not in the feed, stop.
  435. status_rank = redis.zrevrank(timeline_key, status.id)
  436. return false if status_rank.nil?
  437. # 2. Remove reblog from set of this status's reblogs.
  438. reblog_set_key = key(timeline_type, account_id, "reblogs:#{status.reblog_of_id}")
  439. redis.srem(reblog_set_key, status.id)
  440. redis.zrem(reblog_key, status.reblog_of_id)
  441. # 3. Re-insert another reblog or original into the feed if one
  442. # remains in the set. We could pick a random element, but this
  443. # set should generally be small, and it seems ideal to show the
  444. # oldest potential such reblog.
  445. other_reblog = redis.smembers(reblog_set_key).map(&:to_i).min
  446. redis.zadd(timeline_key, other_reblog, other_reblog) if other_reblog
  447. redis.zadd(reblog_key, other_reblog, status.reblog_of_id) if other_reblog
  448. # 4. Remove the reblogging status from the feed (as normal)
  449. # (outside conditional)
  450. else
  451. # If the original is getting deleted, no use for reblog references
  452. redis.del(key(timeline_type, account_id, "reblogs:#{status.id}"))
  453. redis.zrem(reblog_key, status.id)
  454. end
  455. redis.zrem(timeline_key, status.id)
  456. end
  457. # Pre-fetch various objects and relationships for given statuses that
  458. # are going to be checked by the filtering methods
  459. # @param [Integer] receiver_id
  460. # @param [Array<Status>] statuses
  461. # @return [Hash]
  462. def build_crutches(receiver_id, statuses)
  463. crutches = {}
  464. crutches[:active_mentions] = crutches_active_mentions(statuses)
  465. check_for_blocks = statuses.flat_map do |s|
  466. arr = crutches[:active_mentions][s.id] || []
  467. arr.push(s.account_id)
  468. if s.reblog?
  469. arr.push(s.reblog.account_id)
  470. arr.concat(crutches[:active_mentions][s.reblog_of_id] || [])
  471. end
  472. arr
  473. end
  474. lists = List.where(account_id: receiver_id, exclusive: true)
  475. crutches[:following] = Follow.where(account_id: receiver_id, target_account_id: statuses.filter_map(&:in_reply_to_account_id)).pluck(:target_account_id).index_with(true)
  476. crutches[:languages] = Follow.where(account_id: receiver_id, target_account_id: statuses.map(&:account_id)).pluck(:target_account_id, :languages).to_h
  477. crutches[:hiding_reblogs] = Follow.where(account_id: receiver_id, target_account_id: statuses.filter_map { |s| s.account_id if s.reblog? }, show_reblogs: false).pluck(:target_account_id).index_with(true)
  478. crutches[:blocking] = Block.where(account_id: receiver_id, target_account_id: check_for_blocks).pluck(:target_account_id).index_with(true)
  479. crutches[:muting] = Mute.where(account_id: receiver_id, target_account_id: check_for_blocks).pluck(:target_account_id).index_with(true)
  480. crutches[:domain_blocking] = AccountDomainBlock.where(account_id: receiver_id, domain: statuses.flat_map { |s| [s.account.domain, s.reblog&.account&.domain] }.compact).pluck(:domain).index_with(true)
  481. crutches[:blocked_by] = Block.where(target_account_id: receiver_id, account_id: statuses.map { |s| [s.account_id, s.reblog&.account_id] }.flatten.compact).pluck(:account_id).index_with(true)
  482. crutches[:exclusive_list_users] = ListAccount.where(list: lists, account_id: statuses.map(&:account_id)).pluck(:account_id).index_with(true)
  483. crutches
  484. end
  485. def crutches_active_mentions(statuses)
  486. Mention
  487. .active
  488. .where(status_id: statuses.flat_map { |status| [status.id, status.reblog_of_id] }.compact)
  489. .pluck(:status_id, :account_id)
  490. .each_with_object({}) { |(id, account_id), mapping| (mapping[id] ||= []).push(account_id) }
  491. end
  492. end