From 6396f1b58f69b727e801df4b52a32c47da08e517 Mon Sep 17 00:00:00 2001 From: Rin Toshaka Date: Tue, 4 Dec 2018 19:00:45 +0100 Subject: [PATCH] change new to gen in instance.ex . Refactor user.ex --- lib/mix/tasks/pleroma/instance.ex | 4 +- lib/mix/tasks/pleroma/user.ex | 98 +++++++++++++------------------ 2 files changed, 44 insertions(+), 58 deletions(-) diff --git a/lib/mix/tasks/pleroma/instance.ex b/lib/mix/tasks/pleroma/instance.ex index 6a85880bf..eb578644d 100644 --- a/lib/mix/tasks/pleroma/instance.ex +++ b/lib/mix/tasks/pleroma/instance.ex @@ -8,7 +8,7 @@ defmodule Mix.Tasks.Pleroma.Instance do ## 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 @@ -26,7 +26,7 @@ defmodule Mix.Tasks.Pleroma.Instance do - `--dbpass DBPASS` - the password to use for the database connection """ - def run(["new" | rest]) do + def run(["gen" | rest]) do {options, [], []} = OptionParser.parse( rest, diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index 39825762d..68c6bd2b6 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -197,6 +197,8 @@ def run(["unsubscribe", nickname]) do end def run(["set", nickname | rest]) do + Application.ensure_all_started(:pleroma) + {options, [], []} = OptionParser.parse( rest, @@ -207,56 +209,58 @@ def run(["set", nickname | rest]) 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 - info = - user.info - |> Map.put("is_moderator", value) + case Keyword.get(options, :moderator) do + nil -> nil + value -> set_moderator(user, value) + end - cng = User.info_changeset(user, %{info: info}) - {:ok, user} = User.update_and_set_cache(cng) + case Keyword.get(options, :locked) do + 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 _ -> Mix.shell().error("No local user #{nickname}") end end - defp set_admin(nickname, value) do - Application.ensure_all_started(:pleroma) + defp set_moderator(user, value) do + info = + user.info + |> Map.put("is_moderator", value) - with %User{local: true} = user <- User.get_by_nickname(nickname) do - info = - user.info - |> Map.put("is_admin", value) + cng = User.info_changeset(user, %{info: info}) + {:ok, user} = User.update_and_set_cache(cng) - cng = User.info_changeset(user, %{info: info}) - {:ok, user} = User.update_and_set_cache(cng) + Mix.shell().info("Moderator status of #{user.nickname}: #{user.info["is_moderator"]}") + end - Mix.shell().info("Admin status of #{nickname}: #{user.info["is_admin"]}") - else - _ -> - Mix.shell().error("No local user #{nickname}") - end + defp set_admin(user, value) do + info = + user.info + |> Map.put("is_admin", value) + + 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 def run(["invite"]) do @@ -278,22 +282,4 @@ def run(["invite"]) do Mix.shell().error("Could not create invite token.") 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