Add backups deletion

This commit is contained in:
Egor Kislitsyn 2020-09-04 21:48:52 +04:00
commit 739cb1463b
No known key found for this signature in database
GPG key ID: 1B49CB15B71E7805
4 changed files with 91 additions and 10 deletions

View file

@ -15,6 +15,7 @@ defmodule Pleroma.Backup do
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Web.ActivityPub.UserView
alias Pleroma.Workers.BackupWorker
schema "backups" do
field(:content_type, :string)
@ -30,7 +31,7 @@ defmodule Pleroma.Backup do
def create(user) do
with :ok <- validate_limit(user),
{:ok, backup} <- user |> new() |> Repo.insert() do
Pleroma.Workers.BackupWorker.enqueue("process", %{"backup_id" => backup.id})
BackupWorker.process(backup)
end
end
@ -46,6 +47,14 @@ defmodule Pleroma.Backup do
}
end
def delete(backup) do
uploader = Pleroma.Config.get([Pleroma.Upload, :uploader])
with :ok <- uploader.delete_file("backups/" <> backup.file_name) do
Repo.delete(backup)
end
end
defp validate_limit(user) do
case get_last(user.id) do
%__MODULE__{inserted_at: inserted_at} ->
@ -75,7 +84,8 @@ defmodule Pleroma.Backup do
__MODULE__
|> where(user_id: ^user_id)
|> where([b], b.id != ^latest_id)
|> Repo.delete_all()
|> Repo.all()
|> Enum.each(&BackupWorker.delete/1)
end
def get(id), do: Repo.get(__MODULE__, id)