The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

59 rivejä
1.5 KiB

  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_status?(status, follower)
  9. replied_to_user = status.reply? ? status.thread.account : nil
  10. (status.reply? && !(follower.id = replied_to_user.id || follower.following?(replied_to_user)))
  11. end
  12. def push(timeline_type, account, status)
  13. redis.zadd(key(timeline_type, account.id), status.id, status.id)
  14. trim(timeline_type, account.id)
  15. broadcast(account.id, type: 'update', timeline: timeline_type, message: inline_render(account, status))
  16. end
  17. def broadcast(account_id, options = {})
  18. ActionCable.server.broadcast("timeline:#{account_id}", options)
  19. end
  20. def trim(type, account_id)
  21. return unless redis.zcard(key(type, account_id)) > FeedManager::MAX_ITEMS
  22. last = redis.zrevrange(key(type, account_id), FeedManager::MAX_ITEMS - 1, FeedManager::MAX_ITEMS - 1)
  23. redis.zremrangebyscore(key(type, account_id), '-inf', "(#{last.last}")
  24. end
  25. private
  26. def redis
  27. $redis
  28. end
  29. def inline_render(target_account, status)
  30. rabl_scope = Class.new do
  31. include RoutingHelper
  32. def initialize(account)
  33. @account = account
  34. end
  35. def current_user
  36. @account.user
  37. end
  38. def current_account
  39. @account
  40. end
  41. end
  42. Rabl::Renderer.new('api/statuses/show', status, view_path: 'app/views', format: :json, scope: rabl_scope.new(target_account)).render
  43. end
  44. end