Fix detailed poll validation errors not being returned in the API (#10261)
No more "Owned poll is invalid"
This commit is contained in:
		
							parent
							
								
									c20d096e6a
								
							
						
					
					
						commit
						3ad3223b46
					
				@ -71,7 +71,8 @@ class Status < ApplicationRecord
 | 
				
			|||||||
  validates_with DisallowedHashtagsValidator
 | 
					  validates_with DisallowedHashtagsValidator
 | 
				
			||||||
  validates :reblog, uniqueness: { scope: :account }, if: :reblog?
 | 
					  validates :reblog, uniqueness: { scope: :account }, if: :reblog?
 | 
				
			||||||
  validates :visibility, exclusion: { in: %w(direct limited) }, if: :reblog?
 | 
					  validates :visibility, exclusion: { in: %w(direct limited) }, if: :reblog?
 | 
				
			||||||
  validates_associated :owned_poll
 | 
					
 | 
				
			||||||
 | 
					  accepts_nested_attributes_for :owned_poll
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  default_scope { recent }
 | 
					  default_scope { recent }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -29,7 +29,6 @@ class PostStatusService < BaseService
 | 
				
			|||||||
    return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
 | 
					    return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    validate_media!
 | 
					    validate_media!
 | 
				
			||||||
    validate_poll!
 | 
					 | 
				
			||||||
    preprocess_attributes!
 | 
					    preprocess_attributes!
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if scheduled?
 | 
					    if scheduled?
 | 
				
			||||||
@ -71,6 +70,7 @@ class PostStatusService < BaseService
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  def schedule_status!
 | 
					  def schedule_status!
 | 
				
			||||||
    status_for_validation = @account.statuses.build(status_attributes)
 | 
					    status_for_validation = @account.statuses.build(status_attributes)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if status_for_validation.valid?
 | 
					    if status_for_validation.valid?
 | 
				
			||||||
      status_for_validation.destroy
 | 
					      status_for_validation.destroy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -103,12 +103,6 @@ class PostStatusService < BaseService
 | 
				
			|||||||
    raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && @media.find(&:video?)
 | 
					    raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && @media.find(&:video?)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def validate_poll!
 | 
					 | 
				
			||||||
    return if @options[:poll].blank?
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @poll = @account.polls.new(@options[:poll])
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def language_from_option(str)
 | 
					  def language_from_option(str)
 | 
				
			||||||
    ISO_639.find(str)&.alpha2
 | 
					    ISO_639.find(str)&.alpha2
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
@ -161,13 +155,13 @@ class PostStatusService < BaseService
 | 
				
			|||||||
      text: @text,
 | 
					      text: @text,
 | 
				
			||||||
      media_attachments: @media || [],
 | 
					      media_attachments: @media || [],
 | 
				
			||||||
      thread: @in_reply_to,
 | 
					      thread: @in_reply_to,
 | 
				
			||||||
      owned_poll: @poll,
 | 
					      owned_poll_attributes: poll_attributes,
 | 
				
			||||||
      sensitive: (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present?,
 | 
					      sensitive: (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present?,
 | 
				
			||||||
      spoiler_text: @options[:spoiler_text] || '',
 | 
					      spoiler_text: @options[:spoiler_text] || '',
 | 
				
			||||||
      visibility: @visibility,
 | 
					      visibility: @visibility,
 | 
				
			||||||
      language: language_from_option(@options[:language]) || @account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(@text, @account),
 | 
					      language: language_from_option(@options[:language]) || @account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(@text, @account),
 | 
				
			||||||
      application: @options[:application],
 | 
					      application: @options[:application],
 | 
				
			||||||
    }
 | 
					    }.compact
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def scheduled_status_attributes
 | 
					  def scheduled_status_attributes
 | 
				
			||||||
@ -178,6 +172,12 @@ class PostStatusService < BaseService
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def poll_attributes
 | 
				
			||||||
 | 
					    return if @options[:poll].blank?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @options[:poll].merge(account: @account)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def scheduled_options
 | 
					  def scheduled_options
 | 
				
			||||||
    @options.tap do |options_hash|
 | 
					    @options.tap do |options_hash|
 | 
				
			||||||
      options_hash[:in_reply_to_id] = options_hash.delete(:thread)&.id
 | 
					      options_hash[:in_reply_to_id] = options_hash.delete(:thread)&.id
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,9 @@
 | 
				
			|||||||
---
 | 
					---
 | 
				
			||||||
en:
 | 
					en:
 | 
				
			||||||
  activerecord:
 | 
					  activerecord:
 | 
				
			||||||
 | 
					    attributes:
 | 
				
			||||||
 | 
					      status:
 | 
				
			||||||
 | 
					        owned_poll: Poll
 | 
				
			||||||
    errors:
 | 
					    errors:
 | 
				
			||||||
      models:
 | 
					      models:
 | 
				
			||||||
        account:
 | 
					        account:
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user