Add pagination by max_id instead of offset/limit
This commit is contained in:
parent
02e4fb2e06
commit
12559b01ea
|
@ -15,7 +15,7 @@ class Api::AccountsController < ApiController
|
||||||
end
|
end
|
||||||
|
|
||||||
def statuses
|
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
|
end
|
||||||
|
|
||||||
def follow
|
def follow
|
||||||
|
|
|
@ -23,11 +23,11 @@ class Api::StatusesController < ApiController
|
||||||
|
|
||||||
def home
|
def home
|
||||||
feed = Feed.new(:home, current_user.account)
|
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
|
end
|
||||||
|
|
||||||
def mentions
|
def mentions
|
||||||
feed = Feed.new(:mentions, current_user.account)
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,12 +4,12 @@ class Feed
|
||||||
@account = account
|
@account = account
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(limit, offset = 0)
|
def get(limit, max_id = '+inf')
|
||||||
unhydrated = redis.zrevrange(key, offset, limit)
|
unhydrated = redis.zrevrangebyscore(key, "(#{max_id}", '-inf', limit: [0, limit])
|
||||||
status_map = Hash.new
|
status_map = Hash.new
|
||||||
|
|
||||||
# If we're after most recent items and none are there, we need to precompute the feed
|
# 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 }
|
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
|
return unhydrated.map { |id| status_map[id] }.compact
|
||||||
|
|
|
@ -15,8 +15,9 @@ class Status < ActiveRecord::Base
|
||||||
validates :uri, uniqueness: true, unless: 'local?'
|
validates :uri, uniqueness: true, unless: 'local?'
|
||||||
validates :text, presence: true, if: Proc.new { |s| s.local? && !s.reblog? }
|
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_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_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?
|
def local?
|
||||||
self.uri.nil?
|
self.uri.nil?
|
||||||
|
|
|
@ -31,7 +31,7 @@ class FanOutOnWriteService < BaseService
|
||||||
end
|
end
|
||||||
|
|
||||||
def push(type, receiver_id, status)
|
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)
|
trim(type, receiver_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ class PrecomputeFeedService < BaseService
|
||||||
private
|
private
|
||||||
|
|
||||||
def push(type, receiver_id, status)
|
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
|
end
|
||||||
|
|
||||||
def home(account)
|
def home(account)
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue