1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OAuth.Token.Query do
7 Contains queries for OAuth Token.
10 import Ecto.Query, only: [from: 2]
12 @type query :: Ecto.Queryable.t() | Token.t()
14 alias Pleroma.Web.OAuth.Token
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)
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)
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)
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)
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)
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)
47 @spec preload(query, any) :: query
48 def preload(query \\ Token, assoc_preload \\ [])
50 def preload(query, assoc_preload) when is_list(assoc_preload) do
51 from(q in query, preload: ^assoc_preload)
54 def preload(query, _assoc_preload), do: query