闭社主体 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.

131 lines
4.5 KiB

8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. require 'singleton'
  3. class FeedManager
  4. include Singleton
  5. MAX_ITEMS = 800
  6. def key(type, id)
  7. "feed:#{type}:#{id}"
  8. end
  9. def filter?(timeline_type, status, receiver)
  10. if timeline_type == :home
  11. filter_from_home?(status, receiver)
  12. elsif timeline_type == :mentions
  13. filter_from_mentions?(status, receiver)
  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. broadcast(account.id, event: 'update', payload: inline_render(account, 'api/v1/statuses/show', status))
  30. end
  31. def broadcast(timeline_id, options = {})
  32. options[:queued_at] = (Time.now.to_f * 1000.0).to_i
  33. ActionCable.server.broadcast("timeline:#{timeline_id}", options)
  34. end
  35. def trim(type, account_id)
  36. return unless redis.zcard(key(type, account_id)) > FeedManager::MAX_ITEMS
  37. last = redis.zrevrange(key(type, account_id), FeedManager::MAX_ITEMS - 1, FeedManager::MAX_ITEMS - 1)
  38. redis.zremrangebyscore(key(type, account_id), '-inf', "(#{last.last}")
  39. end
  40. def merge_into_timeline(from_account, into_account)
  41. timeline_key = key(:home, into_account.id)
  42. from_account.statuses.limit(MAX_ITEMS).each do |status|
  43. next if filter?(:home, status, into_account)
  44. redis.zadd(timeline_key, status.id, status.id)
  45. end
  46. trim(:home, into_account.id)
  47. end
  48. def unmerge_from_timeline(from_account, into_account)
  49. timeline_key = key(:home, into_account.id)
  50. from_account.statuses.select('id').find_each do |status|
  51. redis.zrem(timeline_key, status.id)
  52. redis.zremrangebyscore(timeline_key, status.id, status.id)
  53. end
  54. end
  55. def inline_render(target_account, template, object)
  56. rabl_scope = Class.new do
  57. include RoutingHelper
  58. def initialize(account)
  59. @account = account
  60. end
  61. def current_user
  62. @account.try(:user)
  63. end
  64. def current_account
  65. @account
  66. end
  67. end
  68. Rabl::Renderer.new(template, object, view_path: 'app/views', format: :json, scope: rabl_scope.new(target_account)).render
  69. end
  70. private
  71. def redis
  72. Redis.current
  73. end
  74. def filter_from_home?(status, receiver)
  75. return true if receiver.muting?(status.account)
  76. should_filter = false
  77. if status.reply? && status.in_reply_to_id.nil?
  78. should_filter = true
  79. elsif status.reply? && !status.in_reply_to_account_id.nil? # Filter out if it's a reply
  80. should_filter = !receiver.following?(status.in_reply_to_account) # and I'm not following the person it's a reply to
  81. should_filter &&= !(receiver.id == status.in_reply_to_account_id) # and it's not a reply to me
  82. should_filter &&= !(status.account_id == status.in_reply_to_account_id) # and it's not a self-reply
  83. elsif status.reblog? # Filter out a reblog
  84. should_filter = receiver.blocking?(status.reblog.account) # if I'm blocking the reblogged person
  85. should_filter ||= receiver.muting?(status.reblog.account) # or muting that person
  86. should_filter ||= status.reblog.account.blocking?(receiver) # or if the author of the reblogged status is blocking me
  87. end
  88. should_filter ||= receiver.blocking?(status.mentions.map(&:account_id)) # or if it mentions someone I blocked
  89. should_filter
  90. end
  91. def filter_from_mentions?(status, receiver)
  92. should_filter = receiver.id == status.account_id # Filter if I'm mentioning myself
  93. should_filter ||= receiver.blocking?(status.account) # or it's from someone I blocked
  94. should_filter ||= receiver.blocking?(status.mentions.includes(:account).map(&:account)) # or if it mentions someone I blocked
  95. should_filter ||= (status.account.silenced? && !receiver.following?(status.account)) # of if the account is silenced and I'm not following them
  96. if status.reply? && !status.in_reply_to_account_id.nil? # or it's a reply
  97. should_filter ||= receiver.blocking?(status.in_reply_to_account) # to a user I blocked
  98. end
  99. should_filter
  100. end
  101. end