Add task to test emails
authorRoman Chvanikov <chvanikoff@gmail.com>
Mon, 3 Jun 2019 23:48:21 +0000 (02:48 +0300)
committerRoman Chvanikov <chvanikoff@gmail.com>
Mon, 3 Jun 2019 23:48:21 +0000 (02:48 +0300)
lib/mix/tasks/pleroma/digest.ex [new file with mode: 0644]
lib/pleroma/digest_email_worker.ex
lib/pleroma/emails/user_email.ex
lib/pleroma/web/templates/email/digest.html.eex
test/mix/tasks/pleroma.digest_test.exs [new file with mode: 0644]

diff --git a/lib/mix/tasks/pleroma/digest.ex b/lib/mix/tasks/pleroma/digest.ex
new file mode 100644 (file)
index 0000000..7ac3df5
--- /dev/null
@@ -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
index f7b3d81cd66a61f2e580fc8a4a4a90b34d5908fc..8c28dca180fe3cd4447af1958af092c7a71c30c4 100644 (file)
@@ -18,9 +18,9 @@ defmodule Pleroma.DigestEmailWorker 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
index 64f8551122c7db5ab9e58fdb4345e83bc0a53c2e..0ad0aed400b1e4533f2de261846f74ab24a41e9e 100644 (file)
@@ -103,12 +103,24 @@ defmodule Pleroma.Emails.UserEmail 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 ->
index 93c9c884fc98a05fd436d2006b966528323b1301..c9dd699fd6e380004d2d8725462c6d3af42e7a0f 100644 (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>
 
diff --git a/test/mix/tasks/pleroma.digest_test.exs b/test/mix/tasks/pleroma.digest_test.exs
new file mode 100644 (file)
index 0000000..1a54ac3
--- /dev/null
@@ -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