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.

130 lines
4.5 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. next if filter?(:home, status, into_account)
  37. redis.zadd(timeline_key, status.id, status.id)
  38. end
  39. trim(:home, into_account.id)
  40. end
  41. def unmerge_from_timeline(from_account, into_account)
  42. timeline_key = key(:home, into_account.id)
  43. from_account.statuses.select('id').find_each do |status|
  44. redis.zrem(timeline_key, status.id)
  45. redis.zremrangebyscore(timeline_key, status.id, status.id)
  46. end
  47. end
  48. def inline_render(target_account, template, object)
  49. rabl_scope = Class.new do
  50. include RoutingHelper
  51. def initialize(account)
  52. @account = account
  53. end
  54. def current_user
  55. @account.try(:user)
  56. end
  57. def current_account
  58. @account
  59. end
  60. end
  61. Rabl::Renderer.new(template, object, view_path: 'app/views', format: :json, scope: rabl_scope.new(target_account)).render
  62. end
  63. private
  64. def redis
  65. Redis.current
  66. end
  67. def filter_from_home?(status, receiver)
  68. should_filter = false
  69. if status.reply? && !status.in_reply_to_account_id.nil? # Filter out if it's a reply
  70. should_filter = !receiver.following?(status.in_reply_to_account) # and I'm not following the person it's a reply to
  71. should_filter &&= !(receiver.id == status.in_reply_to_account_id) # and it's not a reply to me
  72. should_filter &&= !(status.account_id == status.in_reply_to_account_id) # and it's not a self-reply
  73. elsif status.reblog? # Filter out a reblog
  74. should_filter = receiver.blocking?(status.reblog.account) # if I'm blocking the reblogged person
  75. end
  76. should_filter ||= receiver.blocking?(status.mentions.map(&:account_id)) # or if it mentions someone I blocked
  77. should_filter
  78. end
  79. def filter_from_mentions?(status, receiver)
  80. should_filter = receiver.id == status.account_id # Filter if I'm mentioning myself
  81. should_filter ||= receiver.blocking?(status.account) # or it's from someone I blocked
  82. should_filter ||= receiver.blocking?(status.mentions.includes(:account).map(&:account)) # or if it mentions someone I blocked
  83. should_filter ||= (status.account.silenced? && !receiver.following?(status.account)) # of if the account is silenced and I'm not following them
  84. should_filter ||= (status.private_visibility? && !receiver.following?(status.account)) # or if the mentioned account is not permitted to see the private status
  85. if status.reply? && !status.in_reply_to_account_id.nil? # or it's a reply
  86. should_filter ||= receiver.blocking?(status.in_reply_to_account) # to a user I blocked
  87. end
  88. should_filter
  89. end
  90. def filter_from_public?(status, receiver)
  91. should_filter = receiver.blocking?(status.account)
  92. should_filter ||= receiver.blocking?(status.mentions.includes(:account).map(&:account))
  93. if status.reply? && !status.in_reply_to_account_id.nil?
  94. should_filter ||= receiver.blocking?(status.in_reply_to_account)
  95. elsif status.reblog?
  96. should_filter ||= receiver.blocking?(status.reblog.account)
  97. end
  98. should_filter
  99. end
  100. end