Accept description_map when updating media
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
a9b1589528
commit
c34a4d816f
2 changed files with 58 additions and 0 deletions
|
@ -73,6 +73,35 @@ def create2(
|
|||
def create2(_conn, _data), do: {:error, :bad_request}
|
||||
|
||||
@doc "PUT /api/v1/media/:id"
|
||||
def update(
|
||||
%{
|
||||
assigns: %{user: user},
|
||||
private: %{
|
||||
open_api_spex: %{
|
||||
body_params: %{description_map: %{} = description_map},
|
||||
params: %{id: id}
|
||||
}
|
||||
}
|
||||
} = conn,
|
||||
_
|
||||
) do
|
||||
with %Object{} = object <- Object.get_by_id(id),
|
||||
:ok <- Object.authorize_access(object, user),
|
||||
{_, {:ok, %{}}} <-
|
||||
{:description_map, Pleroma.MultiLanguage.validate_map(description_map)},
|
||||
{:ok, %Object{data: data}} <-
|
||||
Object.update_data(object, %{
|
||||
"name" => Pleroma.MultiLanguage.map_to_str(description_map),
|
||||
"nameMap" => description_map
|
||||
}) do
|
||||
attachment_data = Map.put(data, "id", object.id)
|
||||
|
||||
render(conn, "attachment.json", %{attachment: attachment_data})
|
||||
else
|
||||
{:description_map, _} -> render_error(conn, 422, "description_map not valid")
|
||||
end
|
||||
end
|
||||
|
||||
def update(
|
||||
%{
|
||||
assigns: %{user: user},
|
||||
|
|
|
@ -255,6 +255,35 @@ test "/api/v1/media/:id good request", %{conn: conn, object: object} do
|
|||
assert media["description"] == "test-media"
|
||||
assert refresh_record(object).data["name"] == "test-media"
|
||||
end
|
||||
|
||||
test "/api/v1/media/:id description_map", %{conn: conn, object: object} do
|
||||
media =
|
||||
conn
|
||||
|> put_req_header("content-type", "multipart/form-data")
|
||||
|> put("/api/v1/media/#{object.id}", %{
|
||||
"description_map" => %{"a" => "test-media", "b" => "xxx"}
|
||||
})
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|
||||
assert media["description_map"] == %{"a" => "test-media", "b" => "xxx"}
|
||||
assert refresh_record(object).data["nameMap"] == %{"a" => "test-media", "b" => "xxx"}
|
||||
end
|
||||
|
||||
test "/api/v1/media/:id description_map, invalid", %{conn: conn, object: object} do
|
||||
conn
|
||||
|> put_req_header("content-type", "multipart/form-data")
|
||||
|> put("/api/v1/media/#{object.id}", %{
|
||||
"description_map" => %{"a" => "test-media", "b_" => "xxx"}
|
||||
})
|
||||
|> json_response_and_validate_schema(422)
|
||||
end
|
||||
|
||||
test "/api/v1/media/:id description_map, empty", %{conn: conn, object: object} do
|
||||
conn
|
||||
|> put_req_header("content-type", "multipart/form-data")
|
||||
|> put("/api/v1/media/#{object.id}", %{"description_map" => %{}})
|
||||
|> json_response_and_validate_schema(422)
|
||||
end
|
||||
end
|
||||
|
||||
describe "Get media by id (/api/v1/media/:id)" do
|
||||
|
|
Loading…
Reference in a new issue