-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Merge branch 'peck/user-command'"
- Loading branch information
Matthew Peck
committed
Dec 13, 2016
1 parent
a10be9f
commit c0041e4
Showing
8 changed files
with
213 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
defmodule Cog.Commands.User do | ||
use Cog.Command.GenCommand.Base, | ||
bundle: Cog.Util.Misc.embedded_bundle | ||
|
||
alias Cog.Commands.User.{AttachHandle, DetachHandle, Info, List, ListHandles} | ||
require Cog.Commands.Helpers, as: Helpers | ||
|
||
Helpers.usage(:root) | ||
|
||
@description "Manage Cog users and chat handles" | ||
|
||
@note """ | ||
For creation and deletion of users, please continue to use `cogctl`. | ||
""" | ||
|
||
@arguments "<subcommand>" | ||
|
||
@subcommands %{ | ||
"list" => "List all users (default)", | ||
"info <username>" => "Show detailed information on a given user", | ||
"list-handles" => "List all chat handles associated with users", | ||
"attach-handle <username> <handle>" => "Associate a chat handle with a user", | ||
"detach-handle <username>" => "Sever association between a chat handle and a user" | ||
} | ||
|
||
permission "manage_users" | ||
|
||
rule "when command is #{Cog.Util.Misc.embedded_bundle}:user must have #{Cog.Util.Misc.embedded_bundle}:manage_users" | ||
|
||
def handle_message(req, state) do | ||
{subcommand, args} = Helpers.get_subcommand(req.args) | ||
|
||
result = case subcommand do | ||
"attach-handle" -> AttachHandle.attach(req, args) | ||
"detach-handle" -> DetachHandle.detach(req, args) | ||
"list" -> List.list(req, args) | ||
"list-handles" -> ListHandles.list(req, args) | ||
"info" -> Info.info(req, args) | ||
nil -> | ||
if Helpers.flag?(req.options, "help") do | ||
show_usage | ||
else | ||
List.list(req, args) | ||
end | ||
other -> | ||
{:error, {:unknown_subcommand, other}} | ||
end | ||
|
||
case result do | ||
{:ok, template, data} -> | ||
{:reply, req.reply_to, template, data, state} | ||
{:ok, data} -> | ||
{:reply, req.reply_to, data, state} | ||
{:error, err} -> | ||
{:error, req.reply_to, error(err), state} | ||
end | ||
end | ||
|
||
######################################################################## | ||
|
||
defp error(:invalid_handle), | ||
do: "Invalid chat handle" | ||
defp error(error), | ||
do: Helpers.error(error) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,45 @@ | ||
defmodule Cog.Commands.User.AttachHandle do | ||
use Cog.Command.GenCommand.Base, | ||
bundle: Cog.Util.Misc.embedded_bundle, | ||
name: "user-attach-handle" | ||
|
||
alias Cog.Repository.Users | ||
alias Cog.Repository.ChatHandles | ||
|
||
require Cog.Commands.Helpers, as: Helpers | ||
|
||
@description "Attach a chat handle for this chat provider with an existing Cog user." | ||
Helpers.usage """ | ||
Attach a chat handle for this chat provider with an existing Cog user. | ||
@arguments "<username> <handle>" | ||
USAGE | ||
user attach-handle [FLAGS] <username> <handle> | ||
rule "when command is #{Cog.Util.Misc.embedded_bundle}:user-attach-handle must have #{Cog.Util.Misc.embedded_bundle}:manage_users" | ||
ARGS | ||
username The name of a user | ||
handle The user's chat handle | ||
def handle_message(req, state) do | ||
result = with {:ok, [user_name, handle]} <- Helpers.get_args(req.args, 2) do | ||
case Users.by_username(user_name) do | ||
{:error, :not_found} -> | ||
{:error, {:resource_not_found, "user", user_name}} | ||
{:ok, user} -> | ||
provider_name = req.requestor.provider | ||
case ChatHandles.set_handle(user, provider_name, handle) do | ||
{:ok, handle} -> | ||
{:ok, Cog.V1.ChatHandleView.render("show.json", %{chat_handle: handle})} | ||
{:error, error} -> | ||
{:error, error} | ||
end | ||
end | ||
end | ||
FLAGS | ||
-h, --help Display this usage info | ||
""" | ||
|
||
case result do | ||
{:ok, data} -> | ||
{:reply, req.reply_to, "user-attach-handle", data, state} | ||
{:error, error} -> | ||
{:error, req.reply_to, Helpers.error(error), state} | ||
def attach(%{options: %{"help" => true}}, _args) do | ||
show_usage | ||
end | ||
def attach(%Cog.Messages.Command{}=command, [user_name, handle]) do | ||
case Users.by_username(user_name) do | ||
{:error, :not_found} -> | ||
{:error, {:resource_not_found, "user", user_name}} | ||
{:ok, user} -> | ||
provider_name = command.requestor.provider | ||
case ChatHandles.set_handle(user, provider_name, handle) do | ||
{:ok, handle} -> | ||
{:ok, "user-attach-handle", Cog.V1.ChatHandleView.render("show.json", %{chat_handle: handle})} | ||
{:error, error} -> | ||
{:error, error} | ||
end | ||
end | ||
end | ||
def attach(_, []), | ||
do: {:error, {:not_enough_args, 2}} | ||
def attach(_, [_]), | ||
do: {:error, {:not_enough_args, 2}} | ||
def attach(_, _), | ||
do: {:error, {:too_many_args, 2}} | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,41 @@ | ||
defmodule Cog.Commands.User.DetachHandle do | ||
use Cog.Command.GenCommand.Base, | ||
bundle: Cog.Util.Misc.embedded_bundle, | ||
name: "user-detach-handle" | ||
|
||
alias Cog.Repository.Users | ||
alias Cog.Repository.ChatHandles | ||
|
||
require Cog.Commands.Helpers, as: Helpers | ||
|
||
@description "Sever association between a chat handle and a user" | ||
|
||
@long_description """ | ||
Helpers.usage """ | ||
Detach the chat handle for this chat provider from an existing Cog user. | ||
After running this, the user will not be able to interact with Cog via chat. | ||
""" | ||
@arguments "<username>" | ||
USAGE | ||
user detach-handle [FLAGS] <username> | ||
rule "when command is #{Cog.Util.Misc.embedded_bundle}:user-detach-handle must have #{Cog.Util.Misc.embedded_bundle}:manage_users" | ||
ARGS | ||
username The name of a user | ||
def handle_message(req, state) do | ||
result = with {:ok, [user_name]} <- Helpers.get_args(req.args, 1) do | ||
case Users.by_username(user_name) do | ||
{:error, :not_found} -> | ||
{:error, {:resource_not_found, "user", user_name}} | ||
{:ok, user} -> | ||
provider_name = req.requestor.provider | ||
:ok = ChatHandles.remove_handle(user, provider_name) | ||
{:ok, %{"username" => user.username, "chat_provider" => %{"name" => provider_name}}} | ||
end | ||
end | ||
FLAGS | ||
-h, --help Display this usage info | ||
""" | ||
|
||
case result do | ||
{:ok, data} -> | ||
{:reply, req.reply_to, "user-detach-handle", data, state} | ||
{:error, error} -> | ||
{:error, req.reply_to, Helpers.error(error), state} | ||
def detach(%{options: %{"help" => true}}, _args) do | ||
show_usage | ||
end | ||
def detach(%Cog.Messages.Command{}=command, [user_name]) do | ||
case Users.by_username(user_name) do | ||
{:error, :not_found} -> | ||
{:error, {:resource_not_found, "user", user_name}} | ||
{:ok, user} -> | ||
provider_name = command.requestor.provider | ||
:ok = ChatHandles.remove_handle(user, provider_name) | ||
{:ok, "user-detach-handle", %{"username" => user.username, | ||
"chat_provider" => %{"name" => provider_name}}} | ||
end | ||
end | ||
def detach(_, []), | ||
do: {:error, {:not_enough_args, 1}} | ||
def detach(_, _), | ||
do: {:error, {:too_many_args, 1}} | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,35 @@ | ||
defmodule Cog.Commands.User.Info do | ||
use Cog.Command.GenCommand.Base, | ||
bundle: Cog.Util.Misc.embedded_bundle, | ||
name: "user-info" | ||
|
||
alias Cog.Repository.Users | ||
require Cog.Commands.Helpers, as: Helpers | ||
|
||
@description "Show detailed information about a specific user." | ||
Helpers.usage """ | ||
Show detailed information about a specific user. | ||
@arguments "<username>" | ||
USAGE | ||
user info [FLAGS] <username> | ||
rule "when command is #{Cog.Util.Misc.embedded_bundle}:user-info must have #{Cog.Util.Misc.embedded_bundle}:manage_users" | ||
ARGS | ||
username The name of a user | ||
def handle_message(req, state) do | ||
results = with {:ok, [user_name]} <- Helpers.get_args(req.args, 1) do | ||
case Users.by_username(user_name) do | ||
{:error, :not_found} -> | ||
{:error, {:resource_not_found, "user", user_name}} | ||
{:ok, user} -> | ||
rendered = Cog.V1.UserView.render("show.json", %{user: user}) | ||
{:ok, rendered[:user]} | ||
end | ||
end | ||
FLAGS | ||
-h, --help Display this usage info | ||
""" | ||
|
||
case results do | ||
{:ok, data} -> | ||
{:reply, req.reply_to, "user-info", data, state} | ||
{:error, error} -> | ||
{:error, req.reply_to, Helpers.error(error), state} | ||
def info(%{options: %{"help" => true}}, _args) do | ||
show_usage | ||
end | ||
def info(_req, [user_name]) do | ||
case Users.by_username(user_name) do | ||
{:error, :not_found} -> | ||
{:error, {:resource_not_found, "user", user_name}} | ||
{:ok, user} -> | ||
rendered = Cog.V1.UserView.render("show.json", %{user: user}) | ||
{:ok, "user-info", rendered[:user]} | ||
end | ||
end | ||
def info(_req, []), | ||
do: {:error, {:not_enough_args, 1}} | ||
def info(_req, _), | ||
do: {:error, {:too_many_args, 1}} | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,23 @@ | ||
defmodule Cog.Commands.User.List do | ||
use Cog.Command.GenCommand.Base, | ||
bundle: Cog.Util.Misc.embedded_bundle, | ||
name: "user-list" | ||
|
||
alias Cog.Repository.Users | ||
require Cog.Commands.Helpers, as: Helpers | ||
|
||
Helpers.usage """ | ||
List all users. | ||
@description "List all users." | ||
USAGE | ||
user list [FLAGS] | ||
rule "when command is #{Cog.Util.Misc.embedded_bundle}:user-list must have #{Cog.Util.Misc.embedded_bundle}:manage_users" | ||
FLAGS | ||
-h, --help Display this usage info | ||
""" | ||
|
||
def handle_message(req, state) do | ||
def list(%{options: %{"help" => true}}, _args) do | ||
show_usage | ||
end | ||
def list(_req, _args) do | ||
rendered = Cog.V1.UserView.render("index.json", %{users: Users.all}) | ||
{:reply, req.reply_to, "user-list", rendered[:users], state} | ||
{:ok, "user-list", rendered[:users]} | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
defmodule Cog.Commands.User.ListHandles do | ||
use Cog.Command.GenCommand.Base, | ||
bundle: Cog.Util.Misc.embedded_bundle, | ||
name: "user-list-handles" | ||
|
||
alias Cog.Repository.ChatHandles | ||
require Cog.Commands.Helpers, as: Helpers | ||
|
||
Helpers.usage """ | ||
List all chat handles attached to users for the active chat adapter. | ||
@description "List all chat handles attached to users for the active chat adapter." | ||
USAGE | ||
user list-handles [FLAGS] | ||
rule "when command is #{Cog.Util.Misc.embedded_bundle}:user-list-handles must have #{Cog.Util.Misc.embedded_bundle}:manage_users" | ||
FLAGS | ||
-h, --help Display this usage info | ||
""" | ||
|
||
def handle_message(req, state) do | ||
data = req.requestor.provider | ||
|> ChatHandles.for_provider() | ||
|> Enum.map(&Cog.V1.ChatHandleView.render("show.json", %{chat_handle: &1})) | ||
{:reply, req.reply_to, "user-list-handles", data, state} | ||
def list(%{options: %{"help" => true}}, _args), | ||
do: show_usage | ||
def list(%Cog.Messages.Command{}=command, _args) do | ||
provider_name = command.requestor.provider | ||
handles = ChatHandles.for_provider(provider_name) | ||
{:ok, "user-list-handles", | ||
Enum.map(handles, | ||
&Cog.V1.ChatHandleView.render("show.json", %{chat_handle: &1}))} | ||
end | ||
|
||
end |
Oops, something went wrong.