Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 line
1.9KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 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. defp enabled? do
  31. with false <- Pleroma.Config.get([__MODULE__, :enabled], false) do
  32. {:error, :expired_activities_disabled}
  33. end
  34. end
  35. defp find_activity(id) do
  36. with nil <- Activity.get_by_id_with_object(id) do
  37. {:error, :activity_not_found}
  38. end
  39. end
  40. defp find_user(ap_id) do
  41. with nil <- Pleroma.User.get_by_ap_id(ap_id) do
  42. {:error, :user_not_found}
  43. end
  44. end
  45. def get_expiration(id) do
  46. from(j in Oban.Job,
  47. where: j.state == "scheduled",
  48. where: j.queue == "activity_expiration",
  49. where: fragment("?->>'activity_id' = ?", j.args, ^id)
  50. )
  51. |> Pleroma.Repo.one()
  52. end
  53. @spec expires_late_enough?(DateTime.t()) :: boolean()
  54. def expires_late_enough?(scheduled_at) do
  55. now = DateTime.utc_now()
  56. diff = DateTime.diff(scheduled_at, now, :millisecond)
  57. min_lifetime = Pleroma.Config.get([__MODULE__, :min_lifetime], 600)
  58. diff > :timer.seconds(min_lifetime)
  59. end
  60. end