-
Notifications
You must be signed in to change notification settings - Fork 19
/
Remove.php
81 lines (66 loc) · 2.62 KB
/
Remove.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php declare(strict_types = 1);
namespace Room11\Jeeves\BuiltIn\Commands;
use Amp\Promise;
use Room11\Jeeves\Chat\Command as CommandMessage;
use Room11\Jeeves\Storage\Admin as AdminStorage;
use Room11\Jeeves\System\BuiltInCommand;
use Room11\Jeeves\System\BuiltInCommandInfo;
use Room11\StackChat\Client\Client as ChatClient;
use Room11\StackChat\Client\PostedMessageTracker;
use Room11\StackChat\Room\AclDataAccessor;
use function Amp\resolve;
class Remove implements BuiltInCommand
{
private const BIN_ROOM_ID = 48058;
private $chatClient;
private $aclDataAccessor;
private $admin;
private $tracker;
public function __construct(
ChatClient $chatClient,
AclDataAccessor $aclDataAccessor,
AdminStorage $admin,
PostedMessageTracker $tracker
) {
$this->chatClient = $chatClient;
$this->aclDataAccessor = $aclDataAccessor;
$this->admin = $admin;
$this->tracker = $tracker;
}
private function remove(CommandMessage $command)
{
if (!yield $this->admin->isAdmin($command->getRoom(), $command->getUserId())) {
return $this->chatClient->postReply($command, "Sorry, you're not cool enough to do that :(");
}
if (!yield $this->aclDataAccessor->isAuthenticatedUserRoomOwner($command->getRoom())) {
return $this->chatClient->postReply($command, "Sorry, I'm not a room owner so I can't do that :(");
}
if ($this->tracker->getCount($command->getRoom()) === 0) {
return $this->chatClient->postReply($command, "I don't have any messages stored for this room, sorry");
}
return $this->removeMessages($command, (int)($command->getParameter(0) ?? 1));
}
private function removeMessages(CommandMessage $command, int $count)
{
$room = $command->getRoom();
$messages = [$command->getId()];
for ($i = 0; $i < $count && null !== $message = $this->tracker->popMessage($room); $i++) {
$messages[] = $message->getId();
$commandMessage = $message->getParentMessage();
if ($commandMessage !== null) {
$messages[] = $commandMessage->getId();
}
}
return $this->chatClient->moveMessages($room, self::BIN_ROOM_ID, ...$messages);
}
public function handleCommand(CommandMessage $command): Promise
{
return resolve($this->remove($command));
}
public function getCommandInfo(): array
{
return [
new BuiltInCommandInfo('remove', "Remove the last x messages posted by the bot", BuiltInCommandInfo::REQUIRE_ADMIN_USER),
];
}
}