430116a505e4db3229ea0457f01d97f03c07529e
[akkoma] / lib / mix / tasks / pleroma / digest.ex
1 defmodule Mix.Tasks.Pleroma.Digest do
2 use Mix.Task
3
4 @shortdoc "Manages digest emails"
5 @moduledoc """
6 Manages digest emails
7
8 ## Send digest email since given date (user registration date by default)
9 ignoring user activity status.
10
11 ``mix pleroma.digest test <nickname> <since_date>``
12
13 Example: ``mix pleroma.digest test donaldtheduck 2019-05-20``
14 """
15 def run(["test", nickname | opts]) do
16 Mix.Pleroma.start_pleroma()
17
18 user = Pleroma.User.get_by_nickname(nickname)
19
20 last_digest_emailed_at =
21 with [date] <- opts,
22 {:ok, datetime} <- Timex.parse(date, "{YYYY}-{0M}-{0D}") do
23 datetime
24 else
25 _ -> user.inserted_at
26 end
27
28 patched_user = %{user | last_digest_emailed_at: last_digest_emailed_at}
29
30 with %Swoosh.Email{} = email <- Pleroma.Emails.UserEmail.digest_email(patched_user) do
31 {:ok, _} = Pleroma.Emails.Mailer.deliver(email)
32
33 Mix.shell().info("Digest email have been sent to #{nickname} (#{user.email})")
34 else
35 _ ->
36 Mix.shell().info(
37 "Cound't find any mentions for #{nickname} since #{last_digest_emailed_at}"
38 )
39 end
40 end
41 end