fix ScheduledActivity
authorMaksim Pechnikov <parallel588@gmail.com>
Wed, 4 Dec 2019 18:18:05 +0000 (21:18 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Wed, 4 Dec 2019 18:18:05 +0000 (21:18 +0300)
lib/pleroma/scheduled_activity.ex
test/scheduled_activity_test.exs
test/web/mastodon_api/controllers/scheduled_activity_controller_test.exs

index ea78dce6a4749df2ed91c73443fa04a05786d467..d011007028857accf5eb1e4ad976c38f4ff6a97f 100644 (file)
@@ -119,11 +119,7 @@ defmodule Pleroma.ScheduledActivity do
   def create(%User{} = user, attrs) do
     Multi.new()
     |> Multi.insert(:scheduled_activity, new(user, attrs))
-    |> Multi.run(:scheduled_activity_job, fn _repo, %{scheduled_activity: activity} ->
-      %{activity_id: activity.id}
-      |> ScheduledActivityWorker.new(scheduled_at: activity.scheduled_at)
-      |> Oban.insert()
-    end)
+    |> maybe_add_jobs(Config.get([ScheduledActivity, :enabled]))
     |> Repo.transaction()
     |> case do
       {:ok, %{scheduled_activity: scheduled_activity}} ->
@@ -134,6 +130,17 @@ defmodule Pleroma.ScheduledActivity do
     end
   end
 
+  defp maybe_add_jobs(multi, true) do
+    multi
+    |> Multi.run(:scheduled_activity_job, fn _repo, %{scheduled_activity: activity} ->
+      %{activity_id: activity.id}
+      |> ScheduledActivityWorker.new(scheduled_at: activity.scheduled_at)
+      |> Oban.insert()
+    end)
+  end
+
+  defp maybe_add_jobs(multi, _), do: multi
+
   def get(%User{} = user, scheduled_activity_id) do
     ScheduledActivity
     |> where(user_id: ^user.id)
index b367ae5fbf9c8e2818b10baa8a4ad0fcc635820c..d2c5f5aa2c98b744c645ad0b98fe553fc114181a 100644 (file)
@@ -15,7 +15,8 @@ defmodule Pleroma.ScheduledActivityTest do
   end
 
   describe "creation" do
-    test "when daily user limit is exceeded" do
+    test "scheduled activities with jobs when ScheduledActivity enabled" do
+      Pleroma.Config.put([ScheduledActivity, :enabled], true)
       user = insert(:user)
 
       today =
@@ -31,6 +32,39 @@ defmodule Pleroma.ScheduledActivityTest do
         Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
 
       assert jobs == [%{"activity_id" => sa1.id}, %{"activity_id" => sa2.id}]
+    end
+
+    test "scheduled activities without jobs when ScheduledActivity disabled" do
+      Pleroma.Config.put([ScheduledActivity, :enabled], false)
+      user = insert(:user)
+
+      today =
+        NaiveDateTime.utc_now()
+        |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
+        |> NaiveDateTime.to_iso8601()
+
+      attrs = %{params: %{}, scheduled_at: today}
+      {:ok, _sa1} = ScheduledActivity.create(user, attrs)
+      {:ok, _sa2} = ScheduledActivity.create(user, attrs)
+
+      jobs =
+        Repo.all(from(j in Oban.Job, where: j.queue == "scheduled_activities", select: j.args))
+
+      assert jobs == []
+    end
+
+    test "when daily user limit is exceeded" do
+      user = insert(:user)
+
+      today =
+        NaiveDateTime.utc_now()
+        |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
+        |> NaiveDateTime.to_iso8601()
+
+      attrs = %{params: %{}, scheduled_at: today}
+      {:ok, _} = ScheduledActivity.create(user, attrs)
+      {:ok, _} = ScheduledActivity.create(user, attrs)
+
       {:error, changeset} = ScheduledActivity.create(user, attrs)
       assert changeset.errors == [scheduled_at: {"daily limit exceeded", []}]
     end
index 5f3a376be7a522808d26e3a879dbffd67ee223e2..478631a12c642aebb35c4e3e7f7279b87cf41f75 100644 (file)
@@ -11,6 +11,8 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do
   import Pleroma.Factory
   import Ecto.Query
 
+  clear_config([ScheduledActivity, :enabled])
+
   test "shows scheduled activities", %{conn: conn} do
     user = insert(:user)
     scheduled_activity_id1 = insert(:scheduled_activity, user: user).id |> to_string()
@@ -68,6 +70,7 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do
   end
 
   test "updates a scheduled activity", %{conn: conn} do
+    Pleroma.Config.put([ScheduledActivity, :enabled], true)
     user = insert(:user)
 
     scheduled_at =
@@ -120,6 +123,7 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do
   end
 
   test "deletes a scheduled activity", %{conn: conn} do
+    Pleroma.Config.put([ScheduledActivity, :enabled], true)
     user = insert(:user)
 
     {:ok, scheduled_activity} =