1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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"
14 @moduledoc File.read!("docs/administration/CLI_tasks/user.md")
16 def run(["new", nickname, email | rest]) do
33 name = Keyword.get(options, :name, nickname)
34 bio = Keyword.get(options, :bio, "")
36 {password, generated_password?} =
37 case Keyword.get(options, :password) do
39 {:crypto.strong_rand_bytes(16) |> Base.encode64(), true}
45 moderator? = Keyword.get(options, :moderator, false)
46 admin? = Keyword.get(options, :admin, false)
47 assume_yes? = Keyword.get(options, :assume_yes, false)
50 A user will be created with the following information:
51 - nickname: #{nickname}
54 if(generated_password?, do: "[generated; a reset link will be created]", else: password)
58 - moderator: #{if(moderator?, do: "true", else: "false")}
59 - admin: #{if(admin?, do: "true", else: "false")}
62 proceed? = assume_yes? or shell_yes?("Continue?")
71 password_confirmation: password,
76 changeset = User.register_changeset(%User{}, params, need_confirmation: false)
77 {:ok, _user} = User.register(changeset)
79 shell_info("User #{nickname} created")
82 run(["set", nickname, "--moderator"])
86 run(["set", nickname, "--admin"])
89 if generated_password? do
90 run(["reset_password", nickname])
93 shell_info("User will not be created.")
97 def run(["rm", nickname]) do
100 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
101 User.perform(:delete, user)
102 shell_info("User #{nickname} deleted.")
105 shell_error("No local user #{nickname}")
109 def run(["toggle_activated", nickname]) do
112 with %User{} = user <- User.get_cached_by_nickname(nickname) do
113 {:ok, user} = User.deactivate(user, !user.deactivated)
116 "Activation status of #{nickname}: #{if(user.deactivated, do: "de", else: "")}activated"
120 shell_error("No user #{nickname}")
124 def run(["reset_password", nickname]) do
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}")
133 Pleroma.Web.Router.Helpers.reset_password_url(
134 Pleroma.Web.Endpoint,
142 shell_error("No local user #{nickname}")
146 def run(["unsubscribe", nickname]) do
149 with %User{} = user <- User.get_cached_by_nickname(nickname) do
150 shell_info("Deactivating #{user.nickname}")
151 User.deactivate(user)
154 |> User.get_friends()
155 |> Enum.each(fn friend ->
156 user = User.get_cached_by_id(user.id)
158 shell_info("Unsubscribing #{friend.nickname} from #{user.nickname}")
159 User.unfollow(user, friend)
164 user = User.get_cached_by_id(user.id)
166 if Enum.empty?(User.get_friends(user)) do
167 shell_info("Successfully unsubscribed all followers from #{user.nickname}")
171 shell_error("No user #{nickname}")
175 def run(["unsubscribe_all_from_instance", instance]) do
178 Pleroma.User.Query.build(%{nickname: "@#{instance}"})
179 |> Pleroma.RepoStreamer.chunk_stream(500)
180 |> Stream.each(fn users ->
182 |> Enum.each(fn user ->
183 run(["unsubscribe", user.nickname])
189 def run(["set", nickname | rest]) do
202 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
204 case Keyword.get(options, :moderator) do
206 value -> set_moderator(user, value)
210 case Keyword.get(options, :locked) do
212 value -> set_locked(user, value)
216 case Keyword.get(options, :admin) do
218 value -> set_admin(user, value)
222 shell_error("No local user #{nickname}")
226 def run(["tag", nickname | tags]) do
229 with %User{} = user <- User.get_cached_by_nickname(nickname) do
230 user = user |> User.tag(tags)
232 shell_info("Tags of #{user.nickname}: #{inspect(tags)}")
235 shell_error("Could not change user tags for #{nickname}")
239 def run(["untag", nickname | tags]) do
242 with %User{} = user <- User.get_cached_by_nickname(nickname) do
243 user = user |> User.untag(tags)
245 shell_info("Tags of #{user.nickname}: #{inspect(tags)}")
248 shell_error("Could not change user tags for #{nickname}")
252 def run(["invite" | rest]) do
254 OptionParser.parse(rest,
263 |> Keyword.update(:expires_at, {:ok, nil}, fn
265 val -> Date.from_iso8601(val)
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, "_", " "))
277 Pleroma.Web.Router.Helpers.redirect_url(
278 Pleroma.Web.Endpoint,
286 shell_error("Could not create invite token: #{inspect(error)}")
290 def run(["invites"]) do
293 shell_info("Invites list:")
295 UserInviteToken.list_invites()
296 |> Enum.each(fn invite ->
298 with expires_at when not is_nil(expires_at) <- invite.expires_at do
299 " | Expires at: #{Date.to_string(expires_at)}"
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}"
308 "ID: #{invite.id} | Token: #{invite.token} | Token type: #{invite.invite_type} | Used: #{
310 }#{expire_info}#{using_info}"
315 def run(["revoke_invite", token]) do
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.")
322 _ -> shell_error("No invite found with token #{token}")
326 def run(["delete_activities", nickname]) do
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.")
334 shell_error("No local user #{nickname}")
338 def run(["toggle_confirmed", nickname]) do
341 with %User{} = user <- User.get_cached_by_nickname(nickname) do
342 {:ok, user} = User.toggle_confirmation(user)
344 message = if user.confirmation_pending, do: "needs", else: "doesn't need"
346 shell_info("#{nickname} #{message} confirmation.")
349 shell_error("No local user #{nickname}")
353 def run(["sign_out", nickname]) do
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)
360 shell_info("#{nickname} signed out from all apps.")
363 shell_error("No local user #{nickname}")
370 Pleroma.User.Query.build(%{local: true})
371 |> Pleroma.RepoStreamer.chunk_stream(500)
372 |> Stream.each(fn users ->
374 |> Enum.each(fn user ->
376 "#{user.nickname} moderator: #{user.info.is_moderator}, admin: #{user.info.is_admin}, locked: #{
378 }, deactivated: #{user.info.deactivated}"
385 defp set_moderator(user, value) do
388 |> Changeset.change(%{is_moderator: value})
389 |> User.update_and_set_cache()
391 shell_info("Moderator status of #{user.nickname}: #{user.is_moderator}")
395 defp set_admin(user, value) do
398 |> Changeset.change(%{is_admin: value})
399 |> User.update_and_set_cache()
401 shell_info("Admin status of #{user.nickname}: #{user.is_admin}")
405 defp set_locked(user, value) do
408 |> Changeset.change(%{locked: value})
409 |> User.update_and_set_cache()
411 shell_info("Locked status of #{user.nickname}: #{user.locked}")