about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2017-04-25 15:04:49 +0200
committerGitHub <noreply@github.com>2017-04-25 15:04:49 +0200
commit8b5179d006a07cf759e751e9d883bfe472cee868 (patch)
treee3ea9299e7a99c55b62b4ebcac1749304f6f54c0 /app/services
parent3ea5b948a4cee9ea5a1e229f567974c323947ef5 (diff)
Fix #2402 - Add Idempotency-Key header to PostStatusService that prevents (#2419)
duplicates. Web UI regenerates UUID for that header every time the compose
form is changed or successfully submitted

Also, fix Farsi i18n overwriting the English one
Diffstat (limited to 'app/services')
-rw-r--r--app/services/post_status_service.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/app/services/post_status_service.rb b/app/services/post_status_service.rb
index 6ce434a13..958cc2890 100644
--- a/app/services/post_status_service.rb
+++ b/app/services/post_status_service.rb
@@ -11,8 +11,14 @@ class PostStatusService < BaseService
   # @option [String] :spoiler_text
   # @option [Enumerable] :media_ids Optional array of media IDs to attach
   # @option [Doorkeeper::Application] :application
+  # @option [String] :idempotency Optional idempotency key
   # @return [Status]
   def call(account, text, in_reply_to = nil, options = {})
+    if options[:idempotency].present?
+      existing_id = redis.get("idempotency:status:#{account.id}:#{options[:idempotency]}")
+      return Status.find(existing_id) if existing_id
+    end
+
     media  = validate_media!(options[:media_ids])
     status = account.statuses.create!(text: text,
                                       thread: in_reply_to,
@@ -30,6 +36,10 @@ class PostStatusService < BaseService
     DistributionWorker.perform_async(status.id)
     Pubsubhubbub::DistributionWorker.perform_async(status.stream_entry.id)
 
+    if options[:idempotency].present?
+      redis.setex("idempotency:status:#{account.id}:#{options[:idempotency]}", 3_600, status.id)
+    end
+
     status
   end
 
@@ -63,4 +73,8 @@ class PostStatusService < BaseService
   def process_hashtags_service
     @process_hashtags_service ||= ProcessHashtagsService.new
   end
+
+  def redis
+    Redis.current
+  end
 end