Accept status_map and spoiler_text_map in POST statuses

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
tusooa 2023-01-02 22:13:44 -05:00 committed by marcin mikołajczak
parent 0d96c04019
commit 72a2b3329e
3 changed files with 105 additions and 11 deletions

View file

@ -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

View file

@ -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,

View file

@ -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)