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