1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Workers.BackupWorker do
6 use Oban.Worker, queue: :backup, max_attempts: 1
9 alias Pleroma.User.Backup
11 def process(backup, admin_user_id \\ nil) do
12 %{"op" => "process", "backup_id" => backup.id, "admin_user_id" => admin_user_id}
19 Pleroma.Config.get([:workers, :timeout, :backup], :timer.minutes(1))
22 def schedule_deletion(backup) do
23 days = Pleroma.Config.get([Backup, :purge_after_days])
24 time = 60 * 60 * 24 * days
25 scheduled_at = Calendar.NaiveDateTime.add!(backup.inserted_at, time)
27 %{"op" => "delete", "backup_id" => backup.id}
28 |> new(scheduled_at: scheduled_at)
33 %{"op" => "delete", "backup_id" => backup.id}
40 args: %{"op" => "process", "backup_id" => backup_id, "admin_user_id" => admin_user_id}
42 with {:ok, %Backup{} = backup} <-
43 backup_id |> Backup.get() |> Backup.process(),
44 {:ok, _job} <- schedule_deletion(backup),
45 :ok <- Backup.remove_outdated(backup),
46 :ok <- maybe_deliver_email(backup, admin_user_id) do
51 def perform(%Job{args: %{"op" => "delete", "backup_id" => backup_id}}) do
52 case Backup.get(backup_id) do
53 %Backup{} = backup -> Backup.delete(backup)
58 defp has_email?(user) do
59 not is_nil(user.email) and user.email != ""
62 defp maybe_deliver_email(backup, admin_user_id) do
63 has_mailer = Pleroma.Config.get([Pleroma.Emails.Mailer, :enabled])
64 backup = backup |> Pleroma.Repo.preload(:user)
66 if has_email?(backup.user) and has_mailer do
68 |> Pleroma.Emails.UserEmail.backup_is_ready_email(admin_user_id)
69 |> Pleroma.Emails.Mailer.deliver()