fix ScheduledActivity

This commit is contained in:
Maksim Pechnikov 2019-12-04 21:18:05 +03:00
parent c0d572d0bf
commit 3c3bba0b7c
3 changed files with 51 additions and 6 deletions

View 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)

View 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

View 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} =