1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.IdempotencyPlug do
6 import Phoenix.Controller, only: [json: 2]
11 @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
14 def init(opts), do: opts
16 # Sending idempotency keys in `GET` and `DELETE` requests has no effect
17 # and should be avoided, as these requests are idempotent by definition.
20 def call(%{method: method} = conn, _) when method in ["POST", "PUT", "PATCH"] do
21 case get_req_header(conn, "idempotency-key") do
22 [key] -> process_request(conn, key)
27 def call(conn, _), do: conn
29 def process_request(conn, key) do
30 case @cachex.get(:idempotency_cache, key) do
32 cache_resposnse(conn, key)
35 send_cached(conn, key, record)
37 {atom, message} when atom in [:ignore, :error] ->
38 render_error(conn, message)
42 defp cache_resposnse(conn, key) do
43 register_before_send(conn, fn conn ->
44 [request_id] = get_resp_header(conn, "x-request-id")
45 content_type = get_content_type(conn)
47 record = {request_id, content_type, conn.status, conn.resp_body}
48 {:ok, _} = @cachex.put(:idempotency_cache, key, record)
51 |> put_resp_header("idempotency-key", key)
52 |> put_resp_header("x-original-request-id", request_id)
56 defp send_cached(conn, key, record) do
57 {request_id, content_type, status, body} = record
60 |> put_resp_header("idempotency-key", key)
61 |> put_resp_header("idempotent-replayed", "true")
62 |> put_resp_header("x-original-request-id", request_id)
63 |> put_resp_content_type(content_type)
64 |> send_resp(status, body)
68 defp render_error(conn, message) do
70 |> put_status(:unprocessable_entity)
71 |> json(%{error: message})
75 defp get_content_type(conn) do
76 [content_type] = get_resp_header(conn, "content-type")
78 if String.contains?(content_type, ";") do