gearheads
/
mastodon
Archived
2
0
Fork 0
This repository has been archived on 2024-06-09. You can view files and clone it, but cannot push or open issues/pull-requests.
mastodon/app/services/fan_out_on_write_service.rb

66 lines
1.8 KiB
Ruby
Raw Normal View History

class FanOutOnWriteService < BaseService
# Push a status into home and mentions feeds
# @param [Status] status
def call(status)
deliver_to_self(status) if status.account.local?
deliver_to_followers(status)
deliver_to_mentioned(status)
end
private
def deliver_to_self(status)
2016-08-18 15:49:51 +02:00
push(:home, status.account, status)
end
def deliver_to_followers(status)
status.account.followers.each do |follower|
next if !follower.local? || FeedManager.filter_status?(status, follower)
2016-08-18 15:49:51 +02:00
push(:home, follower, status)
end
end
def deliver_to_mentioned(status)
status.mentions.each do |mention|
mentioned_account = mention.account
next unless mentioned_account.local?
2016-08-18 15:49:51 +02:00
push(:mentions, mentioned_account, status)
end
end
2016-08-18 15:49:51 +02:00
def push(type, receiver, status)
redis.zadd(FeedManager.key(type, receiver.id), status.id, status.id)
trim(type, receiver)
ActionCable.server.broadcast("timeline:#{receiver.id}", message: inline_render(receiver, status))
end
2016-08-18 15:49:51 +02:00
def trim(type, receiver)
return unless redis.zcard(FeedManager.key(type, receiver.id)) > FeedManager::MAX_ITEMS
2016-08-18 15:49:51 +02:00
last = redis.zrevrange(FeedManager.key(type, receiver.id), FeedManager::MAX_ITEMS - 1, FeedManager::MAX_ITEMS - 1)
redis.zremrangebyscore(FeedManager.key(type, receiver.id), '-inf', "(#{last.last}")
end
def redis
$redis
end
2016-08-18 15:49:51 +02:00
def inline_render(receiver, status)
rabl_scope = Class.new(BaseService) do
def initialize(account)
@account = account
end
def current_user
@account.user
end
2016-08-18 17:13:41 +02:00
def current_account
@account
end
2016-08-18 15:49:51 +02:00
end
Rabl::Renderer.new('api/statuses/show', status, view_path: 'app/views', format: :json, scope: rabl_scope.new(receiver)).render
end
end