From 8d914847dea993cbb27d65abbe3cdc98a31f2a2d Mon Sep 17 00:00:00 2001
From: Eugen Rochko <eugen@zeonfederated.com>
Date: Sun, 18 Aug 2019 02:19:13 +0200
Subject: [PATCH] Add featured hashtags and endorsed accounts to ActivityPub
 featured collection

---
 .../activitypub/collections_controller.rb     | 16 ++++++++++---
 .../activitypub/collection_serializer.rb      | 15 +++++++++---
 .../activitypub/hashtag_serializer.rb         | 23 +++++++++++++++++++
 .../activitypub/mention_serializer.rb         | 17 ++++++++++++++
 4 files changed, 65 insertions(+), 6 deletions(-)
 create mode 100644 app/serializers/activitypub/hashtag_serializer.rb
 create mode 100644 app/serializers/activitypub/mention_serializer.rb

diff --git a/app/controllers/activitypub/collections_controller.rb b/app/controllers/activitypub/collections_controller.rb
index 989fee385d..a3dade822b 100644
--- a/app/controllers/activitypub/collections_controller.rb
+++ b/app/controllers/activitypub/collections_controller.rb
@@ -7,11 +7,13 @@ class ActivityPub::CollectionsController < ActivityPub::BaseController
   before_action :require_signature!, if: :authorized_fetch_mode?
   before_action :set_size
   before_action :set_statuses
+  before_action :set_featured_tags
+  before_action :set_endorsed_accounts
   before_action :set_cache_headers
 
   def show
     expires_in 3.minutes, public: public_fetch_mode?
-    render_with_cache json: collection_presenter, content_type: 'application/activity+json', serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, skip_activities: true
+    render_with_cache json: collection_presenter, content_type: 'application/activity+json', serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter
   end
 
   private
@@ -21,10 +23,18 @@ class ActivityPub::CollectionsController < ActivityPub::BaseController
     @statuses = cache_collection(@statuses, Status)
   end
 
+  def set_featured_tags
+    @featured_tags = @account.featured_tags
+  end
+
+  def set_endorsed_accounts
+    @endorsed_accounts = @account.endorsed_accounts
+  end
+
   def set_size
     case params[:id]
     when 'featured'
-      @account.pinned_statuses.count
+      @account.pinned_statuses.count + @account.featured_tags.count + @account.account_pins.count
     else
       raise ActiveRecord::RecordNotFound
     end
@@ -46,7 +56,7 @@ class ActivityPub::CollectionsController < ActivityPub::BaseController
       id: account_collection_url(@account, params[:id]),
       type: :ordered,
       size: @size,
-      items: @statuses
+      items: @statuses + @featured_tags + @endorsed_accounts
     )
   end
 end
diff --git a/app/serializers/activitypub/collection_serializer.rb b/app/serializers/activitypub/collection_serializer.rb
index da1ba735fc..38c4980807 100644
--- a/app/serializers/activitypub/collection_serializer.rb
+++ b/app/serializers/activitypub/collection_serializer.rb
@@ -2,9 +2,18 @@
 
 class ActivityPub::CollectionSerializer < ActivityPub::Serializer
   def self.serializer_for(model, options)
-    return ActivityPub::NoteSerializer if model.class.name == 'Status'
-    return ActivityPub::CollectionSerializer if model.class.name == 'ActivityPub::CollectionPresenter'
-    super
+    case model.class.name
+    when 'Status'
+      ActivityPub::NoteSerializer
+    when 'FeaturedTag'
+      ActivityPub::HashtagSerializer
+    when 'Account'
+      ActivityPub::MentionSerializer
+    when 'ActivityPub::CollectionPresenter'
+      ActivityPub::CollectionSerializer
+    else
+      super
+    end
   end
 
   attribute :id, if: -> { object.id.present? }
diff --git a/app/serializers/activitypub/hashtag_serializer.rb b/app/serializers/activitypub/hashtag_serializer.rb
new file mode 100644
index 0000000000..1a56e4dfe4
--- /dev/null
+++ b/app/serializers/activitypub/hashtag_serializer.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+class ActivityPub::HashtagSerializer < ActivityPub::Serializer
+  include RoutingHelper
+
+  attributes :type, :href, :name
+
+  def type
+    'Hashtag'
+  end
+
+  def name
+    "##{object.name}"
+  end
+
+  def href
+    if object.class.name == 'FeaturedTag'
+      short_account_tag_url(object.account, object.tag)
+    else
+      tag_url(object)
+    end
+  end
+end
diff --git a/app/serializers/activitypub/mention_serializer.rb b/app/serializers/activitypub/mention_serializer.rb
new file mode 100644
index 0000000000..24bd570fe9
--- /dev/null
+++ b/app/serializers/activitypub/mention_serializer.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class ActivityPub::MentionSerializer < ActivityPub::Serializer
+  attributes :type, :href, :name
+
+  def type
+    'Mention'
+  end
+
+  def href
+    ActivityPub::TagManager.instance.uri_for(object)
+  end
+
+  def name
+    "@#{object.acct}"
+  end
+end
-- 
GitLab