From fcd3eca167508c1dde290f431353ace9d513de86 Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 4 May 2018 20:30:29 +0200 Subject: [PATCH] MastodonAPI: Support idempotency key. --- .../mastodon_api/mastodon_api_controller.ex | 14 ++++++++++++- .../mastodon_api_controller_test.exs | 21 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index c84c226e8..e2243538a 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -275,7 +275,19 @@ def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do |> Map.put("in_reply_to_status_id", params["in_reply_to_id"]) |> Map.put("no_attachment_links", true) - {:ok, activity} = CommonAPI.post(user, params) + idempotency_key = + case get_req_header(conn, "idempotency-key") do + [key] -> key + _ -> Ecto.UUID.generate() + end + + {:ok, activity} = + Cachex.get!( + :user_cache, + "idem:#{idempotency_key}", + fallback: fn _ -> CommonAPI.post(user, params) end + ) + render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}) end diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 5293b9364..432dca28a 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -63,9 +63,12 @@ test "the public timeline", %{conn: conn} do test "posting a status", %{conn: conn} do user = insert(:user) - conn = + idempotency_key = "Pikachu rocks!" + + conn_one = conn |> assign(:user, user) + |> put_req_header("idempotency-key", idempotency_key) |> post("/api/v1/statuses", %{ "status" => "cofe", "spoiler_text" => "2hu", @@ -73,9 +76,23 @@ test "posting a status", %{conn: conn} do }) assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} = - json_response(conn, 200) + json_response(conn_one, 200) assert Repo.get(Activity, id) + + conn_two = + conn + |> assign(:user, user) + |> put_req_header("idempotency-key", idempotency_key) + |> post("/api/v1/statuses", %{ + "status" => "cofe", + "spoiler_text" => "2hu", + "sensitive" => "false" + }) + + assert %{"id" => second_id} = json_response(conn_two, 200) + + assert id == second_id end test "posting a sensitive status", %{conn: conn} do