Add pagination by max_id instead of offset/limit

pull/69/head
Eugen Rochko 2016-03-22 21:38:47 +01:00
parent 02e4fb2e06
commit 12559b01ea
7 changed files with 19 additions and 10 deletions

View File

@ -15,7 +15,7 @@ class Api::AccountsController < ApiController
end
def statuses
@statuses = @account.statuses.with_includes.with_counters.order('created_at desc')
@statuses = @account.statuses.with_includes.with_counters.paginate_by_max_id(20, params[:max_id])
end
def follow

View File

@ -23,11 +23,11 @@ class Api::StatusesController < ApiController
def home
feed = Feed.new(:home, current_user.account)
@statuses = feed.get(20, (params[:offset] || 0).to_i)
@statuses = feed.get(20, params[:max_id] || '+inf')
end
def mentions
feed = Feed.new(:mentions, current_user.account)
@statuses = feed.get(20, (params[:offset] || 0).to_i)
@statuses = feed.get(20, params[:max_id] || '+inf')
end
end

View File

@ -4,12 +4,12 @@ class Feed
@account = account
end
def get(limit, offset = 0)
unhydrated = redis.zrevrange(key, offset, limit)
def get(limit, max_id = '+inf')
unhydrated = redis.zrevrangebyscore(key, "(#{max_id}", '-inf', limit: [0, 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
return PrecomputeFeedService.new.(@type, @account).take(limit) if unhydrated.empty? && max_id == '+inf'
Status.where(id: unhydrated).with_includes.with_counters.each { |status| status_map[status.id.to_s] = status }
return unhydrated.map { |id| status_map[id] }.compact

View File

@ -15,8 +15,9 @@ class Status < ActiveRecord::Base
validates :uri, uniqueness: true, unless: 'local?'
validates :text, presence: true, if: Proc.new { |s| s.local? && !s.reblog? }
scope :with_counters, -> { select('statuses.*, (select count(r.id) from statuses as r where r.reblog_of_id = statuses.id) as reblogs_count, (select count(f.id) from favourites as f where f.status_id = statuses.id) as favourites_count') }
scope :with_includes, -> { includes(:account, reblog: :account, thread: :account) }
scope :with_counters, -> { select('statuses.*, (select count(r.id) from statuses as r where r.reblog_of_id = statuses.id) as reblogs_count, (select count(f.id) from favourites as f where f.status_id = statuses.id) as favourites_count') }
scope :with_includes, -> { includes(:account, reblog: :account, thread: :account) }
scope :paginate_by_max_id, -> (limit, max_id) { order('id desc').limit(limit).where('id < ?', max_id) }
def local?
self.uri.nil?

View File

@ -31,7 +31,7 @@ class FanOutOnWriteService < BaseService
end
def push(type, receiver_id, status)
redis.zadd(key(type, receiver_id), status.created_at.to_i, status.id)
redis.zadd(key(type, receiver_id), status.id, status.id)
trim(type, receiver_id)
end

View File

@ -14,7 +14,7 @@ class PrecomputeFeedService < BaseService
private
def push(type, receiver_id, status)
redis.zadd(key(type, receiver_id), status.created_at.to_i, status.id)
redis.zadd(key(type, receiver_id), status.id, status.id)
end
def home(account)

8
lib/tasks/feeds.rake Normal file
View File

@ -0,0 +1,8 @@
namespace :feeds do
desc "Removes all feeds from Redis, forcing a precompute on next request for each user"
task clear: :environment do
$redis.keys('feed:*').each { |key| $redis.del(key) }
end
end