[#1260] Merge remote-tracking branch 'remotes/upstream/develop' into 1260-rate-limite...
[akkoma] / lib / pleroma / web / oauth / token / query.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.OAuth.Token.Query do
6 @moduledoc """
7 Contains queries for OAuth Token.
8 """
9
10 import Ecto.Query, only: [from: 2]
11
12 @type query :: Ecto.Queryable.t() | Token.t()
13
14 alias Pleroma.Web.OAuth.Token
15
16 @spec get_by_refresh_token(query, String.t()) :: query
17 def get_by_refresh_token(query \\ Token, refresh_token) do
18 from(q in query, where: q.refresh_token == ^refresh_token)
19 end
20
21 @spec get_by_token(query, String.t()) :: query
22 def get_by_token(query \\ Token, token) do
23 from(q in query, where: q.token == ^token)
24 end
25
26 @spec get_by_app(query, String.t()) :: query
27 def get_by_app(query \\ Token, app_id) do
28 from(q in query, where: q.app_id == ^app_id)
29 end
30
31 @spec get_by_id(query, String.t()) :: query
32 def get_by_id(query \\ Token, id) do
33 from(q in query, where: q.id == ^id)
34 end
35
36 @spec get_expired_tokens(query, DateTime.t() | nil) :: query
37 def get_expired_tokens(query \\ Token, date \\ nil) do
38 expired_date = date || Timex.now()
39 from(q in query, where: fragment("?", q.valid_until) < ^expired_date)
40 end
41
42 @spec get_by_user(query, String.t()) :: query
43 def get_by_user(query \\ Token, user_id) do
44 from(q in query, where: q.user_id == ^user_id)
45 end
46
47 @spec preload(query, any) :: query
48 def preload(query \\ Token, assoc_preload \\ [])
49
50 def preload(query, assoc_preload) when is_list(assoc_preload) do
51 from(q in query, preload: ^assoc_preload)
52 end
53
54 def preload(query, _assoc_preload), do: query
55 end