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