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,26 +209,28 @@ defmodule Mix.Tasks.Pleroma.User do
] ]
) )
with %User{local: true} = user <- User.get_by_nickname(nickname) do
case Keyword.get(options, :moderator) do case Keyword.get(options, :moderator) do
nil -> nil nil -> nil
value -> set_moderator(nickname, value) value -> set_moderator(user, value)
end end
case Keyword.get(options, :locked) do case Keyword.get(options, :locked) do
nil -> nil nil -> nil
value -> set_locked(nickname, value) value -> set_locked(user, value)
end end
case Keyword.get(options, :admin) do case Keyword.get(options, :admin) do
nil -> nil nil -> nil
value -> set_admin(nickname, value) value -> set_admin(user, value)
end
else
_ ->
Mix.shell().error("No local user #{nickname}")
end end
end end
defp set_moderator(nickname, value) do defp set_moderator(user, value) do
Application.ensure_all_started(:pleroma)
with %User{local: true} = user <- User.get_by_nickname(nickname) do
info = info =
user.info user.info
|> Map.put("is_moderator", value) |> Map.put("is_moderator", value)
@ -234,17 +238,10 @@ defmodule Mix.Tasks.Pleroma.User do
cng = User.info_changeset(user, %{info: info}) cng = User.info_changeset(user, %{info: info})
{:ok, user} = User.update_and_set_cache(cng) {:ok, user} = User.update_and_set_cache(cng)
Mix.shell().info("Moderator status of #{nickname}: #{user.info["is_moderator"]}") Mix.shell().info("Moderator status of #{user.nickname}: #{user.info["is_moderator"]}")
else
_ ->
Mix.shell().error("No local user #{nickname}")
end
end end
defp set_admin(nickname, value) do defp set_admin(user, value) do
Application.ensure_all_started(:pleroma)
with %User{local: true} = user <- User.get_by_nickname(nickname) do
info = info =
user.info user.info
|> Map.put("is_admin", value) |> Map.put("is_admin", value)
@ -252,11 +249,18 @@ defmodule Mix.Tasks.Pleroma.User do
cng = User.info_changeset(user, %{info: info}) cng = User.info_changeset(user, %{info: info})
{:ok, user} = User.update_and_set_cache(cng) {:ok, user} = User.update_and_set_cache(cng)
Mix.shell().info("Admin status of #{nickname}: #{user.info["is_admin"]}") Mix.shell().info("Admin status of #{user.nickname}: #{user.info["is_admin"]}")
else
_ ->
Mix.shell().error("No local user #{nickname}")
end 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