1f7eb9375f19e39ab11918de3c974e59216f1f05
[akkoma] / lib / mix / tasks / pleroma / user.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 Mix.Tasks.Pleroma.User do
6 use Mix.Task
7 import Mix.Pleroma
8 alias Ecto.Changeset
9 alias Pleroma.User
10 alias Pleroma.UserInviteToken
11 alias Pleroma.Web.ActivityPub.Builder
12 alias Pleroma.Web.ActivityPub.Pipeline
13
14 @shortdoc "Manages Pleroma users"
15 @moduledoc File.read!("docs/administration/CLI_tasks/user.md")
16
17 def run(["new", nickname, email | rest]) do
18 {options, [], []} =
19 OptionParser.parse(
20 rest,
21 strict: [
22 name: :string,
23 bio: :string,
24 password: :string,
25 moderator: :boolean,
26 admin: :boolean,
27 assume_yes: :boolean
28 ],
29 aliases: [
30 y: :assume_yes
31 ]
32 )
33
34 name = Keyword.get(options, :name, nickname)
35 bio = Keyword.get(options, :bio, "")
36
37 {password, generated_password?} =
38 case Keyword.get(options, :password) do
39 nil ->
40 {:crypto.strong_rand_bytes(16) |> Base.encode64(), true}
41
42 password ->
43 {password, false}
44 end
45
46 moderator? = Keyword.get(options, :moderator, false)
47 admin? = Keyword.get(options, :admin, false)
48 assume_yes? = Keyword.get(options, :assume_yes, false)
49
50 shell_info("""
51 A user will be created with the following information:
52 - nickname: #{nickname}
53 - email: #{email}
54 - password: #{
55 if(generated_password?, do: "[generated; a reset link will be created]", else: password)
56 }
57 - name: #{name}
58 - bio: #{bio}
59 - moderator: #{if(moderator?, do: "true", else: "false")}
60 - admin: #{if(admin?, do: "true", else: "false")}
61 """)
62
63 proceed? = assume_yes? or shell_prompt("Continue?", "n") in ~w(Yn Y y)
64
65 if proceed? do
66 start_pleroma()
67
68 params = %{
69 nickname: nickname,
70 email: email,
71 password: password,
72 password_confirmation: password,
73 name: name,
74 bio: bio
75 }
76
77 changeset = User.register_changeset(%User{}, params, is_confirmed: true)
78 {:ok, _user} = User.register(changeset)
79
80 shell_info("User #{nickname} created")
81
82 if moderator? do
83 run(["set", nickname, "--moderator"])
84 end
85
86 if admin? do
87 run(["set", nickname, "--admin"])
88 end
89
90 if generated_password? do
91 run(["reset_password", nickname])
92 end
93 else
94 shell_info("User will not be created.")
95 end
96 end
97
98 def run(["rm", nickname]) do
99 start_pleroma()
100
101 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
102 {:ok, delete_data, _} <- Builder.delete(user, user.ap_id),
103 {:ok, _delete, _} <- Pipeline.common_pipeline(delete_data, local: true) do
104 shell_info("User #{nickname} deleted.")
105 else
106 _ -> shell_error("No local user #{nickname}")
107 end
108 end
109
110 def run(["reset_password", nickname]) do
111 start_pleroma()
112
113 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
114 {:ok, token} <- Pleroma.PasswordResetToken.create_token(user) do
115 shell_info("Generated password reset token for #{user.nickname}")
116
117 IO.puts(
118 "URL: #{
119 Pleroma.Web.Router.Helpers.reset_password_url(
120 Pleroma.Web.Endpoint,
121 :reset,
122 token.token
123 )
124 }"
125 )
126 else
127 _ ->
128 shell_error("No local user #{nickname}")
129 end
130 end
131
132 def run(["reset_mfa", nickname]) do
133 start_pleroma()
134
135 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
136 {:ok, _token} <- Pleroma.MFA.disable(user) do
137 shell_info("Multi-Factor Authentication disabled for #{user.nickname}")
138 else
139 _ ->
140 shell_error("No local user #{nickname}")
141 end
142 end
143
144 def run(["deactivate", nickname]) do
145 start_pleroma()
146
147 with %User{} = user <- User.get_cached_by_nickname(nickname) do
148 shell_info("Deactivating #{user.nickname}")
149 User.set_activation(user, false)
150 :timer.sleep(500)
151
152 user = User.get_cached_by_id(user.id)
153
154 if Enum.empty?(Enum.filter(User.get_friends(user), & &1.local)) do
155 shell_info("Successfully unsubscribed all local followers from #{user.nickname}")
156 end
157 else
158 _ ->
159 shell_error("No user #{nickname}")
160 end
161 end
162
163 def run(["deactivate_all_from_instance", instance]) do
164 start_pleroma()
165
166 Pleroma.User.Query.build(%{nickname: "@#{instance}"})
167 |> Pleroma.Repo.chunk_stream(500, :batches)
168 |> Stream.each(fn users ->
169 users
170 |> Enum.each(fn user ->
171 run(["deactivate", user.nickname])
172 end)
173 end)
174 |> Stream.run()
175 end
176
177 def run(["set", nickname | rest]) do
178 start_pleroma()
179
180 {options, [], []} =
181 OptionParser.parse(
182 rest,
183 strict: [
184 admin: :boolean,
185 confirmed: :boolean,
186 locked: :boolean,
187 moderator: :boolean
188 ]
189 )
190
191 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
192 user =
193 case Keyword.get(options, :admin) do
194 nil -> user
195 value -> set_admin(user, value)
196 end
197
198 user =
199 case Keyword.get(options, :confirmed) do
200 nil -> user
201 value -> set_confirmation(user, value)
202 end
203
204 user =
205 case Keyword.get(options, :locked) do
206 nil -> user
207 value -> set_locked(user, value)
208 end
209
210 _user =
211 case Keyword.get(options, :moderator) do
212 nil -> user
213 value -> set_moderator(user, value)
214 end
215 else
216 _ ->
217 shell_error("No local user #{nickname}")
218 end
219 end
220
221 def run(["tag", nickname | tags]) do
222 start_pleroma()
223
224 with %User{} = user <- User.get_cached_by_nickname(nickname) do
225 user = user |> User.tag(tags)
226
227 shell_info("Tags of #{user.nickname}: #{inspect(user.tags)}")
228 else
229 _ ->
230 shell_error("Could not change user tags for #{nickname}")
231 end
232 end
233
234 def run(["untag", nickname | tags]) do
235 start_pleroma()
236
237 with %User{} = user <- User.get_cached_by_nickname(nickname) do
238 user = user |> User.untag(tags)
239
240 shell_info("Tags of #{user.nickname}: #{inspect(user.tags)}")
241 else
242 _ ->
243 shell_error("Could not change user tags for #{nickname}")
244 end
245 end
246
247 def run(["invite" | rest]) do
248 {options, [], []} =
249 OptionParser.parse(rest,
250 strict: [
251 expires_at: :string,
252 max_use: :integer
253 ]
254 )
255
256 options =
257 options
258 |> Keyword.update(:expires_at, {:ok, nil}, fn
259 nil -> {:ok, nil}
260 val -> Date.from_iso8601(val)
261 end)
262 |> Enum.into(%{})
263
264 start_pleroma()
265
266 with {:ok, val} <- options[:expires_at],
267 options = Map.put(options, :expires_at, val),
268 {:ok, invite} <- UserInviteToken.create_invite(options) do
269 shell_info("Generated user invite token " <> String.replace(invite.invite_type, "_", " "))
270
271 url =
272 Pleroma.Web.Router.Helpers.redirect_url(
273 Pleroma.Web.Endpoint,
274 :registration_page,
275 invite.token
276 )
277
278 IO.puts(url)
279 else
280 error ->
281 shell_error("Could not create invite token: #{inspect(error)}")
282 end
283 end
284
285 def run(["invites"]) do
286 start_pleroma()
287
288 shell_info("Invites list:")
289
290 UserInviteToken.list_invites()
291 |> Enum.each(fn invite ->
292 expire_info =
293 with expires_at when not is_nil(expires_at) <- invite.expires_at do
294 " | Expires at: #{Date.to_string(expires_at)}"
295 end
296
297 using_info =
298 with max_use when not is_nil(max_use) <- invite.max_use do
299 " | Max use: #{max_use} Left use: #{max_use - invite.uses}"
300 end
301
302 shell_info(
303 "ID: #{invite.id} | Token: #{invite.token} | Token type: #{invite.invite_type} | Used: #{
304 invite.used
305 }#{expire_info}#{using_info}"
306 )
307 end)
308 end
309
310 def run(["revoke_invite", token]) do
311 start_pleroma()
312
313 with {:ok, invite} <- UserInviteToken.find_by_token(token),
314 {:ok, _} <- UserInviteToken.update_invite(invite, %{used: true}) do
315 shell_info("Invite for token #{token} was revoked.")
316 else
317 _ -> shell_error("No invite found with token #{token}")
318 end
319 end
320
321 def run(["delete_activities", nickname]) do
322 start_pleroma()
323
324 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
325 User.delete_user_activities(user)
326 shell_info("User #{nickname} statuses deleted.")
327 else
328 _ ->
329 shell_error("No local user #{nickname}")
330 end
331 end
332
333 def run(["confirm", nickname]) do
334 start_pleroma()
335
336 with %User{} = user <- User.get_cached_by_nickname(nickname) do
337 {:ok, user} = User.confirm(user)
338
339 message = if !user.is_confirmed, do: "needs", else: "doesn't need"
340
341 shell_info("#{nickname} #{message} confirmation.")
342 else
343 _ ->
344 shell_error("No local user #{nickname}")
345 end
346 end
347
348 def run(["confirm_all"]) do
349 start_pleroma()
350
351 Pleroma.User.Query.build(%{
352 local: true,
353 is_active: true,
354 is_moderator: false,
355 is_admin: false,
356 invisible: false
357 })
358 |> Pleroma.Repo.chunk_stream(500, :batches)
359 |> Stream.each(fn users ->
360 users
361 |> Enum.each(fn user -> User.set_confirmation(user, true) end)
362 end)
363 |> Stream.run()
364 end
365
366 def run(["unconfirm_all"]) do
367 start_pleroma()
368
369 Pleroma.User.Query.build(%{
370 local: true,
371 is_active: true,
372 is_moderator: false,
373 is_admin: false,
374 invisible: false
375 })
376 |> Pleroma.Repo.chunk_stream(500, :batches)
377 |> Stream.each(fn users ->
378 users
379 |> Enum.each(fn user -> User.set_confirmation(user, false) end)
380 end)
381 |> Stream.run()
382 end
383
384 def run(["sign_out", nickname]) do
385 start_pleroma()
386
387 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
388 User.global_sign_out(user)
389
390 shell_info("#{nickname} signed out from all apps.")
391 else
392 _ ->
393 shell_error("No local user #{nickname}")
394 end
395 end
396
397 def run(["list"]) do
398 start_pleroma()
399
400 Pleroma.User.Query.build(%{local: true})
401 |> Pleroma.Repo.chunk_stream(500, :batches)
402 |> Stream.each(fn users ->
403 users
404 |> Enum.each(fn user ->
405 shell_info(
406 "#{user.nickname} moderator: #{user.is_moderator}, admin: #{user.is_admin}, locked: #{
407 user.is_locked
408 }, is_active: #{user.is_active}"
409 )
410 end)
411 end)
412 |> Stream.run()
413 end
414
415 defp set_moderator(user, value) do
416 {:ok, user} =
417 user
418 |> Changeset.change(%{is_moderator: value})
419 |> User.update_and_set_cache()
420
421 shell_info("Moderator status of #{user.nickname}: #{user.is_moderator}")
422 user
423 end
424
425 defp set_admin(user, value) do
426 {:ok, user} = User.admin_api_update(user, %{is_admin: value})
427
428 shell_info("Admin status of #{user.nickname}: #{user.is_admin}")
429 user
430 end
431
432 defp set_locked(user, value) do
433 {:ok, user} =
434 user
435 |> Changeset.change(%{is_locked: value})
436 |> User.update_and_set_cache()
437
438 shell_info("Locked status of #{user.nickname}: #{user.is_locked}")
439 user
440 end
441
442 defp set_confirmation(user, value) do
443 {:ok, user} = User.set_confirmation(user, value)
444
445 shell_info("Confirmation status of #{user.nickname}: #{user.is_confirmed}")
446 user
447 end
448 end