Skip to content

Commit 38996d0

Browse files
ClearlyClaireMage
authored andcommitted
Fix serialization of replies when some of them are URIs (mastodon#13957)
* Fix serialization of replies when some of them are URIs Fixes mastodon#13956 * Add test
1 parent a24c59c commit 38996d0

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

app/serializers/activitypub/collection_serializer.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
# frozen_string_literal: true
22

33
class ActivityPub::CollectionSerializer < ActivityPub::Serializer
4+
class StringSerializer < ActiveModel::Serializer
5+
# Despite the name, it does not return a hash, but the same can be said of
6+
# the ActiveModel::Serializer::CollectionSerializer class which handles
7+
# arrays.
8+
def serializable_hash(*_args)
9+
object
10+
end
11+
end
12+
413
def self.serializer_for(model, options)
514
case model.class.name
615
when 'Status'
@@ -9,6 +18,8 @@ def self.serializer_for(model, options)
918
ActivityPub::DeviceSerializer
1019
when 'ActivityPub::CollectionPresenter'
1120
ActivityPub::CollectionSerializer
21+
when 'String'
22+
StringSerializer
1223
else
1324
super
1425
end

spec/controllers/activitypub/replies_controller_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
RSpec.describe ActivityPub::RepliesController, type: :controller do
66
let(:status) { Fabricate(:status, visibility: parent_visibility) }
7+
let(:remote_reply_id) { nil }
78
let(:remote_account) { nil }
89

910
before do
@@ -14,6 +15,8 @@
1415
Fabricate(:status, thread: status, visibility: :private)
1516
Fabricate(:status, account: status.account, thread: status, visibility: :public)
1617
Fabricate(:status, account: status.account, thread: status, visibility: :private)
18+
19+
Fabricate(:status, account: remote_account, thread: status, visibility: :public, uri: remote_reply_id) if remote_reply_id
1720
end
1821

1922
describe 'GET #index' do
@@ -110,6 +113,20 @@
110113
expect(json[:first][:items].size).to eq 2
111114
expect(json[:first][:items].all? { |item| item[:to].include?(ActivityPub::TagManager::COLLECTIONS[:public]) || item[:cc].include?(ActivityPub::TagManager::COLLECTIONS[:public]) }).to be true
112115
end
116+
117+
context 'with remote responses' do
118+
let(:remote_reply_id) { 'foo' }
119+
120+
it 'returned items are all inlined local toots or are ids' do
121+
json = body_as_json
122+
123+
expect(json[:first]).to be_a Hash
124+
expect(json[:first][:items]).to be_an Array
125+
expect(json[:first][:items].size).to eq 3
126+
expect(json[:first][:items].all? { |item| item.is_a?(Hash) ? ActivityPub::TagManager.instance.local_uri?(item[:id]) : item.is_a?(String) }).to be true
127+
expect(json[:first][:items]).to include remote_reply_id
128+
end
129+
end
113130
end
114131
end
115132

0 commit comments

Comments
 (0)