remove validate_expires_at from enqueue method
authorAlexander Strizhakov <alex.strizhakov@gmail.com>
Tue, 8 Sep 2020 12:11:18 +0000 (15:11 +0300)
committerrinpatch <rinpatch@sdf.org>
Thu, 10 Sep 2020 18:52:31 +0000 (21:52 +0300)
lib/mix/tasks/pleroma/database.ex
lib/pleroma/workers/purge_expired_activity.ex
priv/repo/migrations/20200825061316_move_activity_expirations_to_oban.exs
test/workers/purge_expired_activity_test.exs

index aab4b5e9a893729f6d1e792125886886765d8758..7f1108dcfa87cee488266a2cc1343be7824ece75 100644 (file)
@@ -155,8 +155,7 @@ defmodule Mix.Tasks.Pleroma.Database do
 
         Pleroma.Workers.PurgeExpiredActivity.enqueue(%{
           activity_id: activity.id,
-          expires_at: expires_at,
-          validate: false
+          expires_at: expires_at
         })
       end)
     end)
index ffcb89dc374a29bb249ad2cc10d14aa52ee71f83..c168890a25d02f1894c7ca2a7b76e37646b97cfb 100644 (file)
@@ -18,8 +18,7 @@ defmodule Pleroma.Workers.PurgeExpiredActivity do
           | {:error, :expired_activities_disabled}
           | {:error, :expiration_too_close}
   def enqueue(args) do
-    with true <- enabled?(),
-         args when is_map(args) <- validate_expires_at(args) do
+    with true <- enabled?() do
       {scheduled_at, args} = Map.pop(args, :expires_at)
 
       args
@@ -42,16 +41,6 @@ defmodule Pleroma.Workers.PurgeExpiredActivity do
     end
   end
 
-  defp validate_expires_at(%{validate: false} = args), do: Map.delete(args, :validate)
-
-  defp validate_expires_at(args) do
-    if expires_late_enough?(args[:expires_at]) do
-      args
-    else
-      {:error, :expiration_too_close}
-    end
-  end
-
   defp find_activity(id) do
     with nil <- Activity.get_by_id_with_object(id) do
       {:error, :activity_not_found}
index 1379333682399e474e2b1b179b1212ef70e3c26c..cdc00d20baf97a5ea0bec858d08a73f770caea54 100644 (file)
@@ -17,8 +17,7 @@ defmodule Pleroma.Repo.Migrations.MoveActivityExpirationsToOban do
       with {:ok, expires_at} <- DateTime.from_naive(expiration.scheduled_at, "Etc/UTC") do
         Pleroma.Workers.PurgeExpiredActivity.enqueue(%{
           activity_id: FlakeId.to_string(expiration.activity_id),
-          expires_at: expires_at,
-          validate: false
+          expires_at: expires_at
         })
       end
     end)
index 8b5dc9fd208d694d64cbd90f772fe35f3a3c4844..b5938776db4780aa2195287ea48307e33b320fed 100644 (file)
@@ -10,22 +10,27 @@ defmodule Pleroma.Workers.PurgeExpiredActivityTest do
 
   alias Pleroma.Workers.PurgeExpiredActivity
 
-  test "denies expirations that don't live long enough" do
+  test "enqueue job" do
     activity = insert(:note_activity)
 
-    assert {:error, :expiration_too_close} =
+    assert {:ok, _} =
              PurgeExpiredActivity.enqueue(%{
                activity_id: activity.id,
-               expires_at: DateTime.utc_now()
+               expires_at: DateTime.add(DateTime.utc_now(), 3601)
              })
 
-    refute_enqueued(
+    assert_enqueued(
       worker: Pleroma.Workers.PurgeExpiredActivity,
       args: %{activity_id: activity.id}
     )
+
+    assert {:ok, _} =
+             perform_job(Pleroma.Workers.PurgeExpiredActivity, %{activity_id: activity.id})
+
+    assert %Oban.Job{} = Pleroma.Workers.PurgeExpiredActivity.get_expiration(activity.id)
   end
 
-  test "enqueue job" do
+  test "error if user was not found" do
     activity = insert(:note_activity)
 
     assert {:ok, _} =
@@ -34,14 +39,21 @@ defmodule Pleroma.Workers.PurgeExpiredActivityTest do
                expires_at: DateTime.add(DateTime.utc_now(), 3601)
              })
 
-    assert_enqueued(
-      worker: Pleroma.Workers.PurgeExpiredActivity,
-      args: %{activity_id: activity.id}
-    )
+    user = Pleroma.User.get_by_ap_id(activity.actor)
+    Pleroma.Repo.delete(user)
 
-    assert {:ok, _} =
+    assert {:error, :user_not_found} =
              perform_job(Pleroma.Workers.PurgeExpiredActivity, %{activity_id: activity.id})
+  end
 
-    assert %Oban.Job{} = Pleroma.Workers.PurgeExpiredActivity.get_expiration(activity.id)
+  test "error if actiivity was not found" do
+    assert {:ok, _} =
+             PurgeExpiredActivity.enqueue(%{
+               activity_id: "some_id",
+               expires_at: DateTime.add(DateTime.utc_now(), 3601)
+             })
+
+    assert {:error, :activity_not_found} =
+             perform_job(Pleroma.Workers.PurgeExpiredActivity, %{activity_id: "some_if"})
   end
 end