]> cat aescling's git repositories - mastodon.git/commitdiff
Fix media not being marked sensitive when client sets a CW but no text (#13277)
authorThibG <thib@sitedethib.com>
Wed, 25 Mar 2020 21:40:58 +0000 (22:40 +0100)
committerGitHub <noreply@github.com>
Wed, 25 Mar 2020 21:40:58 +0000 (22:40 +0100)
Mastodon enforces the “sensitive” flag on media attachments whenever a toot
is posted with a Content Warning. However, it does so *after* potentially
converting the Content Warning to toot text (when there is no toot text),
which leads to inconsistent and surprising behavior for API clients.
This commit fixes this inconsistency.

app/services/post_status_service.rb
spec/services/post_status_service_spec.rb

index c61b3baa256970f63f78428e7c7d6bad8d8c2466..0a383d6a392afefc326dd28c2651bea3a517a5ce 100644 (file)
@@ -48,6 +48,7 @@ class PostStatusService < BaseService
   private
 
   def preprocess_attributes!
+    @sensitive    = (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present?
     @text         = @options.delete(:spoiler_text) if @text.blank? && @options[:spoiler_text].present?
     @visibility   = @options[:visibility] || @account.user&.setting_default_privacy
     @visibility   = :unlisted if @visibility&.to_sym == :public && @account.silenced?
@@ -157,7 +158,7 @@ class PostStatusService < BaseService
       media_attachments: @media || [],
       thread: @in_reply_to,
       poll_attributes: poll_attributes,
-      sensitive: (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present?,
+      sensitive: @sensitive,
       spoiler_text: @options[:spoiler_text] || '',
       visibility: @visibility,
       language: language_from_option(@options[:language]) || @account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(@text, @account),
index 025a3da40c1dea17f29377234f5d83dcffb5c13b..147a59fc315c7c81c702315b288b0059369ed0fc 100644 (file)
@@ -79,6 +79,13 @@ RSpec.describe PostStatusService, type: :service do
     expect(status.spoiler_text).to eq spoiler_text
   end
 
+  it 'creates a sensitive status when there is a CW but no text' do
+    status = subject.call(Fabricate(:account), text: '', spoiler_text: 'foo')
+
+    expect(status).to be_persisted
+    expect(status).to be_sensitive
+  end
+
   it 'creates a status with empty default spoiler text' do
     status = create_status_with_options(spoiler_text: nil)