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

120 lines
4.2 KiB

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. elsif timeline_type == :public
  15. filter_from_public?(status, receiver)
  16. else
  17. false
  18. end
  19. end
  20. def push(timeline_type, account, status)
  21. redis.zadd(key(timeline_type, account.id), status.id, status.reblog? ? status.reblog_of_id : status.id)
  22. trim(timeline_type, account.id)
  23. broadcast(account.id, type: 'update', timeline: timeline_type, message: inline_render(account, 'api/v1/statuses/show', status))
  24. end
  25. def broadcast(timeline_id, options = {})
  26. ActionCable.server.broadcast("timeline:#{timeline_id}", options)
  27. end
  28. def trim(type, account_id)
  29. return unless redis.zcard(key(type, account_id)) > FeedManager::MAX_ITEMS
  30. last = redis.zrevrange(key(type, account_id), FeedManager::MAX_ITEMS - 1, FeedManager::MAX_ITEMS - 1)
  31. redis.zremrangebyscore(key(type, account_id), '-inf', "(#{last.last}")
  32. end
  33. def merge_into_timeline(from_account, into_account)
  34. timeline_key = key(:home, into_account.id)
  35. from_account.statuses.limit(MAX_ITEMS).each do |status|
  36. redis.zadd(timeline_key, status.id, status.id)
  37. end
  38. trim(:home, into_account.id)
  39. end
  40. def inline_render(target_account, template, object)
  41. rabl_scope = Class.new do
  42. include RoutingHelper
  43. def initialize(account)
  44. @account = account
  45. end
  46. def current_user
  47. @account.try(:user)
  48. end
  49. def current_account
  50. @account
  51. end
  52. end
  53. Rabl::Renderer.new(template, object, view_path: 'app/views', format: :json, scope: rabl_scope.new(target_account)).render
  54. end
  55. private
  56. def redis
  57. Redis.current
  58. end
  59. def filter_from_home?(status, receiver)
  60. should_filter = false
  61. if status.reply? && !status.in_reply_to_account_id.nil? # Filter out if it's a reply
  62. should_filter = !receiver.following?(status.in_reply_to_account) # and I'm not following the person it's a reply to
  63. should_filter &&= !(receiver.id == status.in_reply_to_account_id) # and it's not a reply to me
  64. should_filter &&= !(status.account_id == status.in_reply_to_account_id) # and it's not a self-reply
  65. elsif status.reblog? # Filter out a reblog
  66. should_filter = receiver.blocking?(status.reblog.account) # if I'm blocking the reblogged person
  67. end
  68. should_filter ||= receiver.blocking?(status.mentions.map(&:account_id)) # or if it mentions someone I blocked
  69. should_filter
  70. end
  71. def filter_from_mentions?(status, receiver)
  72. should_filter = receiver.id == status.account_id # Filter if I'm mentioning myself
  73. should_filter ||= receiver.blocking?(status.account) # or it's from someone I blocked
  74. should_filter ||= receiver.blocking?(status.mentions.includes(:account).map(&:account)) # or if it mentions someone I blocked
  75. should_filter ||= (status.account.silenced? && !receiver.following?(status.account)) # of if the account is silenced and I'm not following them
  76. should_filter ||= (status.private_visibility? && !receiver.following?(status.account)) # or if the mentioned account is not permitted to see the private status
  77. if status.reply? && !status.in_reply_to_account_id.nil? # or it's a reply
  78. should_filter ||= receiver.blocking?(status.in_reply_to_account) # to a user I blocked
  79. end
  80. should_filter
  81. end
  82. def filter_from_public?(status, receiver)
  83. should_filter = receiver.blocking?(status.account)
  84. should_filter ||= receiver.blocking?(status.mentions.includes(:account).map(&:account))
  85. if status.reply? && !status.in_reply_to_account_id.nil?
  86. should_filter ||= receiver.blocking?(status.in_reply_to_account)
  87. elsif status.reblog?
  88. should_filter ||= receiver.blocking?(status.reblog.account)
  89. end
  90. should_filter
  91. end
  92. end