Add "Your backup is ready" email
authorEgor Kislitsyn <egor@kislitsyn.com>
Thu, 17 Sep 2020 14:42:24 +0000 (18:42 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Wed, 7 Oct 2020 14:34:30 +0000 (18:34 +0400)
lib/pleroma/emails/user_email.ex
lib/pleroma/workers/backup_worker.ex
test/backup_test.exs

index 1d8c72ae93a5b057c106e34d45a31efcad8a2eaf..f943dda0df59349cde11f395c79d02a0dc679a66 100644 (file)
@@ -189,4 +189,20 @@ defmodule Pleroma.Emails.UserEmail do
 
     Router.Helpers.subscription_url(Endpoint, :unsubscribe, token)
   end
+
+  def backup_is_ready_email(backup) do
+    %{user: user} = Pleroma.Repo.preload(backup, :user)
+    download_url = Pleroma.Web.PleromaAPI.BackupView.download_url(backup)
+
+    html_body = """
+    <p>You requested a full backup of your Pleroma account. It's ready for download:</p>
+    <p><a href="#{download_url}"></a></p>
+    """
+
+    new()
+    |> to(recipient(user))
+    |> from(sender())
+    |> subject("Your account archive is ready")
+    |> html_body(html_body)
+  end
 end
index f400207942e0ee449a66ee182076c42192b83747..405d55269b4307d836934049a15f678d2d65adc8 100644 (file)
@@ -34,7 +34,11 @@ defmodule Pleroma.Workers.BackupWorker do
     with {:ok, %Backup{} = backup} <-
            backup_id |> Backup.get() |> Backup.process(),
          {:ok, _job} <- schedule_deletion(backup),
-         :ok <- Backup.remove_outdated(backup) do
+         :ok <- Backup.remove_outdated(backup),
+         {:ok, _} <-
+           backup
+           |> Pleroma.Emails.UserEmail.backup_is_ready_email()
+           |> Pleroma.Emails.Mailer.deliver() do
       {:ok, backup}
     end
   end
index 318c8c419d2f083f476fcaaade85c3f64bd0123a..0ea40e6fd83df3b5179a26f0ced6d6b925b2e874 100644 (file)
@@ -6,8 +6,9 @@ defmodule Pleroma.BackupTest do
   use Oban.Testing, repo: Pleroma.Repo
   use Pleroma.DataCase
 
-  import Pleroma.Factory
   import Mock
+  import Pleroma.Factory
+  import Swoosh.TestAssertions
 
   alias Pleroma.Backup
   alias Pleroma.Bookmark
@@ -65,6 +66,8 @@ defmodule Pleroma.BackupTest do
     assert_enqueued(worker: BackupWorker, args: delete_job_args)
     assert {:ok, backup} = perform_job(BackupWorker, delete_job_args)
     refute Backup.get(backup_id)
+
+    assert_email_sent(Pleroma.Emails.UserEmail.backup_is_ready_email(backup))
   end
 
   test "it removes outdated backups after creating a fresh one" do