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
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)
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
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)
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
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)
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