logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

feed_manager.rb (10947B)


  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)
  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. return false unless add_to_feed(:list, list.id, status)
  34. trim(:list, list.id)
  35. PushUpdateWorker.perform_async(list.account_id, status.id, "timeline:list:#{list.id}") if push_update_required?("timeline:list:#{list.id}")
  36. true
  37. end
  38. def unpush_from_list(list, status)
  39. return false unless remove_from_feed(:list, list.id, status)
  40. Redis.current.publish("timeline:list:#{list.id}", Oj.dump(event: :delete, payload: status.id.to_s))
  41. true
  42. end
  43. def trim(type, account_id)
  44. timeline_key = key(type, account_id)
  45. reblog_key = key(type, account_id, 'reblogs')
  46. # Remove any items past the MAX_ITEMS'th entry in our feed
  47. redis.zremrangebyrank(timeline_key, '0', (-(FeedManager::MAX_ITEMS + 1)).to_s)
  48. # Get the score of the REBLOG_FALLOFF'th item in our feed, and stop
  49. # tracking anything after it for deduplication purposes.
  50. falloff_rank = FeedManager::REBLOG_FALLOFF - 1
  51. falloff_range = redis.zrevrange(timeline_key, falloff_rank, falloff_rank, with_scores: true)
  52. falloff_score = falloff_range&.first&.last&.to_i || 0
  53. # Get any reblogs we might have to clean up after.
  54. redis.zrangebyscore(reblog_key, 0, falloff_score).each do |reblogged_id|
  55. # Remove it from the set of reblogs we're tracking *first* to avoid races.
  56. redis.zrem(reblog_key, reblogged_id)
  57. # Just drop any set we might have created to track additional reblogs.
  58. # This means that if this reblog is deleted, we won't automatically insert
  59. # another reblog, but also that any new reblog can be inserted into the
  60. # feed.
  61. redis.del(key(type, account_id, "reblogs:#{reblogged_id}"))
  62. end
  63. end
  64. def merge_into_timeline(from_account, into_account)
  65. timeline_key = key(:home, into_account.id)
  66. query = from_account.statuses.limit(FeedManager::MAX_ITEMS / 4)
  67. if redis.zcard(timeline_key) >= FeedManager::MAX_ITEMS / 4
  68. oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true)&.first&.last&.to_i || 0
  69. query = query.where('id > ?', oldest_home_score)
  70. end
  71. query.each do |status|
  72. next if status.direct_visibility? || filter?(:home, status, into_account)
  73. add_to_feed(:home, into_account.id, status)
  74. end
  75. trim(:home, into_account.id)
  76. end
  77. def unmerge_from_timeline(from_account, into_account)
  78. timeline_key = key(:home, into_account.id)
  79. oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true)&.first&.last&.to_i || 0
  80. from_account.statuses.select('id, reblog_of_id').where('id > ?', oldest_home_score).reorder(nil).find_each do |status|
  81. remove_from_feed(:home, into_account.id, status)
  82. end
  83. end
  84. def clear_from_timeline(account, target_account)
  85. timeline_key = key(:home, account.id)
  86. timeline_status_ids = redis.zrange(timeline_key, 0, -1)
  87. target_statuses = Status.where(id: timeline_status_ids, account: target_account)
  88. target_statuses.each do |status|
  89. unpush_from_home(account, status)
  90. end
  91. end
  92. def populate_feed(account)
  93. added = 0
  94. limit = FeedManager::MAX_ITEMS / 2
  95. max_id = nil
  96. loop do
  97. statuses = Status.as_home_timeline(account)
  98. .paginate_by_max_id(limit, max_id)
  99. break if statuses.empty?
  100. statuses.each do |status|
  101. next if filter_from_home?(status, account)
  102. added += 1 if add_to_feed(:home, account.id, status)
  103. end
  104. break unless added.zero?
  105. max_id = statuses.last.id
  106. end
  107. end
  108. private
  109. def redis
  110. Redis.current
  111. end
  112. def push_update_required?(timeline_id)
  113. redis.exists("subscribed:#{timeline_id}")
  114. end
  115. def filter_from_home?(status, receiver_id)
  116. return false if receiver_id == status.account_id
  117. return true if status.reply? && (status.in_reply_to_id.nil? || status.in_reply_to_account_id.nil?)
  118. check_for_mutes = [status.account_id]
  119. check_for_mutes.concat([status.reblog.account_id]) if status.reblog?
  120. return true if Mute.where(account_id: receiver_id, target_account_id: check_for_mutes).any?
  121. check_for_blocks = status.mentions.pluck(:account_id)
  122. check_for_blocks.concat([status.reblog.account_id]) if status.reblog?
  123. return true if Block.where(account_id: receiver_id, target_account_id: check_for_blocks).any?
  124. if status.reply? && !status.in_reply_to_account_id.nil? # Filter out if it's a reply
  125. 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
  126. should_filter &&= receiver_id != status.in_reply_to_account_id # and it's not a reply to me
  127. should_filter &&= status.account_id != status.in_reply_to_account_id # and it's not a self-reply
  128. return should_filter
  129. elsif status.reblog? # Filter out a reblog
  130. 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
  131. 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
  132. should_filter ||= AccountDomainBlock.where(account_id: receiver_id, domain: status.reblog.account.domain).exists? # or the author's domain is blocked
  133. return should_filter
  134. end
  135. false
  136. end
  137. def filter_from_mentions?(status, receiver_id)
  138. return true if receiver_id == status.account_id
  139. check_for_blocks = [status.account_id]
  140. check_for_blocks.concat(status.mentions.pluck(:account_id))
  141. check_for_blocks.concat([status.in_reply_to_account]) if status.reply? && !status.in_reply_to_account_id.nil?
  142. 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
  143. 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
  144. should_filter
  145. end
  146. # Adds a status to an account's feed, returning true if a status was
  147. # added, and false if it was not added to the feed. Note that this is
  148. # an internal helper: callers must call trim or push updates if
  149. # either action is appropriate.
  150. def add_to_feed(timeline_type, account_id, status)
  151. timeline_key = key(timeline_type, account_id)
  152. reblog_key = key(timeline_type, account_id, 'reblogs')
  153. if status.reblog?
  154. # If the original status or a reblog of it is within
  155. # REBLOG_FALLOFF statuses from the top, do not re-insert it into
  156. # the feed
  157. rank = redis.zrevrank(timeline_key, status.reblog_of_id)
  158. return false if !rank.nil? && rank < FeedManager::REBLOG_FALLOFF
  159. reblog_rank = redis.zrevrank(reblog_key, status.reblog_of_id)
  160. if reblog_rank.nil?
  161. # This is not something we've already seen reblogged, so we
  162. # can just add it to the feed (and note that we're
  163. # reblogging it).
  164. redis.zadd(timeline_key, status.id, status.id)
  165. redis.zadd(reblog_key, status.id, status.reblog_of_id)
  166. else
  167. # Another reblog of the same status was already in the
  168. # REBLOG_FALLOFF most recent statuses, so we note that this
  169. # is an "extra" reblog, by storing it in reblog_set_key.
  170. reblog_set_key = key(timeline_type, account_id, "reblogs:#{status.reblog_of_id}")
  171. redis.sadd(reblog_set_key, status.id)
  172. return false
  173. end
  174. else
  175. # A reblog may reach earlier than the original status because of the
  176. # delay of the worker deliverying the original status, the late addition
  177. # by merging timelines, and other reasons.
  178. # If such a reblog already exists, just do not re-insert it into the feed.
  179. rank = redis.zrevrank(reblog_key, status.id)
  180. return false unless rank.nil?
  181. redis.zadd(timeline_key, status.id, status.id)
  182. end
  183. true
  184. end
  185. # Removes an individual status from a feed, correctly handling cases
  186. # with reblogs, and returning true if a status was removed. As with
  187. # `add_to_feed`, this does not trigger push updates, so callers must
  188. # do so if appropriate.
  189. def remove_from_feed(timeline_type, account_id, status)
  190. timeline_key = key(timeline_type, account_id)
  191. if status.reblog?
  192. # 1. If the reblogging status is not in the feed, stop.
  193. status_rank = redis.zrevrank(timeline_key, status.id)
  194. return false if status_rank.nil?
  195. # 2. Remove reblog from set of this status's reblogs.
  196. reblog_set_key = key(timeline_type, account_id, "reblogs:#{status.reblog_of_id}")
  197. redis.srem(reblog_set_key, status.id)
  198. # 3. Re-insert another reblog or original into the feed if one
  199. # remains in the set. We could pick a random element, but this
  200. # set should generally be small, and it seems ideal to show the
  201. # oldest potential such reblog.
  202. other_reblog = redis.smembers(reblog_set_key).map(&:to_i).sort.first
  203. redis.zadd(timeline_key, other_reblog, other_reblog) if other_reblog
  204. # 4. Remove the reblogging status from the feed (as normal)
  205. # (outside conditional)
  206. else
  207. # If the original is getting deleted, no use for reblog references
  208. redis.del(key(timeline_type, account_id, "reblogs:#{status.id}"))
  209. end
  210. redis.zrem(timeline_key, status.id)
  211. end
  212. end