use Pleroma.Workers.WorkerHelper, queue: "attachments_cleanup"
@impl Oban.Worker
- def perform(
- %{
+ def perform(%Job{
+ args: %{
"op" => "cleanup_attachments",
"object" => %{"data" => %{"attachment" => [_ | _] = attachments, "actor" => actor}}
- },
- _job
- ) do
- hrefs =
- Enum.flat_map(attachments, fn attachment ->
- Enum.map(attachment["url"], & &1["href"])
- end)
-
- # find all objects for copies of the attachments, name and actor doesn't matter here
- hrefs
+ }
+ }) do
+ attachments
+ |> Enum.flat_map(fn item -> Enum.map(item["url"], & &1["href"]) end)
|> fetch_objects
|> prepare_objects(actor, Enum.map(attachments, & &1["name"]))
- |> Enum.reduce({[], []}, fn {href, %{id: id, count: count}}, {ids, hrefs} ->
- with 1 <- count do
- {ids ++ [id], hrefs ++ [href]}
- else
- _ -> {ids ++ [id], hrefs}
- end
- end)
+ |> filter_objects
|> do_clean
{:ok, :success}
end
- def perform(%{"op" => "cleanup_attachments", "object" => _object}, _job), do: {:ok, :skip}
+ def perform(%Job{args: %{"op" => "cleanup_attachments", "object" => _object}}), do: {:ok, :skip}
defp do_clean({object_ids, attachment_urls}) do
uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
- prefix =
- case Pleroma.Config.get([Pleroma.Upload, :base_url]) do
- nil -> "media"
- _ -> ""
- end
-
base_url =
String.trim_trailing(
- Pleroma.Config.get([Pleroma.Upload, :base_url], Pleroma.Web.base_url()),
+ Pleroma.Upload.base_url(),
"/"
)
Enum.each(attachment_urls, fn href ->
href
- |> String.trim_leading("#{base_url}/#{prefix}")
+ |> String.trim_leading("#{base_url}")
|> uploader.delete_file()
end)
# we should delete 1 object for any given attachment, but don't delete
# files if there are more than 1 object for it
- def prepare_objects(objects, actor, names) do
+ defp filter_objects(objects) do
+ Enum.reduce(objects, {[], []}, fn {href, %{id: id, count: count}}, {ids, hrefs} ->
+ with 1 <- count do
+ {ids ++ [id], hrefs ++ [href]}
+ else
+ _ -> {ids ++ [id], hrefs}
+ end
+ end)
+ end
+
+ defp prepare_objects(objects, actor, names) do
objects
|> Enum.reduce(%{}, fn %{
id: id,
end)
end
- def fetch_objects(hrefs) do
+ defp fetch_objects(hrefs) do
from(o in Object,
where:
fragment(