and i yoink (#275)
authorfloatingghost <hannah@coffee-and-dreams.uk>
Mon, 14 Nov 2022 15:07:26 +0000 (15:07 +0000)
committerfloatingghost <hannah@coffee-and-dreams.uk>
Mon, 14 Nov 2022 15:07:26 +0000 (15:07 +0000)
Co-authored-by: Mark Felder <feld@feld.me>
Co-authored-by: FloatingGhost <hannah@coffee-and-dreams.uk>
Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/275

lib/pleroma/web/federator.ex
lib/pleroma/workers/backup_worker.ex
lib/pleroma/workers/purge_expired_activity.ex
lib/pleroma/workers/purge_expired_filter.ex
lib/pleroma/workers/purge_expired_token.ex
lib/pleroma/workers/worker_helper.ex
test/pleroma/workers/publisher_worker_test.exs [new file with mode: 0644]

index 770044de26bbe38566053df5dd99886c3c28bd9b..3a00424c67b72ec848e5a49cc4578e8cbf64ffe4 100644 (file)
@@ -48,7 +48,9 @@ defmodule Pleroma.Web.Federator do
 
   @impl true
   def publish(%{data: %{"object" => object}} = activity) when is_binary(object) do
-    PublisherWorker.enqueue("publish", %{"activity_id" => activity.id, "object_data" => nil})
+    PublisherWorker.enqueue("publish", %{"activity_id" => activity.id, "object_data" => nil},
+      priority: publish_priority(activity)
+    )
   end
 
   @impl true
@@ -63,7 +65,7 @@ defmodule Pleroma.Web.Federator do
     )
   end
 
-  defp publish_priority(%{type: "Delete"}), do: 3
+  defp publish_priority(%{data: %{"type" => "Delete"}}), do: 3
   defp publish_priority(_), do: 0
 
   # Job Worker Callbacks
index 4ab08706e6a903109715dd49fcc837f6d47a2754..cf78f1cb9048bc9f574c6d35ebe0f5f381533208 100644 (file)
@@ -16,7 +16,7 @@ defmodule Pleroma.Workers.BackupWorker do
 
   @impl Oban.Worker
   def timeout(_job) do
-    Pleroma.Config.get([:workers, :timeout, :backup]) || :timer.minutes(1)
+    Pleroma.Config.get([:workers, :timeout, :backup], :timer.minutes(1))
   end
 
   def schedule_deletion(backup) do
index ece84df3735cf9f5740970da1120beb70c1c2232..652e1d6b19008c563f4c973122eba6f9f2450ce7 100644 (file)
@@ -29,7 +29,7 @@ defmodule Pleroma.Workers.PurgeExpiredActivity do
 
   @impl Oban.Worker
   def timeout(_job) do
-    Pleroma.Config.get([:workers, :timeout, :activity_expiration]) || :timer.minutes(1)
+    Pleroma.Config.get([:workers, :timeout, :activity_expiration], :timer.minutes(1))
   end
 
   @impl true
index dd6a22a689340f21b10cf809e5990d481ca36050..593380d13e405c4efc546fc071dd1b1469c7281f 100644 (file)
@@ -26,7 +26,7 @@ defmodule Pleroma.Workers.PurgeExpiredFilter do
 
   @impl Oban.Worker
   def timeout(_job) do
-    Pleroma.Config.get([:workers, :timeout, :filter_expiration]) || :timer.minutes(1)
+    Pleroma.Config.get([:workers, :timeout, :filter_expiration], :timer.minutes(1))
   end
 
   @impl true
index 1773aeff98f1b810873983b3f89b6d1c9736c556..b4db84f4ebceb3b2b54a9fb68923f4911b3a7a1d 100644 (file)
@@ -21,7 +21,7 @@ defmodule Pleroma.Workers.PurgeExpiredToken do
 
   @impl Oban.Worker
   def timeout(_job) do
-    Pleroma.Config.get([:workers, :timeout, :token_expiration]) || :timer.minutes(1)
+    Pleroma.Config.get([:workers, :timeout, :token_expiration], :timer.minutes(1))
   end
 
   @impl true
index 97c0e4e5c73e426ced7908c16e0dc9853d63e0e1..4c0a55774d6df3364b56ac2a639d3a6ac413060a 100644 (file)
@@ -47,7 +47,7 @@ defmodule Pleroma.Workers.WorkerHelper do
       @impl Oban.Worker
       def timeout(_job) do
         queue_atom = String.to_atom(unquote(queue))
-        Config.get([:workers, :timeout, queue_atom]) || :timer.minutes(1)
+        Config.get([:workers, :timeout, queue_atom], :timer.minutes(1))
       end
     end
   end
diff --git a/test/pleroma/workers/publisher_worker_test.exs b/test/pleroma/workers/publisher_worker_test.exs
new file mode 100644 (file)
index 0000000..cf0ac0c
--- /dev/null
@@ -0,0 +1,52 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Workers.PublisherWorkerTest do
+  use Pleroma.DataCase, async: true
+  use Oban.Testing, repo: Pleroma.Repo
+
+  import Pleroma.Factory
+
+  alias Pleroma.Object
+  alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.Web.ActivityPub.Builder
+  alias Pleroma.Web.CommonAPI
+  alias Pleroma.Web.Federator
+
+  describe "Oban job priority:" do
+    setup do
+      user = insert(:user)
+
+      {:ok, post} = CommonAPI.post(user, %{status: "Regrettable post"})
+      object = Object.normalize(post, fetch: false)
+      {:ok, delete_data, _meta} = Builder.delete(user, object.data["id"])
+      {:ok, delete, _meta} = ActivityPub.persist(delete_data, local: true)
+
+      %{
+        post: post,
+        delete: delete
+      }
+    end
+
+    test "Deletions are lower priority", %{delete: delete} do
+      assert {:ok, %Oban.Job{priority: 3}} = Federator.publish(delete)
+    end
+
+    test "Creates are normal priority", %{post: post} do
+      assert {:ok, %Oban.Job{priority: 0}} = Federator.publish(post)
+    end
+  end
+
+  describe "Oban job timeout" do
+    test "should have a timeout" do
+      clear_config([:workers, :timeout, :federator_outgoing], :timer.minutes(2))
+      assert Pleroma.Workers.PublisherWorker.timeout(nil) == :timer.minutes(2)
+    end
+
+    test "should use a default timeout if none specified" do
+      clear_config([:workers, :timeout, :federator_outgoing])
+      assert Pleroma.Workers.PublisherWorker.timeout(nil) == :timer.seconds(10)
+    end
+  end
+end