fixed delete ScheduledActivity
authorMaksim Pechnikov <parallel588@gmail.com>
Thu, 23 Jan 2020 14:18:23 +0000 (17:18 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Thu, 23 Jan 2020 14:18:23 +0000 (17:18 +0300)
lib/pleroma/scheduled_activity.ex
lib/pleroma/web/mastodon_api/controllers/scheduled_activity_controller.ex

index 68da0550c3366bdec3b01e2bf7f73896f970042e..e81bfcd7d72a7ca3189553e2272f2978c90082a2 100644 (file)
@@ -121,13 +121,7 @@ defmodule Pleroma.ScheduledActivity do
     |> Multi.insert(:scheduled_activity, new(user, attrs))
     |> maybe_add_jobs(Config.get([ScheduledActivity, :enabled]))
     |> Repo.transaction()
-    |> case do
-      {:ok, %{scheduled_activity: scheduled_activity}} ->
-        {:ok, scheduled_activity}
-
-      {:error, _, changeset, _} ->
-        {:error, changeset}
-    end
+    |> transaction_response
   end
 
   defp maybe_add_jobs(multi, true) do
@@ -159,34 +153,32 @@ defmodule Pleroma.ScheduledActivity do
         set: [scheduled_at: get_field(changeset, :scheduled_at)]
       )
       |> Repo.transaction()
-      |> case do
-        {:ok, %{scheduled_activity: scheduled_activity}} ->
-          {:ok, scheduled_activity}
-
-        {:error, _, changeset, _} ->
-          {:error, changeset}
-      end
+      |> transaction_response
     end
   end
 
-  def delete_job(%ScheduledActivity{id: id} = _scheduled_activity) do
-    id
-    |> job_query
-    |> Repo.delete_all()
+  @doc "Deletes a ScheduledActivity and linked jobs."
+  @spec delete(ScheduledActivity.t() | binary() | integer) ::
+          {:ok, ScheduledActivity.t()} | {:error, Ecto.Changeset.t()}
+  def delete(%ScheduledActivity{id: id} = scheduled_activity) do
+    Multi.new()
+    |> Multi.delete(:scheduled_activity, scheduled_activity, stale_error_field: :id)
+    |> Multi.delete_all(:jobs, job_query(id))
+    |> Repo.transaction()
+    |> transaction_response
   end
 
-  def delete(%ScheduledActivity{} = scheduled_activity) do
-    Repo.delete(scheduled_activity)
+  def delete(id) when is_binary(id) or is_integer(id) do
+    delete(%__MODULE__{id: id})
   end
 
-  def delete(id) when is_binary(id) or is_integer(id) do
-    ScheduledActivity
-    |> where(id: ^id)
-    |> select([sa], sa)
-    |> Repo.delete_all()
-    |> case do
-      {1, [scheduled_activity]} -> {:ok, scheduled_activity}
-      _ -> :error
+  defp transaction_response(result) do
+    case result do
+      {:ok, %{scheduled_activity: scheduled_activity}} ->
+        {:ok, scheduled_activity}
+
+      {:error, _, changeset, _} ->
+        {:error, changeset}
     end
   end
 
index 4f9a8bdbec7237cee3328699909de8b88c721492..ff9276541614f675491ae644e32678aabd63f627 100644 (file)
@@ -45,8 +45,7 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityController do
 
   @doc "DELETE /api/v1/scheduled_statuses/:id"
   def delete(%{assigns: %{scheduled_activity: scheduled_activity}} = conn, _params) do
-    with {:ok, scheduled_activity} <- ScheduledActivity.delete(scheduled_activity),
-         _ <- ScheduledActivity.delete_job(scheduled_activity) do
+    with {:ok, scheduled_activity} <- ScheduledActivity.delete(scheduled_activity) do
       render(conn, "show.json", scheduled_activity: scheduled_activity)
     end
   end