Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/spreed.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/Chat/Parser/Command.php4
-rw-r--r--lib/Controller/CommandController.php99
-rw-r--r--lib/Migration/Version5099Date20190121102337.php12
-rw-r--r--lib/Model/Command.php31
-rw-r--r--lib/Model/CommandMapper.php16
-rw-r--r--lib/Service/CommandService.php156
6 files changed, 251 insertions, 67 deletions
diff --git a/lib/Chat/Parser/Command.php b/lib/Chat/Parser/Command.php
index eda01f90a..63d12c20c 100644
--- a/lib/Chat/Parser/Command.php
+++ b/lib/Chat/Parser/Command.php
@@ -50,12 +50,12 @@ class Command {
throw new \OutOfBoundsException('Invalid message');
}
- if ($data['visibility'] === \OCA\Spreed\Model\Command::OUTPUT_NONE) {
+ if ($data['visibility'] === \OCA\Spreed\Model\Command::RESPONSE_NONE) {
throw new \RuntimeException('Message should not print');
}
if ($this->recipient instanceof IUser &&
- $data['visibility'] !== \OCA\Spreed\Model\Command::OUTPUT_ALL &&
+ $data['visibility'] !== \OCA\Spreed\Model\Command::RESPONSE_ALL &&
$data['user'] !== $this->recipient->getUID()) {
throw new \RuntimeException('Message should not print');
}
diff --git a/lib/Controller/CommandController.php b/lib/Controller/CommandController.php
index 60858a304..d4d42bcc0 100644
--- a/lib/Controller/CommandController.php
+++ b/lib/Controller/CommandController.php
@@ -23,8 +23,7 @@
namespace OCA\Spreed\Controller;
-use OCA\Spreed\Model\Command;
-use OCA\Spreed\Model\CommandMapper;
+use OCA\Spreed\Service\CommandService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
@@ -32,35 +31,39 @@ use OCP\AppFramework\OCSController;
use OCP\IRequest;
class CommandController extends OCSController {
- /** @var CommandMapper */
- protected $commandMapper;
+
+
+ /** @var CommandService */
+ protected $commandService;
/**
* @param string $appName
* @param IRequest $request
- * @param CommandMapper $commandMapper
+ * @param CommandService $commandService
*/
public function __construct($appName,
IRequest $request,
- CommandMapper $commandMapper) {
+ CommandService $commandService) {
parent::__construct($appName, $request);
- $this->commandMapper = $commandMapper;
+ $this->commandService = $commandService;
}
/**
* @return DataResponse
*/
public function index(): DataResponse {
- $commands = $this->commandMapper->findAll();
+ $commands = $this->commandService->findAll();
$result = [];
foreach ($commands as $command) {
$result[] = [
'id' => $command->getId(),
+ 'app' => $command->getApp(),
'name' => $command->getName(),
- 'pattern' => $command->getPattern(),
+ 'pattern' => $command->getCommand(),
'script' => $command->getScript(),
- 'output' => $command->getOutput(),
+ 'response' => $command->getResponse(),
+ 'enabled' => $command->getEnabled(),
];
}
@@ -68,36 +71,28 @@ class CommandController extends OCSController {
}
/**
+ * @param string $cmd
* @param string $name
- * @param string $pattern
* @param string $script
- * @param int $output
+ * @param int $response
+ * @param int $enabled
* @return DataResponse
*/
- public function create(string $name, string $pattern, string $script, int $output): DataResponse {
- $command = new Command();
-
- if (!\in_array($output, [Command::OUTPUT_NONE, Command::OUTPUT_USER, Command::OUTPUT_ALL], true)) {
- return new DataResponse([], Http::STATUS_BAD_REQUEST);
+ public function create(string $cmd, string $name, string $script, int $response, int $enabled): DataResponse {
+ try {
+ $command = $this->commandService->create('', $name, $cmd, $script, $response, $enabled);
+ } catch (\InvalidArgumentException $e) {
+ return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
}
- // FIXME Validate "bot name"
- // FIXME Validate "pattern"
- // FIXME Validate "script"
-
- $command->setName($name);
- $command->setName($pattern);
- $command->setName($script);
- $command->setName($output);
-
- $this->commandMapper->insert($command);
-
return new DataResponse([
'id' => $command->getId(),
+ 'app' => $command->getApp(),
'name' => $command->getName(),
- 'pattern' => $command->getPattern(),
+ 'pattern' => $command->getCommand(),
'script' => $command->getScript(),
- 'output' => $command->getOutput(),
+ 'response' => $command->getResponse(),
+ 'enabled' => $command->getEnabled(),
]);
}
@@ -107,56 +102,48 @@ class CommandController extends OCSController {
*/
public function show(int $id): DataResponse {
try {
- $command = $this->commandMapper->findById($id);
+ $command = $this->commandService->findById($id);
} catch (DoesNotExistException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
return new DataResponse([
'id' => $command->getId(),
+ 'app' => $command->getApp(),
'name' => $command->getName(),
- 'pattern' => $command->getPattern(),
+ 'pattern' => $command->getCommand(),
'script' => $command->getScript(),
- 'output' => $command->getOutput(),
+ 'response' => $command->getResponse(),
+ 'enabled' => $command->getEnabled(),
]);
}
/**
* @param int $id
+ * @param string $cmd
* @param string $name
- * @param string $pattern
* @param string $script
- * @param int $output
+ * @param int $response
+ * @param int $enabled
* @return DataResponse
*/
- public function update(int $id, string $name, string $pattern, string $script, int $output): DataResponse {
+ public function update(int $id, string $cmd, string $name, string $script, int $response, int $enabled): DataResponse {
try {
- $command = $this->commandMapper->findById($id);
+ $command = $this->commandService->update($id, $name, $cmd, $script, $response, $enabled);
} catch (DoesNotExistException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
+ } catch (\InvalidArgumentException $e) {
+ return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
}
- if (!\in_array($output, [Command::OUTPUT_NONE, Command::OUTPUT_USER, Command::OUTPUT_ALL], true)) {
- return new DataResponse([], Http::STATUS_BAD_REQUEST);
- }
-
- // FIXME Validate "bot name"
- // FIXME Validate "pattern"
- // FIXME Validate "script"
-
- $command->setName($name);
- $command->setName($pattern);
- $command->setName($script);
- $command->setName($output);
-
- $this->commandMapper->update($command);
-
return new DataResponse([
'id' => $command->getId(),
+ 'app' => $command->getApp(),
'name' => $command->getName(),
- 'pattern' => $command->getPattern(),
+ 'pattern' => $command->getCommand(),
'script' => $command->getScript(),
- 'output' => $command->getOutput(),
+ 'response' => $command->getResponse(),
+ 'enabled' => $command->getEnabled(),
]);
}
@@ -166,13 +153,11 @@ class CommandController extends OCSController {
*/
public function destroy(int $id): DataResponse {
try {
- $command = $this->commandMapper->findById($id);
+ $this->commandService->delete($id);
} catch (DoesNotExistException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
- $this->commandMapper->delete($command);
-
return new DataResponse();
}
diff --git a/lib/Migration/Version5099Date20190121102337.php b/lib/Migration/Version5099Date20190121102337.php
index d1807f2c2..edae86006 100644
--- a/lib/Migration/Version5099Date20190121102337.php
+++ b/lib/Migration/Version5099Date20190121102337.php
@@ -47,6 +47,11 @@ class Version5099Date20190121102337 extends SimpleMigrationStep {
'notnull' => true,
'length' => 20,
]);
+ $table->addColumn('app', Type::STRING, [
+ 'notnull' => false,
+ 'length' => 64,
+ 'default' => '',
+ ]);
$table->addColumn('name', Type::STRING, [
'notnull' => true,
'length' => 64,
@@ -58,7 +63,12 @@ class Version5099Date20190121102337 extends SimpleMigrationStep {
$table->addColumn('script', Type::TEXT, [
'notnull' => true,
]);
- $table->addColumn('output', Type::INTEGER, [
+ $table->addColumn('response', Type::INTEGER, [
+ 'notnull' => true,
+ 'length' => 6,
+ 'default' => 1,
+ ]);
+ $table->addColumn('enabled', Type::INTEGER, [
'notnull' => true,
'length' => 6,
'default' => 1,
diff --git a/lib/Model/Command.php b/lib/Model/Command.php
index 03ce5c513..be8329f09 100644
--- a/lib/Model/Command.php
+++ b/lib/Model/Command.php
@@ -25,20 +25,32 @@ namespace OCA\Spreed\Model;
use OCP\AppFramework\Db\Entity;
/**
+ * @method void setApp(string $app)
+ * @method string getApp()
* @method void setName(string $name)
* @method string getName()
* @method void setCommand(string $command)
* @method string getCommand()
* @method void setScript(string $name)
* @method string getScript()
- * @method void setOutput(int $output)
- * @method int getOutput()
+ * @method void setResponse(int $response)
+ * @method int getResponse()
+ * @method void setEnabled(int $enabled)
+ * @method int getEnabled()
*/
class Command extends Entity {
- public const OUTPUT_NONE = 0;
- public const OUTPUT_USER = 1;
- public const OUTPUT_ALL = 2;
+ public const RESPONSE_NONE = 0;
+ public const RESPONSE_USER = 1;
+ public const RESPONSE_ALL = 2;
+
+ public const ENABLED_OFF = 0;
+ public const ENABLED_MODERATOR = 1;
+ public const ENABLED_USERS = 2;
+ public const ENABLED_ALL = 3;
+
+ /** @var string */
+ protected $app;
/** @var string */
protected $name;
@@ -50,12 +62,17 @@ class Command extends Entity {
protected $script;
/** @var int */
- protected $output;
+ protected $response;
+
+ /** @var int */
+ protected $enabled;
public function __construct() {
+ $this->addType('app', 'string');
$this->addType('name', 'string');
$this->addType('command', 'string');
$this->addType('script', 'string');
- $this->addType('output', 'int');
+ $this->addType('response', 'int');
+ $this->addType('enabled', 'int');
}
}
diff --git a/lib/Model/CommandMapper.php b/lib/Model/CommandMapper.php
index 4107e7324..226e673fc 100644
--- a/lib/Model/CommandMapper.php
+++ b/lib/Model/CommandMapper.php
@@ -59,4 +59,20 @@ class CommandMapper extends QBMapper {
return $this->findEntity($query);
}
+
+ /**
+ * @param string $app
+ * @param string $cmd
+ * @return Command
+ * @throws \OCP\AppFramework\Db\DoesNotExistException
+ */
+ public function find(string $app, string $cmd): Command {
+ $query = $this->db->getQueryBuilder();
+ $query->select('*')
+ ->from($this->getTableName())
+ ->where($query->expr()->eq('app', $query->createNamedParameter($app)))
+ ->andWhere($query->expr()->eq('command', $query->createNamedParameter($cmd)));
+
+ return $this->findEntity($query);
+ }
}
diff --git a/lib/Service/CommandService.php b/lib/Service/CommandService.php
new file mode 100644
index 000000000..71fa15aeb
--- /dev/null
+++ b/lib/Service/CommandService.php
@@ -0,0 +1,156 @@
+<?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\Service;
+
+
+use OCA\Spreed\Model\Command;
+use OCA\Spreed\Model\CommandMapper;
+use OCP\AppFramework\Db\DoesNotExistException;
+
+class CommandService {
+
+ /** @var CommandMapper */
+ protected $mapper;
+
+ public function __construct(CommandMapper $mapper) {
+ $this->mapper = $mapper;
+ }
+
+ /**
+ * @param string $app
+ * @param string $cmd
+ * @param string $name
+ * @param string $script
+ * @param int $response
+ * @param int $enabled
+ * @return Command
+ * @throws \InvalidArgumentException
+ */
+ public function create(string $app, string $cmd, string $name, string $script, int $response, int $enabled): Command {
+ try {
+ $this->mapper->find($app, $cmd);
+ throw new \InvalidArgumentException('command', 1);
+ } catch (DoesNotExistException $e) {
+ }
+
+ if (!\in_array($response, [Command::RESPONSE_NONE, Command::RESPONSE_USER, Command::RESPONSE_ALL], true)) {
+ throw new \InvalidArgumentException('response', 4);
+ }
+
+ if (!\in_array($enabled, [Command::ENABLED_OFF, Command::ENABLED_MODERATOR, Command::ENABLED_USERS, Command::ENABLED_ALL], true)) {
+ throw new \InvalidArgumentException('enabled', 5);
+ }
+
+ $command = new Command();
+ $command->setApp($app);
+ $command->setCommand($cmd);
+ // FIXME Validate "bot name"
+ $command->setName($name);
+ // FIXME Validate "script"
+ $command->setScript($script);
+ $command->setResponse($response);
+ $command->setEnabled($enabled);
+
+ return $this->mapper->insert($command);
+ }
+
+ /**
+ * @param int $id
+ * @param string $cmd
+ * @param string $name
+ * @param string $script
+ * @param int $response
+ * @param int $enabled
+ * @return Command
+ * @throws \InvalidArgumentException
+ * @throws DoesNotExistException
+ */
+ public function update(int $id, string $cmd, string $name, string $script, int $response, int $enabled): Command {
+ $command = $this->mapper->findById($id);
+ if (!\in_array($response, [Command::RESPONSE_NONE, Command::RESPONSE_USER, Command::RESPONSE_ALL], true)) {
+ throw new \InvalidArgumentException('response', 4);
+ }
+
+ if (!\in_array($enabled, [Command::ENABLED_OFF, Command::ENABLED_MODERATOR, Command::ENABLED_USERS, Command::ENABLED_ALL], true)) {
+ throw new \InvalidArgumentException('enabled', 5);
+ }
+
+ if ($command->getApp() !== '' || $command->getCommand() === 'help') {
+ throw new \InvalidArgumentException('app', 0);
+ }
+
+ if ($cmd !== $command->getCommand()) {
+ try {
+ $this->mapper->find('', $cmd);
+ throw new \InvalidArgumentException('command', 1);
+ } catch (DoesNotExistException $e) {
+ $command->setCommand($cmd);
+ }
+ }
+
+ // FIXME Validate "bot name"
+ $command->setName($name);
+ // FIXME Validate "script"
+ $command->setScript($script);
+
+ $command->setResponse($response);
+ $command->setEnabled($enabled);
+
+ return $this->mapper->update($command);
+ }
+
+ /**
+ * @param int $id
+ * @return Command
+ * @throws DoesNotExistException
+ */
+ public function delete(int $id): Command {
+ $command = $this->mapper->findById($id);
+ return $this->mapper->delete($command);
+ }
+ /**
+ * @param string $app
+ * @param string $cmd
+ * @return Command
+ * @throws DoesNotExistException
+ */
+ public function find(string $app, string $cmd): Command {
+ return $this->mapper->find($app, $cmd);
+ }
+
+ /**
+ * @param int $id
+ * @return Command
+ * @throws DoesNotExistException
+ */
+ public function findById(int $id): Command {
+ return $this->mapper->findById($id);
+ }
+
+ /**
+ * @return Command[]
+ */
+ public function findAll(): array {
+ return $this->mapper->findAll();
+ }
+}