Improve readability
[akkoma] / lib / pleroma / workers / backup_worker.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Workers.BackupWorker do
6 use Oban.Worker, queue: :backup, max_attempts: 1
7
8 alias Oban.Job
9 alias Pleroma.User.Backup
10
11 def process(backup, admin_user_id \\ nil) do
12 %{"op" => "process", "backup_id" => backup.id, "admin_user_id" => admin_user_id}
13 |> new()
14 |> Oban.insert()
15 end
16
17 def schedule_deletion(backup) do
18 days = Pleroma.Config.get([Backup, :purge_after_days])
19 time = 60 * 60 * 24 * days
20 scheduled_at = Calendar.NaiveDateTime.add!(backup.inserted_at, time)
21
22 %{"op" => "delete", "backup_id" => backup.id}
23 |> new(scheduled_at: scheduled_at)
24 |> Oban.insert()
25 end
26
27 def delete(backup) do
28 %{"op" => "delete", "backup_id" => backup.id}
29 |> new()
30 |> Oban.insert()
31 end
32
33 def perform(%Job{
34 args: %{"op" => "process", "backup_id" => backup_id, "admin_user_id" => admin_user_id}
35 }) do
36 with {:ok, %Backup{} = backup} <-
37 backup_id |> Backup.get() |> Backup.process(),
38 {:ok, _job} <- schedule_deletion(backup),
39 :ok <- Backup.remove_outdated(backup),
40 {:ok, _} <-
41 backup
42 |> Pleroma.Emails.UserEmail.backup_is_ready_email(admin_user_id)
43 |> Pleroma.Emails.Mailer.deliver() do
44 {:ok, backup}
45 end
46 end
47
48 def perform(%Job{args: %{"op" => "delete", "backup_id" => backup_id}}) do
49 case Backup.get(backup_id) do
50 %Backup{} = backup -> Backup.delete(backup)
51 nil -> :ok
52 end
53 end
54 end