Skip to content

Commit 719fcc6

Browse files
committed
Add ability to search for a collection by URL
1 parent 8fa91b4 commit 719fcc6

7 files changed

Lines changed: 69 additions & 5 deletions

File tree

app/serializers/rest/search_serializer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ class REST::SearchSerializer < ActiveModel::Serializer
44
has_many :accounts, serializer: REST::AccountSerializer
55
has_many :statuses, serializer: REST::StatusSerializer
66
has_many :hashtags, serializer: REST::TagSerializer
7+
has_many :collections, serializer: REST::CollectionSerializer
78
end

app/services/activitypub/fetch_remote_featured_collection_service.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
class ActivityPub::FetchRemoteFeaturedCollectionService < BaseService
44
include JsonLdHelper
55

6-
def call(uri, request_id: nil, on_behalf_of: nil)
7-
json = fetch_resource(uri, true, on_behalf_of)
6+
def call(uri, request_id: nil, prefetched_body: nil, on_behalf_of: nil)
7+
json = if prefetched_body.nil?
8+
fetch_resource(uri, true, on_behalf_of)
9+
else
10+
body_to_json(prefetched_body, compare_id: uri)
11+
end
812

913
return unless supported_context?(json)
1014
return unless json['type'] == 'FeaturedCollection'

app/services/fetch_resource_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def process_response(response, terminal: false)
6363
end
6464

6565
def expected_type?(json)
66-
equals_or_includes_any?(json['type'], ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES)
66+
equals_or_includes_any?(json['type'], ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES + %w(FeaturedCollection))
6767
end
6868

6969
def process_html(response)

app/services/resolve_url_service.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def process_url
2828
status = FetchRemoteStatusService.new.call(resource_url, prefetched_body: body)
2929
authorize_with @on_behalf_of, status, :show? unless status.nil?
3030
status
31+
elsif type == 'FeaturedCollection' && Mastodon::Feature.collections_enabled?
32+
collection = ActivityPub::FetchRemoteFeaturedCollectionService.new.call(resource_url, prefetched_body: body)
33+
authorize_with @on_behalf_of, collection, :show? unless collection.nil?
34+
collection
3135
end
3236
end
3337

@@ -111,9 +115,21 @@ def process_local_url
111115

112116
Account.find_remote(username, domain)
113117
end
118+
when 'collections'
119+
return unless recognized_params[:action] == 'show'
120+
121+
check_collection(Collection.find_by(id: recognized_params[:id]))
114122
end
115123
end
116124

125+
def check_collection(collection)
126+
return if collection.nil?
127+
128+
authorize_with @on_behalf_of, collection, :show?
129+
rescue Mastodon::NotPermittedError
130+
nil
131+
end
132+
117133
def check_local_status(status)
118134
return if status.nil?
119135

app/services/search_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def perform_hashtags_search!
6464
end
6565

6666
def default_results
67-
{ accounts: [], hashtags: [], statuses: [] }
67+
{ accounts: [], hashtags: [], statuses: [], collections: [] }
6868
end
6969

7070
def url_query?

spec/services/resolve_url_service_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,49 @@
3030
expect(subject.call(url)).to eq known_account
3131
end
3232

33+
context 'when searching for a remote collection', feature: :collections do
34+
let(:account) { Fabricate(:account) }
35+
let(:collection_account) { Fabricate(:account, domain: 'example.com', protocol: :activitypub) }
36+
37+
let(:uri) { 'https://example.com/featured_collections/1' }
38+
39+
let(:payload) do
40+
{
41+
'@context' => 'https://www.w3.org/ns/activitystreams',
42+
'id' => uri,
43+
'type' => 'FeaturedCollection',
44+
'name' => 'Incredible people',
45+
'summary' => 'These are really amazing',
46+
'attributedTo' => collection_account.uri,
47+
'sensitive' => false,
48+
'discoverable' => true,
49+
'totalItems' => 0,
50+
}
51+
end
52+
53+
before do
54+
stub_request(:get, uri).to_return(status: 200, body: payload.to_json, headers: { 'Content-Type': 'application/activity+json' })
55+
end
56+
57+
it 'returns the collection' do
58+
expect(subject.call(uri, on_behalf_of: account))
59+
.to be_a(Collection)
60+
.and have_attributes(
61+
uri: uri
62+
)
63+
end
64+
end
65+
66+
context 'when searching for a local collection', feature: :collections do
67+
let(:account) { Fabricate(:account) }
68+
let(:collection) { Fabricate(:collection) }
69+
70+
it 'returns the collection' do
71+
expect(subject.call(ActivityPub::TagManager.instance.uri_for(collection), on_behalf_of: account))
72+
.to eq(collection)
73+
end
74+
end
75+
3376
context 'when searching for a remote private status' do
3477
let(:account) { Fabricate(:account) }
3578
let(:poster) { Fabricate(:account, domain: 'example.com') }

spec/services/search_service_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,6 @@
8686
end
8787

8888
def empty_results
89-
{ accounts: [], hashtags: [], statuses: [] }
89+
{ accounts: [], hashtags: [], statuses: [], collections: [] }
9090
end
9191
end

0 commit comments

Comments
 (0)