Resolve follow activity from accept/reject without ID (#328)
[akkoma] / lib / pleroma / web / o_auth / app.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.Web.OAuth.App do
6 use Ecto.Schema
7 import Ecto.Changeset
8 import Ecto.Query
9 alias Pleroma.Repo
10 alias Pleroma.User
11
12 @type t :: %__MODULE__{}
13
14 schema "apps" do
15 field(:client_name, :string)
16 field(:redirect_uris, :string)
17 field(:scopes, {:array, :string}, default: [])
18 field(:website, :string)
19 field(:client_id, :string)
20 field(:client_secret, :string)
21 field(:trusted, :boolean, default: false)
22
23 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
24
25 has_many(:oauth_authorizations, Pleroma.Web.OAuth.Authorization, on_delete: :delete_all)
26 has_many(:oauth_tokens, Pleroma.Web.OAuth.Token, on_delete: :delete_all)
27
28 timestamps()
29 end
30
31 @spec changeset(t(), map()) :: Ecto.Changeset.t()
32 def changeset(struct, params) do
33 cast(struct, params, [:client_name, :redirect_uris, :scopes, :website, :trusted, :user_id])
34 end
35
36 @spec register_changeset(t(), map()) :: Ecto.Changeset.t()
37 def register_changeset(struct, params \\ %{}) do
38 changeset =
39 struct
40 |> changeset(params)
41 |> validate_required([:client_name, :redirect_uris, :scopes])
42
43 if changeset.valid? do
44 changeset
45 |> put_change(
46 :client_id,
47 :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
48 )
49 |> put_change(
50 :client_secret,
51 :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
52 )
53 else
54 changeset
55 end
56 end
57
58 @spec create(map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
59 def create(params) do
60 %__MODULE__{}
61 |> register_changeset(params)
62 |> Repo.insert()
63 end
64
65 @spec update(pos_integer(), map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
66 def update(id, params) do
67 with %__MODULE__{} = app <- Repo.get(__MODULE__, id) do
68 app
69 |> changeset(params)
70 |> Repo.update()
71 end
72 end
73
74 @doc """
75 Gets app by attrs or create new with attrs.
76 And updates the scopes if need.
77 """
78 @spec get_or_make(map(), list(String.t())) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
79 def get_or_make(attrs, scopes) do
80 with %__MODULE__{} = app <- Repo.get_by(__MODULE__, attrs) do
81 update_scopes(app, scopes)
82 else
83 _e ->
84 %__MODULE__{}
85 |> register_changeset(Map.put(attrs, :scopes, scopes))
86 |> Repo.insert()
87 end
88 end
89
90 defp update_scopes(%__MODULE__{} = app, []), do: {:ok, app}
91 defp update_scopes(%__MODULE__{scopes: scopes} = app, scopes), do: {:ok, app}
92
93 defp update_scopes(%__MODULE__{} = app, scopes) do
94 app
95 |> change(%{scopes: scopes})
96 |> Repo.update()
97 end
98
99 @spec search(map()) :: {:ok, [t()], non_neg_integer()}
100 def search(params) do
101 query = from(a in __MODULE__)
102
103 query =
104 if params[:client_name] do
105 from(a in query, where: a.client_name == ^params[:client_name])
106 else
107 query
108 end
109
110 query =
111 if params[:client_id] do
112 from(a in query, where: a.client_id == ^params[:client_id])
113 else
114 query
115 end
116
117 query =
118 if Map.has_key?(params, :trusted) do
119 from(a in query, where: a.trusted == ^params[:trusted])
120 else
121 query
122 end
123
124 query =
125 from(u in query,
126 limit: ^params[:page_size],
127 offset: ^((params[:page] - 1) * params[:page_size])
128 )
129
130 count = Repo.aggregate(__MODULE__, :count, :id)
131
132 {:ok, Repo.all(query), count}
133 end
134
135 @spec get_user_apps(User.t()) :: {:ok, [t()], non_neg_integer()}
136 def get_user_apps(%User{id: user_id}) do
137 from(a in __MODULE__, where: a.user_id == ^user_id)
138 |> Repo.all()
139 end
140
141 @spec destroy(pos_integer()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
142 def destroy(id) do
143 with %__MODULE__{} = app <- Repo.get(__MODULE__, id) do
144 Repo.delete(app)
145 end
146 end
147
148 @spec errors(Ecto.Changeset.t()) :: map()
149 def errors(changeset) do
150 Enum.reduce(changeset.errors, %{}, fn
151 {:client_name, {error, _}}, acc ->
152 Map.put(acc, :name, error)
153
154 {key, {error, _}}, acc ->
155 Map.put(acc, key, error)
156 end)
157 end
158 end