Add polls (#10111)
* Add polls Fix #1629 * Add tests * Fixes * Change API for creating polls * Use name instead of content for votes * Remove poll validation for remote polls * Add polls to public pages * When updating the poll, update options just in case they were changed * Fix public pages showing both poll and other media
This commit is contained in:
		| @@ -15,12 +15,18 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer | ||||
|  | ||||
|   has_one :replies, serializer: ActivityPub::CollectionSerializer, if: :local? | ||||
|  | ||||
|   has_many :poll_loaded_options, key: :one_of, if: :poll_and_not_multiple? | ||||
|   has_many :poll_loaded_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,32 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer | ||||
|     object.account.local? | ||||
|   end | ||||
|  | ||||
|   def poll_loaded_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 | ||||
|  | ||||
| @@ -181,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 | ||||
|   | ||||
							
								
								
									
										48
									
								
								app/serializers/activitypub/vote_serializer.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								app/serializers/activitypub/vote_serializer.rb
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,48 @@ | ||||
| # 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 | ||||
|       nil | ||||
|     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 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 | ||||
|     nil | ||||
|   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 | ||||
		Reference in New Issue
	
	Block a user