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.

118 lines
4.1 KiB

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