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
8 alias Mix.Tasks.Pleroma.Common
12 @shortdoc "Manages Pleroma users"
14 Manages Pleroma users.
18 mix pleroma.user new NICKNAME EMAIL [OPTION...]
21 - `--name NAME` - the user's name (i.e., "Lain Iwakura")
22 - `--bio BIO` - the user's bio
23 - `--password PASSWORD` - the user's password
24 - `--moderator`/`--no-moderator` - whether the user is a moderator
25 - `--admin`/`--no-admin` - whether the user is an admin
26 - `-y`, `--assume-yes`/`--no-assume-yes` - whether to assume yes to all questions
28 ## Generate an invite link.
30 mix pleroma.user invite
32 ## Delete the user's account.
34 mix pleroma.user rm NICKNAME
36 ## Delete the user's activities.
38 mix pleroma.user delete_activities NICKNAME
40 ## Deactivate or activate the user's account.
42 mix pleroma.user toggle_activated NICKNAME
44 ## Unsubscribe local users from user's account and deactivate it
46 mix pleroma.user unsubscribe NICKNAME
48 ## Create a password reset link.
50 mix pleroma.user reset_password NICKNAME
52 ## Set the value of the given user's settings.
54 mix pleroma.user set NICKNAME [OPTION...]
57 - `--locked`/`--no-locked` - whether the user's account is locked
58 - `--moderator`/`--no-moderator` - whether the user is a moderator
59 - `--admin`/`--no-admin` - whether the user is an admin
61 ## Add tags to a user.
63 mix pleroma.user tag NICKNAME TAGS
65 ## Delete tags from a user.
67 mix pleroma.user untag NICKNAME TAGS
69 def run(["new", nickname, email | rest]) do
86 name = Keyword.get(options, :name, nickname)
87 bio = Keyword.get(options, :bio, "")
89 {password, generated_password?} =
90 case Keyword.get(options, :password) do
92 {:crypto.strong_rand_bytes(16) |> Base.encode64(), true}
98 moderator? = Keyword.get(options, :moderator, false)
99 admin? = Keyword.get(options, :admin, false)
100 assume_yes? = Keyword.get(options, :assume_yes, false)
103 A user will be created with the following information:
104 - nickname: #{nickname}
107 if(generated_password?, do: "[generated; a reset link will be created]", else: password)
111 - moderator: #{if(moderator?, do: "true", else: "false")}
112 - admin: #{if(admin?, do: "true", else: "false")}
115 proceed? = assume_yes? or Mix.shell().yes?("Continue?")
117 unless not proceed? do
118 Common.start_pleroma()
124 password_confirmation: password,
129 changeset = User.register_changeset(%User{}, params, confirmed: true)
130 {:ok, _user} = User.register(changeset)
132 Mix.shell().info("User #{nickname} created")
135 run(["set", nickname, "--moderator"])
139 run(["set", nickname, "--admin"])
142 if generated_password? do
143 run(["reset_password", nickname])
146 Mix.shell().info("User will not be created.")
150 def run(["rm", nickname]) do
151 Common.start_pleroma()
153 with %User{local: true} = user <- User.get_by_nickname(nickname) do
155 Mix.shell().info("User #{nickname} deleted.")
158 Mix.shell().error("No local user #{nickname}")
162 def run(["toggle_activated", nickname]) do
163 Common.start_pleroma()
165 with %User{} = user <- User.get_by_nickname(nickname) do
166 {:ok, user} = User.deactivate(user, !user.info.deactivated)
169 "Activation status of #{nickname}: #{if(user.info.deactivated, do: "de", else: "")}activated"
173 Mix.shell().error("No user #{nickname}")
177 def run(["reset_password", nickname]) do
178 Common.start_pleroma()
180 with %User{local: true} = user <- User.get_by_nickname(nickname),
181 {:ok, token} <- Pleroma.PasswordResetToken.create_token(user) do
182 Mix.shell().info("Generated password reset token for #{user.nickname}")
186 Pleroma.Web.Router.Helpers.util_url(
187 Pleroma.Web.Endpoint,
188 :show_password_reset,
195 Mix.shell().error("No local user #{nickname}")
199 def run(["unsubscribe", nickname]) do
200 Common.start_pleroma()
202 with %User{} = user <- User.get_by_nickname(nickname) do
203 Mix.shell().info("Deactivating #{user.nickname}")
204 User.deactivate(user)
206 {:ok, friends} = User.get_friends(user)
208 Enum.each(friends, fn friend ->
209 user = Repo.get(User, user.id)
211 Mix.shell().info("Unsubscribing #{friend.nickname} from #{user.nickname}")
212 User.unfollow(user, friend)
217 user = Repo.get(User, user.id)
219 if Enum.empty?(user.following) do
220 Mix.shell().info("Successfully unsubscribed all followers from #{user.nickname}")
224 Mix.shell().error("No user #{nickname}")
228 def run(["set", nickname | rest]) do
229 Common.start_pleroma()
241 with %User{local: true} = user <- User.get_by_nickname(nickname) do
243 case Keyword.get(options, :moderator) do
245 value -> set_moderator(user, value)
249 case Keyword.get(options, :locked) do
251 value -> set_locked(user, value)
255 case Keyword.get(options, :admin) do
257 value -> set_admin(user, value)
261 Mix.shell().error("No local user #{nickname}")
265 def run(["tag", nickname | tags]) do
266 Common.start_pleroma()
268 with %User{} = user <- User.get_by_nickname(nickname) do
269 user = user |> User.tag(tags)
271 Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}")
274 Mix.shell().error("Could not change user tags for #{nickname}")
278 def run(["untag", nickname | tags]) do
279 Common.start_pleroma()
281 with %User{} = user <- User.get_by_nickname(nickname) do
282 user = user |> User.untag(tags)
284 Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}")
287 Mix.shell().error("Could not change user tags for #{nickname}")
291 def run(["invite"]) do
292 Common.start_pleroma()
294 with {:ok, token} <- Pleroma.UserInviteToken.create_token() do
295 Mix.shell().info("Generated user invite token")
298 Pleroma.Web.Router.Helpers.redirect_url(
299 Pleroma.Web.Endpoint,
307 Mix.shell().error("Could not create invite token.")
311 def run(["delete_activities", nickname]) do
312 Common.start_pleroma()
314 with %User{local: true} = user <- User.get_by_nickname(nickname) do
315 User.delete_user_activities(user)
316 Mix.shell().info("User #{nickname} statuses deleted.")
319 Mix.shell().error("No local user #{nickname}")
323 defp set_moderator(user, value) do
324 info_cng = User.Info.admin_api_update(user.info, %{is_moderator: value})
327 Ecto.Changeset.change(user)
328 |> put_embed(:info, info_cng)
330 {:ok, user} = User.update_and_set_cache(user_cng)
332 Mix.shell().info("Moderator status of #{user.nickname}: #{user.info.is_moderator}")
336 defp set_admin(user, value) do
337 info_cng = User.Info.admin_api_update(user.info, %{is_admin: value})
340 Ecto.Changeset.change(user)
341 |> put_embed(:info, info_cng)
343 {:ok, user} = User.update_and_set_cache(user_cng)
345 Mix.shell().info("Admin status of #{user.nickname}: #{user.info.is_admin}")
349 defp set_locked(user, value) do
350 info_cng = User.Info.user_upgrade(user.info, %{locked: value})
353 Ecto.Changeset.change(user)
354 |> put_embed(:info, info_cng)
356 {:ok, user} = User.update_and_set_cache(user_cng)
358 Mix.shell().info("Locked status of #{user.nickname}: #{user.info.locked}")