logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

digest_email_worker.ex (1370B)


      1 # Pleroma: A lightweight social networking server
      2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
      3 # SPDX-License-Identifier: AGPL-3.0-only
      4 
      5 defmodule Pleroma.DigestEmailWorker do
      6   import Ecto.Query
      7 
      8   @queue_name :digest_emails
      9 
     10   def perform do
     11     config = Pleroma.Config.get([:email_notifications, :digest])
     12     negative_interval = -Map.fetch!(config, :interval)
     13     inactivity_threshold = Map.fetch!(config, :inactivity_threshold)
     14     inactive_users_query = Pleroma.User.list_inactive_users_query(inactivity_threshold)
     15 
     16     now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
     17 
     18     from(u in inactive_users_query,
     19       where: fragment(~s(? #> '{"email_notifications","digest"}' @> 'true'), u.info),
     20       where: u.last_digest_emailed_at < datetime_add(^now, ^negative_interval, "day"),
     21       select: u
     22     )
     23     |> Pleroma.Repo.all()
     24     |> Enum.each(&PleromaJobQueue.enqueue(@queue_name, __MODULE__, [&1]))
     25   end
     26 
     27   @doc """
     28   Send digest email to the given user.
     29   Updates `last_digest_emailed_at` field for the user and returns the updated user.
     30   """
     31   @spec perform(Pleroma.User.t()) :: Pleroma.User.t()
     32   def perform(user) do
     33     with %Swoosh.Email{} = email <- Pleroma.Emails.UserEmail.digest_email(user) do
     34       Pleroma.Emails.Mailer.deliver_async(email)
     35     end
     36 
     37     Pleroma.User.touch_last_digest_emailed_at(user)
     38   end
     39 end