mirror of https://github.com/mastodon/mastodon
Distrubute statuses as a fan-out-on-write system, with optional precomputing
parent
fe57f6330f
commit
6c4c84b161
@ -0,0 +1,27 @@
|
||||
class Feed
|
||||
def initialize(type, account)
|
||||
@type = type
|
||||
@account = account
|
||||
end
|
||||
|
||||
def get(limit, offset = 0)
|
||||
unhydrated = redis.zrevrange(key, offset, limit)
|
||||
status_map = Hash.new
|
||||
|
||||
# If we're after most recent items and none are there, we need to precompute the feed
|
||||
return PrecomputeFeedService.new.(@type, @account).take(limit) if unhydrated.empty? && offset == 0
|
||||
|
||||
Status.where(id: unhydrated).each { |status| status_map[status.id.to_s] = status }
|
||||
return unhydrated.map { |id| status_map[id] }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def key
|
||||
"feed:#{@type}:#{@account.id}"
|
||||
end
|
||||
|
||||
def redis
|
||||
$redis
|
||||
end
|
||||
end
|
@ -0,0 +1,46 @@
|
||||
class FanOutOnWriteService < BaseService
|
||||
MAX_FEED_SIZE = 800
|
||||
|
||||
# Push a status into home and mentions feeds
|
||||
# @param [Status] status
|
||||
def call(status)
|
||||
replied_to_user = status.reply? ? status.thread.account : nil
|
||||
|
||||
# Deliver to local self
|
||||
push(:home, status.account.id, status) if status.account.local?
|
||||
|
||||
# Deliver to local followers
|
||||
status.account.followers.each do |follower|
|
||||
next if (status.reply? && !follower.following?(replied_to_user)) || !follower.local?
|
||||
push(:home, follower.id, status)
|
||||
end
|
||||
|
||||
# Deliver to local mentioned
|
||||
status.mentions.each do |mentioned_account|
|
||||
next unless mentioned_account.local?
|
||||
push(:mentions, mentioned_account.id, status)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def push(type, receiver_id, status)
|
||||
redis.zadd(key(type, receiver_id), status.created_at.to_i, status.id)
|
||||
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
|
@ -0,0 +1,35 @@
|
||||
class PrecomputeFeedService < BaseService
|
||||
MAX_FEED_SIZE = 800
|
||||
|
||||
# Fill up a user's home/mentions feed from DB and return it
|
||||
# @param [Symbol] type :home or :mentions
|
||||
# @param [Account] account
|
||||
# @return [Array]
|
||||
def call(type, account)
|
||||
statuses = send(type.to_s, account).order('created_at desc').limit(MAX_FEED_SIZE)
|
||||
statuses.each { |status| push(type, account.id, status) }
|
||||
statuses
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def push(type, receiver_id, status)
|
||||
redis.zadd(key(type, receiver_id), status.created_at.to_i, status.id)
|
||||
end
|
||||
|
||||
def home(account)
|
||||
Status.where(account: [account] + account.following)
|
||||
end
|
||||
|
||||
def mentions(account)
|
||||
Status.where(id: Mention.where(account: account).pluck(:status_id))
|
||||
end
|
||||
|
||||
def key(type, id)
|
||||
"feed:#{type}:#{id}"
|
||||
end
|
||||
|
||||
def redis
|
||||
$redis
|
||||
end
|
||||
end
|
@ -0,0 +1 @@
|
||||
$redis = Redis.new(host: ENV['REDIS_HOST'] || 'localhost', port: ENV['REDIS_PORT'] || 6379)
|
Loading…
Reference in New Issue