logo

pleroma

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

digest_emails_worker.ex (1681B)


  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.Cron.DigestEmailsWorker do
  5. @moduledoc """
  6. The worker to send digest emails.
  7. """
  8. use Oban.Worker, queue: "mailer"
  9. alias Pleroma.Config
  10. alias Pleroma.Emails
  11. alias Pleroma.Repo
  12. alias Pleroma.User
  13. import Ecto.Query
  14. require Logger
  15. @impl true
  16. def perform(_job) do
  17. config = Config.get([:email_notifications, :digest])
  18. if config[:active] do
  19. negative_interval = -Map.fetch!(config, :interval)
  20. inactivity_threshold = Map.fetch!(config, :inactivity_threshold)
  21. inactive_users_query = User.list_inactive_users_query(inactivity_threshold)
  22. now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
  23. from(u in inactive_users_query,
  24. where: fragment(~s(? ->'digest' @> 'true'), u.email_notifications),
  25. where: not is_nil(u.email),
  26. where: u.last_digest_emailed_at < datetime_add(^now, ^negative_interval, "day"),
  27. select: u
  28. )
  29. |> Repo.all()
  30. |> send_emails
  31. end
  32. :ok
  33. end
  34. def send_emails(users) do
  35. Enum.each(users, &send_email/1)
  36. end
  37. @doc """
  38. Send digest email to the given user.
  39. Updates `last_digest_emailed_at` field for the user and returns the updated user.
  40. """
  41. @spec send_email(User.t()) :: User.t()
  42. def send_email(user) do
  43. with %Swoosh.Email{} = email <- Emails.UserEmail.digest_email(user) do
  44. Emails.Mailer.deliver_async(email)
  45. end
  46. User.touch_last_digest_emailed_at(user)
  47. end
  48. @impl true
  49. def timeout(_job), do: :timer.seconds(5)
  50. end