logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

purge_expired_activity.ex (2023B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Workers.PurgeExpiredActivity do
  5. @moduledoc """
  6. Worker which purges expired activity.
  7. """
  8. @queue :background
  9. use Oban.Worker, queue: @queue, max_attempts: 1, unique: [period: :infinity]
  10. import Ecto.Query
  11. alias Pleroma.Activity
  12. @spec enqueue(map(), list()) ::
  13. {:ok, Oban.Job.t()}
  14. | {:error, :expired_activities_disabled}
  15. | {:error, :expiration_too_close}
  16. def enqueue(params, worker_args) do
  17. with true <- enabled?() do
  18. new(params, worker_args)
  19. |> Oban.insert()
  20. end
  21. end
  22. @impl true
  23. def perform(%Oban.Job{args: %{"activity_id" => id}}) do
  24. with %Activity{} = activity <- find_activity(id),
  25. %Pleroma.User{} = user <- find_user(activity.object.data["actor"]) do
  26. Pleroma.Web.CommonAPI.delete(activity.id, user)
  27. end
  28. end
  29. @impl true
  30. def timeout(_job), do: :timer.seconds(5)
  31. defp enabled? do
  32. with false <- Pleroma.Config.get([__MODULE__, :enabled], false) do
  33. {:error, :expired_activities_disabled}
  34. end
  35. end
  36. defp find_activity(id) do
  37. with nil <- Activity.get_by_id_with_object(id) do
  38. {:cancel, :activity_not_found}
  39. end
  40. end
  41. defp find_user(ap_id) do
  42. with nil <- Pleroma.User.get_by_ap_id(ap_id) do
  43. {:cancel, :user_not_found}
  44. end
  45. end
  46. def get_expiration(id) do
  47. queue = Atom.to_string(@queue)
  48. from(j in Oban.Job,
  49. where: j.state == "scheduled",
  50. where: j.queue == ^queue,
  51. where: fragment("?->>'activity_id' = ?", j.args, ^id)
  52. )
  53. |> Pleroma.Repo.one()
  54. end
  55. @spec expires_late_enough?(DateTime.t()) :: boolean()
  56. def expires_late_enough?(scheduled_at) do
  57. now = DateTime.utc_now()
  58. diff = DateTime.diff(scheduled_at, now, :millisecond)
  59. min_lifetime = Pleroma.Config.get([__MODULE__, :min_lifetime], 600)
  60. diff > :timer.seconds(min_lifetime)
  61. end
  62. end