Skip to content

Commit

Permalink
Fix Idempotency-Key ignored when scheduling a post (mastodon#30084)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire authored and noellabo committed Sep 19, 2024
1 parent 1842080 commit 5ff07d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions app/services/post_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def idempotency_given?

def idempotency_duplicate
if scheduled?
@account.schedule_statuses.find(@idempotency_duplicate)
@account.scheduled_statuses.find(@idempotency_duplicate)
else
@account.statuses.find(@idempotency_duplicate)
end
Expand Down Expand Up @@ -292,12 +292,12 @@ def poll_attributes
end

def scheduled_options
@options.tap do |options_hash|
options_hash[:in_reply_to_id] = options_hash.delete(:thread)&.id&.to_s
@options.dup.tap do |options_hash|
options_hash[:in_reply_to_id] = options_hash.delete(:thread)&.id
options_hash[:application_id] = options_hash.delete(:application)&.id
options_hash[:scheduled_at] = nil
options_hash[:idempotency] = nil
options_hash[:status_reference_ids] = options_hash[:status_reference_ids]&.map(&:to_s)&.filter{ |id| id != options_hash[:quote_id] }
options_hash[:status_reference_ids] = options_hash.delete(:status_reference_ids)&.map(&:to_s)&.filter{ |id| id != options_hash[:quote_id] }
options_hash[:with_rate_limit] = false
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/services/post_status_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@
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

it 'does not change statuses count' do
account = Fabricate(:account)
future = Time.now.utc + 2.hours
previous_status = Fabricate(:status, account: account)

expect { subject.call(account, text: 'Hi future!', scheduled_at: future, thread: previous_status) }.not_to change { [account.statuses_count, previous_status.replies_count] }
end

it 'returns existing status when used twice with idempotency key' do
account = Fabricate(:account)
future = Time.now.utc + 2.hours

status1 = subject.call(account, text: 'test', idempotency: 'meepmeep', scheduled_at: future)
status2 = subject.call(account, text: 'test', idempotency: 'meepmeep', scheduled_at: future)
expect(status2.id).to eq status1.id
end

it 'creates response to the original status of boost' do
Expand Down

0 comments on commit 5ff07d4

Please sign in to comment.