From 230a012f0090c496fc5cdb011bcc8ed732fd0f5c Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 3 Mar 2019 22:18:23 +0100 Subject: 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 --- app/models/status.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'app/models/status.rb') diff --git a/app/models/status.rb b/app/models/status.rb index 035423b40..db3c130de 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -21,6 +21,7 @@ # account_id :bigint(8) not null # application_id :bigint(8) # in_reply_to_account_id :bigint(8) +# poll_id :bigint(8) # class Status < ApplicationRecord @@ -44,6 +45,7 @@ class Status < ApplicationRecord belongs_to :account, inverse_of: :statuses belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account', optional: true belongs_to :conversation, optional: true + belongs_to :poll, optional: true belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies, optional: true belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, optional: true @@ -61,6 +63,7 @@ class Status < ApplicationRecord has_one :notification, as: :activity, dependent: :destroy has_one :stream_entry, as: :activity, inverse_of: :status has_one :status_stat, inverse_of: :status + has_one :owned_poll, class_name: 'Poll', inverse_of: :status, dependent: :destroy validates :uri, uniqueness: true, presence: true, unless: :local? validates :text, presence: true, unless: -> { with_media? || reblog? } @@ -101,6 +104,7 @@ class Status < ApplicationRecord :tags, :preview_cards, :stream_entry, + :poll, account: :account_stat, active_mentions: { account: :account_stat }, reblog: [ @@ -111,6 +115,7 @@ class Status < ApplicationRecord :media_attachments, :conversation, :status_stat, + :poll, account: :account_stat, active_mentions: { account: :account_stat }, ], @@ -250,6 +255,8 @@ class Status < ApplicationRecord before_validation :set_conversation before_validation :set_local + before_save :set_poll_id + class << self def selectable_visibilities visibilities.keys - %w(direct limited) @@ -438,6 +445,10 @@ class Status < ApplicationRecord self.reblog = reblog.reblog if reblog? && reblog.reblog? end + def set_poll_id + self.poll_id = owned_poll.id unless owned_poll.nil? + end + def set_visibility self.visibility = (account.locked? ? :private : :public) if visibility.nil? self.visibility = reblog.visibility if reblog? -- cgit From e13d3792f322c6313fde10d639b13bc31723ec63 Mon Sep 17 00:00:00 2001 From: ThibG Date: Mon, 4 Mar 2019 00:39:06 +0100 Subject: Make sure the poll is created before storing its id (#10142) * Make sure the poll is created before storing its id * Fix updating poll results * Support fetching Question activities from the search bar --- app/models/status.rb | 4 ++-- app/services/activitypub/fetch_remote_poll_service.rb | 2 +- app/services/resolve_url_service.rb | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'app/models/status.rb') diff --git a/app/models/status.rb b/app/models/status.rb index db3c130de..74deeeb50 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -255,7 +255,7 @@ class Status < ApplicationRecord before_validation :set_conversation before_validation :set_local - before_save :set_poll_id + after_create :set_poll_id class << self def selectable_visibilities @@ -446,7 +446,7 @@ class Status < ApplicationRecord end def set_poll_id - self.poll_id = owned_poll.id unless owned_poll.nil? + update_column(:poll_id, owned_poll.id) unless owned_poll.nil? end def set_visibility diff --git a/app/services/activitypub/fetch_remote_poll_service.rb b/app/services/activitypub/fetch_remote_poll_service.rb index 6f0ac5624..ea75e8ef9 100644 --- a/app/services/activitypub/fetch_remote_poll_service.rb +++ b/app/services/activitypub/fetch_remote_poll_service.rb @@ -46,6 +46,6 @@ class ActivityPub::FetchRemotePollService < BaseService end def expected_type? - equals_or_includes_any?(@json['type'], 'Question') + equals_or_includes_any?(@json['type'], %w(Question)) end end diff --git a/app/services/resolve_url_service.rb b/app/services/resolve_url_service.rb index ed0c56923..b98759bf6 100644 --- a/app/services/resolve_url_service.rb +++ b/app/services/resolve_url_service.rb @@ -20,7 +20,7 @@ class ResolveURLService < BaseService def process_url if equals_or_includes_any?(type, %w(Application Group Organization Person Service)) FetchRemoteAccountService.new.call(atom_url, body, protocol) - elsif equals_or_includes_any?(type, %w(Note Article Image Video Page)) + elsif equals_or_includes_any?(type, %w(Note Article Image Video Page Question)) FetchRemoteStatusService.new.call(atom_url, body, protocol) end end -- cgit From 0c43c320dbae8f72f5f5c40e7a7944196cce368a Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 5 Mar 2019 15:21:31 +0100 Subject: Fix status creation API silently discarding invalid poll (#10171) --- app/models/status.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'app/models/status.rb') diff --git a/app/models/status.rb b/app/models/status.rb index 74deeeb50..f33130dd6 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -70,6 +70,7 @@ class Status < ApplicationRecord validates_with StatusLengthValidator validates_with DisallowedHashtagsValidator validates :reblog, uniqueness: { scope: :account }, if: :reblog? + validates_associated :owned_poll default_scope { recent } -- cgit