Merge develop branch upstream
[akkoma] / lib / pleroma / web / admin_api / controllers / user_controller.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.AdminAPI.UserController do
6 use Pleroma.Web, :controller
7
8 import Pleroma.Web.ControllerHelper,
9 only: [fetch_integer_param: 3]
10
11 alias Pleroma.ModerationLog
12 alias Pleroma.User
13 alias Pleroma.Web.ActivityPub.Builder
14 alias Pleroma.Web.ActivityPub.Pipeline
15 alias Pleroma.Web.AdminAPI
16 alias Pleroma.Web.AdminAPI.Search
17 alias Pleroma.Web.Plugs.OAuthScopesPlug
18
19 @users_page_size 50
20
21 plug(Pleroma.Web.ApiSpec.CastAndValidate)
22
23 plug(
24 OAuthScopesPlug,
25 %{scopes: ["admin:read:accounts"]}
26 when action in [:index, :show]
27 )
28
29 plug(
30 OAuthScopesPlug,
31 %{scopes: ["admin:write:accounts"]}
32 when action in [
33 :delete,
34 :create,
35 :toggle_activation,
36 :activate,
37 :deactivate,
38 :approve
39 ]
40 )
41
42 plug(
43 OAuthScopesPlug,
44 %{scopes: ["admin:write:follows"]}
45 when action in [:follow, :unfollow]
46 )
47
48 action_fallback(AdminAPI.FallbackController)
49
50 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.UserOperation
51
52 def delete(conn, %{nickname: nickname}) do
53 conn
54 |> Map.put(:body_params, %{nicknames: [nickname]})
55 |> delete(%{})
56 end
57
58 def delete(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
59 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
60
61 Enum.each(users, fn user ->
62 {:ok, delete_data, _} = Builder.delete(admin, user.ap_id)
63 Pipeline.common_pipeline(delete_data, local: true)
64 end)
65
66 ModerationLog.insert_log(%{
67 actor: admin,
68 subject: users,
69 action: "delete"
70 })
71
72 json(conn, nicknames)
73 end
74
75 def follow(
76 %{
77 assigns: %{user: admin},
78 body_params: %{
79 follower: follower_nick,
80 followed: followed_nick
81 }
82 } = conn,
83 _
84 ) do
85 with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
86 %User{} = followed <- User.get_cached_by_nickname(followed_nick) do
87 User.follow(follower, followed)
88
89 ModerationLog.insert_log(%{
90 actor: admin,
91 followed: followed,
92 follower: follower,
93 action: "follow"
94 })
95 end
96
97 json(conn, "ok")
98 end
99
100 def unfollow(
101 %{
102 assigns: %{user: admin},
103 body_params: %{
104 follower: follower_nick,
105 followed: followed_nick
106 }
107 } = conn,
108 _
109 ) do
110 with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
111 %User{} = followed <- User.get_cached_by_nickname(followed_nick) do
112 User.unfollow(follower, followed)
113
114 ModerationLog.insert_log(%{
115 actor: admin,
116 followed: followed,
117 follower: follower,
118 action: "unfollow"
119 })
120 end
121
122 json(conn, "ok")
123 end
124
125 def create(%{assigns: %{user: admin}, body_params: %{users: users}} = conn, _) do
126 changesets =
127 users
128 |> Enum.map(fn %{nickname: nickname, email: email, password: password} ->
129 user_data = %{
130 nickname: nickname,
131 name: nickname,
132 email: email,
133 password: password,
134 password_confirmation: password,
135 bio: "."
136 }
137
138 User.register_changeset(%User{}, user_data, need_confirmation: false)
139 end)
140 |> Enum.reduce(Ecto.Multi.new(), fn changeset, multi ->
141 Ecto.Multi.insert(multi, Ecto.UUID.generate(), changeset)
142 end)
143
144 case Pleroma.Repo.transaction(changesets) do
145 {:ok, users_map} ->
146 users =
147 users_map
148 |> Map.values()
149 |> Enum.map(fn user ->
150 {:ok, user} = User.post_register_action(user)
151
152 user
153 end)
154
155 ModerationLog.insert_log(%{
156 actor: admin,
157 subjects: users,
158 action: "create"
159 })
160
161 render(conn, "created_many.json", users: users)
162
163 {:error, id, changeset, _} ->
164 changesets =
165 Enum.map(changesets.operations, fn
166 {^id, {:changeset, _current_changeset, _}} ->
167 changeset
168
169 {_, {:changeset, current_changeset, _}} ->
170 current_changeset
171 end)
172
173 conn
174 |> put_status(:conflict)
175 |> render("create_errors.json", changesets: changesets)
176 end
177 end
178
179 def show(%{assigns: %{user: admin}} = conn, %{nickname: nickname}) do
180 with %User{} = user <- User.get_cached_by_nickname_or_id(nickname, for: admin) do
181 render(conn, "show.json", %{user: user})
182 else
183 _ -> {:error, :not_found}
184 end
185 end
186
187 def toggle_activation(%{assigns: %{user: admin}} = conn, %{nickname: nickname}) do
188 user = User.get_cached_by_nickname(nickname)
189
190 {:ok, updated_user} = User.set_activation(user, !user.is_active)
191
192 action = if !user.is_active, do: "activate", else: "deactivate"
193
194 ModerationLog.insert_log(%{
195 actor: admin,
196 subject: [user],
197 action: action
198 })
199
200 render(conn, "show.json", user: updated_user)
201 end
202
203 def activate(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
204 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
205 {:ok, updated_users} = User.set_activation(users, true)
206
207 ModerationLog.insert_log(%{
208 actor: admin,
209 subject: users,
210 action: "activate"
211 })
212
213 render(conn, "index.json", users: Keyword.values(updated_users))
214 end
215
216 def deactivate(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
217 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
218 {:ok, updated_users} = User.set_activation(users, false)
219
220 ModerationLog.insert_log(%{
221 actor: admin,
222 subject: users,
223 action: "deactivate"
224 })
225
226 render(conn, "index.json", users: Keyword.values(updated_users))
227 end
228
229 def approve(%{assigns: %{user: admin}, body_params: %{nicknames: nicknames}} = conn, _) do
230 users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
231 {:ok, updated_users} = User.approve(users)
232
233 ModerationLog.insert_log(%{
234 actor: admin,
235 subject: users,
236 action: "approve"
237 })
238
239 render(conn, "index.json", users: updated_users)
240 end
241
242 def index(conn, params) do
243 {page, page_size} = page_params(params)
244 filters = maybe_parse_filters(params[:filters])
245
246 search_params =
247 %{
248 query: params[:query],
249 page: page,
250 page_size: page_size,
251 tags: params[:tags],
252 name: params[:name],
253 email: params[:email],
254 actor_types: params[:actor_types]
255 }
256 |> Map.merge(filters)
257
258 with {:ok, users, count} <- Search.user(search_params) do
259 render(conn, "index.json", users: users, count: count, page_size: page_size)
260 end
261 end
262
263 @filters ~w(local external active deactivated need_approval unconfirmed is_admin is_moderator)
264
265 @spec maybe_parse_filters(String.t()) :: %{required(String.t()) => true} | %{}
266 defp maybe_parse_filters(filters) when is_nil(filters) or filters == "", do: %{}
267
268 defp maybe_parse_filters(filters) do
269 filters
270 |> String.split(",")
271 |> Enum.filter(&Enum.member?(@filters, &1))
272 |> Map.new(&{String.to_existing_atom(&1), true})
273 end
274
275 defp page_params(params) do
276 {
277 fetch_integer_param(params, :page, 1),
278 fetch_integer_param(params, :page_size, @users_page_size)
279 }
280 end
281 end