1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Workers.Cron.DigestEmailsWorker do
7 The worker to send digest emails.
10 use Oban.Worker, queue: "mailer"
23 config = Config.get([:email_notifications, :digest])
26 negative_interval = -Map.fetch!(config, :interval)
27 inactivity_threshold = Map.fetch!(config, :inactivity_threshold)
28 inactive_users_query = User.list_inactive_users_query(inactivity_threshold)
30 now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
32 from(u in inactive_users_query,
33 where: fragment(~s(? ->'digest' @> 'true'), u.email_notifications),
34 where: not is_nil(u.email),
35 where: u.last_digest_emailed_at < datetime_add(^now, ^negative_interval, "day"),
45 def send_emails(users) do
46 Enum.each(users, &send_email/1)
50 Send digest email to the given user.
51 Updates `last_digest_emailed_at` field for the user and returns the updated user.
53 @spec send_email(User.t()) :: User.t()
54 def send_email(user) do
55 with %Swoosh.Email{} = email <- Emails.UserEmail.digest_email(user) do
56 Emails.Mailer.deliver_async(email)
59 User.touch_last_digest_emailed_at(user)