about summary refs log tree commit diff
path: root/app/serializers/activitypub
diff options
context:
space:
mode:
Diffstat (limited to 'app/serializers/activitypub')
-rw-r--r--app/serializers/activitypub/activity_serializer.rb10
-rw-r--r--app/serializers/activitypub/collection_serializer.rb5
-rw-r--r--app/serializers/activitypub/note_serializer.rb82
-rw-r--r--app/serializers/activitypub/vote_serializer.rb52
4 files changed, 142 insertions, 7 deletions
diff --git a/app/serializers/activitypub/activity_serializer.rb b/app/serializers/activitypub/activity_serializer.rb
index b51e8c544..c001e28aa 100644
--- a/app/serializers/activitypub/activity_serializer.rb
+++ b/app/serializers/activitypub/activity_serializer.rb
@@ -3,8 +3,8 @@
 class ActivityPub::ActivitySerializer < ActiveModel::Serializer
   attributes :id, :type, :actor, :published, :to, :cc
 
-  has_one :proper, key: :object, serializer: ActivityPub::NoteSerializer, unless: :owned_announce?
-  attribute :proper_uri, key: :object, if: :owned_announce?
+  has_one :proper, key: :object, serializer: ActivityPub::NoteSerializer, if: :serialize_object?
+  attribute :proper_uri, key: :object, unless: :serialize_object?
   attribute :atom_uri, if: :announce?
 
   def id
@@ -43,7 +43,9 @@ class ActivityPub::ActivitySerializer < ActiveModel::Serializer
     object.reblog?
   end
 
-  def owned_announce?
-    announce? && object.account == object.proper.account && object.proper.private_visibility?
+  def serialize_object?
+    return true unless announce?
+    # Serialize private self-boosts of local toots
+    object.account == object.proper.account && object.proper.private_visibility? && object.local?
   end
 end
diff --git a/app/serializers/activitypub/collection_serializer.rb b/app/serializers/activitypub/collection_serializer.rb
index e8960131b..b03609957 100644
--- a/app/serializers/activitypub/collection_serializer.rb
+++ b/app/serializers/activitypub/collection_serializer.rb
@@ -7,7 +7,8 @@ class ActivityPub::CollectionSerializer < ActiveModel::Serializer
     super
   end
 
-  attributes :id, :type
+  attribute :id, if: -> { object.id.present? }
+  attribute :type
   attribute :total_items, if: -> { object.size.present? }
   attribute :next, if: -> { object.next.present? }
   attribute :prev, if: -> { object.prev.present? }
@@ -37,6 +38,6 @@ class ActivityPub::CollectionSerializer < ActiveModel::Serializer
   end
 
   def page?
-    object.part_of.present?
+    object.part_of.present? || object.page.present?
   end
 end
diff --git a/app/serializers/activitypub/note_serializer.rb b/app/serializers/activitypub/note_serializer.rb
index c9d23e25f..553f333d8 100644
--- a/app/serializers/activitypub/note_serializer.rb
+++ b/app/serializers/activitypub/note_serializer.rb
@@ -13,12 +13,20 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer
   has_many :media_attachments, key: :attachment
   has_many :virtual_tags, key: :tag
 
+  has_one :replies, serializer: ActivityPub::CollectionSerializer, if: :local?
+
+  has_many :poll_options, key: :one_of, if: :poll_and_not_multiple?
+  has_many :poll_options, key: :any_of, if: :poll_and_multiple?
+
+  attribute :end_time, if: :poll_and_expires?
+  attribute :closed, if: :poll_and_expired?
+
   def id
     ActivityPub::TagManager.instance.uri_for(object)
   end
 
   def type
-    'Note'
+    object.poll ? 'Question' : 'Note'
   end
 
   def summary
@@ -33,6 +41,22 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer
     { object.language => Formatter.instance.format(object) }
   end
 
+  def replies
+    replies = object.self_replies(5).pluck(:id, :uri)
+    last_id = replies.last&.first
+
+    ActivityPub::CollectionPresenter.new(
+      type: :unordered,
+      id: ActivityPub::TagManager.instance.replies_uri_for(object),
+      first: ActivityPub::CollectionPresenter.new(
+        type: :unordered,
+        part_of: ActivityPub::TagManager.instance.replies_uri_for(object),
+        items: replies.map(&:second),
+        next: last_id ? ActivityPub::TagManager.instance.replies_uri_for(object, page: true, min_id: last_id) : nil
+      )
+    )
+  end
+
   def language?
     object.language.present?
   end
@@ -97,6 +121,32 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer
     object.account.local?
   end
 
+  def poll_options
+    object.poll.loaded_options
+  end
+
+  def poll_and_multiple?
+    object.poll&.multiple?
+  end
+
+  def poll_and_not_multiple?
+    object.poll && !object.poll.multiple?
+  end
+
+  def closed
+    object.poll.expires_at.iso8601
+  end
+
+  alias end_time closed
+
+  def poll_and_expires?
+    object.poll&.expires_at&.present?
+  end
+
+  def poll_and_expired?
+    object.poll&.expired?
+  end
+
   class MediaAttachmentSerializer < ActiveModel::Serializer
     include RoutingHelper
 
@@ -164,4 +214,34 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer
 
   class CustomEmojiSerializer < ActivityPub::EmojiSerializer
   end
+
+  class OptionSerializer < ActiveModel::Serializer
+    class RepliesSerializer < ActiveModel::Serializer
+      attributes :type, :total_items
+
+      def type
+        'Collection'
+      end
+
+      def total_items
+        object.votes_count
+      end
+    end
+
+    attributes :type, :name
+
+    has_one :replies, serializer: ActivityPub::NoteSerializer::OptionSerializer::RepliesSerializer
+
+    def type
+      'Note'
+    end
+
+    def name
+      object.title
+    end
+
+    def replies
+      object
+    end
+  end
 end
diff --git a/app/serializers/activitypub/vote_serializer.rb b/app/serializers/activitypub/vote_serializer.rb
new file mode 100644
index 000000000..248190404
--- /dev/null
+++ b/app/serializers/activitypub/vote_serializer.rb
@@ -0,0 +1,52 @@
+# frozen_string_literal: true
+
+class ActivityPub::VoteSerializer < ActiveModel::Serializer
+  class NoteSerializer < ActiveModel::Serializer
+    attributes :id, :type, :name, :attributed_to,
+               :in_reply_to, :to
+
+    def id
+      ActivityPub::TagManager.instance.uri_for(object) || [ActivityPub::TagManager.instance.uri_for(object.account), '#votes/', object.id].join
+    end
+
+    def type
+      'Note'
+    end
+
+    def name
+      object.poll.options[object.choice.to_i]
+    end
+
+    def attributed_to
+      ActivityPub::TagManager.instance.uri_for(object.account)
+    end
+
+    def in_reply_to
+      ActivityPub::TagManager.instance.uri_for(object.poll.status)
+    end
+
+    def to
+      ActivityPub::TagManager.instance.uri_for(object.poll.account)
+    end
+  end
+
+  attributes :id, :type, :actor, :to
+
+  has_one :object, serializer: ActivityPub::VoteSerializer::NoteSerializer
+
+  def id
+    [ActivityPub::TagManager.instance.uri_for(object.account), '#votes/', object.id, '/activity'].join
+  end
+
+  def type
+    'Create'
+  end
+
+  def actor
+    ActivityPub::TagManager.instance.uri_for(object.account)
+  end
+
+  def to
+    ActivityPub::TagManager.instance.uri_for(object.poll.account)
+  end
+end