removed try/rescue
This commit is contained in:
parent
6f77b77428
commit
ce7c887a27
6 changed files with 95 additions and 43 deletions
|
@ -156,7 +156,7 @@ def update(%ScheduledActivity{id: id} = scheduled_activity, attrs) do
|
||||||
Multi.new()
|
Multi.new()
|
||||||
|> Multi.update(:scheduled_activity, changeset)
|
|> Multi.update(:scheduled_activity, changeset)
|
||||||
|> Multi.update_all(:scheduled_job, job_query(id),
|
|> 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()
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
|
|
|
@ -26,14 +26,21 @@ def perform(_opts, _job) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_activity(expiration) do
|
def delete_activity(%ActivityExpiration{activity_id: activity_id}) do
|
||||||
try do
|
with {:activity, %Activity{} = activity} <-
|
||||||
activity = Activity.get_by_id_with_object(expiration.activity_id)
|
{:activity, Activity.get_by_id_with_object(activity_id)},
|
||||||
user = User.get_by_ap_id(activity.object.data["actor"])
|
{:user, %User{} = user} <- {:user, User.get_by_ap_id(activity.object.data["actor"])} do
|
||||||
CommonAPI.delete(activity.id, user)
|
CommonAPI.delete(activity.id, user)
|
||||||
rescue
|
else
|
||||||
error ->
|
{:activity, _} ->
|
||||||
Logger.error("#{__MODULE__} Couldn't delete expired activity: #{inspect(error)}")
|
Logger.error(
|
||||||
|
"#{__MODULE__} Couldn't delete expired activity: not found activity ##{activity_id}"
|
||||||
|
)
|
||||||
|
|
||||||
|
{:user, _} ->
|
||||||
|
Logger.error(
|
||||||
|
"#{__MODULE__} Couldn't delete expired activity: not found actorof ##{activity_id}"
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -29,12 +29,12 @@ def perform(%{"activity_id" => activity_id}, _job) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp post_activity(%ScheduledActivity{} = scheduled_activity) do
|
defp post_activity(%ScheduledActivity{user_id: user_id, params: params} = scheduled_activity) do
|
||||||
try do
|
with {:delete, {:ok, _}} <- {:delete, ScheduledActivity.delete(scheduled_activity)},
|
||||||
{:ok, scheduled_activity} = ScheduledActivity.delete(scheduled_activity)
|
{:user, %User{} = user} <- {:user, User.get_cached_by_id(user_id)},
|
||||||
%User{} = user = User.get_cached_by_id(scheduled_activity.user_id)
|
{:post, {:ok, _}} <- {:post, CommonAPI.post(user, params)} do
|
||||||
{:ok, _result} = CommonAPI.post(user, scheduled_activity.params)
|
:ok
|
||||||
rescue
|
else
|
||||||
error ->
|
error ->
|
||||||
Logger.error(
|
Logger.error(
|
||||||
"#{__MODULE__} Couldn't create a status from the scheduled activity: #{inspect(error)}"
|
"#{__MODULE__} Couldn't create a status from the scheduled activity: #{inspect(error)}"
|
||||||
|
|
|
@ -102,33 +102,4 @@ test "when scheduled_at is earlier than 5 minute from now" do
|
||||||
assert changeset.errors == [scheduled_at: {"must be at least 5 minutes from now", []}]
|
assert changeset.errors == [scheduled_at: {"must be at least 5 minutes from now", []}]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
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.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))
|
|
||||||
assert Pleroma.Object.normalize(activity).data["content"] == "hi"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,8 +4,12 @@
|
||||||
|
|
||||||
defmodule Pleroma.Workers.Cron.PurgeExpiredActivitiesWorkerTest do
|
defmodule Pleroma.Workers.Cron.PurgeExpiredActivitiesWorkerTest do
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase
|
||||||
|
|
||||||
alias Pleroma.ActivityExpiration
|
alias Pleroma.ActivityExpiration
|
||||||
|
alias Pleroma.Workers.Cron.PurgeExpiredActivitiesWorker
|
||||||
|
|
||||||
import Pleroma.Factory
|
import Pleroma.Factory
|
||||||
|
import ExUnit.CaptureLog
|
||||||
|
|
||||||
clear_config([ActivityExpiration, :enabled])
|
clear_config([ActivityExpiration, :enabled])
|
||||||
|
|
||||||
|
@ -31,4 +35,22 @@ test "deletes an expiration activity" do
|
||||||
refute Pleroma.Repo.get(Pleroma.Activity, activity.id)
|
refute Pleroma.Repo.get(Pleroma.Activity, activity.id)
|
||||||
refute Pleroma.Repo.get(Pleroma.ActivityExpiration, expiration.id)
|
refute Pleroma.Repo.get(Pleroma.ActivityExpiration, expiration.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "delete_activity/1" do
|
||||||
|
test "adds log message if activity isn't find" do
|
||||||
|
assert capture_log([level: :error], fn ->
|
||||||
|
PurgeExpiredActivitiesWorker.delete_activity(%ActivityExpiration{
|
||||||
|
activity_id: "test-activity"
|
||||||
|
})
|
||||||
|
end) =~ "Couldn't delete expired activity: not found activity"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "adds log message if actor isn't find" do
|
||||||
|
assert capture_log([level: :error], fn ->
|
||||||
|
PurgeExpiredActivitiesWorker.delete_activity(%ActivityExpiration{
|
||||||
|
activity_id: "test-activity"
|
||||||
|
})
|
||||||
|
end) =~ "Couldn't delete expired activity: not found activity"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
52
test/workers/scheduled_activity_worker_test.exs
Normal file
52
test/workers/scheduled_activity_worker_test.exs
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Workers.ScheduledActivityWorkerTest do
|
||||||
|
use Pleroma.DataCase
|
||||||
|
|
||||||
|
alias Pleroma.ScheduledActivity
|
||||||
|
alias Pleroma.Workers.ScheduledActivityWorker
|
||||||
|
|
||||||
|
import Pleroma.Factory
|
||||||
|
import ExUnit.CaptureLog
|
||||||
|
|
||||||
|
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"}
|
||||||
|
)
|
||||||
|
|
||||||
|
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))
|
||||||
|
assert Pleroma.Object.normalize(activity).data["content"] == "hi"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "adds log message if ScheduledActivity isn't find" do
|
||||||
|
Pleroma.Config.put([ScheduledActivity, :enabled], true)
|
||||||
|
|
||||||
|
assert capture_log([level: :error], fn ->
|
||||||
|
ScheduledActivityWorker.perform(%{"activity_id" => 42}, :pid)
|
||||||
|
end) =~ "Couldn't find scheduled activity"
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue