Pleroma/lib/pleroma/digest_email_worker.ex

33 lines
968 B
Elixir
Raw Normal View History

2019-04-20 14:42:19 +02:00
defmodule Pleroma.DigestEmailWorker do
import Ecto.Query
2019-04-20 14:57:43 +02:00
def run do
config = Pleroma.Config.get([:email_notifications, :digest])
2019-04-20 14:42:19 +02:00
negative_interval = -Map.fetch!(config, :interval)
inactivity_threshold = Map.fetch!(config, :inactivity_threshold)
inactive_users_query = Pleroma.User.list_inactive_users_query(inactivity_threshold)
now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
from(u in inactive_users_query,
2019-04-20 14:57:43 +02:00
where: fragment(~s(? #> '{"email_notifications","digest"}' @> 'true'), u.info),
2019-04-20 14:42:19 +02:00
where: u.last_digest_emailed_at < datetime_add(^now, ^negative_interval, "day"),
select: u
)
|> Pleroma.Repo.all()
2019-04-21 11:40:05 +02:00
|> run()
2019-04-20 14:42:19 +02:00
end
2019-06-04 01:48:21 +02:00
def run([]), do: :ok
2019-04-20 14:42:19 +02:00
2019-06-04 01:48:21 +02:00
def run([user | users]) do
2019-04-20 14:42:19 +02:00
with %Swoosh.Email{} = email <- Pleroma.Emails.UserEmail.digest_email(user) do
Pleroma.Emails.Mailer.deliver_async(email)
end
Pleroma.User.touch_last_digest_emailed_at(user)
run(users)
end
end