Include collection url in API responses (#38708)

This commit is contained in:
David Roetzel 2026-04-16 12:09:39 +02:00 committed by GitHub
parent e05ac2ec04
commit 961acaf202
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View File

@ -1,8 +1,10 @@
# frozen_string_literal: true
class REST::CollectionSerializer < ActiveModel::Serializer
include RoutingHelper
attributes :id, :uri, :name, :description, :language, :account_id,
:local, :sensitive, :discoverable, :item_count,
:local, :sensitive, :discoverable, :url, :item_count,
:created_at, :updated_at
belongs_to :tag, serializer: REST::ShallowTagSerializer
@ -17,6 +19,10 @@ class REST::CollectionSerializer < ActiveModel::Serializer
ActivityPub::TagManager.instance.uri_for(object)
end
def url
object.local? ? account_collection_url(object.account, object) : object.url
end
def description
return object.description if object.local?
return if object.description_html.nil?
@ -31,4 +37,8 @@ class REST::CollectionSerializer < ActiveModel::Serializer
def account_id
object.account_id.to_s
end
def tag
object.tag
end
end

View File

@ -3,6 +3,8 @@
require 'rails_helper'
RSpec.describe REST::CollectionSerializer do
include RoutingHelper
subject do
serialized_record_json(collection, described_class, options: {
scope: current_user,
@ -37,6 +39,7 @@ RSpec.describe REST::CollectionSerializer do
'local' => true,
'sensitive' => true,
'discoverable' => false,
'url' => account_collection_url(collection.account, collection),
'tag' => a_hash_including('name' => 'discovery'),
'created_at' => match_api_datetime_format,
'updated_at' => match_api_datetime_format,
@ -46,7 +49,11 @@ RSpec.describe REST::CollectionSerializer do
end
context 'when the collection is remote' do
let(:collection) { Fabricate(:remote_collection, description_html: '<p>remote</p>') }
let(:collection) { Fabricate(:remote_collection, description_html: '<p>remote</p>', url: 'https://example.com/c/1') }
it 'includes the uri' do
expect(subject).to include('url' => 'https://example.com/c/1')
end
it 'includes the html description' do
expect(subject)