2016-03-09 04:16:11 +09:00
|
|
|
class FanOutOnWriteService < BaseService
|
|
|
|
MAX_FEED_SIZE = 800
|
|
|
|
|
|
|
|
# Push a status into home and mentions feeds
|
|
|
|
# @param [Status] status
|
|
|
|
def call(status)
|
2016-03-22 01:02:16 +09:00
|
|
|
deliver_to_self(status) if status.account.local?
|
|
|
|
deliver_to_followers(status, status.reply? ? status.thread.account : nil)
|
|
|
|
deliver_to_mentioned(status)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-03-09 04:16:11 +09:00
|
|
|
|
2016-03-22 01:02:16 +09:00
|
|
|
def deliver_to_self(status)
|
|
|
|
push(:home, status.account.id, status)
|
|
|
|
end
|
2016-03-09 04:16:11 +09:00
|
|
|
|
2016-03-22 01:02:16 +09:00
|
|
|
def deliver_to_followers(status, replied_to_user)
|
2016-03-09 04:16:11 +09:00
|
|
|
status.account.followers.each do |follower|
|
2016-03-21 16:41:00 +09:00
|
|
|
next if (status.reply? && !(follower.id = replied_to_user.id || follower.following?(replied_to_user))) || !follower.local?
|
2016-03-09 04:16:11 +09:00
|
|
|
push(:home, follower.id, status)
|
|
|
|
end
|
2016-03-22 01:02:16 +09:00
|
|
|
end
|
2016-03-09 04:16:11 +09:00
|
|
|
|
2016-03-22 01:02:16 +09:00
|
|
|
def deliver_to_mentioned(status)
|
2016-03-20 03:20:07 +09:00
|
|
|
status.mentioned_accounts.each do |mention|
|
|
|
|
mentioned_account = mention.account
|
2016-03-09 04:16:11 +09:00
|
|
|
next unless mentioned_account.local?
|
|
|
|
push(:mentions, mentioned_account.id, status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def push(type, receiver_id, status)
|
2016-03-23 05:38:47 +09:00
|
|
|
redis.zadd(key(type, receiver_id), status.id, status.id)
|
2016-03-09 04:16:11 +09:00
|
|
|
trim(type, receiver_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def trim(type, receiver_id)
|
|
|
|
return unless redis.zcard(key(type, receiver_id)) > MAX_FEED_SIZE
|
|
|
|
|
|
|
|
last = redis.zrevrange(key(type, receiver_id), MAX_FEED_SIZE - 1, MAX_FEED_SIZE - 1)
|
|
|
|
redis.zremrangebyscore(key(type, receiver_id), '-inf', "(#{last.last}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def key(type, id)
|
|
|
|
"feed:#{type}:#{id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def redis
|
|
|
|
$redis
|
|
|
|
end
|
|
|
|
end
|