Add task to test emails

This commit is contained in:
Roman Chvanikov 2019-06-04 02:48:21 +03:00
parent 6ef145b4fc
commit 3e17610587
5 changed files with 96 additions and 8 deletions

View file

@ -0,0 +1,34 @@
defmodule Mix.Tasks.Pleroma.Digest do
use Mix.Task
alias Mix.Tasks.Pleroma.Common
@shortdoc "Manages digest emails"
@moduledoc """
Manages digest emails
## Send digest email since given date (user registration date by default)
ignoring user activity status.
``mix pleroma.digest test <nickname> <since_date>``
Example: ``mix pleroma.digest test donaldtheduck 2019-05-20``
"""
def run(["test", nickname | opts]) do
Common.start_pleroma()
user = Pleroma.User.get_by_nickname(nickname)
last_digest_emailed_at =
with [date] <- opts,
{:ok, datetime} <- Timex.parse(date, "{YYYY}-{0M}-{0D}") do
datetime
else
_ -> user.inserted_at
end
patched_user = %{user | last_digest_emailed_at: last_digest_emailed_at}
:ok = Pleroma.DigestEmailWorker.run([patched_user])
Mix.shell().info("Digest email have been sent to #{nickname} (#{user.email})")
end
end

View file

@ -18,9 +18,9 @@ def run do
|> run()
end
defp run([]), do: :ok
def run([]), do: :ok
defp run([user | users]) do
def run([user | users]) do
with %Swoosh.Email{} = email <- Pleroma.Emails.UserEmail.digest_email(user) do
Pleroma.Emails.Mailer.deliver_async(email)
end

View file

@ -103,12 +103,24 @@ def digest_email(user) do
new_notifications =
Pleroma.Notification.for_user_since(user, user.last_digest_emailed_at)
|> Enum.reduce(%{followers: [], mentions: []}, fn
%{activity: %{data: %{"type" => "Create"}, actor: actor}} = notification, acc ->
new_mention = %{data: notification, from: Pleroma.User.get_by_ap_id(actor)}
%{activity: %{data: %{"type" => "Create"}, actor: actor} = activity} = notification,
acc ->
new_mention = %{
data: notification,
object: Pleroma.Object.normalize(activity),
from: Pleroma.User.get_by_ap_id(actor)
}
%{acc | mentions: [new_mention | acc.mentions]}
%{activity: %{data: %{"type" => "Follow"}, actor: actor}} = notification, acc ->
new_follower = %{data: notification, from: Pleroma.User.get_by_ap_id(actor)}
%{activity: %{data: %{"type" => "Follow"}, actor: actor} = activity} = notification,
acc ->
new_follower = %{
data: notification,
object: Pleroma.Object.normalize(activity),
from: Pleroma.User.get_by_ap_id(actor)
}
%{acc | followers: [new_follower | acc.followers]}
_, acc ->

View file

@ -2,8 +2,8 @@
<h2>New Mentions:</h2>
<ul>
<%= for %{data: mention, from: from} <- @mentions do %>
<li><%= link from.nickname, to: mention.activity.actor %>: <%= raw mention.activity.object.data["content"] %></li>
<%= for %{data: mention, object: object, from: from} <- @mentions do %>
<li><%= link from.nickname, to: mention.activity.actor %>: <%= raw object.data["content"] %></li>
<% end %>
</ul>

View file

@ -0,0 +1,42 @@
defmodule Mix.Tasks.Pleroma.DigestTest do
use Pleroma.DataCase
import Pleroma.Factory
import Swoosh.TestAssertions
alias Pleroma.Web.CommonAPI
setup_all do
Mix.shell(Mix.Shell.Process)
on_exit(fn ->
Mix.shell(Mix.Shell.IO)
end)
:ok
end
describe "pleroma.digest test" do
test "Sends digest to the given user" do
user1 = insert(:user)
user2 = insert(:user)
Enum.each(0..10, fn i ->
{:ok, _activity} =
CommonAPI.post(user1, %{
"status" => "hey ##{i} @#{user2.nickname}!"
})
end)
Mix.Tasks.Pleroma.Digest.run(["test", user2.nickname])
assert_email_sent(
to: {user2.name, user2.email},
html_body: ~r/new mentions:/i
)
assert_received {:mix_shell, :info, [message]}
assert message =~ "Digest email have been sent"
end
end
end