Add task to test emails
[akkoma] / lib / pleroma / digest_email_worker.ex
1 defmodule Pleroma.DigestEmailWorker do
2 import Ecto.Query
3
4 def run 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
10 now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
11
12 from(u in inactive_users_query,
13 where: fragment(~s(? #> '{"email_notifications","digest"}' @> 'true'), u.info),
14 where: u.last_digest_emailed_at < datetime_add(^now, ^negative_interval, "day"),
15 select: u
16 )
17 |> Pleroma.Repo.all()
18 |> run()
19 end
20
21 def run([]), do: :ok
22
23 def run([user | users]) do
24 with %Swoosh.Email{} = email <- Pleroma.Emails.UserEmail.digest_email(user) do
25 Pleroma.Emails.Mailer.deliver_async(email)
26 end
27
28 Pleroma.User.touch_last_digest_emailed_at(user)
29
30 run(users)
31 end
32 end