1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Daemons.DigestEmailDaemon do
7 alias Pleroma.Workers.DigestEmailsWorker
12 config = Pleroma.Config.get([:email_notifications, :digest])
13 negative_interval = -Map.fetch!(config, :interval)
14 inactivity_threshold = Map.fetch!(config, :inactivity_threshold)
15 inactive_users_query = Pleroma.User.list_inactive_users_query(inactivity_threshold)
17 now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
19 from(u in inactive_users_query,
20 where: fragment(~s(? #> '{"email_notifications","digest"}' @> 'true'), u.info),
21 where: u.last_digest_emailed_at < datetime_add(^now, ^negative_interval, "day"),
25 |> Enum.each(fn user ->
26 DigestEmailsWorker.enqueue("digest_email", %{"user_id" => user.id})
31 Send digest email to the given user.
32 Updates `last_digest_emailed_at` field for the user and returns the updated user.
34 @spec perform(Pleroma.User.t()) :: Pleroma.User.t()
36 with %Swoosh.Email{} = email <- Pleroma.Emails.UserEmail.digest_email(user) do
37 Pleroma.Emails.Mailer.deliver_async(email)
40 Pleroma.User.touch_last_digest_emailed_at(user)