Merge branch 'preload-data' into 'develop'
[akkoma] / lib / pleroma / workers / attachments_cleanup_worker.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Workers.AttachmentsCleanupWorker do
6 import Ecto.Query
7
8 alias Pleroma.Object
9 alias Pleroma.Repo
10
11 use Pleroma.Workers.WorkerHelper, queue: "attachments_cleanup"
12
13 @impl Oban.Worker
14 def perform(
15 %{
16 "op" => "cleanup_attachments",
17 "object" => %{"data" => %{"attachment" => [_ | _] = attachments, "actor" => actor}}
18 },
19 _job
20 ) do
21 attachments
22 |> Enum.flat_map(fn item -> Enum.map(item["url"], & &1["href"]) end)
23 |> fetch_objects
24 |> prepare_objects(actor, Enum.map(attachments, & &1["name"]))
25 |> filter_objects
26 |> do_clean
27
28 {:ok, :success}
29 end
30
31 def perform(%{"op" => "cleanup_attachments", "object" => _object}, _job), do: {:ok, :skip}
32
33 defp do_clean({object_ids, attachment_urls}) do
34 uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
35
36 prefix =
37 case Pleroma.Config.get([Pleroma.Upload, :base_url]) do
38 nil -> "media"
39 _ -> ""
40 end
41
42 base_url =
43 String.trim_trailing(
44 Pleroma.Config.get([Pleroma.Upload, :base_url], Pleroma.Web.base_url()),
45 "/"
46 )
47
48 Enum.each(attachment_urls, fn href ->
49 href
50 |> String.trim_leading("#{base_url}/#{prefix}")
51 |> uploader.delete_file()
52 end)
53
54 delete_objects(object_ids)
55 end
56
57 defp delete_objects([_ | _] = object_ids) do
58 Repo.delete_all(from(o in Object, where: o.id in ^object_ids))
59 end
60
61 defp delete_objects(_), do: :ok
62
63 # we should delete 1 object for any given attachment, but don't delete
64 # files if there are more than 1 object for it
65 defp filter_objects(objects) do
66 Enum.reduce(objects, {[], []}, fn {href, %{id: id, count: count}}, {ids, hrefs} ->
67 with 1 <- count do
68 {ids ++ [id], hrefs ++ [href]}
69 else
70 _ -> {ids ++ [id], hrefs}
71 end
72 end)
73 end
74
75 defp prepare_objects(objects, actor, names) do
76 objects
77 |> Enum.reduce(%{}, fn %{
78 id: id,
79 data: %{
80 "url" => [%{"href" => href}],
81 "actor" => obj_actor,
82 "name" => name
83 }
84 },
85 acc ->
86 Map.update(acc, href, %{id: id, count: 1}, fn val ->
87 case obj_actor == actor and name in names do
88 true ->
89 # set id of the actor's object that will be deleted
90 %{val | id: id, count: val.count + 1}
91
92 false ->
93 # another actor's object, just increase count to not delete file
94 %{val | count: val.count + 1}
95 end
96 end)
97 end)
98 end
99
100 defp fetch_objects(hrefs) do
101 from(o in Object,
102 where:
103 fragment(
104 "to_jsonb(array(select jsonb_array_elements((?)#>'{url}') ->> 'href' where jsonb_typeof((?)#>'{url}') = 'array'))::jsonb \\?| (?)",
105 o.data,
106 o.data,
107 ^hrefs
108 )
109 )
110 # The query above can be time consumptive on large instances until we
111 # refactor how uploads are stored
112 |> Repo.all(timeout: :infinity)
113 end
114 end