c140bc66a3a275ba830a34e9249ceb9993b381ed
[anni] / priv / repo / migrations / 20200907092050_move_tokens_expiration_into_oban.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Repo.Migrations.MoveTokensExpirationIntoOban do
6   use Ecto.Migration
7
8   import Ecto.Query, only: [from: 2]
9
10   def change do
11     Pleroma.Config.Oban.warn()
12
13     Application.ensure_all_started(:oban)
14
15     Supervisor.start_link([{Oban, Pleroma.Config.get(Oban)}],
16       strategy: :one_for_one,
17       name: Pleroma.Supervisor
18     )
19
20     if Pleroma.Config.get([:oauth2, :clean_expired_tokens]) do
21       from(t in Pleroma.Web.OAuth.Token, where: t.valid_until > ^NaiveDateTime.utc_now())
22       |> Pleroma.Repo.stream()
23       |> Stream.each(fn token ->
24         Pleroma.Workers.PurgeExpiredToken.enqueue(%{
25           token_id: token.id,
26           valid_until: DateTime.from_naive!(token.valid_until, "Etc/UTC"),
27           mod: Pleroma.Web.OAuth.Token
28         })
29       end)
30       |> Stream.run()
31     end
32
33     from(t in Pleroma.MFA.Token, where: t.valid_until > ^NaiveDateTime.utc_now())
34     |> Pleroma.Repo.stream()
35     |> Stream.each(fn token ->
36       Pleroma.Workers.PurgeExpiredToken.enqueue(%{
37         token_id: token.id,
38         valid_until: DateTime.from_naive!(token.valid_until, "Etc/UTC"),
39         mod: Pleroma.MFA.Token
40       })
41     end)
42     |> Stream.run()
43   end
44 end