Improve readability
[akkoma] / lib / pleroma / workers / purge_expired_token.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.PurgeExpiredToken do
6 @moduledoc """
7 Worker which purges expired OAuth tokens
8 """
9
10 use Oban.Worker, queue: :token_expiration, max_attempts: 1
11
12 @spec enqueue(%{token_id: integer(), valid_until: DateTime.t(), mod: module()}) ::
13 {:ok, Oban.Job.t()} | {:error, Ecto.Changeset.t()}
14 def enqueue(args) do
15 {scheduled_at, args} = Map.pop(args, :valid_until)
16
17 args
18 |> __MODULE__.new(scheduled_at: scheduled_at)
19 |> Oban.insert()
20 end
21
22 @impl true
23 def perform(%Oban.Job{args: %{"token_id" => id, "mod" => module}}) do
24 module
25 |> String.to_existing_atom()
26 |> Pleroma.Repo.get(id)
27 |> Pleroma.Repo.delete()
28 end
29 end