X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fscheduled_activity.ex;h=0937cb7db405740378a49c52732340e019ea3b36;hb=4f8e0170e98cb91e11e70a5cb7dc3972b3035599;hp=ea78dce6a4749df2ed91c73443fa04a05786d467;hpb=680e071dba95d1f46a7a0eeb05d55252752c0e29;p=akkoma
diff --git a/lib/pleroma/scheduled_activity.ex b/lib/pleroma/scheduled_activity.ex
index ea78dce6a..0937cb7db 100644
--- a/lib/pleroma/scheduled_activity.ex
+++ b/lib/pleroma/scheduled_activity.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ScheduledActivity do
@@ -40,7 +40,7 @@ defmodule Pleroma.ScheduledActivity do
%{changes: %{params: %{"media_ids" => media_ids} = params}} = changeset
)
when is_list(media_ids) do
- media_attachments = Utils.attachments_from_ids(%{"media_ids" => media_ids})
+ media_attachments = Utils.attachments_from_ids(%{media_ids: media_ids})
params =
params
@@ -119,21 +119,22 @@ defmodule Pleroma.ScheduledActivity do
def create(%User{} = user, attrs) do
Multi.new()
|> Multi.insert(:scheduled_activity, new(user, attrs))
+ |> maybe_add_jobs(Config.get([ScheduledActivity, :enabled]))
+ |> Repo.transaction()
+ |> transaction_response
+ 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)
- |> Repo.transaction()
- |> case do
- {:ok, %{scheduled_activity: scheduled_activity}} ->
- {:ok, scheduled_activity}
-
- {:error, _, changeset, _} ->
- {:error, changeset}
- end
end
+ defp maybe_add_jobs(multi, _), do: multi
+
def get(%User{} = user, scheduled_activity_id) do
ScheduledActivity
|> where(user_id: ^user.id)
@@ -149,37 +150,35 @@ defmodule Pleroma.ScheduledActivity do
Multi.new()
|> Multi.update(:scheduled_activity, changeset)
|> Multi.update_all(:scheduled_job, job_query(id),
- set: [scheduled_at: changeset.changes[:scheduled_at]]
+ 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