about summary refs log tree commit diff
path: root/app/services/activitypub/fetch_featured_collection_service.rb
diff options
context:
space:
mode:
authorClaire <claire.github-309c@sitedethib.com>2022-05-02 17:41:01 +0200
committerGitHub <noreply@github.com>2022-05-02 17:41:01 +0200
commit71d02ffcf3a79dfc1c413dcc7ff45c77ce9cb94c (patch)
tree2e1ba02ccb3b10717d9c7570c0320ab5d533ebbd /app/services/activitypub/fetch_featured_collection_service.rb
parent6b7765a73bfd74d42006629eb2aa2cebed1dc2b0 (diff)
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
Diffstat (limited to 'app/services/activitypub/fetch_featured_collection_service.rb')
-rw-r--r--app/services/activitypub/fetch_featured_collection_service.rb34
1 files changed, 23 insertions, 11 deletions
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