logo

pleroma

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

purge_expired_activity.ex (2056B)


  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. use Oban.Worker, queue: :activity_expiration, max_attempts: 1, unique: [period: :infinity]
  9. import Ecto.Query
  10. alias Pleroma.Activity
  11. @spec enqueue(map()) ::
  12. {:ok, Oban.Job.t()}
  13. | {:error, :expired_activities_disabled}
  14. | {:error, :expiration_too_close}
  15. def enqueue(args) do
  16. with true <- enabled?() do
  17. {scheduled_at, args} = Map.pop(args, :expires_at)
  18. args
  19. |> new(scheduled_at: scheduled_at)
  20. |> Oban.insert()
  21. end
  22. end
  23. @impl true
  24. def perform(%Oban.Job{args: %{"activity_id" => id}}) do
  25. with %Activity{} = activity <- find_activity(id),
  26. %Pleroma.User{} = user <- find_user(activity.object.data["actor"]) do
  27. Pleroma.Web.CommonAPI.delete(activity.id, user)
  28. end
  29. end
  30. @impl Oban.Worker
  31. def timeout(_job), do: :timer.seconds(5)
  32. defp enabled? do
  33. with false <- Pleroma.Config.get([__MODULE__, :enabled], false) do
  34. {:error, :expired_activities_disabled}
  35. end
  36. end
  37. defp find_activity(id) do
  38. with nil <- Activity.get_by_id_with_object(id) do
  39. {:error, :activity_not_found}
  40. end
  41. end
  42. defp find_user(ap_id) do
  43. with nil <- Pleroma.User.get_by_ap_id(ap_id) do
  44. {:error, :user_not_found}
  45. end
  46. end
  47. def get_expiration(id) do
  48. from(j in Oban.Job,
  49. where: j.state == "scheduled",
  50. where: j.queue == "activity_expiration",
  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