mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-12-18 07:48:19 +00:00
Allow users to create backups without providing email address
Ref: backup-without-email
This commit is contained in:
parent
c9600dbbbf
commit
f08241c8ab
|
@ -32,9 +32,7 @@ defmodule Pleroma.User.Backup do
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(user, admin_id \\ nil) do
|
def create(user, admin_id \\ nil) do
|
||||||
with :ok <- validate_email_enabled(),
|
with :ok <- validate_limit(user, admin_id),
|
||||||
:ok <- validate_user_email(user),
|
|
||||||
:ok <- validate_limit(user, admin_id),
|
|
||||||
{:ok, backup} <- user |> new() |> Repo.insert() do
|
{:ok, backup} <- user |> new() |> Repo.insert() do
|
||||||
BackupWorker.process(backup, admin_id)
|
BackupWorker.process(backup, admin_id)
|
||||||
end
|
end
|
||||||
|
@ -86,20 +84,6 @@ defmodule Pleroma.User.Backup do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp validate_email_enabled do
|
|
||||||
if Pleroma.Config.get([Pleroma.Emails.Mailer, :enabled]) do
|
|
||||||
:ok
|
|
||||||
else
|
|
||||||
{:error, dgettext("errors", "Backups require enabled email")}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp validate_user_email(%User{email: nil}) do
|
|
||||||
{:error, dgettext("errors", "Email is required")}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp validate_user_email(%User{email: email}) when is_binary(email), do: :ok
|
|
||||||
|
|
||||||
def get_last(user_id) do
|
def get_last(user_id) do
|
||||||
__MODULE__
|
__MODULE__
|
||||||
|> where(user_id: ^user_id)
|
|> where(user_id: ^user_id)
|
||||||
|
|
|
@ -37,10 +37,7 @@ defmodule Pleroma.Workers.BackupWorker do
|
||||||
backup_id |> Backup.get() |> Backup.process(),
|
backup_id |> Backup.get() |> Backup.process(),
|
||||||
{:ok, _job} <- schedule_deletion(backup),
|
{:ok, _job} <- schedule_deletion(backup),
|
||||||
:ok <- Backup.remove_outdated(backup),
|
:ok <- Backup.remove_outdated(backup),
|
||||||
{:ok, _} <-
|
:ok <- maybe_deliver_email(backup, admin_user_id) do
|
||||||
backup
|
|
||||||
|> Pleroma.Emails.UserEmail.backup_is_ready_email(admin_user_id)
|
|
||||||
|> Pleroma.Emails.Mailer.deliver() do
|
|
||||||
{:ok, backup}
|
{:ok, backup}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -51,4 +48,23 @@ defmodule Pleroma.Workers.BackupWorker do
|
||||||
nil -> :ok
|
nil -> :ok
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp has_email?(user) do
|
||||||
|
not is_nil(user.email) and user.email != ""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_deliver_email(backup, admin_user_id) do
|
||||||
|
has_mailer = Pleroma.Config.get([Pleroma.Emails.Mailer, :enabled])
|
||||||
|
backup = backup |> Pleroma.Repo.preload(:user)
|
||||||
|
|
||||||
|
if has_email?(backup.user) and has_mailer do
|
||||||
|
backup
|
||||||
|
|> Pleroma.Emails.UserEmail.backup_is_ready_email(admin_user_id)
|
||||||
|
|> Pleroma.Emails.Mailer.deliver()
|
||||||
|
|
||||||
|
:ok
|
||||||
|
else
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,15 +22,15 @@ defmodule Pleroma.User.BackupTest do
|
||||||
clear_config([Pleroma.Emails.Mailer, :enabled], true)
|
clear_config([Pleroma.Emails.Mailer, :enabled], true)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it requries enabled email" do
|
test "it does not requrie enabled email" do
|
||||||
clear_config([Pleroma.Emails.Mailer, :enabled], false)
|
clear_config([Pleroma.Emails.Mailer, :enabled], false)
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
assert {:error, "Backups require enabled email"} == Backup.create(user)
|
assert {:ok, _} = Backup.create(user)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it requries user's email" do
|
test "it does not require user's email" do
|
||||||
user = insert(:user, %{email: nil})
|
user = insert(:user, %{email: nil})
|
||||||
assert {:error, "Email is required"} == Backup.create(user)
|
assert {:ok, _} = Backup.create(user)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it creates a backup record and an Oban job" do
|
test "it creates a backup record and an Oban job" do
|
||||||
|
@ -75,6 +75,43 @@ defmodule Pleroma.User.BackupTest do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it does not send an email if the user does not have an email" do
|
||||||
|
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
|
||||||
|
%{id: user_id} = user = insert(:user, %{email: nil})
|
||||||
|
|
||||||
|
assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id} = args}} = Backup.create(user)
|
||||||
|
assert {:ok, backup} = perform_job(BackupWorker, args)
|
||||||
|
assert backup.file_size > 0
|
||||||
|
assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
|
||||||
|
|
||||||
|
assert_no_email_sent()
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it does not send an email if mailer is not on" do
|
||||||
|
clear_config([Pleroma.Emails.Mailer, :enabled], false)
|
||||||
|
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
|
||||||
|
%{id: user_id} = user = insert(:user)
|
||||||
|
|
||||||
|
assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id} = args}} = Backup.create(user)
|
||||||
|
assert {:ok, backup} = perform_job(BackupWorker, args)
|
||||||
|
assert backup.file_size > 0
|
||||||
|
assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
|
||||||
|
|
||||||
|
assert_no_email_sent()
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it does not send an email if the user has an empty email" do
|
||||||
|
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
|
||||||
|
%{id: user_id} = user = insert(:user, %{email: ""})
|
||||||
|
|
||||||
|
assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id} = args}} = Backup.create(user)
|
||||||
|
assert {:ok, backup} = perform_job(BackupWorker, args)
|
||||||
|
assert backup.file_size > 0
|
||||||
|
assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
|
||||||
|
|
||||||
|
assert_no_email_sent()
|
||||||
|
end
|
||||||
|
|
||||||
test "it removes outdated backups after creating a fresh one" do
|
test "it removes outdated backups after creating a fresh one" do
|
||||||
clear_config([Backup, :limit_days], -1)
|
clear_config([Backup, :limit_days], -1)
|
||||||
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
|
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
|
||||||
|
|
|
@ -82,4 +82,24 @@ defmodule Pleroma.Web.PleromaAPI.BackupControllerTest do
|
||||||
|> post("/api/v1/pleroma/backups")
|
|> post("/api/v1/pleroma/backups")
|
||||||
|> json_response_and_validate_schema(400)
|
|> json_response_and_validate_schema(400)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "Backup without email address" do
|
||||||
|
user = Pleroma.Factory.insert(:user, email: nil)
|
||||||
|
%{conn: conn} = oauth_access(["read:accounts"], user: user)
|
||||||
|
|
||||||
|
assert is_nil(user.email)
|
||||||
|
|
||||||
|
assert [
|
||||||
|
%{
|
||||||
|
"content_type" => "application/zip",
|
||||||
|
"url" => _url,
|
||||||
|
"file_size" => 0,
|
||||||
|
"processed" => false,
|
||||||
|
"inserted_at" => _
|
||||||
|
}
|
||||||
|
] =
|
||||||
|
conn
|
||||||
|
|> post("/api/v1/pleroma/backups")
|
||||||
|
|> json_response_and_validate_schema(:ok)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue