about summary refs log tree commit diff
path: root/app/serializers
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2019-03-05 22:20:58 +0100
committerGitHub <noreply@github.com>2019-03-05 22:20:58 +0100
commit772b4ba24c60e0394d25d0fad4eefb338a9befea (patch)
tree804bb8ba01b1ff55471cc558d72456d2b9d0aea9 /app/serializers
parent2a4ce7458a16c64029842fde210089453be2ede1 (diff)
parent866496ac16b47ee17138db600519a630be833e4e (diff)
Merge pull request #935 from ThibG/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'app/serializers')
-rw-r--r--app/serializers/activitypub/note_serializer.rb69
-rw-r--r--app/serializers/activitypub/vote_serializer.rb52
-rw-r--r--app/serializers/rest/poll_serializer.rb38
-rw-r--r--app/serializers/rest/status_serializer.rb1
-rw-r--r--app/serializers/rss/account_serializer.rb2
5 files changed, 160 insertions, 2 deletions
diff --git a/app/serializers/activitypub/note_serializer.rb b/app/serializers/activitypub/note_serializer.rb
index 4aab993a9..3a9e388a5 100644
--- a/app/serializers/activitypub/note_serializer.rb
+++ b/app/serializers/activitypub/note_serializer.rb
@@ -15,12 +15,18 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer
 
   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
@@ -38,6 +44,7 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer
   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),
@@ -114,6 +121,36 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer
     object.account.local?
   end
 
+  def poll_options
+    if !object.poll.expired? && object.poll.hide_totals?
+      object.poll.unloaded_options
+    else
+      object.poll.loaded_options
+    end
+  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
 
@@ -181,4 +218,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
diff --git a/app/serializers/rest/poll_serializer.rb b/app/serializers/rest/poll_serializer.rb
new file mode 100644
index 000000000..b02e8ca93
--- /dev/null
+++ b/app/serializers/rest/poll_serializer.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+class REST::PollSerializer < ActiveModel::Serializer
+  attributes :id, :expires_at, :expired,
+             :multiple, :votes_count
+
+  has_many :dynamic_options, key: :options
+
+  attribute :voted, if: :current_user?
+
+  def id
+    object.id.to_s
+  end
+
+  def dynamic_options
+    if !object.expired? && object.hide_totals?
+      object.unloaded_options
+    else
+      object.loaded_options
+    end
+  end
+
+  def expired
+    object.expired?
+  end
+
+  def voted
+    object.votes.where(account: current_user.account).exists?
+  end
+
+  def current_user?
+    !current_user.nil?
+  end
+
+  class OptionSerializer < ActiveModel::Serializer
+    attributes :title, :votes_count
+  end
+end
diff --git a/app/serializers/rest/status_serializer.rb b/app/serializers/rest/status_serializer.rb
index b72eebb10..7185121d6 100644
--- a/app/serializers/rest/status_serializer.rb
+++ b/app/serializers/rest/status_serializer.rb
@@ -23,6 +23,7 @@ class REST::StatusSerializer < ActiveModel::Serializer
   has_many :emojis, serializer: REST::CustomEmojiSerializer
 
   has_one :preview_card, key: :card, serializer: REST::PreviewCardSerializer
+  has_one :poll, serializer: REST::PollSerializer
 
   def id
     object.id.to_s
diff --git a/app/serializers/rss/account_serializer.rb b/app/serializers/rss/account_serializer.rb
index bde360a41..712b1347a 100644
--- a/app/serializers/rss/account_serializer.rb
+++ b/app/serializers/rss/account_serializer.rb
@@ -22,7 +22,7 @@ class RSS::AccountSerializer
         item.title(status.title)
             .link(TagManager.instance.url_for(status))
             .pub_date(status.created_at)
-            .description(status.spoiler_text.presence || Formatter.instance.format(status).to_str)
+            .description(status.spoiler_text.presence || Formatter.instance.format(status, inline_poll_options: true).to_str)
 
         status.media_attachments.each do |media|
           item.enclosure(full_asset_url(media.file.url(:original, false)), media.file.content_type, length: media.file.size)