2020-09-02 11:45:22 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-09-02 11:45:22 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Workers.BackupWorker do
|
2020-09-04 11:48:52 -06:00
|
|
|
use Oban.Worker, queue: :backup, max_attempts: 1
|
|
|
|
|
|
|
|
alias Oban.Job
|
2020-10-20 07:16:58 -06:00
|
|
|
alias Pleroma.User.Backup
|
2020-09-02 11:45:22 -06:00
|
|
|
|
2020-09-18 12:18:34 -06:00
|
|
|
def process(backup, admin_user_id \\ nil) do
|
|
|
|
%{"op" => "process", "backup_id" => backup.id, "admin_user_id" => admin_user_id}
|
2020-09-04 11:48:52 -06:00
|
|
|
|> new()
|
|
|
|
|> Oban.insert()
|
|
|
|
end
|
|
|
|
|
|
|
|
def schedule_deletion(backup) do
|
2020-10-20 07:16:58 -06:00
|
|
|
days = Pleroma.Config.get([Backup, :purge_after_days])
|
2020-09-04 11:48:52 -06:00
|
|
|
time = 60 * 60 * 24 * days
|
|
|
|
scheduled_at = Calendar.NaiveDateTime.add!(backup.inserted_at, time)
|
|
|
|
|
|
|
|
%{"op" => "delete", "backup_id" => backup.id}
|
|
|
|
|> new(scheduled_at: scheduled_at)
|
|
|
|
|> Oban.insert()
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(backup) do
|
|
|
|
%{"op" => "delete", "backup_id" => backup.id}
|
|
|
|
|> new()
|
|
|
|
|> Oban.insert()
|
|
|
|
end
|
2020-09-02 11:45:22 -06:00
|
|
|
|
2020-09-18 12:18:34 -06:00
|
|
|
def perform(%Job{
|
|
|
|
args: %{"op" => "process", "backup_id" => backup_id, "admin_user_id" => admin_user_id}
|
|
|
|
}) do
|
2020-09-02 11:45:22 -06:00
|
|
|
with {:ok, %Backup{} = backup} <-
|
2020-09-04 11:48:52 -06:00
|
|
|
backup_id |> Backup.get() |> Backup.process(),
|
|
|
|
{:ok, _job} <- schedule_deletion(backup),
|
2020-09-17 08:42:24 -06:00
|
|
|
:ok <- Backup.remove_outdated(backup),
|
2022-05-06 22:20:50 -06:00
|
|
|
:ok <- maybe_deliver_email(backup, admin_user_id) do
|
2020-09-02 11:45:22 -06:00
|
|
|
{:ok, backup}
|
|
|
|
end
|
|
|
|
end
|
2020-09-04 11:48:52 -06:00
|
|
|
|
|
|
|
def perform(%Job{args: %{"op" => "delete", "backup_id" => backup_id}}) do
|
|
|
|
case Backup.get(backup_id) do
|
|
|
|
%Backup{} = backup -> Backup.delete(backup)
|
|
|
|
nil -> :ok
|
|
|
|
end
|
|
|
|
end
|
2022-05-06 22:20:50 -06:00
|
|
|
|
|
|
|
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
|
2020-09-02 11:45:22 -06:00
|
|
|
end
|