about summary refs log tree commit diff
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
parent02e4fb2e06f424c16ab25ea294a4af6490a5f7e3 (diff)
Add pagination by max_id instead of offset/limit
-rw-r--r--app/controllers/api/accounts_controller.rb2
-rw-r--r--app/controllers/api/statuses_controller.rb4
-rw-r--r--app/models/feed.rb6
-rw-r--r--app/models/status.rb5
-rw-r--r--app/services/fan_out_on_write_service.rb2
-rw-r--r--app/services/precompute_feed_service.rb2
-rw-r--r--lib/tasks/feeds.rake8
7 files changed, 19 insertions, 10 deletions
diff --git a/app/controllers/api/accounts_controller.rb b/app/controllers/api/accounts_controller.rb
index f543ea98d..5bac98f81 100644
--- a/app/controllers/api/accounts_controller.rb
+++ b/app/controllers/api/accounts_controller.rb
@@ -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
diff --git a/app/controllers/api/statuses_controller.rb b/app/controllers/api/statuses_controller.rb
index 951f7113a..ba216a7b3 100644
--- a/app/controllers/api/statuses_controller.rb
+++ b/app/controllers/api/statuses_controller.rb
@@ -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
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?
diff --git a/app/services/fan_out_on_write_service.rb b/app/services/fan_out_on_write_service.rb
index 45814cfb5..62cf2a1fe 100644
--- a/app/services/fan_out_on_write_service.rb
+++ b/app/services/fan_out_on_write_service.rb
@@ -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
 
diff --git a/app/services/precompute_feed_service.rb b/app/services/precompute_feed_service.rb
index 0fb33db23..9d3b8d370 100644
--- a/app/services/precompute_feed_service.rb
+++ b/app/services/precompute_feed_service.rb
@@ -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)
diff --git a/lib/tasks/feeds.rake b/lib/tasks/feeds.rake
new file mode 100644
index 000000000..8f4dc78b9
--- /dev/null
+++ b/lib/tasks/feeds.rake
@@ -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