1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.User do
10 alias Pleroma.UserInviteToken
11 alias Pleroma.Web.OAuth
13 @shortdoc "Manages Pleroma users"
15 Manages Pleroma users.
19 mix pleroma.user new NICKNAME EMAIL [OPTION...]
22 - `--name NAME` - the user's name (i.e., "Lain Iwakura")
23 - `--bio BIO` - the user's bio
24 - `--password PASSWORD` - the user's password
25 - `--moderator`/`--no-moderator` - whether the user is a moderator
26 - `--admin`/`--no-admin` - whether the user is an admin
27 - `-y`, `--assume-yes`/`--no-assume-yes` - whether to assume yes to all questions
29 ## Generate an invite link.
31 mix pleroma.user invite [OPTION...]
34 - `--expires-at DATE` - last day on which token is active (e.g. "2019-04-05")
35 - `--max-use NUMBER` - maximum numbers of token uses
37 ## List generated invites
39 mix pleroma.user invites
43 mix pleroma.user revoke_invite TOKEN OR TOKEN_ID
45 ## Delete the user's account.
47 mix pleroma.user rm NICKNAME
49 ## Delete the user's activities.
51 mix pleroma.user delete_activities NICKNAME
53 ## Sign user out from all applications (delete user's OAuth tokens and authorizations).
55 mix pleroma.user sign_out NICKNAME
57 ## Deactivate or activate the user's account.
59 mix pleroma.user toggle_activated NICKNAME
61 ## Unsubscribe local users from user's account and deactivate it
63 mix pleroma.user unsubscribe NICKNAME
65 ## Unsubscribe local users from an entire instance and deactivate all accounts
67 mix pleroma.user unsubscribe_all_from_instance INSTANCE
69 ## Create a password reset link.
71 mix pleroma.user reset_password NICKNAME
73 ## Set the value of the given user's settings.
75 mix pleroma.user set NICKNAME [OPTION...]
78 - `--locked`/`--no-locked` - whether the user's account is locked
79 - `--moderator`/`--no-moderator` - whether the user is a moderator
80 - `--admin`/`--no-admin` - whether the user is an admin
82 ## Add tags to a user.
84 mix pleroma.user tag NICKNAME TAGS
86 ## Delete tags from a user.
88 mix pleroma.user untag NICKNAME TAGS
90 ## Toggle confirmation of the user's account.
92 mix pleroma.user toggle_confirmed NICKNAME
94 def run(["new", nickname, email | rest]) do
111 name = Keyword.get(options, :name, nickname)
112 bio = Keyword.get(options, :bio, "")
114 {password, generated_password?} =
115 case Keyword.get(options, :password) do
117 {:crypto.strong_rand_bytes(16) |> Base.encode64(), true}
123 moderator? = Keyword.get(options, :moderator, false)
124 admin? = Keyword.get(options, :admin, false)
125 assume_yes? = Keyword.get(options, :assume_yes, false)
128 A user will be created with the following information:
129 - nickname: #{nickname}
132 if(generated_password?, do: "[generated; a reset link will be created]", else: password)
136 - moderator: #{if(moderator?, do: "true", else: "false")}
137 - admin: #{if(admin?, do: "true", else: "false")}
140 proceed? = assume_yes? or shell_yes?("Continue?")
149 password_confirmation: password,
154 changeset = User.register_changeset(%User{}, params, need_confirmation: false)
155 {:ok, _user} = User.register(changeset)
157 shell_info("User #{nickname} created")
160 run(["set", nickname, "--moderator"])
164 run(["set", nickname, "--admin"])
167 if generated_password? do
168 run(["reset_password", nickname])
171 shell_info("User will not be created.")
175 def run(["rm", nickname]) do
178 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
179 User.perform(:delete, user)
180 shell_info("User #{nickname} deleted.")
183 shell_error("No local user #{nickname}")
187 def run(["toggle_activated", nickname]) do
190 with %User{} = user <- User.get_cached_by_nickname(nickname) do
191 {:ok, user} = User.deactivate(user, !user.info.deactivated)
194 "Activation status of #{nickname}: #{if(user.info.deactivated, do: "de", else: "")}activated"
198 shell_error("No user #{nickname}")
202 def run(["reset_password", nickname]) do
205 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
206 {:ok, token} <- Pleroma.PasswordResetToken.create_token(user) do
207 shell_info("Generated password reset token for #{user.nickname}")
211 Pleroma.Web.Router.Helpers.reset_password_url(
212 Pleroma.Web.Endpoint,
220 shell_error("No local user #{nickname}")
224 def run(["unsubscribe", nickname]) do
227 with %User{} = user <- User.get_cached_by_nickname(nickname) do
228 shell_info("Deactivating #{user.nickname}")
229 User.deactivate(user)
231 {:ok, friends} = User.get_friends(user)
233 Enum.each(friends, fn friend ->
234 user = User.get_cached_by_id(user.id)
236 shell_info("Unsubscribing #{friend.nickname} from #{user.nickname}")
237 User.unfollow(user, friend)
242 user = User.get_cached_by_id(user.id)
244 if Enum.empty?(user.following) do
245 shell_info("Successfully unsubscribed all followers from #{user.nickname}")
249 shell_error("No user #{nickname}")
253 def run(["unsubscribe_all_from_instance", instance]) do
256 Pleroma.User.Query.build(%{nickname: "@#{instance}"})
257 |> Pleroma.RepoStreamer.chunk_stream(500)
258 |> Stream.each(fn users ->
260 |> Enum.each(fn user ->
261 run(["unsubscribe", user.nickname])
267 def run(["set", nickname | rest]) do
280 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
282 case Keyword.get(options, :moderator) do
284 value -> set_moderator(user, value)
288 case Keyword.get(options, :locked) do
290 value -> set_locked(user, value)
294 case Keyword.get(options, :admin) do
296 value -> set_admin(user, value)
300 shell_error("No local user #{nickname}")
304 def run(["tag", nickname | tags]) do
307 with %User{} = user <- User.get_cached_by_nickname(nickname) do
308 user = user |> User.tag(tags)
310 shell_info("Tags of #{user.nickname}: #{inspect(tags)}")
313 shell_error("Could not change user tags for #{nickname}")
317 def run(["untag", nickname | tags]) do
320 with %User{} = user <- User.get_cached_by_nickname(nickname) do
321 user = user |> User.untag(tags)
323 shell_info("Tags of #{user.nickname}: #{inspect(tags)}")
326 shell_error("Could not change user tags for #{nickname}")
330 def run(["invite" | rest]) do
332 OptionParser.parse(rest,
341 |> Keyword.update(:expires_at, {:ok, nil}, fn
343 val -> Date.from_iso8601(val)
349 with {:ok, val} <- options[:expires_at],
350 options = Map.put(options, :expires_at, val),
351 {:ok, invite} <- UserInviteToken.create_invite(options) do
352 shell_info("Generated user invite token " <> String.replace(invite.invite_type, "_", " "))
355 Pleroma.Web.Router.Helpers.redirect_url(
356 Pleroma.Web.Endpoint,
364 shell_error("Could not create invite token: #{inspect(error)}")
368 def run(["invites"]) do
371 shell_info("Invites list:")
373 UserInviteToken.list_invites()
374 |> Enum.each(fn invite ->
376 with expires_at when not is_nil(expires_at) <- invite.expires_at do
377 " | Expires at: #{Date.to_string(expires_at)}"
381 with max_use when not is_nil(max_use) <- invite.max_use do
382 " | Max use: #{max_use} Left use: #{max_use - invite.uses}"
386 "ID: #{invite.id} | Token: #{invite.token} | Token type: #{invite.invite_type} | Used: #{
388 }#{expire_info}#{using_info}"
393 def run(["revoke_invite", token]) do
396 with {:ok, invite} <- UserInviteToken.find_by_token(token),
397 {:ok, _} <- UserInviteToken.update_invite(invite, %{used: true}) do
398 shell_info("Invite for token #{token} was revoked.")
400 _ -> shell_error("No invite found with token #{token}")
404 def run(["delete_activities", nickname]) do
407 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
408 {:ok, _} = User.delete_user_activities(user)
409 shell_info("User #{nickname} statuses deleted.")
412 shell_error("No local user #{nickname}")
416 def run(["toggle_confirmed", nickname]) do
419 with %User{} = user <- User.get_cached_by_nickname(nickname) do
420 {:ok, user} = User.toggle_confirmation(user)
422 message = if user.info.confirmation_pending, do: "needs", else: "doesn't need"
424 shell_info("#{nickname} #{message} confirmation.")
427 shell_error("No local user #{nickname}")
431 def run(["sign_out", nickname]) do
434 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
435 OAuth.Token.delete_user_tokens(user)
436 OAuth.Authorization.delete_user_authorizations(user)
438 shell_info("#{nickname} signed out from all apps.")
441 shell_error("No local user #{nickname}")
445 defp set_moderator(user, value) do
446 info_cng = User.Info.admin_api_update(user.info, %{is_moderator: value})
449 Ecto.Changeset.change(user)
450 |> put_embed(:info, info_cng)
452 {:ok, user} = User.update_and_set_cache(user_cng)
454 shell_info("Moderator status of #{user.nickname}: #{user.info.is_moderator}")
458 defp set_admin(user, value) do
459 info_cng = User.Info.admin_api_update(user.info, %{is_admin: value})
462 Ecto.Changeset.change(user)
463 |> put_embed(:info, info_cng)
465 {:ok, user} = User.update_and_set_cache(user_cng)
467 shell_info("Admin status of #{user.nickname}: #{user.info.is_admin}")
471 defp set_locked(user, value) do
472 info_cng = User.Info.user_upgrade(user.info, %{locked: value})
475 Ecto.Changeset.change(user)
476 |> put_embed(:info, info_cng)
478 {:ok, user} = User.update_and_set_cache(user_cng)
480 shell_info("Locked status of #{user.nickname}: #{user.info.locked}")