change new to gen in instance.ex . Refactor user.ex

This commit is contained in:
Rin Toshaka 2018-12-04 19:00:45 +01:00
parent 03b2d1016d
commit 6396f1b58f
2 changed files with 44 additions and 58 deletions

View file

@ -8,7 +8,7 @@ defmodule Mix.Tasks.Pleroma.Instance do
## Generate a new instance config. ## Generate a new instance config.
mix pleroma.instance new [OPTION...] mix pleroma.instance gen [OPTION...]
If any options are left unspecified, you will be prompted interactively If any options are left unspecified, you will be prompted interactively
@ -26,7 +26,7 @@ defmodule Mix.Tasks.Pleroma.Instance do
- `--dbpass DBPASS` - the password to use for the database connection - `--dbpass DBPASS` - the password to use for the database connection
""" """
def run(["new" | rest]) do def run(["gen" | rest]) do
{options, [], []} = {options, [], []} =
OptionParser.parse( OptionParser.parse(
rest, rest,

View file

@ -197,6 +197,8 @@ defmodule Mix.Tasks.Pleroma.User do
end end
def run(["set", nickname | rest]) do def run(["set", nickname | rest]) do
Application.ensure_all_started(:pleroma)
{options, [], []} = {options, [], []} =
OptionParser.parse( OptionParser.parse(
rest, rest,
@ -207,56 +209,58 @@ defmodule Mix.Tasks.Pleroma.User do
] ]
) )
case Keyword.get(options, :moderator) do
nil -> nil
value -> set_moderator(nickname, value)
end
case Keyword.get(options, :locked) do
nil -> nil
value -> set_locked(nickname, value)
end
case Keyword.get(options, :admin) do
nil -> nil
value -> set_admin(nickname, value)
end
end
defp set_moderator(nickname, value) do
Application.ensure_all_started(:pleroma)
with %User{local: true} = user <- User.get_by_nickname(nickname) do with %User{local: true} = user <- User.get_by_nickname(nickname) do
info = case Keyword.get(options, :moderator) do
user.info nil -> nil
|> Map.put("is_moderator", value) value -> set_moderator(user, value)
end
cng = User.info_changeset(user, %{info: info}) case Keyword.get(options, :locked) do
{:ok, user} = User.update_and_set_cache(cng) nil -> nil
value -> set_locked(user, value)
end
Mix.shell().info("Moderator status of #{nickname}: #{user.info["is_moderator"]}") case Keyword.get(options, :admin) do
nil -> nil
value -> set_admin(user, value)
end
else else
_ -> _ ->
Mix.shell().error("No local user #{nickname}") Mix.shell().error("No local user #{nickname}")
end end
end end
defp set_admin(nickname, value) do defp set_moderator(user, value) do
Application.ensure_all_started(:pleroma) info =
user.info
|> Map.put("is_moderator", value)
with %User{local: true} = user <- User.get_by_nickname(nickname) do cng = User.info_changeset(user, %{info: info})
info = {:ok, user} = User.update_and_set_cache(cng)
user.info
|> Map.put("is_admin", value)
cng = User.info_changeset(user, %{info: info}) Mix.shell().info("Moderator status of #{user.nickname}: #{user.info["is_moderator"]}")
{:ok, user} = User.update_and_set_cache(cng) end
Mix.shell().info("Admin status of #{nickname}: #{user.info["is_admin"]}") defp set_admin(user, value) do
else info =
_ -> user.info
Mix.shell().error("No local user #{nickname}") |> Map.put("is_admin", value)
end
cng = User.info_changeset(user, %{info: info})
{:ok, user} = User.update_and_set_cache(cng)
Mix.shell().info("Admin status of #{user.nickname}: #{user.info["is_admin"]}")
end
defp set_locked(user, value) do
info =
user.info
|> Map.put("locked", value)
cng = User.info_changeset(user, %{info: info})
user = Repo.update!(cng)
IO.puts("Locked status of #{user.nickname}: #{user.info["locked"]}")
end end
def run(["invite"]) do def run(["invite"]) do
@ -278,22 +282,4 @@ defmodule Mix.Tasks.Pleroma.User do
Mix.shell().error("Could not create invite token.") Mix.shell().error("Could not create invite token.")
end end
end end
defp set_locked(nickname, value) do
Mix.Ecto.ensure_started(Repo, [])
with %User{local: true} = user <- User.get_by_nickname(nickname) do
info =
user.info
|> Map.put("locked", value)
cng = User.info_changeset(user, %{info: info})
user = Repo.update!(cng)
IO.puts("Locked status of #{nickname}: #{user.info["locked"]}")
else
_ ->
IO.puts("No local user #{nickname}")
end
end
end end