From 72a2b3329ec18757cba4a87f8929f9572804f5c9 Mon Sep 17 00:00:00 2001 From: tusooa Date: Mon, 2 Jan 2023 22:13:44 -0500 Subject: [PATCH] Accept status_map and spoiler_text_map in POST statuses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- lib/pleroma/web/common_api/activity_draft.ex | 21 +++--- .../controllers/status_controller.ex | 20 +++++ .../controllers/status_controller_test.exs | 75 +++++++++++++++++++ 3 files changed, 105 insertions(+), 11 deletions(-) diff --git a/lib/pleroma/web/common_api/activity_draft.ex b/lib/pleroma/web/common_api/activity_draft.ex index 804a2162dd..967123288d 100644 --- a/lib/pleroma/web/common_api/activity_draft.ex +++ b/lib/pleroma/web/common_api/activity_draft.ex @@ -168,18 +168,17 @@ defp summary(%{params: params} = draft) do %__MODULE__{draft | summary: Map.get(params, :spoiler_text, "")} end - defp full_payload(%{status_map: status_map, summary_map: summary_map} = draft) do - status = status_map |> Enum.reduce("", fn {_lang, content}, acc -> acc <> content end) - summary = summary_map |> Enum.reduce("", fn {_lang, content}, acc -> acc <> content end) - full_payload = String.trim(status <> summary) - - case Utils.validate_character_limit(full_payload, draft.attachments) do - :ok -> %__MODULE__{draft | full_payload: full_payload} - {:error, message} -> add_error(draft, message) - end + defp full_payload(%{status: status, status_map: nil} = draft) do + full_payload(%__MODULE__{draft | status_map: %{"und" => status}}) end - defp full_payload(%{status: status, summary: summary} = draft) do + defp full_payload(%{summary: summary, summary_map: nil} = draft) do + full_payload(%__MODULE__{draft | summary_map: %{"und" => summary}}) + end + + defp full_payload(%{status_map: %{} = status_map, summary_map: %{} = summary_map} = draft) do + status = status_map |> Enum.reduce("", fn {_lang, content}, acc -> acc <> content end) + summary = summary_map |> Enum.reduce("", fn {_lang, content}, acc -> acc <> content end) full_payload = String.trim(status <> summary) case Utils.validate_character_limit(full_payload, draft.attachments) do @@ -497,7 +496,7 @@ defp validate(%{errors: [message | _]}), do: {:error, message} defp differentiate_string_map(%{} = map), do: {nil, map} defp differentiate_string_map(str) when is_binary(str), do: {str, nil} - defp get_source_map(%{status_map: status_map} = _draft) do + defp get_source_map(%{status_map: %{} = status_map} = _draft) do %{ "content" => Pleroma.MultiLanguage.map_to_str(status_map, mutiline: true), "contentMap" => status_map diff --git a/lib/pleroma/web/mastodon_api/controllers/status_controller.ex b/lib/pleroma/web/mastodon_api/controllers/status_controller.ex index a4085db617..efc1d9fd7d 100644 --- a/lib/pleroma/web/mastodon_api/controllers/status_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/status_controller.ex @@ -232,6 +232,26 @@ defp do_create( end end + def create( + %{ + assigns: %{user: _user}, + private: %{open_api_spex: %{body_params: %{status_map: _}}} = params + } = conn, + _ + ) do + create(conn |> put_in([:private, :open_api_spex, :body_params, :status], ""), %{}) + end + + def create( + %{ + assigns: %{user: _user}, + private: %{open_api_spex: %{body_params: %{media_ids: _}}} = params + } = conn, + _ + ) do + create(conn |> put_in([:private, :open_api_spex, :body_params, :status], ""), %{}) + end + @doc "GET /api/v1/statuses/:id/history" def show_history( %{assigns: assigns, private: %{open_api_spex: %{params: %{id: id} = params}}} = conn, diff --git a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs index a534890e1b..20b772b106 100644 --- a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs @@ -139,6 +139,81 @@ test "posting a status", %{conn: conn} do ) end + test "posting a multilang status", %{conn: conn} do + idempotency_key = "Pikachu rocks!" + + conn_one = + conn + |> put_req_header("content-type", "application/json") + |> put_req_header("idempotency-key", idempotency_key) + |> post("/api/v1/statuses", %{ + "status_map" => %{"a" => "mew mew", "b" => "lol lol"}, + "spoiler_text_map" => %{"a" => "mew", "b" => "lol"}, + "sensitive" => "0" + }) + + assert %{ + "content" => _content, + "content_map" => %{"a" => "mew mew", "b" => "lol lol"}, + "id" => id, + "spoiler_text" => _spoiler_text, + "spoiler_text_map" => %{"a" => "mew", "b" => "lol"}, + "sensitive" => false + } = json_response_and_validate_schema(conn_one, 200) + + assert Activity.get_by_id(id) + end + + test "posting a multilang status with singlelang summary", %{conn: conn} do + idempotency_key = "Pikachu rocks!" + + conn_one = + conn + |> put_req_header("content-type", "application/json") + |> put_req_header("idempotency-key", idempotency_key) + |> post("/api/v1/statuses", %{ + "status_map" => %{"a" => "mew mew", "b" => "lol lol"}, + "spoiler_text" => "mewlol", + "sensitive" => "0" + }) + + assert %{ + "content" => _content, + "content_map" => %{"a" => "mew mew", "b" => "lol lol"}, + "id" => id, + "spoiler_text" => "mewlol", + "spoiler_text_map" => %{}, + "sensitive" => false + } = json_response_and_validate_schema(conn_one, 200) + + assert Activity.get_by_id(id) + end + + test "posting a multilang summary with singlelang status", %{conn: conn} do + idempotency_key = "Pikachu rocks!" + + conn_one = + conn + |> put_req_header("content-type", "application/json") + |> put_req_header("idempotency-key", idempotency_key) + |> post("/api/v1/statuses", %{ + "spoiler_text_map" => %{"a" => "mew mew", "b" => "lol lol"}, + "status" => "mewlol", + "sensitive" => "0" + }) + + assert %{ + "content" => "mewlol", + "content_map" => %{}, + "id" => id, + "spoiler_text" => _, + "spoiler_text_map" => %{"a" => "mew mew", "b" => "lol lol"}, + "sensitive" => false + } = json_response_and_validate_schema(conn_one, 200) + + assert Activity.get_by_id(id) + end + test "posting a quote post", %{conn: conn} do user = insert(:user)