X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fmix%2Ftasks%2Fpleroma%2Fuser.ex;h=4e3b80db3040ace90fd6c5a39a9f011220390ab5;hb=c506cc48ef230da30d5786285806de904b725981;hp=6a83a8c0d9669e1ea45eac1bfc3e33538a37e849;hpb=aacac9da0beddcf797958acca330747b8f2d1f06;p=akkoma
diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex
index 6a83a8c0d..4e3b80db3 100644
--- a/lib/mix/tasks/pleroma/user.ex
+++ b/lib/mix/tasks/pleroma/user.ex
@@ -1,83 +1,18 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2018 Pleroma Authors
+# Copyright © 2017-2019 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Mix.Tasks.Pleroma.User do
use Mix.Task
- import Ecto.Changeset
- alias Mix.Tasks.Pleroma.Common
+ import Mix.Pleroma
+ alias Ecto.Changeset
alias Pleroma.User
alias Pleroma.UserInviteToken
+ alias Pleroma.Web.OAuth
@shortdoc "Manages Pleroma users"
- @moduledoc """
- Manages Pleroma users.
+ @moduledoc File.read!("docs/administration/CLI_tasks/user.md")
- ## Create a new user.
-
- mix pleroma.user new NICKNAME EMAIL [OPTION...]
-
- Options:
- - `--name NAME` - the user's name (i.e., "Lain Iwakura")
- - `--bio BIO` - the user's bio
- - `--password PASSWORD` - the user's password
- - `--moderator`/`--no-moderator` - whether the user is a moderator
- - `--admin`/`--no-admin` - whether the user is an admin
- - `-y`, `--assume-yes`/`--no-assume-yes` - whether to assume yes to all questions
-
- ## Generate an invite link.
-
- mix pleroma.user invite [OPTION...]
-
- Options:
- - `--expires_at DATE` - last day on which token is active (e.g. "2019-04-05")
- - `--max_use NUMBER` - maximum numbers of token uses
-
- ## List generated invites
-
- mix pleroma.user invites
-
- ## Revoke invite
-
- mix pleroma.user revoke_invite TOKEN OR TOKEN_ID
-
- ## Delete the user's account.
-
- mix pleroma.user rm NICKNAME
-
- ## Delete the user's activities.
-
- mix pleroma.user delete_activities NICKNAME
-
- ## Deactivate or activate the user's account.
-
- mix pleroma.user toggle_activated NICKNAME
-
- ## Unsubscribe local users from user's account and deactivate it
-
- mix pleroma.user unsubscribe NICKNAME
-
- ## Create a password reset link.
-
- mix pleroma.user reset_password NICKNAME
-
- ## Set the value of the given user's settings.
-
- mix pleroma.user set NICKNAME [OPTION...]
-
- Options:
- - `--locked`/`--no-locked` - whether the user's account is locked
- - `--moderator`/`--no-moderator` - whether the user is a moderator
- - `--admin`/`--no-admin` - whether the user is an admin
-
- ## Add tags to a user.
-
- mix pleroma.user tag NICKNAME TAGS
-
- ## Delete tags from a user.
-
- mix pleroma.user untag NICKNAME TAGS
- """
def run(["new", nickname, email | rest]) do
{options, [], []} =
OptionParser.parse(
@@ -111,7 +46,7 @@ defmodule Mix.Tasks.Pleroma.User do
admin? = Keyword.get(options, :admin, false)
assume_yes? = Keyword.get(options, :assume_yes, false)
- Mix.shell().info("""
+ shell_info("""
A user will be created with the following information:
- nickname: #{nickname}
- email: #{email}
@@ -124,10 +59,10 @@ defmodule Mix.Tasks.Pleroma.User do
- admin: #{if(admin?, do: "true", else: "false")}
""")
- proceed? = assume_yes? or Mix.shell().yes?("Continue?")
+ proceed? = assume_yes? or shell_yes?("Continue?")
if proceed? do
- Common.start_pleroma()
+ start_pleroma()
params = %{
nickname: nickname,
@@ -138,10 +73,10 @@ defmodule Mix.Tasks.Pleroma.User do
bio: bio
}
- changeset = User.register_changeset(%User{}, params, confirmed: true)
+ changeset = User.register_changeset(%User{}, params, need_confirmation: false)
{:ok, _user} = User.register(changeset)
- Mix.shell().info("User #{nickname} created")
+ shell_info("User #{nickname} created")
if moderator? do
run(["set", nickname, "--moderator"])
@@ -155,72 +90,72 @@ defmodule Mix.Tasks.Pleroma.User do
run(["reset_password", nickname])
end
else
- Mix.shell().info("User will not be created.")
+ shell_info("User will not be created.")
end
end
def run(["rm", nickname]) do
- Common.start_pleroma()
+ start_pleroma()
with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
User.perform(:delete, user)
- Mix.shell().info("User #{nickname} deleted.")
+ shell_info("User #{nickname} deleted.")
else
_ ->
- Mix.shell().error("No local user #{nickname}")
+ shell_error("No local user #{nickname}")
end
end
def run(["toggle_activated", nickname]) do
- Common.start_pleroma()
+ start_pleroma()
with %User{} = user <- User.get_cached_by_nickname(nickname) do
- {:ok, user} = User.deactivate(user, !user.info.deactivated)
+ {:ok, user} = User.deactivate(user, !user.deactivated)
- Mix.shell().info(
- "Activation status of #{nickname}: #{if(user.info.deactivated, do: "de", else: "")}activated"
+ shell_info(
+ "Activation status of #{nickname}: #{if(user.deactivated, do: "de", else: "")}activated"
)
else
_ ->
- Mix.shell().error("No user #{nickname}")
+ shell_error("No user #{nickname}")
end
end
def run(["reset_password", nickname]) do
- Common.start_pleroma()
+ start_pleroma()
with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
{:ok, token} <- Pleroma.PasswordResetToken.create_token(user) do
- Mix.shell().info("Generated password reset token for #{user.nickname}")
+ shell_info("Generated password reset token for #{user.nickname}")
IO.puts(
"URL: #{
- Pleroma.Web.Router.Helpers.util_url(
+ Pleroma.Web.Router.Helpers.reset_password_url(
Pleroma.Web.Endpoint,
- :show_password_reset,
+ :reset,
token.token
)
}"
)
else
_ ->
- Mix.shell().error("No local user #{nickname}")
+ shell_error("No local user #{nickname}")
end
end
def run(["unsubscribe", nickname]) do
- Common.start_pleroma()
+ start_pleroma()
with %User{} = user <- User.get_cached_by_nickname(nickname) do
- Mix.shell().info("Deactivating #{user.nickname}")
+ shell_info("Deactivating #{user.nickname}")
User.deactivate(user)
- {:ok, friends} = User.get_friends(user)
-
- Enum.each(friends, fn friend ->
+ user
+ |> User.get_friends()
+ |> Enum.each(fn friend ->
user = User.get_cached_by_id(user.id)
- Mix.shell().info("Unsubscribing #{friend.nickname} from #{user.nickname}")
+ shell_info("Unsubscribing #{friend.nickname} from #{user.nickname}")
User.unfollow(user, friend)
end)
@@ -228,17 +163,31 @@ defmodule Mix.Tasks.Pleroma.User do
user = User.get_cached_by_id(user.id)
- if Enum.empty?(user.following) do
- Mix.shell().info("Successfully unsubscribed all followers from #{user.nickname}")
+ if Enum.empty?(User.get_friends(user)) do
+ shell_info("Successfully unsubscribed all followers from #{user.nickname}")
end
else
_ ->
- Mix.shell().error("No user #{nickname}")
+ shell_error("No user #{nickname}")
end
end
+ def run(["unsubscribe_all_from_instance", instance]) do
+ start_pleroma()
+
+ Pleroma.User.Query.build(%{nickname: "@#{instance}"})
+ |> Pleroma.RepoStreamer.chunk_stream(500)
+ |> Stream.each(fn users ->
+ users
+ |> Enum.each(fn user ->
+ run(["unsubscribe", user.nickname])
+ end)
+ end)
+ |> Stream.run()
+ end
+
def run(["set", nickname | rest]) do
- Common.start_pleroma()
+ start_pleroma()
{options, [], []} =
OptionParser.parse(
@@ -270,33 +219,33 @@ defmodule Mix.Tasks.Pleroma.User do
end
else
_ ->
- Mix.shell().error("No local user #{nickname}")
+ shell_error("No local user #{nickname}")
end
end
def run(["tag", nickname | tags]) do
- Common.start_pleroma()
+ start_pleroma()
with %User{} = user <- User.get_cached_by_nickname(nickname) do
user = user |> User.tag(tags)
- Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}")
+ shell_info("Tags of #{user.nickname}: #{inspect(tags)}")
else
_ ->
- Mix.shell().error("Could not change user tags for #{nickname}")
+ shell_error("Could not change user tags for #{nickname}")
end
end
def run(["untag", nickname | tags]) do
- Common.start_pleroma()
+ start_pleroma()
with %User{} = user <- User.get_cached_by_nickname(nickname) do
user = user |> User.untag(tags)
- Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}")
+ shell_info("Tags of #{user.nickname}: #{inspect(tags)}")
else
_ ->
- Mix.shell().error("Could not change user tags for #{nickname}")
+ shell_error("Could not change user tags for #{nickname}")
end
end
@@ -317,14 +266,12 @@ defmodule Mix.Tasks.Pleroma.User do
end)
|> Enum.into(%{})
- Common.start_pleroma()
+ start_pleroma()
with {:ok, val} <- options[:expires_at],
options = Map.put(options, :expires_at, val),
{:ok, invite} <- UserInviteToken.create_invite(options) do
- Mix.shell().info(
- "Generated user invite token " <> String.replace(invite.invite_type, "_", " ")
- )
+ shell_info("Generated user invite token " <> String.replace(invite.invite_type, "_", " "))
url =
Pleroma.Web.Router.Helpers.redirect_url(
@@ -336,14 +283,14 @@ defmodule Mix.Tasks.Pleroma.User do
IO.puts(url)
else
error ->
- Mix.shell().error("Could not create invite token: #{inspect(error)}")
+ shell_error("Could not create invite token: #{inspect(error)}")
end
end
def run(["invites"]) do
- Common.start_pleroma()
+ start_pleroma()
- Mix.shell().info("Invites list:")
+ shell_info("Invites list:")
UserInviteToken.list_invites()
|> Enum.each(fn invite ->
@@ -357,7 +304,7 @@ defmodule Mix.Tasks.Pleroma.User do
" | Max use: #{max_use} Left use: #{max_use - invite.uses}"
end
- Mix.shell().info(
+ shell_info(
"ID: #{invite.id} | Token: #{invite.token} | Token type: #{invite.invite_type} | Used: #{
invite.used
}#{expire_info}#{using_info}"
@@ -366,64 +313,84 @@ defmodule Mix.Tasks.Pleroma.User do
end
def run(["revoke_invite", token]) do
- Common.start_pleroma()
+ start_pleroma()
with {:ok, invite} <- UserInviteToken.find_by_token(token),
{:ok, _} <- UserInviteToken.update_invite(invite, %{used: true}) do
- Mix.shell().info("Invite for token #{token} was revoked.")
+ shell_info("Invite for token #{token} was revoked.")
else
- _ -> Mix.shell().error("No invite found with token #{token}")
+ _ -> shell_error("No invite found with token #{token}")
end
end
def run(["delete_activities", nickname]) do
- Common.start_pleroma()
+ start_pleroma()
with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
- {:ok, _} = User.delete_user_activities(user)
- Mix.shell().info("User #{nickname} statuses deleted.")
+ User.delete_user_activities(user)
+ shell_info("User #{nickname} statuses deleted.")
else
_ ->
- Mix.shell().error("No local user #{nickname}")
+ shell_error("No local user #{nickname}")
end
end
- defp set_moderator(user, value) do
- info_cng = User.Info.admin_api_update(user.info, %{is_moderator: value})
+ def run(["toggle_confirmed", nickname]) do
+ start_pleroma()
- user_cng =
- Ecto.Changeset.change(user)
- |> put_embed(:info, info_cng)
+ with %User{} = user <- User.get_cached_by_nickname(nickname) do
+ {:ok, user} = User.toggle_confirmation(user)
- {:ok, user} = User.update_and_set_cache(user_cng)
+ message = if user.confirmation_pending, do: "needs", else: "doesn't need"
- Mix.shell().info("Moderator status of #{user.nickname}: #{user.info.is_moderator}")
- user
+ shell_info("#{nickname} #{message} confirmation.")
+ else
+ _ ->
+ shell_error("No local user #{nickname}")
+ end
end
- defp set_admin(user, value) do
- info_cng = User.Info.admin_api_update(user.info, %{is_admin: value})
+ def run(["sign_out", nickname]) do
+ start_pleroma()
- user_cng =
- Ecto.Changeset.change(user)
- |> put_embed(:info, info_cng)
+ with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
+ OAuth.Token.delete_user_tokens(user)
+ OAuth.Authorization.delete_user_authorizations(user)
- {:ok, user} = User.update_and_set_cache(user_cng)
+ shell_info("#{nickname} signed out from all apps.")
+ else
+ _ ->
+ shell_error("No local user #{nickname}")
+ end
+ end
- Mix.shell().info("Admin status of #{user.nickname}: #{user.info.is_admin}")
+ defp set_moderator(user, value) do
+ {:ok, user} =
+ user
+ |> Changeset.change(%{is_moderator: value})
+ |> User.update_and_set_cache()
+
+ shell_info("Moderator status of #{user.nickname}: #{user.is_moderator}")
user
end
- defp set_locked(user, value) do
- info_cng = User.Info.user_upgrade(user.info, %{locked: value})
+ defp set_admin(user, value) do
+ {:ok, user} =
+ user
+ |> Changeset.change(%{is_admin: value})
+ |> User.update_and_set_cache()
- user_cng =
- Ecto.Changeset.change(user)
- |> put_embed(:info, info_cng)
+ shell_info("Admin status of #{user.nickname}: #{user.is_admin}")
+ user
+ end
- {:ok, user} = User.update_and_set_cache(user_cng)
+ defp set_locked(user, value) do
+ {:ok, user} =
+ user
+ |> Changeset.change(%{locked: value})
+ |> User.update_and_set_cache()
- Mix.shell().info("Locked status of #{user.nickname}: #{user.info.locked}")
+ shell_info("Locked status of #{user.nickname}: #{user.locked}")
user
end
end