Skip to content

Commit

Permalink
Revert "Merge branch 'peck/user-command'"
Browse files Browse the repository at this point in the history
This reverts commit 4eea191, reversing
changes made to 0f1a55b.
  • Loading branch information
Matthew Peck committed Dec 13, 2016
1 parent a10be9f commit c0041e4
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 127 deletions.
2 changes: 0 additions & 2 deletions lib/cog/commands/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ defmodule Cog.Commands.Helpers do
do: "Could not find 'rule' with the id '#{id}'"
def error({:resource_not_found, resource_type, resource_name}),
do: "Could not find '#{resource_type}' with the name '#{resource_name}'"
def error(:invalid_handle),
do: "Invalid chat handle"

@doc """
Returns an alias. If the visibility isn't passed we first search for a user
Expand Down
66 changes: 66 additions & 0 deletions lib/cog/commands/user.ex
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
58 changes: 31 additions & 27 deletions lib/cog/commands/user/attach_handle.ex
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
50 changes: 24 additions & 26 deletions lib/cog/commands/user/detach_handle.ex
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
45 changes: 23 additions & 22 deletions lib/cog/commands/user/info.ex
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
22 changes: 14 additions & 8 deletions lib/cog/commands/user/list.ex
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
28 changes: 17 additions & 11 deletions lib/cog/commands/user/list_handles.ex
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
Loading

0 comments on commit c0041e4

Please sign in to comment.