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.

97 lines
3.1 KiB

7 years ago
  1. require 'singleton'
  2. class FeedManager
  3. include Singleton
  4. MAX_ITEMS = 800
  5. def key(type, id)
  6. "feed:#{type}:#{id}"
  7. end
  8. def filter?(timeline_type, status, receiver)
  9. if timeline_type == :home
  10. filter_from_home?(status, receiver)
  11. elsif timeline_type == :mentions
  12. filter_from_mentions?(status, receiver)
  13. elsif timeline_type == :public
  14. filter_from_public?(status, receiver)
  15. else
  16. false
  17. end
  18. end
  19. def push(timeline_type, account, status)
  20. redis.zadd(key(timeline_type, account.id), status.id, status.reblog? ? status.reblog_of_id : status.id)
  21. trim(timeline_type, account.id)
  22. broadcast(account.id, type: 'update', timeline: timeline_type, message: inline_render(account, status))
  23. end
  24. def broadcast(timeline_id, options = {})
  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 inline_render(target_account, status)
  33. rabl_scope = Class.new do
  34. include RoutingHelper
  35. def initialize(account)
  36. @account = account
  37. end
  38. def current_user
  39. @account.try(:user)
  40. end
  41. def current_account
  42. @account
  43. end
  44. end
  45. Rabl::Renderer.new('api/v1/statuses/show', status, view_path: 'app/views', format: :json, scope: rabl_scope.new(target_account)).render
  46. end
  47. private
  48. def redis
  49. $redis
  50. end
  51. def filter_from_home?(status, receiver)
  52. should_filter = false
  53. if status.reply? && !status.thread.account.nil? # Filter out if it's a reply
  54. should_filter = !receiver.following?(status.thread.account) # and I'm not following the person it's a reply to
  55. should_filter = should_filter && !(receiver.id == status.thread.account_id) # and it's not a reply to me
  56. should_filter = should_filter && !(status.account_id == status.thread.account_id) # and it's not a self-reply
  57. elsif status.reblog? # Filter out a reblog
  58. should_filter = receiver.blocking?(status.reblog.account) # if I'm blocking the reblogged person
  59. end
  60. should_filter
  61. end
  62. def filter_from_mentions?(status, receiver)
  63. should_filter = receiver.id == status.account_id # Filter if I'm mentioning myself
  64. should_filter = should_filter || receiver.blocking?(status.account) # or it's from someone I blocked
  65. should_filter
  66. end
  67. def filter_from_public?(status, receiver)
  68. should_filter = receiver.blocking?(status.account)
  69. if status.reply? && !status.thread.account.nil?
  70. should_filter = should_filter || receiver.blocking?(status.thread.account)
  71. elsif status.reblog?
  72. should_filter = should_filter || receiver.blocking?(status.reblog.account)
  73. end
  74. should_filter
  75. end
  76. end