Require email

This commit is contained in:
Egor Kislitsyn 2020-09-15 18:07:28 +04:00
parent 386199063b
commit 27bc121ec0
No known key found for this signature in database
GPG Key ID: 1B49CB15B71E7805
3 changed files with 33 additions and 5 deletions

View File

@ -1085,6 +1085,9 @@ Control favicons for instances.
## Account Backup
!!! note
Requires enabled email
* `:purge_after_days` an integer, remove backup achives after N days.
* `:limit_days` an integer, limit user to export not more often than once per N days.

View File

@ -31,7 +31,9 @@ defmodule Pleroma.Backup do
end
def create(user) do
with :ok <- validate_limit(user),
with :ok <- validate_email_enabled(),
:ok <- validate_user_email(user),
:ok <- validate_limit(user),
{:ok, backup} <- user |> new() |> Repo.insert() do
BackupWorker.process(backup)
end
@ -74,6 +76,17 @@ defmodule Pleroma.Backup do
end
end
defp validate_email_enabled do
if Pleroma.Config.get([Pleroma.Emails.Mailer, :enabled]) do
:ok
else
{:error, "Backups require enabled email"}
end
end
defp validate_user_email(%User{email: nil}), do: {:error, "Email is required"}
defp validate_user_email(%User{email: email}) when is_binary(email), do: :ok
def get_last(user_id) do
__MODULE__
|> where(user_id: ^user_id)
@ -100,7 +113,7 @@ defmodule Pleroma.Backup do
def get(id), do: Repo.get(__MODULE__, id)
def process(%__MODULE__{} = backup) do
with {:ok, zip_file} <- zip(backup),
with {:ok, zip_file} <- export(backup),
{:ok, %{size: size}} <- File.stat(zip_file),
{:ok, _upload} <- upload(backup, zip_file) do
backup
@ -110,7 +123,7 @@ defmodule Pleroma.Backup do
end
@files ['actor.json', 'outbox.json', 'likes.json', 'bookmarks.json']
def zip(%__MODULE__{} = backup) do
def export(%__MODULE__{} = backup) do
backup = Repo.preload(backup, :user)
name = String.trim_trailing(backup.file_name, ".zip")
dir = Path.join(System.tmp_dir!(), name)

View File

@ -18,6 +18,18 @@ defmodule Pleroma.BackupTest do
setup do
clear_config([Pleroma.Upload, :uploader])
clear_config([Pleroma.Backup, :limit_days])
clear_config([Pleroma.Emails.Mailer, :enabled])
end
test "it requries enabled email" do
Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], false)
user = insert(:user)
assert {:error, "Backups require enabled email"} == Backup.create(user)
end
test "it requries user's email" do
user = insert(:user, %{email: nil})
assert {:error, "Email is required"} == Backup.create(user)
end
test "it creates a backup record and an Oban job" do
@ -91,7 +103,7 @@ defmodule Pleroma.BackupTest do
Bookmark.create(user.id, status3.id)
assert {:ok, backup} = user |> Backup.new() |> Repo.insert()
assert {:ok, path} = Backup.zip(backup)
assert {:ok, path} = Backup.export(backup)
assert {:ok, zipfile} = :zip.zip_open(String.to_charlist(path), [:memory])
assert {:ok, {'actor.json', json}} = :zip.zip_get('actor.json', zipfile)
@ -193,7 +205,7 @@ defmodule Pleroma.BackupTest do
Bookmark.create(user.id, status3.id)
assert {:ok, backup} = user |> Backup.new() |> Repo.insert()
assert {:ok, path} = Backup.zip(backup)
assert {:ok, path} = Backup.export(backup)
[path: path, backup: backup]
end