Skip to content

Commit

Permalink
PendingMessage VO w/ CommandID (#145)
Browse files Browse the repository at this point in the history
* Send commandID to post

* pendingMessage added to builtIn commands
  • Loading branch information
JayPHP authored and PeeHaa committed Feb 10, 2017
1 parent 599c609 commit 3599efa
Show file tree
Hide file tree
Showing 11 changed files with 542 additions and 108 deletions.
53 changes: 43 additions & 10 deletions src/BuiltIn/Commands/Admin.php
Expand Up @@ -5,6 +5,7 @@
use Amp\Artax\HttpClient;
use Amp\Promise;
use Room11\Jeeves\Chat\Client\ChatClient;
use Room11\Jeeves\Chat\Client\PendingMessage;
use Room11\Jeeves\Chat\Client\PostFlags;
use Room11\Jeeves\Chat\Entities\ChatUser;
use Room11\Jeeves\Chat\Message\Command as CommandMessage;
Expand Down Expand Up @@ -42,7 +43,10 @@ private function list(CommandMessage $command)
$admins = yield $this->storage->getAll($command->getRoom());

if ($admins['owners'] === [] && $admins['admins'] === []) {
return $this->chatClient->postMessage($command->getRoom(), "There are no registered admins");
return $this->chatClient->postMessage(
$command->getRoom(),
new PendingMessage('There are no registered admins', $command->getId())
);
}

$userIds = array_merge($admins['owners'], $admins['admins']);
Expand All @@ -57,44 +61,70 @@ private function list(CommandMessage $command)
: $user->getName();
}, $users));

return $this->chatClient->postMessage($command->getRoom(), $list);
return $this->chatClient->postMessage(
$command->getRoom(),
new PendingMessage($list, $command->getId())
);
}

private function add(CommandMessage $command, int $userId)
{
$admins = yield $this->storage->getAll($command->getRoom());

if (in_array($userId, $admins['admins'])) {
return $this->chatClient->postReply($command, "User already on admin list.");
return $this->chatClient->postReply(
$command,
new PendingMessage('User already on admin list.', $command->getId())
);
}

if (in_array($userId, $admins['owners'])) {
return $this->chatClient->postReply($command, "User is a room owner and has implicit admin rights.");
return $this->chatClient->postReply(
$command,
new PendingMessage('User is a room owner and has implicit admin rights.', $command->getId())
);
}

yield $this->storage->add($command->getRoom(), $userId);

return $this->chatClient->postMessage($command->getRoom(), "User added to the admin list.");
return $this->chatClient->postMessage(
$command->getRoom(),
new PendingMessage('User added to the admin list.', $command->getId())
);
}

private function remove(CommandMessage $command, int $userId)
{
$admins = yield $this->storage->getAll($command->getRoom());

if (in_array($userId, $admins['owners'])) {
return $this->chatClient->postReply($command, "User is a room owner and has implicit admin rights.");
return $this->chatClient->postReply(
$command,
new PendingMessage('User is a room owner and has implicit admin rights.', $command->getId())
);
}
if (!in_array($userId, $admins['admins'])) {
return $this->chatClient->postReply($command, "User not currently on admin list.");
return $this->chatClient->postReply(
$command,
new PendingMessage('User not currently on admin list.', $command->getId())
);
}

yield $this->storage->remove($command->getRoom(), $userId);

return $this->chatClient->postMessage($command->getRoom(), "User removed from the admin list.");
return $this->chatClient->postMessage(
$command->getRoom(),
new PendingMessage('User removed from the admin list.', $command->getId())
);
}

private function showCommandHelp(CommandMessage $command): Promise
{
return $this->chatClient->postMessage($command->getRoom(), self::COMMAND_HELP_TEXT, PostFlags::FIXED_FONT);
return $this->chatClient->postMessage(
$command->getRoom(),
new PendingMessage(self::COMMAND_HELP_TEXT, $command->getId()),
PostFlags::FIXED_FONT
);
}

private function execute(CommandMessage $command)
Expand All @@ -112,7 +142,10 @@ private function execute(CommandMessage $command)
}

if (!yield $this->storage->isAdmin($command->getRoom(), $command->getUserId())) {
return $this->chatClient->postReply($command, "I'm sorry Dave, I'm afraid I can't do that");
return $this->chatClient->postReply(
$command,
new PendingMessage('I\'m sorry Dave, I\'m afraid I can\'t do that', $command->getId())
);
}

switch ($command->getParameter(0)) {
Expand Down
45 changes: 38 additions & 7 deletions src/BuiltIn/Commands/Alias.php
Expand Up @@ -4,6 +4,7 @@

use Amp\Promise;
use Room11\Jeeves\Chat\Client\ChatClient;
use Room11\Jeeves\Chat\Client\PendingMessage;
use Room11\Jeeves\Chat\Message\Command as CommandMessage;
use Room11\Jeeves\Storage\Admin as AdminStorage;
use Room11\Jeeves\Storage\CommandAlias as CommandAliasStorage;
Expand Down Expand Up @@ -42,33 +43,60 @@ private function addAlias(CommandMessage $command)
$mapping = implode(' ', $command->getParameters(1));

if ($this->builtInCommandManager->hasRegisteredCommand($aliasCommand)) {
return $this->chatClient->postReply($command, "Command '{$aliasCommand}' is built in and cannot be altered");
return $this->chatClient->postReply(
$command,
new PendingMessage(
'Command \'{$aliasCommand}\' is built in and cannot be altered',
$command->getId()
)
);
}

if ($this->pluginManager->isCommandMappedForRoom($room, $aliasCommand)) {
return $this->chatClient->postReply($command, "Command '{$aliasCommand}' is already mapped. Use `!!command list` to display the currently mapped commands.");
return $this->chatClient->postReply(
$command,
new PendingMessage(
'Command \'{$aliasCommand}\' is already mapped. Use `!!command list` to display the currently mapped commands.',
$command->getId()
)
);
}

if (yield $this->aliasStorage->exists($room, $aliasCommand)) {
return $this->chatClient->postReply($command, "Alias '!!{$aliasCommand}' already exists.");
return $this->chatClient->postReply(
$command,
new PendingMessage('Alias \'!!{$aliasCommand}\' already exists.', $command->getId())
);
}

yield $this->aliasStorage->add($room, $aliasCommand, $mapping);

return $this->chatClient->postMessage($room, "Command '!!{$aliasCommand}' aliased to '!!{$mapping}'");
return $this->chatClient->postMessage(
$room,
new PendingMessage(
'Command \'!!{$aliasCommand}\' aliased to \'!!{$mapping}\'',
$command->getId()
)
);
}

private function removeAlias(CommandMessage $command): \Generator
{
$aliasCommand = $command->getParameter(0);

if (!yield $this->aliasStorage->exists($command->getRoom(), $aliasCommand)) {
return $this->chatClient->postMessage($command->getRoom(), "Alias '!!{$aliasCommand}' is not currently mapped");
return $this->chatClient->postMessage(
$command->getRoom(),
new PendingMessage('Alias \'!!{$aliasCommand}\' is not currently mapped', $command->getId())
);
}

yield $this->aliasStorage->remove($command->getRoom(), $aliasCommand);

return $this->chatClient->postMessage($command->getRoom(), "Alias '!!{$aliasCommand}' removed");
return $this->chatClient->postMessage(
$command->getRoom(),
new PendingMessage('Alias \'!!{$aliasCommand}\' removed', $command->getId())
);
}

/**
Expand All @@ -85,7 +113,10 @@ public function handleCommand(CommandMessage $command): Promise
}

if (!yield $this->adminStorage->isAdmin($command->getRoom(), $command->getUserId())) {
return $this->chatClient->postReply($command, "I'm sorry Dave, I'm afraid I can't do that");
return $this->chatClient->postReply(
$command,
new PendingMessage('I\'m sorry Dave, I\'m afraid I can\'t do that', $command->getId())
);
}

return $command->getCommandName() === 'alias'
Expand Down
31 changes: 25 additions & 6 deletions src/BuiltIn/Commands/Ban.php
Expand Up @@ -5,6 +5,7 @@
use Amp\Promise;
use Amp\Success;
use Room11\Jeeves\Chat\Client\ChatClient;
use Room11\Jeeves\Chat\Client\PendingMessage;
use Room11\Jeeves\Chat\Message\Command as CommandMessage;
use Room11\Jeeves\Storage\Admin as AdminStorage;
use Room11\Jeeves\Storage\Ban as BanStorage;
Expand Down Expand Up @@ -32,14 +33,20 @@ private function execute(CommandMessage $command)
}

if (!yield $this->adminStorage->isAdmin($command->getRoom(), $command->getUserId())) {
return $this->chatClient->postReply($command, "I'm sorry Dave, I'm afraid I can't do that");
return $this->chatClient->postReply(
$command,
new PendingMessage('I\'m sorry Dave, I\'m afraid I can\'t do that', $command->getId())
);
}

if ($command->getCommandName() === "ban" && $command->getParameter(0) === 'list') {
yield from $this->list($command);
} else if ($command->getCommandName() === "ban") {
if (!$command->hasParameters(2)) {
return $this->chatClient->postReply($command, "Ban length must be specified");
return $this->chatClient->postReply(
$command,
new PendingMessage('Ban length must be specified', $command->getId())
);
}

yield from $this->add($command, (int)$command->getParameter(0), $command->getParameter(1));
Expand All @@ -53,27 +60,39 @@ private function list(CommandMessage $command): \Generator
$bans = yield $this->banStorage->getAll($command->getRoom());

if (!$bans) {
yield $this->chatClient->postMessage($command->getRoom(), "No users are currently on the naughty list.");
yield $this->chatClient->postMessage(
$command->getRoom(),
new PendingMessage('No users are currently on the naughty list.', $command->getId())
);
return;
}

$list = implode(", ", array_map(function($expiration, $userId) {
return sprintf("%s (%s)", $userId, $expiration);
}, $bans, array_keys($bans)));

yield $this->chatClient->postMessage($command->getRoom(), $list);
yield $this->chatClient->postMessage(
$command->getRoom(),
new PendingMessage($list, $command->getId())
);
}

private function add(CommandMessage $command, int $userId, string $duration): \Generator {
yield $this->banStorage->add($command->getRoom(), $userId, $duration);

yield $this->chatClient->postMessage($command->getRoom(), "User is banned.");
yield $this->chatClient->postMessage(
$command->getRoom(),
new PendingMessage('User is banned.', $command->getId())
);
}

private function remove(CommandMessage $command, int $userId): \Generator {
yield $this->banStorage->remove($command->getRoom(), $userId);

yield $this->chatClient->postMessage($command->getRoom(), "User is unbanned.");
yield $this->chatClient->postMessage(
$command->getRoom(),
new PendingMessage('User is unbanned.', $command->getId())
);
}

/**
Expand Down

0 comments on commit 3599efa

Please sign in to comment.