Add the possibility to create profiles and groups from CLI
- Create an actor at the same time when creating an user - or create either a profile and attach it to an existing user - or create a group and set the admin to an existing profile Closes #785 Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
102
lib/mix/tasks/mobilizon/actors/new.ex
Normal file
102
lib/mix/tasks/mobilizon/actors/new.ex
Normal file
@@ -0,0 +1,102 @@
|
||||
defmodule Mix.Tasks.Mobilizon.Actors.New do
|
||||
@moduledoc """
|
||||
Task to create a new user
|
||||
"""
|
||||
use Mix.Task
|
||||
import Mix.Tasks.Mobilizon.Actors.Utils
|
||||
import Mix.Tasks.Mobilizon.Common
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.{Actors, Users}
|
||||
alias Mobilizon.Users.User
|
||||
|
||||
@shortdoc "Manages Mobilizon users"
|
||||
|
||||
@impl Mix.Task
|
||||
def run(rest) do
|
||||
{options, [], []} =
|
||||
OptionParser.parse(
|
||||
rest,
|
||||
strict: [
|
||||
email: :string,
|
||||
username: :string,
|
||||
display_name: :string,
|
||||
group_admin: :string,
|
||||
type: :string
|
||||
],
|
||||
aliases: [
|
||||
e: :email,
|
||||
u: :username,
|
||||
d: :display_name,
|
||||
t: :type,
|
||||
a: :group_admin
|
||||
]
|
||||
)
|
||||
|
||||
start_mobilizon()
|
||||
|
||||
profile_username = Keyword.get(options, :username)
|
||||
profile_name = Keyword.get(options, :display_name)
|
||||
|
||||
if profile_name != nil || profile_username != nil do
|
||||
else
|
||||
shell_error("You need to provide at least --username or --display-name.")
|
||||
end
|
||||
|
||||
case Keyword.get(options, :type, "profile") do
|
||||
"profile" ->
|
||||
do_create_profile(options, profile_username, profile_name)
|
||||
|
||||
"group" ->
|
||||
do_create_group(options, profile_username, profile_name)
|
||||
end
|
||||
end
|
||||
|
||||
@spec do_create_profile(Keyword.t(), String.t(), String.t()) :: Actor.t() | nil
|
||||
defp do_create_profile(options, profile_username, profile_name) do
|
||||
with {:email, email} when is_binary(email) <- {:email, Keyword.get(options, :email)},
|
||||
{:ok, %User{} = user} <- Users.get_user_by_email(email),
|
||||
%Actor{preferred_username: preferred_username, name: name} <-
|
||||
create_profile(user, profile_username, profile_name, default: false) do
|
||||
shell_info("""
|
||||
A profile was created for user #{email} with the following information:
|
||||
- username: #{preferred_username}
|
||||
- display name: #{name}
|
||||
""")
|
||||
else
|
||||
{:email, nil} ->
|
||||
shell_error("You need to provide an email for creating a new profile.")
|
||||
|
||||
{:error, :user_not_found} ->
|
||||
shell_error("No user with this email was found.")
|
||||
|
||||
nil ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defp do_create_group(options, profile_username, profile_name) do
|
||||
with {:option, admin_name} when is_binary(admin_name) <-
|
||||
{:option, Keyword.get(options, :group_admin)},
|
||||
{:admin, %Actor{} = admin} <- {:admin, Actors.get_local_actor_by_name(admin_name)},
|
||||
{:ok, %Actor{preferred_username: preferred_username, name: name}} <-
|
||||
create_group(admin, profile_username, profile_name) do
|
||||
shell_info("""
|
||||
A group was created with profile #{admin_name} as the admin and with the following information:
|
||||
- username: #{preferred_username}
|
||||
- display name: #{name}
|
||||
""")
|
||||
else
|
||||
{:option, nil} ->
|
||||
shell_error(
|
||||
"You need to provide --group-admin with the username of the admin to create a group."
|
||||
)
|
||||
|
||||
{:admin, nil} ->
|
||||
shell_error("Profile with username #{Keyword.get(options, :group_admin)} wasn't found")
|
||||
|
||||
{:error, :insert_group, %Ecto.Changeset{errors: errors}, _} ->
|
||||
shell_error(inspect(errors))
|
||||
shell_error("Error while creating group because of the above reason")
|
||||
end
|
||||
end
|
||||
end
|
||||
58
lib/mix/tasks/mobilizon/actors/utils.ex
Normal file
58
lib/mix/tasks/mobilizon/actors/utils.ex
Normal file
@@ -0,0 +1,58 @@
|
||||
defmodule Mix.Tasks.Mobilizon.Actors.Utils do
|
||||
@moduledoc """
|
||||
Tools for generating usernames from display names
|
||||
"""
|
||||
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Users.User
|
||||
|
||||
@doc """
|
||||
Removes all spaces, accents, special characters and diacritics from a string to create a plain ascii username (a-z0-9_)
|
||||
|
||||
See https://stackoverflow.com/a/37511463
|
||||
"""
|
||||
@spec generate_username(String.t()) :: String.t()
|
||||
def generate_username(""), do: ""
|
||||
|
||||
def generate_username(name) do
|
||||
name
|
||||
|> String.downcase()
|
||||
|> String.normalize(:nfd)
|
||||
|> String.replace(~r/[\x{0300}-\x{036f}]/u, "")
|
||||
|> String.replace(~r/ /, "_")
|
||||
|> String.replace(~r/[^a-z0-9_]/, "")
|
||||
end
|
||||
|
||||
# Profile from name
|
||||
@spec username_and_name(String.t() | nil, String.t() | nil) :: String.t()
|
||||
def username_and_name(nil, profile_name) do
|
||||
{generate_username(profile_name), profile_name}
|
||||
end
|
||||
|
||||
def username_and_name(profile_username, nil) do
|
||||
{profile_username, profile_username}
|
||||
end
|
||||
|
||||
def username_and_name(profile_username, profile_name) do
|
||||
{profile_username, profile_name}
|
||||
end
|
||||
|
||||
def create_profile(%User{id: user_id}, username, name, options \\ []) do
|
||||
{username, name} = username_and_name(username, name)
|
||||
|
||||
{:ok, %Actor{} = new_person} =
|
||||
Actors.new_person(
|
||||
%{preferred_username: username, user_id: user_id, name: name},
|
||||
Keyword.get(options, :default, true)
|
||||
)
|
||||
|
||||
new_person
|
||||
end
|
||||
|
||||
def create_group(%Actor{id: admin_id}, username, name, _options \\ []) do
|
||||
{username, name} = username_and_name(username, name)
|
||||
|
||||
Actors.create_group(%{creator_actor_id: admin_id, preferred_username: username, name: name})
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user