From 9a3be0ad688ea80fffee78635395141ad2419e8a Mon Sep 17 00:00:00 2001 From: Claire Date: Mon, 2 May 2022 01:00:08 +0200 Subject: Fix error when looking handle with surrounding spaces (#18225) --- app/services/resolve_account_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/services') diff --git a/app/services/resolve_account_service.rb b/app/services/resolve_account_service.rb index 21332a03e..387e2e09b 100644 --- a/app/services/resolve_account_service.rb +++ b/app/services/resolve_account_service.rb @@ -66,7 +66,7 @@ class ResolveAccountService < BaseService @username = @account.username @domain = @account.domain else - @username, @domain = uri.split('@') + @username, @domain = uri.strip.gsub(/\A@/, '').split('@') end @domain = begin -- cgit From 71d02ffcf3a79dfc1c413dcc7ff45c77ce9cb94c Mon Sep 17 00:00:00 2001 From: Claire Date: Mon, 2 May 2022 17:41:01 +0200 Subject: Fix compatibility with Friendica regarding pinned posts (#18254) * Fix multiple database queries when fetching pinned posts for remote account * Fix compatibility with Friendica regarding pinned posts Fixes #18066 * Add tests --- .../fetch_featured_collection_service.rb | 34 ++++-- .../fetch_featured_collection_service_spec.rb | 123 +++++++++++++++++++++ 2 files changed, 146 insertions(+), 11 deletions(-) create mode 100644 spec/services/activitypub/fetch_featured_collection_service_spec.rb (limited to 'app/services') diff --git a/app/services/activitypub/fetch_featured_collection_service.rb b/app/services/activitypub/fetch_featured_collection_service.rb index 07a9fe039..e62470f70 100644 --- a/app/services/activitypub/fetch_featured_collection_service.rb +++ b/app/services/activitypub/fetch_featured_collection_service.rb @@ -7,19 +7,33 @@ class ActivityPub::FetchFeaturedCollectionService < BaseService return if account.featured_collection_url.blank? || account.suspended? || account.local? @account = account - @json = fetch_resource(@account.featured_collection_url, true) + @json = fetch_resource(@account.featured_collection_url, true, local_follower) - return unless supported_context? + return unless supported_context?(@json) - case @json['type'] + process_items(collection_items(@json)) + end + + private + + def collection_items(collection) + collection = fetch_collection(collection['first']) if collection['first'].present? + return unless collection.is_a?(Hash) + + case collection['type'] when 'Collection', 'CollectionPage' - process_items @json['items'] + collection['items'] when 'OrderedCollection', 'OrderedCollectionPage' - process_items @json['orderedItems'] + collection['orderedItems'] end end - private + def fetch_collection(collection_or_uri) + return collection_or_uri if collection_or_uri.is_a?(Hash) + return if invalid_origin?(collection_or_uri) + + fetch_resource_without_id_validation(collection_or_uri, nil, true, local_follower) + end def process_items(items) status_ids = items.filter_map do |item| @@ -53,11 +67,9 @@ class ActivityPub::FetchFeaturedCollectionService < BaseService end end - def supported_context? - super(@json) - end - def local_follower - @local_follower ||= @account.followers.local.without_suspended.first + return @local_follower if defined?(@local_follower) + + @local_follower = @account.followers.local.without_suspended.first end end diff --git a/spec/services/activitypub/fetch_featured_collection_service_spec.rb b/spec/services/activitypub/fetch_featured_collection_service_spec.rb new file mode 100644 index 000000000..f552b9dc0 --- /dev/null +++ b/spec/services/activitypub/fetch_featured_collection_service_spec.rb @@ -0,0 +1,123 @@ +require 'rails_helper' + +RSpec.describe ActivityPub::FetchFeaturedCollectionService, type: :service do + let(:actor) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/account', featured_collection_url: 'https://example.com/account/pinned') } + + let!(:known_status) { Fabricate(:status, account: actor, uri: 'https://example.com/account/pinned/1') } + + let(:status_json_1) do + { + '@context': 'https://www.w3.org/ns/activitystreams', + type: 'Note', + id: 'https://example.com/account/pinned/1', + content: 'foo', + attributedTo: actor.uri, + to: 'https://www.w3.org/ns/activitystreams#Public', + } + end + + let(:status_json_2) do + { + '@context': 'https://www.w3.org/ns/activitystreams', + type: 'Note', + id: 'https://example.com/account/pinned/2', + content: 'foo', + attributedTo: actor.uri, + to: 'https://www.w3.org/ns/activitystreams#Public', + } + end + + let(:status_json_4) do + { + '@context': 'https://www.w3.org/ns/activitystreams', + type: 'Note', + id: 'https://example.com/account/pinned/4', + content: 'foo', + attributedTo: actor.uri, + to: 'https://www.w3.org/ns/activitystreams#Public', + } + end + + let(:items) do + [ + 'https://example.com/account/pinned/1', # known + status_json_2, # unknown inlined + 'https://example.com/account/pinned/3', # unknown unreachable + 'https://example.com/account/pinned/4', # unknown reachable + ] + end + + let(:payload) do + { + '@context': 'https://www.w3.org/ns/activitystreams', + type: 'Collection', + id: actor.featured_collection_url, + items: items, + }.with_indifferent_access + end + + subject { described_class.new } + + shared_examples 'sets pinned posts' do + before do + stub_request(:get, 'https://example.com/account/pinned/1').to_return(status: 200, body: Oj.dump(status_json_1)) + stub_request(:get, 'https://example.com/account/pinned/2').to_return(status: 200, body: Oj.dump(status_json_2)) + stub_request(:get, 'https://example.com/account/pinned/3').to_return(status: 404) + stub_request(:get, 'https://example.com/account/pinned/4').to_return(status: 200, body: Oj.dump(status_json_4)) + + subject.call(actor) + end + + it 'sets expected posts as pinned posts' do + expect(actor.pinned_statuses.pluck(:uri)).to match_array ['https://example.com/account/pinned/1', 'https://example.com/account/pinned/2', 'https://example.com/account/pinned/4'] + end + end + + describe '#call' do + context 'when the endpoint is a Collection' do + before do + stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: Oj.dump(payload)) + end + + it_behaves_like 'sets pinned posts' + end + + context 'when the endpoint is an OrderedCollection' do + let(:payload) do + { + '@context': 'https://www.w3.org/ns/activitystreams', + type: 'OrderedCollection', + id: actor.featured_collection_url, + orderedItems: items, + }.with_indifferent_access + end + + before do + stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: Oj.dump(payload)) + end + + it_behaves_like 'sets pinned posts' + end + + context 'when the endpoint is a paginated Collection' do + let(:payload) do + { + '@context': 'https://www.w3.org/ns/activitystreams', + type: 'Collection', + id: actor.featured_collection_url, + first: { + type: 'CollectionPage', + partOf: actor.featured_collection_url, + items: items, + } + }.with_indifferent_access + end + + before do + stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: Oj.dump(payload)) + end + + it_behaves_like 'sets pinned posts' + end + end +end -- cgit From 0597e5db68a4b43b69d3bfcb93d0b0042d91b17f Mon Sep 17 00:00:00 2001 From: Claire Date: Mon, 2 May 2022 19:34:39 +0200 Subject: Fix ArgumentError when processing pinned posts from Friendica (#18260) Follow-up to #18254 --- app/services/activitypub/fetch_featured_collection_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/services') diff --git a/app/services/activitypub/fetch_featured_collection_service.rb b/app/services/activitypub/fetch_featured_collection_service.rb index e62470f70..37d05e055 100644 --- a/app/services/activitypub/fetch_featured_collection_service.rb +++ b/app/services/activitypub/fetch_featured_collection_service.rb @@ -32,7 +32,7 @@ class ActivityPub::FetchFeaturedCollectionService < BaseService return collection_or_uri if collection_or_uri.is_a?(Hash) return if invalid_origin?(collection_or_uri) - fetch_resource_without_id_validation(collection_or_uri, nil, true, local_follower) + fetch_resource_without_id_validation(collection_or_uri, local_follower, true) end def process_items(items) -- cgit