20fe6c6e408bc85275a5cb88a73774a13b937554
[akkoma] / lib / mix / tasks / pleroma / user.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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, need_confirmation: false)
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(["toggle_activated", nickname]) do
111 start_pleroma()
112
113 with %User{} = user <- User.get_cached_by_nickname(nickname) do
114 {:ok, user} = User.deactivate(user, !user.deactivated)
115
116 shell_info(
117 "Activation status of #{nickname}: #{if(user.deactivated, do: "de", else: "")}activated"
118 )
119 else
120 _ ->
121 shell_error("No user #{nickname}")
122 end
123 end
124
125 def run(["reset_password", nickname]) do
126 start_pleroma()
127
128 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
129 {:ok, token} <- Pleroma.PasswordResetToken.create_token(user) do
130 shell_info("Generated password reset token for #{user.nickname}")
131
132 IO.puts(
133 "URL: #{
134 Pleroma.Web.Router.Helpers.reset_password_url(
135 Pleroma.Web.Endpoint,
136 :reset,
137 token.token
138 )
139 }"
140 )
141 else
142 _ ->
143 shell_error("No local user #{nickname}")
144 end
145 end
146
147 def run(["reset_mfa", nickname]) do
148 start_pleroma()
149
150 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
151 {:ok, _token} <- Pleroma.MFA.disable(user) do
152 shell_info("Multi-Factor Authentication disabled for #{user.nickname}")
153 else
154 _ ->
155 shell_error("No local user #{nickname}")
156 end
157 end
158
159 def run(["deactivate", nickname]) do
160 start_pleroma()
161
162 with %User{} = user <- User.get_cached_by_nickname(nickname) do
163 shell_info("Deactivating #{user.nickname}")
164 User.deactivate(user)
165 :timer.sleep(500)
166
167 user = User.get_cached_by_id(user.id)
168
169 if Enum.empty?(Enum.filter(User.get_friends(user), & &1.local)) do
170 shell_info("Successfully unsubscribed all local followers from #{user.nickname}")
171 end
172 else
173 _ ->
174 shell_error("No user #{nickname}")
175 end
176 end
177
178 def run(["deactivate_all_from_instance", instance]) do
179 start_pleroma()
180
181 Pleroma.User.Query.build(%{nickname: "@#{instance}"})
182 |> Pleroma.Repo.chunk_stream(500, :batches)
183 |> Stream.each(fn users ->
184 users
185 |> Enum.each(fn user ->
186 run(["deactivate", user.nickname])
187 end)
188 end)
189 |> Stream.run()
190 end
191
192 def run(["set", nickname | rest]) do
193 start_pleroma()
194
195 {options, [], []} =
196 OptionParser.parse(
197 rest,
198 strict: [
199 admin: :boolean,
200 confirmed: :boolean,
201 locked: :boolean,
202 moderator: :boolean
203 ]
204 )
205
206 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
207 user =
208 case Keyword.get(options, :admin) do
209 nil -> user
210 value -> set_admin(user, value)
211 end
212
213 user =
214 case Keyword.get(options, :confirmed) do
215 nil -> user
216 value -> set_confirmed(user, value)
217 end
218
219 user =
220 case Keyword.get(options, :locked) do
221 nil -> user
222 value -> set_locked(user, value)
223 end
224
225 _user =
226 case Keyword.get(options, :moderator) do
227 nil -> user
228 value -> set_moderator(user, value)
229 end
230 else
231 _ ->
232 shell_error("No local user #{nickname}")
233 end
234 end
235
236 def run(["tag", nickname | tags]) do
237 start_pleroma()
238
239 with %User{} = user <- User.get_cached_by_nickname(nickname) do
240 user = user |> User.tag(tags)
241
242 shell_info("Tags of #{user.nickname}: #{inspect(user.tags)}")
243 else
244 _ ->
245 shell_error("Could not change user tags for #{nickname}")
246 end
247 end
248
249 def run(["untag", nickname | tags]) do
250 start_pleroma()
251
252 with %User{} = user <- User.get_cached_by_nickname(nickname) do
253 user = user |> User.untag(tags)
254
255 shell_info("Tags of #{user.nickname}: #{inspect(user.tags)}")
256 else
257 _ ->
258 shell_error("Could not change user tags for #{nickname}")
259 end
260 end
261
262 def run(["invite" | rest]) do
263 {options, [], []} =
264 OptionParser.parse(rest,
265 strict: [
266 expires_at: :string,
267 max_use: :integer
268 ]
269 )
270
271 options =
272 options
273 |> Keyword.update(:expires_at, {:ok, nil}, fn
274 nil -> {:ok, nil}
275 val -> Date.from_iso8601(val)
276 end)
277 |> Enum.into(%{})
278
279 start_pleroma()
280
281 with {:ok, val} <- options[:expires_at],
282 options = Map.put(options, :expires_at, val),
283 {:ok, invite} <- UserInviteToken.create_invite(options) do
284 shell_info("Generated user invite token " <> String.replace(invite.invite_type, "_", " "))
285
286 url =
287 Pleroma.Web.Router.Helpers.redirect_url(
288 Pleroma.Web.Endpoint,
289 :registration_page,
290 invite.token
291 )
292
293 IO.puts(url)
294 else
295 error ->
296 shell_error("Could not create invite token: #{inspect(error)}")
297 end
298 end
299
300 def run(["invites"]) do
301 start_pleroma()
302
303 shell_info("Invites list:")
304
305 UserInviteToken.list_invites()
306 |> Enum.each(fn invite ->
307 expire_info =
308 with expires_at when not is_nil(expires_at) <- invite.expires_at do
309 " | Expires at: #{Date.to_string(expires_at)}"
310 end
311
312 using_info =
313 with max_use when not is_nil(max_use) <- invite.max_use do
314 " | Max use: #{max_use} Left use: #{max_use - invite.uses}"
315 end
316
317 shell_info(
318 "ID: #{invite.id} | Token: #{invite.token} | Token type: #{invite.invite_type} | Used: #{
319 invite.used
320 }#{expire_info}#{using_info}"
321 )
322 end)
323 end
324
325 def run(["revoke_invite", token]) do
326 start_pleroma()
327
328 with {:ok, invite} <- UserInviteToken.find_by_token(token),
329 {:ok, _} <- UserInviteToken.update_invite(invite, %{used: true}) do
330 shell_info("Invite for token #{token} was revoked.")
331 else
332 _ -> shell_error("No invite found with token #{token}")
333 end
334 end
335
336 def run(["delete_activities", nickname]) do
337 start_pleroma()
338
339 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
340 User.delete_user_activities(user)
341 shell_info("User #{nickname} statuses deleted.")
342 else
343 _ ->
344 shell_error("No local user #{nickname}")
345 end
346 end
347
348 def run(["confirm", nickname]) do
349 start_pleroma()
350
351 with %User{} = user <- User.get_cached_by_nickname(nickname) do
352 {:ok, user} = User.confirm(user)
353
354 message = if user.confirmation_pending, do: "needs", else: "doesn't need"
355
356 shell_info("#{nickname} #{message} confirmation.")
357 else
358 _ ->
359 shell_error("No local user #{nickname}")
360 end
361 end
362
363 def run(["confirm_all"]) do
364 start_pleroma()
365
366 Pleroma.User.Query.build(%{
367 local: true,
368 deactivated: false,
369 is_moderator: false,
370 is_admin: false,
371 invisible: false
372 })
373 |> Pleroma.Repo.chunk_stream(500, :batches)
374 |> Stream.each(fn users ->
375 users
376 |> Enum.each(fn user -> User.need_confirmation(user, false) end)
377 end)
378 |> Stream.run()
379 end
380
381 def run(["unconfirm_all"]) do
382 start_pleroma()
383
384 Pleroma.User.Query.build(%{
385 local: true,
386 deactivated: false,
387 is_moderator: false,
388 is_admin: false,
389 invisible: false
390 })
391 |> Pleroma.Repo.chunk_stream(500, :batches)
392 |> Stream.each(fn users ->
393 users
394 |> Enum.each(fn user -> User.need_confirmation(user, true) end)
395 end)
396 |> Stream.run()
397 end
398
399 def run(["sign_out", nickname]) do
400 start_pleroma()
401
402 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
403 User.global_sign_out(user)
404
405 shell_info("#{nickname} signed out from all apps.")
406 else
407 _ ->
408 shell_error("No local user #{nickname}")
409 end
410 end
411
412 def run(["list"]) do
413 start_pleroma()
414
415 Pleroma.User.Query.build(%{local: true})
416 |> Pleroma.Repo.chunk_stream(500, :batches)
417 |> Stream.each(fn users ->
418 users
419 |> Enum.each(fn user ->
420 shell_info(
421 "#{user.nickname} moderator: #{user.is_moderator}, admin: #{user.is_admin}, locked: #{
422 user.is_locked
423 }, deactivated: #{user.deactivated}"
424 )
425 end)
426 end)
427 |> Stream.run()
428 end
429
430 defp set_moderator(user, value) do
431 {:ok, user} =
432 user
433 |> Changeset.change(%{is_moderator: value})
434 |> User.update_and_set_cache()
435
436 shell_info("Moderator status of #{user.nickname}: #{user.is_moderator}")
437 user
438 end
439
440 defp set_admin(user, value) do
441 {:ok, user} = User.admin_api_update(user, %{is_admin: value})
442
443 shell_info("Admin status of #{user.nickname}: #{user.is_admin}")
444 user
445 end
446
447 defp set_locked(user, value) do
448 {:ok, user} =
449 user
450 |> Changeset.change(%{is_locked: value})
451 |> User.update_and_set_cache()
452
453 shell_info("Locked status of #{user.nickname}: #{user.is_locked}")
454 user
455 end
456
457 defp set_confirmed(user, value) do
458 {:ok, user} = User.need_confirmation(user, !value)
459
460 shell_info("Confirmation pending status of #{user.nickname}: #{user.confirmation_pending}")
461 user
462 end
463 end