diff --git a/lib/pleroma/web/mastodon_api/controllers/media_controller.ex b/lib/pleroma/web/mastodon_api/controllers/media_controller.ex index 9101fa1b67..079dcf7320 100644 --- a/lib/pleroma/web/mastodon_api/controllers/media_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/media_controller.ex @@ -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}, diff --git a/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs index 39c44443da..a171815a8b 100644 --- a/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/media_controller_test.exs @@ -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