Local copy of Pleroma, an ActivityPub server software. Contains modifications running live on fedi.underscore.world
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.

digest_email_worker.ex 1.2KB

1234567891011121314151617181920212223242526272829303132333435
  1. defmodule Pleroma.DigestEmailWorker do
  2. import Ecto.Query
  3. @queue_name :digest_emails
  4. def perform do
  5. config = Pleroma.Config.get([:email_notifications, :digest])
  6. negative_interval = -Map.fetch!(config, :interval)
  7. inactivity_threshold = Map.fetch!(config, :inactivity_threshold)
  8. inactive_users_query = Pleroma.User.list_inactive_users_query(inactivity_threshold)
  9. now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
  10. from(u in inactive_users_query,
  11. where: fragment(~s(? #> '{"email_notifications","digest"}' @> 'true'), u.info),
  12. where: u.last_digest_emailed_at < datetime_add(^now, ^negative_interval, "day"),
  13. select: u
  14. )
  15. |> Pleroma.Repo.all()
  16. |> Enum.each(&PleromaJobQueue.enqueue(@queue_name, __MODULE__, [&1]))
  17. end
  18. @doc """
  19. Send digest email to the given user.
  20. Updates `last_digest_emailed_at` field for the user and returns the updated user.
  21. """
  22. @spec perform(Pleroma.User.t()) :: Pleroma.User.t()
  23. def perform(user) do
  24. with %Swoosh.Email{} = email <- Pleroma.Emails.UserEmail.digest_email(user) do
  25. Pleroma.Emails.Mailer.deliver_async(email)
  26. end
  27. Pleroma.User.touch_last_digest_emailed_at(user)
  28. end
  29. end