Browse Source
No patterns for now
No patterns for now
Signed-off-by: Joas Schilling <coding@schilljs.com>pull/1453/head
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
8 changed files with 145 additions and 12 deletions
-
2js/admin/commands.js
-
2js/admin/commands.js.map
-
2lib/AppInfo/Application.php
-
4lib/Chat/ChatManager.php
-
35lib/Chat/Command/DefaultExecutor.php
-
93lib/Chat/Command/Listener.php
-
11lib/Migration/Version5099Date20190121102337.php
-
8lib/Model/Command.php
2
js/admin/commands.js
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
2
js/admin/commands.js.map
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,35 @@ |
|||
<?php |
|||
declare(strict_types=1); |
|||
/** |
|||
* @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.com> |
|||
* |
|||
* @license GNU AGPL version 3 or any later version |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU Affero General Public License as |
|||
* published by the Free Software Foundation, either version 3 of the |
|||
* License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU Affero General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU Affero General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
* |
|||
*/ |
|||
|
|||
namespace OCA\Spreed\Chat\Command; |
|||
|
|||
|
|||
use OCA\Spreed\Model\Command; |
|||
use OCA\Spreed\Room; |
|||
use OCP\Comments\IComment; |
|||
|
|||
class DefaultExecutor { |
|||
|
|||
public function exec(Room $room, IComment $message, Command $command): void { |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,93 @@ |
|||
<?php |
|||
declare(strict_types=1); |
|||
/** |
|||
* @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.com> |
|||
* |
|||
* @license GNU AGPL version 3 or any later version |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU Affero General Public License as |
|||
* published by the Free Software Foundation, either version 3 of the |
|||
* License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU Affero General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU Affero General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
* |
|||
*/ |
|||
|
|||
namespace OCA\Spreed\Chat\Command; |
|||
|
|||
|
|||
use OCA\Spreed\Chat\ChatManager; |
|||
use OCA\Spreed\Model\CommandMapper; |
|||
use OCA\Spreed\Room; |
|||
use OCP\Comments\IComment; |
|||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|||
use Symfony\Component\EventDispatcher\GenericEvent; |
|||
|
|||
class Listener { |
|||
|
|||
/** @var EventDispatcherInterface */ |
|||
protected $dispatcher; |
|||
/** @var CommandMapper */ |
|||
protected $commandMapper; |
|||
/** @var DefaultExecutor */ |
|||
protected $defaultExecutor; |
|||
|
|||
public function __construct(EventDispatcherInterface $dispatcher, CommandMapper $commandMapper, DefaultExecutor $defaultExecutor) { |
|||
$this->dispatcher = $dispatcher; |
|||
$this->commandMapper = $commandMapper; |
|||
$this->defaultExecutor = $defaultExecutor; |
|||
} |
|||
|
|||
public static function register(EventDispatcherInterface $dispatcher): void { |
|||
$dispatcher->addListener(ChatManager::class . '::preSendMessage', function(GenericEvent $event) { |
|||
/** @var IComment $message */ |
|||
$message = $event->getArgument('comment'); |
|||
if (strpos($message->getMessage(), '/') === 0) { |
|||
/** @var self $listener */ |
|||
$listener = \OC::$server->query(self::class); |
|||
|
|||
if (!$listener->executeCommands($event)) { |
|||
$listener->showHelp($event); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
|
|||
public function executeCommands(GenericEvent $event): bool { |
|||
/** @var Room $room */ |
|||
$room = $event->getSubject(); |
|||
/** @var IComment $message */ |
|||
$message = $event->getArgument('comment'); |
|||
|
|||
$commands = $this->commandMapper->findAll(); |
|||
foreach ($commands as $command) { |
|||
if ($this->matchesCommand($message->getMessage(), $command->getCommand())) { |
|||
$this->defaultExecutor->exec($room, $message, $command); |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public function showHelp(GenericEvent $event): void { |
|||
// FIXME
|
|||
} |
|||
|
|||
protected function matchesCommand(string $message, string $command): bool { |
|||
$command = '/' . $command; |
|||
|
|||
if ($message === $command) { |
|||
return true; |
|||
} |
|||
|
|||
return strpos($message, $command . ' ') === 0; |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue