akkoma/lib/pleroma/workers/purge_expired_activity.ex

72 lines
1.9 KiB
Elixir
Raw Normal View History

2020-08-25 03:45:06 -06:00
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
2020-08-25 03:45:06 -06:00
# SPDX-License-Identifier: AGPL-3.0-only
2020-08-22 11:46:01 -06:00
defmodule Pleroma.Workers.PurgeExpiredActivity do
@moduledoc """
Worker which purges expired activity.
"""
2022-12-14 06:38:27 -07:00
use Pleroma.Workers.WorkerHelper, queue: "activity_expiration", max_attempts: 1, unique: [period: :infinity]
2020-08-22 11:46:01 -06:00
import Ecto.Query
alias Pleroma.Activity
2022-12-14 06:38:27 -07:00
def schedule(args) do
with true <- enabled?() do
2020-08-22 11:46:01 -06:00
{scheduled_at, args} = Map.pop(args, :expires_at)
2022-12-14 06:38:27 -07:00
enqueue("delete", args, scheduled_at: scheduled_at)
2020-08-22 11:46:01 -06:00
end
end
@impl Oban.Worker
def timeout(_job) do
Pleroma.Config.get([:workers, :timeout, :activity_expiration], :timer.minutes(1))
end
2020-08-22 11:46:01 -06:00
@impl true
def perform(%Oban.Job{args: %{"activity_id" => id}}) do
with %Activity{} = activity <- find_activity(id),
%Pleroma.User{} = user <- find_user(activity.object.data["actor"]) do
2020-08-22 11:46:01 -06:00
Pleroma.Web.CommonAPI.delete(activity.id, user)
end
end
defp enabled? do
with false <- Pleroma.Config.get([__MODULE__, :enabled], false) do
{:error, :expired_activities_disabled}
end
end
defp find_activity(id) do
with nil <- Activity.get_by_id_with_object(id) do
2020-08-22 11:46:01 -06:00
{:error, :activity_not_found}
end
end
defp find_user(ap_id) do
with nil <- Pleroma.User.get_by_ap_id(ap_id) do
{:error, :user_not_found}
end
end
def get_expiration(id) do
from(j in Oban.Job,
where: j.state == "scheduled",
where: j.queue == "activity_expiration",
where: fragment("?->>'activity_id' = ?", j.args, ^id)
)
|> Pleroma.Repo.one()
end
@spec expires_late_enough?(DateTime.t()) :: boolean()
def expires_late_enough?(scheduled_at) do
now = DateTime.utc_now()
diff = DateTime.diff(scheduled_at, now, :millisecond)
min_lifetime = Pleroma.Config.get([__MODULE__, :min_lifetime], 600)
diff > :timer.seconds(min_lifetime)
2020-08-22 11:46:01 -06:00
end
end