diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2016-11-09 17:48:44 +0100 |
---|---|---|
committer | Eugen Rochko <eugen@zeonfederated.com> | 2016-11-09 17:48:44 +0100 |
commit | b13e7dda1f33be43d1667b754b67df71f3187a5c (patch) | |
tree | 62f451e95bb93846744f7bcdc58fcf8dc8d6d6f9 /app/models | |
parent | 8d7fc5da6c880e356e6861b5c5bd564c242c7991 (diff) |
API pagination for all collections using Link header
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/account.rb | 50 | ||||
-rw-r--r-- | app/models/concerns/paginable.rb | 6 | ||||
-rw-r--r-- | app/models/favourite.rb | 1 | ||||
-rw-r--r-- | app/models/feed.rb | 6 | ||||
-rw-r--r-- | app/models/follow.rb | 1 | ||||
-rw-r--r-- | app/models/status.rb | 8 |
6 files changed, 35 insertions, 37 deletions
diff --git a/app/models/account.rb b/app/models/account.rb index 92286369e..4fb0baebe 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -133,36 +133,38 @@ class Account < ApplicationRecord [] end - def self.find_local!(username) - find_remote!(username, nil) - end + class << self + def find_local!(username) + find_remote!(username, nil) + end - def self.find_remote!(username, domain) - where(arel_table[:username].matches(username)).where(domain.nil? ? { domain: nil } : arel_table[:domain].matches(domain)).take! - end + def find_remote!(username, domain) + where(arel_table[:username].matches(username)).where(domain.nil? ? { domain: nil } : arel_table[:domain].matches(domain)).take! + end - def self.find_local(username) - find_local!(username) - rescue ActiveRecord::RecordNotFound - nil - end + def find_local(username) + find_local!(username) + rescue ActiveRecord::RecordNotFound + nil + end - def self.find_remote(username, domain) - find_remote!(username, domain) - rescue ActiveRecord::RecordNotFound - nil - end + def find_remote(username, domain) + find_remote!(username, domain) + rescue ActiveRecord::RecordNotFound + nil + end - def self.following_map(target_account_ids, account_id) - Follow.where(target_account_id: target_account_ids).where(account_id: account_id).map { |f| [f.target_account_id, true] }.to_h - end + def following_map(target_account_ids, account_id) + Follow.where(target_account_id: target_account_ids).where(account_id: account_id).map { |f| [f.target_account_id, true] }.to_h + end - def self.followed_by_map(target_account_ids, account_id) - Follow.where(account_id: target_account_ids).where(target_account_id: account_id).map { |f| [f.account_id, true] }.to_h - end + def followed_by_map(target_account_ids, account_id) + Follow.where(account_id: target_account_ids).where(target_account_id: account_id).map { |f| [f.account_id, true] }.to_h + end - def self.blocking_map(target_account_ids, account_id) - Block.where(target_account_id: target_account_ids).where(account_id: account_id).map { |b| [b.target_account_id, true] }.to_h + def blocking_map(target_account_ids, account_id) + Block.where(target_account_id: target_account_ids).where(account_id: account_id).map { |b| [b.target_account_id, true] }.to_h + end end before_create do diff --git a/app/models/concerns/paginable.rb b/app/models/concerns/paginable.rb index 0a542d534..08f207621 100644 --- a/app/models/concerns/paginable.rb +++ b/app/models/concerns/paginable.rb @@ -2,11 +2,11 @@ module Paginable extend ActiveSupport::Concern included do - def self.paginate_by_max_id(limit, max_id = nil, since_id = nil) - query = order('id desc').limit(limit) + scope :paginate_by_max_id, -> (limit, max_id = nil, since_id = nil) { + query = order(arel_table[:id].desc).limit(limit) query = query.where(arel_table[:id].lt(max_id)) unless max_id.blank? query = query.where(arel_table[:id].gt(since_id)) unless since_id.blank? query - end + } end end diff --git a/app/models/favourite.rb b/app/models/favourite.rb index 8b8082f1e..60d853ce2 100644 --- a/app/models/favourite.rb +++ b/app/models/favourite.rb @@ -1,4 +1,5 @@ class Favourite < ApplicationRecord + include Paginable include Streamable belongs_to :account, inverse_of: :favourites diff --git a/app/models/feed.rb b/app/models/feed.rb index e7a39f5da..408403873 100644 --- a/app/models/feed.rb +++ b/app/models/feed.rb @@ -12,11 +12,13 @@ class Feed # If we're after most recent items and none are there, we need to precompute the feed if unhydrated.empty? && max_id == '+inf' && since_id == '-inf' RegenerationWorker.perform_async(@account.id, @type) - Status.send("as_#{@type}_timeline", @account).paginate_by_max_id(limit, nil, nil) + @statuses = Status.send("as_#{@type}_timeline", @account).paginate_by_max_id(limit, nil, nil) else status_map = Status.where(id: unhydrated).with_includes.with_counters.map { |status| [status.id, status] }.to_h - unhydrated.map { |id| status_map[id] }.compact + @statuses = unhydrated.map { |id| status_map[id] }.compact end + + @statuses end private diff --git a/app/models/follow.rb b/app/models/follow.rb index 656b28d35..720812b6d 100644 --- a/app/models/follow.rb +++ b/app/models/follow.rb @@ -1,4 +1,5 @@ class Follow < ApplicationRecord + include Paginable include Streamable belongs_to :account diff --git a/app/models/status.rb b/app/models/status.rb index 07aef26ee..9db7a0a3a 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -78,14 +78,6 @@ class Status < ApplicationRecord ids.map { |id| statuses[id].first } end - def reblogged_by(limit) - Account.where(id: reblogs.limit(limit).pluck(:account_id)).with_counters - end - - def favourited_by(limit) - Account.where(id: favourites.limit(limit).pluck(:account_id)).with_counters - end - class << self def as_home_timeline(account) where(account: [account] + account.following).with_includes.with_counters |