summaryrefslogtreecommitdiff
path: root/priv/repo/migrations/20200907092050_move_tokens_expiration_into_oban.exs
blob: 725c5ab0b1cd6d2a7baad2369bff07ad2c4c8261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
defmodule Pleroma.Repo.Migrations.MoveTokensExpirationIntoOban do
  use Ecto.Migration

  import Ecto.Query, only: [from: 2]

  def change do
    Pleroma.Config.Oban.warn()

    Application.ensure_all_started(:oban)

    Supervisor.start_link([{Oban, Pleroma.Config.get(Oban)}],
      strategy: :one_for_one,
      name: Pleroma.Supervisor
    )

    if Pleroma.Config.get([:oauth2, :clean_expired_tokens]) do
      from(t in Pleroma.Web.OAuth.Token, where: t.valid_until > ^NaiveDateTime.utc_now())
      |> Pleroma.Repo.stream()
      |> Stream.each(fn token ->
        Pleroma.Workers.PurgeExpiredToken.enqueue(%{
          token_id: token.id,
          valid_until: DateTime.from_naive!(token.valid_until, "Etc/UTC"),
          mod: Pleroma.Web.OAuth.Token
        })
      end)
      |> Stream.run()
    end

    from(t in Pleroma.MFA.Token, where: t.valid_until > ^NaiveDateTime.utc_now())
    |> Pleroma.Repo.stream()
    |> Stream.each(fn token ->
      Pleroma.Workers.PurgeExpiredToken.enqueue(%{
        token_id: token.id,
        valid_until: DateTime.from_naive!(token.valid_until, "Etc/UTC"),
        mod: Pleroma.MFA.Token
      })
    end)
    |> Stream.run()
  end
end