mirror of https://github.com/mastodon/mastodon
Use OrderedCollectionPage to return followers/following list (#4949)
This commit is contained in:
parent
0401a24558
commit
7d16bb379d
|
@ -17,12 +17,29 @@ class FollowerAccountsController < ApplicationController
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def page_url(page)
|
||||||
|
account_followers_url(@account, page: page) unless page.nil?
|
||||||
|
end
|
||||||
|
|
||||||
def collection_presenter
|
def collection_presenter
|
||||||
ActivityPub::CollectionPresenter.new(
|
page = ActivityPub::CollectionPresenter.new(
|
||||||
id: account_followers_url(@account),
|
id: account_followers_url(@account, page: params.fetch(:page, 1)),
|
||||||
type: :ordered,
|
type: :ordered,
|
||||||
size: @account.followers_count,
|
size: @account.followers_count,
|
||||||
items: @follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.account) }
|
items: @follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.account) },
|
||||||
|
part_of: account_followers_url(@account),
|
||||||
|
next: page_url(@follows.next_page),
|
||||||
|
prev: page_url(@follows.prev_page)
|
||||||
)
|
)
|
||||||
|
if params[:page].present?
|
||||||
|
page
|
||||||
|
else
|
||||||
|
ActivityPub::CollectionPresenter.new(
|
||||||
|
id: account_followers_url(@account),
|
||||||
|
type: :ordered,
|
||||||
|
size: @account.followers_count,
|
||||||
|
first: page
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,12 +17,29 @@ class FollowingAccountsController < ApplicationController
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def page_url(page)
|
||||||
|
account_following_index_url(@account, page: page) unless page.nil?
|
||||||
|
end
|
||||||
|
|
||||||
def collection_presenter
|
def collection_presenter
|
||||||
ActivityPub::CollectionPresenter.new(
|
page = ActivityPub::CollectionPresenter.new(
|
||||||
id: account_following_index_url(@account),
|
id: account_following_index_url(@account, page: params.fetch(:page, 1)),
|
||||||
type: :ordered,
|
type: :ordered,
|
||||||
size: @account.following_count,
|
size: @account.following_count,
|
||||||
items: @follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.target_account) }
|
items: @follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.target_account) },
|
||||||
|
part_of: account_following_index_url(@account),
|
||||||
|
next: page_url(@follows.next_page),
|
||||||
|
prev: page_url(@follows.prev_page)
|
||||||
)
|
)
|
||||||
|
if params[:page].present?
|
||||||
|
page
|
||||||
|
else
|
||||||
|
ActivityPub::CollectionPresenter.new(
|
||||||
|
id: account_following_index_url(@account),
|
||||||
|
type: :ordered,
|
||||||
|
size: @account.following_count,
|
||||||
|
first: page
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class ActivityPub::CollectionPresenter < ActiveModelSerializers::Model
|
class ActivityPub::CollectionPresenter < ActiveModelSerializers::Model
|
||||||
attributes :id, :type, :size, :items
|
attributes :id, :type, :size, :items, :part_of, :first, :next, :prev
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,23 +3,38 @@
|
||||||
class ActivityPub::CollectionSerializer < ActiveModel::Serializer
|
class ActivityPub::CollectionSerializer < ActiveModel::Serializer
|
||||||
def self.serializer_for(model, options)
|
def self.serializer_for(model, options)
|
||||||
return ActivityPub::ActivitySerializer if model.class.name == 'Status'
|
return ActivityPub::ActivitySerializer if model.class.name == 'Status'
|
||||||
|
return ActivityPub::CollectionSerializer if model.class.name == 'ActivityPub::CollectionPresenter'
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
attributes :id, :type, :total_items
|
attributes :id, :type, :total_items
|
||||||
|
attribute :next, if: -> { object.next.present? }
|
||||||
|
attribute :prev, if: -> { object.prev.present? }
|
||||||
|
attribute :part_of, if: -> { object.part_of.present? }
|
||||||
|
|
||||||
has_many :items, key: :ordered_items
|
has_one :first, if: -> { object.first.present? }
|
||||||
|
has_many :items, key: :items, if: -> { (object.items.present? || page?) && !ordered? }
|
||||||
|
has_many :items, key: :ordered_items, if: -> { (object.items.present? || page?) && ordered? }
|
||||||
|
|
||||||
def type
|
def type
|
||||||
case object.type
|
if page?
|
||||||
when :ordered
|
ordered? ? 'OrderedCollectionPage' : 'CollectionPage'
|
||||||
'OrderedCollection'
|
|
||||||
else
|
else
|
||||||
'Collection'
|
ordered? ? 'OrderedCollection' : 'Collection'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def total_items
|
def total_items
|
||||||
object.size
|
object.size
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def ordered?
|
||||||
|
object.type == :ordered
|
||||||
|
end
|
||||||
|
|
||||||
|
def page?
|
||||||
|
object.part_of.present?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue