crontab: [
{"0 0 * * *", Pleroma.Workers.Cron.ClearOauthTokenWorker},
{"0 * * * *", Pleroma.Workers.Cron.StatsWorker},
- {"* * * * *", Pleroma.Workers.Cron.ScheduledActivityWorker},
{"* * * * *", Pleroma.Workers.Cron.PurgeExpiredActivitiesWorker},
{"0 0 * * 0", Pleroma.Workers.Cron.DigestEmailsWorker}
]
defmodule Pleroma.ScheduledActivity do
use Ecto.Schema
+ alias Ecto.Multi
alias Pleroma.Config
alias Pleroma.Repo
alias Pleroma.ScheduledActivity
alias Pleroma.User
alias Pleroma.Web.CommonAPI.Utils
+ alias Pleroma.Workers.ScheduledActivityWorker
import Ecto.Query
import Ecto.Changeset
end
def new(%User{} = user, attrs) do
- %ScheduledActivity{user_id: user.id}
- |> changeset(attrs)
+ changeset(%ScheduledActivity{user_id: user.id}, attrs)
end
+ @doc """
+ Creates ScheduledActivity and add to queue to perform at scheduled_at date
+ """
+ @spec create(User.t(), map()) :: {:ok, ScheduledActivity.t()} | {:error, Ecto.Changeset.t()}
def create(%User{} = user, attrs) do
- user
- |> new(attrs)
- |> Repo.insert()
+ 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)
+ |> Repo.transaction()
+ |> case do
+ {:ok, %{scheduled_activity: scheduled_activity}} ->
+ {:ok, scheduled_activity}
+
+ {:error, _, changeset, _} ->
+ {:error, changeset}
+ end
end
def get(%User{} = user, scheduled_activity_id) do
|> Repo.one()
end
- def update(%ScheduledActivity{} = scheduled_activity, attrs) do
- scheduled_activity
- |> update_changeset(attrs)
- |> Repo.update()
+ @spec update(ScheduledActivity.t(), map()) ::
+ {:ok, ScheduledActivity.t()} | {:error, Ecto.Changeset.t()}
+ def update(%ScheduledActivity{id: id} = scheduled_activity, attrs) do
+ with {:error, %Ecto.Changeset{valid?: true} = changeset} <-
+ {:error, update_changeset(scheduled_activity, attrs)} do
+ Multi.new()
+ |> Multi.update(:scheduled_activity, changeset)
+ |> Multi.update_all(:scheduled_job, job_query(id),
+ set: [scheduled_at: changeset.changes[:scheduled_at]]
+ )
+ |> Repo.transaction()
+ |> case do
+ {:ok, %{scheduled_activity: scheduled_activity}} ->
+ {:ok, scheduled_activity}
+
+ {:error, _, changeset, _} ->
+ {:error, changeset}
+ end
+ end
+ end
+
+ def delete_job(%ScheduledActivity{id: id} = _scheduled_activity) do
+ id
+ |> job_query
+ |> Repo.delete_all()
end
def delete(%ScheduledActivity{} = scheduled_activity) do
- scheduled_activity
- |> Repo.delete()
+ Repo.delete(scheduled_activity)
end
def delete(id) when is_binary(id) or is_integer(id) do
|> where([sa], sa.scheduled_at < ^naive_datetime)
|> Repo.all()
end
+
+ def job_query(scheduled_activity_id) do
+ from(j in Oban.Job,
+ where: j.queue == "scheduled_activities",
+ where: fragment("args ->> 'activity_id' = ?::text", ^to_string(scheduled_activity_id))
+ )
+ end
end
@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) do
+ with {:ok, scheduled_activity} <- ScheduledActivity.delete(scheduled_activity),
+ _ <- ScheduledActivity.delete_job(scheduled_activity) do
render(conn, "show.json", scheduled_activity: scheduled_activity)
end
end
) do
params = Map.put(params, "in_reply_to_status_id", params["in_reply_to_id"])
- if ScheduledActivity.far_enough?(scheduled_at) do
- with {:ok, scheduled_activity} <-
- ScheduledActivity.create(user, %{"params" => params, "scheduled_at" => scheduled_at}) do
- conn
- |> put_view(ScheduledActivityView)
- |> render("show.json", scheduled_activity: scheduled_activity)
- end
+ with {:far_enough, true} <- {:far_enough, ScheduledActivity.far_enough?(scheduled_at)},
+ attrs <- %{"params" => params, "scheduled_at" => scheduled_at},
+ {:ok, scheduled_activity} <- ScheduledActivity.create(user, attrs) do
+ conn
+ |> put_view(ScheduledActivityView)
+ |> render("show.json", scheduled_activity: scheduled_activity)
else
- create(conn, Map.drop(params, ["scheduled_at"]))
+ {:far_enough, _} ->
+ create(conn, Map.drop(params, ["scheduled_at"]))
+
+ error ->
+ error
end
end
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
-defmodule Pleroma.Workers.Cron.ScheduledActivityWorker do
+defmodule Pleroma.Workers.ScheduledActivityWorker do
@moduledoc """
- The worker to post scheduled actvities.
+ The worker to post scheduled activity.
"""
- use Oban.Worker, queue: "scheduled_activities"
+ use Pleroma.Workers.WorkerHelper, queue: "scheduled_activities"
+
alias Pleroma.Config
alias Pleroma.ScheduledActivity
alias Pleroma.User
require Logger
- @schedule_interval :timer.minutes(1)
-
@impl Oban.Worker
- def perform(_opts, _job) do
+ def perform(%{"activity_id" => activity_id}, _job) do
if Config.get([ScheduledActivity, :enabled]) do
- @schedule_interval
- |> ScheduledActivity.due_activities()
- |> Enum.each(&post_activity/1)
+ case Pleroma.Repo.get(ScheduledActivity, activity_id) do
+ %ScheduledActivity{} = scheduled_activity ->
+ post_activity(scheduled_activity)
+
+ _ ->
+ Logger.error("#{__MODULE__} Couldn't find scheduled activity: #{activity_id}")
+ end
end
end
- def post_activity(scheduled_activity) do
+ defp post_activity(%ScheduledActivity{} = scheduled_activity) do
try do
{:ok, scheduled_activity} = ScheduledActivity.delete(scheduled_activity)
%User{} = user = User.get_cached_by_id(scheduled_activity.user_id)
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
params: %{status: "hi"}
)
- Pleroma.Workers.Cron.ScheduledActivityWorker.perform(:opts, :pid)
+ Pleroma.Workers.ScheduledActivityWorker.perform(
+ %{"activity_id" => scheduled_activity.id},
+ :pid
+ )
refute Repo.get(ScheduledActivity, scheduled_activity.id)
activity = Repo.all(Pleroma.Activity) |> Enum.find(&(&1.actor == user.ap_id))
clear_config_all: 2
]
+ def to_datetime(naive_datetime) do
+ naive_datetime
+ |> DateTime.from_naive!("Etc/UTC")
+ |> DateTime.truncate(:second)
+ end
+
def collect_ids(collection) do
collection
|> Enum.map(& &1.id)
alias Pleroma.ScheduledActivity
import Pleroma.Factory
+ import Ecto.Query
test "shows scheduled activities", %{conn: conn} do
user = insert(:user)
test "updates a scheduled activity", %{conn: conn} do
user = insert(:user)
- scheduled_activity = insert(:scheduled_activity, user: user)
+
+ scheduled_at =
+ NaiveDateTime.add(
+ NaiveDateTime.utc_now(),
+ :timer.minutes(60),
+ :millisecond
+ )
+
+ {:ok, scheduled_activity} =
+ ScheduledActivity.create(
+ user,
+ %{
+ scheduled_at: scheduled_at,
+ params: build(:note).data
+ }
+ )
+
+ scheduled_activity_job =
+ Repo.one(from(j in Oban.Job, where: j.queue == "scheduled_activities"))
+
+ assert scheduled_activity_job.args == %{"activity_id" => scheduled_activity.id}
+
+ assert DateTime.truncate(scheduled_activity_job.scheduled_at, :second) ==
+ to_datetime(scheduled_at)
new_scheduled_at =
NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
assert %{"scheduled_at" => expected_scheduled_at} = json_response(res_conn, 200)
assert expected_scheduled_at == Pleroma.Web.CommonAPI.Utils.to_masto_date(new_scheduled_at)
+ scheduled_activity_job = refresh_record(scheduled_activity_job)
+
+ assert DateTime.truncate(scheduled_activity_job.scheduled_at, :second) ==
+ to_datetime(new_scheduled_at)
res_conn =
conn
test "deletes a scheduled activity", %{conn: conn} do
user = insert(:user)
- scheduled_activity = insert(:scheduled_activity, user: user)
+
+ {:ok, scheduled_activity} =
+ ScheduledActivity.create(
+ user,
+ %{
+ scheduled_at:
+ NaiveDateTime.add(
+ NaiveDateTime.utc_now(),
+ :timer.minutes(60),
+ :millisecond
+ ),
+ params: build(:note).data
+ }
+ )
+
+ scheduled_activity_job =
+ Repo.one(from(j in Oban.Job, where: j.queue == "scheduled_activities"))
+
+ assert scheduled_activity_job.args == %{"activity_id" => scheduled_activity.id}
res_conn =
conn
|> delete("/api/v1/scheduled_statuses/#{scheduled_activity.id}")
assert %{} = json_response(res_conn, 200)
- assert nil == Repo.get(ScheduledActivity, scheduled_activity.id)
+ refute Repo.get(ScheduledActivity, scheduled_activity.id)
+ refute Repo.get(Oban.Job, scheduled_activity_job.id)
res_conn =
conn
+++ /dev/null
-# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
-# SPDX-License-Identifier: AGPL-3.0-only
-
-defmodule Pleroma.Workers.Cron.ScheduledActivityWorkerTest do
- use Pleroma.DataCase
- alias Pleroma.ScheduledActivity
- import Pleroma.Factory
-
- clear_config([ScheduledActivity, :enabled])
-
- test "creates a status from the scheduled activity" do
- Pleroma.Config.put([ScheduledActivity, :enabled], true)
- user = insert(:user)
-
- naive_datetime =
- NaiveDateTime.add(
- NaiveDateTime.utc_now(),
- -:timer.minutes(2),
- :millisecond
- )
-
- scheduled_activity =
- insert(
- :scheduled_activity,
- scheduled_at: naive_datetime,
- user: user,
- params: %{status: "hi"}
- )
-
- Pleroma.Workers.Cron.ScheduledActivityWorker.perform(:opts, :pid)
-
- refute Repo.get(ScheduledActivity, scheduled_activity.id)
- activity = Repo.all(Pleroma.Activity) |> Enum.find(&(&1.actor == user.ap_id))
- assert Pleroma.Object.normalize(activity).data["content"] == "hi"
- end
-end