2019-08-19 10:10:00 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-04-20 06:42:19 -06:00
|
|
|
defmodule Pleroma.DigestEmailWorker do
|
2019-08-13 11:20:26 -06:00
|
|
|
alias Pleroma.Repo
|
2019-08-31 12:58:42 -06:00
|
|
|
alias Pleroma.Workers.DigestEmailsWorker
|
2019-08-13 11:20:26 -06:00
|
|
|
|
2019-04-20 06:42:19 -06:00
|
|
|
import Ecto.Query
|
|
|
|
|
2019-07-16 07:49:50 -06:00
|
|
|
def perform do
|
2019-05-29 12:31:27 -06:00
|
|
|
config = Pleroma.Config.get([:email_notifications, :digest])
|
2019-04-20 06:42:19 -06: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 06:57:43 -06:00
|
|
|
where: fragment(~s(? #> '{"email_notifications","digest"}' @> 'true'), u.info),
|
2019-04-20 06:42:19 -06:00
|
|
|
where: u.last_digest_emailed_at < datetime_add(^now, ^negative_interval, "day"),
|
|
|
|
select: u
|
|
|
|
)
|
2019-08-31 12:58:42 -06:00
|
|
|
|> Repo.all()
|
2019-08-13 11:20:26 -06:00
|
|
|
|> Enum.each(fn user ->
|
2019-08-31 12:58:42 -06:00
|
|
|
DigestEmailsWorker.enqueue("digest_email", %{"user_id" => user.id})
|
2019-08-13 11:20:26 -06:00
|
|
|
end)
|
2019-04-20 06:42:19 -06:00
|
|
|
end
|
|
|
|
|
2019-07-14 13:32:11 -06:00
|
|
|
@doc """
|
|
|
|
Send digest email to the given user.
|
|
|
|
Updates `last_digest_emailed_at` field for the user and returns the updated user.
|
|
|
|
"""
|
|
|
|
@spec perform(Pleroma.User.t()) :: Pleroma.User.t()
|
|
|
|
def perform(user) do
|
2019-04-20 06:42:19 -06: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)
|
|
|
|
end
|
|
|
|
end
|