Remove _misskey_reaction matching (#500)
[akkoma] / lib / pleroma / web / plugs / idempotency_plug.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Plugs.IdempotencyPlug do
6 import Phoenix.Controller, only: [json: 2]
7 import Plug.Conn
8
9 @behaviour Plug
10
11 @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
12
13 @impl true
14 def init(opts), do: opts
15
16 # Sending idempotency keys in `GET` and `DELETE` requests has no effect
17 # and should be avoided, as these requests are idempotent by definition.
18
19 @impl true
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)
23 _ -> conn
24 end
25 end
26
27 def call(conn, _), do: conn
28
29 def process_request(conn, key) do
30 case @cachex.get(:idempotency_cache, key) do
31 {:ok, nil} ->
32 cache_resposnse(conn, key)
33
34 {:ok, record} ->
35 send_cached(conn, key, record)
36
37 {atom, message} when atom in [:ignore, :error] ->
38 render_error(conn, message)
39 end
40 end
41
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)
46
47 record = {request_id, content_type, conn.status, conn.resp_body}
48 {:ok, _} = @cachex.put(:idempotency_cache, key, record)
49
50 conn
51 |> put_resp_header("idempotency-key", key)
52 |> put_resp_header("x-original-request-id", request_id)
53 end)
54 end
55
56 defp send_cached(conn, key, record) do
57 {request_id, content_type, status, body} = record
58
59 conn
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)
65 |> halt()
66 end
67
68 defp render_error(conn, message) do
69 conn
70 |> put_status(:unprocessable_entity)
71 |> json(%{error: message})
72 |> halt()
73 end
74
75 defp get_content_type(conn) do
76 [content_type] = get_resp_header(conn, "content-type")
77
78 if String.contains?(content_type, ";") do
79 content_type
80 |> String.split(";")
81 |> hd()
82 else
83 content_type
84 end
85 end
86 end