about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-03-22 21:38:47 +0100
committerEugen Rochko <eugen@zeonfederated.com>2016-03-22 21:38:47 +0100
commit12559b01eae408c0f3e820ef9c6bc6e356ab889a (patch)
tree87d857393ce24b1937c654ce7252b6315b7e77f9 /app/models
parent02e4fb2e06f424c16ab25ea294a4af6490a5f7e3 (diff)
Add pagination by max_id instead of offset/limit
Diffstat (limited to 'app/models')
-rw-r--r--app/models/feed.rb6
-rw-r--r--app/models/status.rb5
2 files changed, 6 insertions, 5 deletions
diff --git a/app/models/feed.rb b/app/models/feed.rb
index 9f2b34c82..206f287e7 100644
--- a/app/models/feed.rb
+++ b/app/models/feed.rb
@@ -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
diff --git a/app/models/status.rb b/app/models/status.rb
index 58d25546c..f1c12383b 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -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?