Output attachment objects when listing uploaded media, error if used

Produce an error if the attachment is used in any posts
This commit is contained in:
Ekaterina Vaartis 2026-11-03 20:28:25 +03:00
commit 70de22bf9b
4 changed files with 18 additions and 7 deletions

View file

@ -83,7 +83,8 @@ defmodule Pleroma.Web.ApiSpec.MediaOperation do
security: [%{"oAuth" => ["write:media"]}],
parameters: [id_param()],
responses: %{
200 => Operation.response("Media", "application/json", Attachment),
200 => Operation.response("Media", "text/plain", nil),
400 => Operation.response("Media", "application/json", ApiError)
}
}
end

View file

@ -397,7 +397,7 @@ defmodule Pleroma.Web.ApiSpec.PleromaUtilOperation do
def uploaded_media_operation do
%Operation{
tags: ["Media"],
summary: "List uploaded media",
summary: "List media uploaded by the current user",
security: [%{"oAuth" => ["read:accounts"]}],
operationId: "PleromaAPI.UtilController.uploaded_media",
responses: %{
@ -411,9 +411,7 @@ defmodule Pleroma.Web.ApiSpec.PleromaUtilOperation do
example: ["foo@example.org"]
}
}
}),
400 => Operation.response("Error", "application/json", ApiError),
403 => Operation.response("Error", "application/json", ApiError)
})
}
}
end

View file

@ -79,6 +79,7 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do
end
end
@doc "DELETE /api/v1/media/:id"
def delete(
%{
assigns: %{user: user},
@ -88,11 +89,17 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do
) do
with %Object{} = object <- Object.get_by_id(id),
:ok <- Object.authorize_access(object, user),
href <- object.data["url"] |> List.first |> Map.get("href"),
{:used, []} <- {:used, Pleroma.Upload.list_objects_with_attachment(href)},
{:ok, _, _} <- Object.delete(object) do
conn
|> put_resp_header("content-type", "text/plain")
|> send_resp(200, "")
else
{:used, list} ->
conn
|> put_status(:bad_request)
|> json(%{error: "Refusing to delete attachment that is still used by #{Enum.count(list)} objects"})
end
end

View file

@ -286,7 +286,12 @@ defmodule Pleroma.Web.PleromaAPI.UtilController do
Pleroma.Upload.list_uploaded_by(user, 0, 50)
|> Enum.map(
fn %Pleroma.Object{ id: id, data: %{ "url" => [ mediaUrl ], "name" => name }, inserted_at: uploaded } ->
Map.merge(%{ id: id, uploaded: uploaded, name: name }, mediaUrl) |> Map.drop(["type"]) end
%{
uploaded: uploaded, attachment: %{
description: name, id: to_string(id), url: mediaUrl["href"], pleroma: %{ mime_type: mediaUrl["mediaType"] }, type: "unknown"
}
}
end
)
json(conn, result)