Add `account_activation_required` to /api/v1/instance
[akkoma] / lib / pleroma / workers / attachments_cleanup_worker.ex
index 3f421db4079c1a86841cc7f29f8683b809d8e9f0..49352db2a9306b9b690f2766ba74463d50d178b1 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Workers.AttachmentsCleanupWorker do
@@ -12,7 +12,10 @@ defmodule Pleroma.Workers.AttachmentsCleanupWorker do
 
   @impl Oban.Worker
   def perform(
-        %{"object" => %{"data" => %{"attachment" => [_ | _] = attachments, "actor" => actor}}},
+        %{
+          "op" => "cleanup_attachments",
+          "object" => %{"data" => %{"attachment" => [_ | _] = attachments, "actor" => actor}}
+        },
         _job
       ) do
     hrefs =
@@ -24,8 +27,20 @@ defmodule Pleroma.Workers.AttachmentsCleanupWorker 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()),
+        "/"
+      )
+
     # find all objects for copies of the attachments, name and actor doesn't matter here
-    delete_ids =
+    object_ids_and_hrefs =
       from(o in Object,
         where:
           fragment(
@@ -37,7 +52,7 @@ defmodule Pleroma.Workers.AttachmentsCleanupWorker do
       )
       # The query above can be time consumptive on large instances until we
       # refactor how uploads are stored
-      |> Repo.all(timout: :infinity)
+      |> Repo.all(timeout: :infinity)
       # we should delete 1 object for any given attachment, but don't delete
       # files if there are more than 1 object for it
       |> Enum.reduce(%{}, fn %{
@@ -64,25 +79,28 @@ defmodule Pleroma.Workers.AttachmentsCleanupWorker do
       |> Enum.map(fn {href, %{id: id, count: count}} ->
         # only delete files that have single instance
         with 1 <- count do
-          prefix =
-            case Pleroma.Config.get([Pleroma.Upload, :base_url]) do
-              nil -> "media"
-              _ -> ""
-            end
-
-          base_url = Pleroma.Config.get([__MODULE__, :base_url], Pleroma.Web.base_url())
-
-          file_path = String.trim_leading(href, "#{base_url}/#{prefix}")
+          href
+          |> String.trim_leading("#{base_url}/#{prefix}")
+          |> uploader.delete_file()
 
-          uploader.delete_file(file_path)
+          {id, href}
+        else
+          _ -> {id, nil}
         end
-
-        id
       end)
 
-    from(o in Object, where: o.id in ^delete_ids)
+    object_ids = Enum.map(object_ids_and_hrefs, fn {id, _} -> id end)
+
+    from(o in Object, where: o.id in ^object_ids)
     |> Repo.delete_all()
+
+    object_ids_and_hrefs
+    |> Enum.filter(fn {_, href} -> not is_nil(href) end)
+    |> Enum.map(&elem(&1, 1))
+    |> Pleroma.Web.MediaProxy.Invalidation.purge()
+
+    {:ok, :success}
   end
 
-  def perform(%{"object" => _object}, _job), do: :ok
+  def perform(%{"op" => "cleanup_attachments", "object" => _object}, _job), do: {:ok, :skip}
 end