]> cat aescling's git repositories - mastodon.git/commitdiff
Fix scheduled toot with media immediately creating a toot (#9894)
authorThibG <thib@sitedethib.com>
Mon, 21 Jan 2019 19:03:04 +0000 (20:03 +0100)
committerEugen Rochko <eugen@zeonfederated.com>
Mon, 21 Jan 2019 19:03:04 +0000 (20:03 +0100)
* Add test for not persisting status when attaching media to scheduled toot

* Prevent status used for validation from being persisted to the database

Fixes #9893

Thanks to tateisu for the help investigating this.

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

index cc3453f995a1722cc9cd2dcfea2889b64f0de65c..1f5a3f4cf9ee5e4238cfe47613dd6611ed926d6e 100644 (file)
@@ -66,7 +66,10 @@ class PostStatusService < BaseService
   end
 
   def schedule_status!
-    if @account.statuses.build(status_attributes).valid?
+    status_for_validation = @account.statuses.build(status_attributes)
+    if status_for_validation.valid?
+      status_for_validation.destroy
+
       # The following transaction block is needed to wrap the UPDATEs to
       # the media attachments when the scheduled status is created
 
index 3774fed6f3e8a641e5bac4b515060ff9c2e5b5d8..680cebbcf383112023c6baff5eb7cd842c89ac83 100644 (file)
@@ -36,6 +36,20 @@ RSpec.describe PostStatusService, type: :service do
     expect(status.params['text']).to eq 'Hi future!'
   end
 
+  it 'does not immediately create a status when scheduling a status' do
+    account = Fabricate(:account)
+    media = Fabricate(:media_attachment)
+    future  = Time.now.utc + 2.hours
+
+    status = subject.call(account, text: 'Hi future!', media_ids: [media.id], scheduled_at: future)
+
+    expect(status).to be_a ScheduledStatus
+    expect(status.scheduled_at).to eq future
+    expect(status.params['text']).to eq 'Hi future!'
+    expect(media.reload.status).to be_nil
+    expect(Status.where(text: 'Hi future!').exists?).to be_falsey
+  end
+
   it 'creates response to the original status of boost' do
     boosted_status = Fabricate(:status)
     in_reply_to_status = Fabricate(:status, reblog: boosted_status)