Browse Source

No patterns for now

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/1453/head
Joas Schilling 7 years ago
parent
commit
c460128531
No known key found for this signature in database GPG Key ID: 7076EA9751AACDDA
  1. 2
      js/admin/commands.js
  2. 2
      js/admin/commands.js.map
  3. 2
      lib/AppInfo/Application.php
  4. 4
      lib/Chat/ChatManager.php
  5. 35
      lib/Chat/Command/DefaultExecutor.php
  6. 93
      lib/Chat/Command/Listener.php
  7. 11
      lib/Migration/Version5099Date20190121102337.php
  8. 8
      lib/Model/Command.php

2
js/admin/commands.js
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

2
lib/AppInfo/Application.php

@ -25,6 +25,7 @@ namespace OCA\Spreed\AppInfo;
use OCA\Spreed\Activity\Listener as ActivityListener;
use OCA\Spreed\Capabilities;
use OCA\Spreed\Chat\ChatManager;
use OCA\Spreed\Chat\Command\Listener as CommandListener;
use OCA\Spreed\Chat\Listener as ChatListener;
use OCA\Spreed\Chat\SystemMessage\Listener as SystemMessageListener;
use OCA\Spreed\Config;
@ -72,6 +73,7 @@ class Application extends App {
FilesTemplateLoader::register($dispatcher);
RoomShareProvider::register($dispatcher);
SignalingListener::register($dispatcher);
CommandListener::register($dispatcher);
$this->registerRoomActivityHooks($dispatcher);
$this->registerChatHooks($dispatcher);

4
lib/Chat/ChatManager.php

@ -116,6 +116,10 @@ class ChatManager {
// comment
$comment->setVerb('comment');
$this->dispatcher->dispatch(self::class . '::preSendMessage', new GenericEvent($chat, [
'comment' => $comment,
]));
try {
$this->commentsManager->save($comment);

35
lib/Chat/Command/DefaultExecutor.php

@ -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 {
}
}

93
lib/Chat/Command/Listener.php

@ -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;
}
}

11
lib/Migration/Version5099Date20190121102337.php

@ -35,7 +35,7 @@ class Version5099Date20190121102337 extends SimpleMigrationStep {
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
@ -51,18 +51,17 @@ class Version5099Date20190121102337 extends SimpleMigrationStep {
'notnull' => true,
'length' => 64,
]);
$table->addColumn('pattern', Type::STRING, [
$table->addColumn('command', Type::STRING, [
'notnull' => true,
'length' => 512,
'length' => 64,
]);
$table->addColumn('script', Type::STRING, [
$table->addColumn('script', Type::TEXT, [
'notnull' => true,
'length' => 512,
]);
$table->addColumn('output', Type::INTEGER, [
'notnull' => true,
'length' => 6,
'default' => 0,
'default' => 1,
]);
$table->setPrimaryKey(['id']);

8
lib/Model/Command.php

@ -27,8 +27,8 @@ use OCP\AppFramework\Db\Entity;
/**
* @method void setName(string $name)
* @method string getName()
* @method void setPattern(string $pattern)
* @method string getPattern()
* @method void setCommand(string $command)
* @method string getCommand()
* @method void setScript(string $name)
* @method string getScript()
* @method void setOutput(int $output)
@ -44,7 +44,7 @@ class Command extends Entity {
protected $name;
/** @var string */
protected $pattern;
protected $command;
/** @var string */
protected $script;
@ -54,7 +54,7 @@ class Command extends Entity {
public function __construct() {
$this->addType('name', 'string');
$this->addType('pattern', 'string');
$this->addType('command', 'string');
$this->addType('script', 'string');
$this->addType('output', 'int');
}

Loading…
Cancel
Save