UPSTREAM BASELINE: nextcloud/spreed v22.0.12 (без изменений)
Type checking / changes (push) Has been cancelled
Type checking / test (push) Has been cancelled
Type checking / typescript-summary (push) Has been cancelled
Node tests / changes (push) Has been cancelled
Node tests / test (push) Has been cancelled
Node tests / test-summary (push) Has been cancelled
Type checking / changes (push) Has been cancelled
Type checking / test (push) Has been cancelled
Type checking / typescript-summary (push) Has been cancelled
Node tests / changes (push) Has been cancelled
Node tests / test (push) Has been cancelled
Node tests / test-summary (push) Has been cancelled
Источник: https://github.com/nextcloud/spreed/archive/refs/tags/v22.0.12.tar.gz С этого коммита ветка официального Nextcloud Talk отрезана (решение владельца 2026-07-06). Все дальнейшие изменения — только наши; версии релизов: 22.0.12-f7.N.
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\Talk\Command\Bot;
|
||||
|
||||
use OC\Core\Command\Base;
|
||||
use OCA\Talk\Model\Bot;
|
||||
use OCA\Talk\Model\BotServer;
|
||||
use OCA\Talk\Model\BotServerMapper;
|
||||
use OCA\Talk\Service\BotService;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\DB\Exception;
|
||||
use OCP\Security\ISecureRandom;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class Create extends Base {
|
||||
public function __construct(
|
||||
private BotService $botService,
|
||||
private BotServerMapper $botServerMapper,
|
||||
private ISecureRandom $secureRandom,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function configure(): void {
|
||||
parent::configure();
|
||||
$this
|
||||
->setName('talk:bot:create')
|
||||
->setDescription('Creates a new bot on the server with \'response\' feature only.')
|
||||
->addArgument(
|
||||
'name',
|
||||
InputArgument::REQUIRED,
|
||||
'The name under which the messages will be posted (min. 1 char, max. 64 chars)'
|
||||
)
|
||||
->addArgument(
|
||||
'description',
|
||||
InputArgument::OPTIONAL,
|
||||
'Optional description shown in the admin settings (max. 4000 chars)'
|
||||
)
|
||||
->addOption(
|
||||
'secret',
|
||||
's',
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Secret used to validate API calls (min. 40 chars, max. 128 chars). When none is provided, a random 64 chars string is generated and output.'
|
||||
)
|
||||
->addOption(
|
||||
'no-setup',
|
||||
null,
|
||||
InputOption::VALUE_NONE,
|
||||
'Prevent moderators from setting up the bot in a conversation'
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$name = $input->getArgument('name');
|
||||
$description = $input->getArgument('description') ?? '';
|
||||
$noSetup = $input->getOption('no-setup');
|
||||
$featureFlags = Bot::FEATURE_RESPONSE;
|
||||
|
||||
$secret = $input->getOption('secret') ?? $this->secureRandom->generate(64);
|
||||
$url = Bot::URL_RESPONSE_ONLY_PREFIX . bin2hex(random_bytes(16));
|
||||
|
||||
try {
|
||||
$this->botService->validateBotParameters($name, $secret, $url, $description);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$output->writeln('<error>' . $e->getMessage() . '</error>');
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->botServerMapper->findByUrl($url);
|
||||
$output->writeln('<error>Bot with the same URL is already registered</error>');
|
||||
return 2;
|
||||
} catch (DoesNotExistException) {
|
||||
}
|
||||
|
||||
$bot = new BotServer();
|
||||
$bot->setName($name);
|
||||
$bot->setSecret($secret);
|
||||
$bot->setUrl($url);
|
||||
$bot->setUrlHash(sha1($url));
|
||||
$bot->setDescription($description);
|
||||
$bot->setState($noSetup ? Bot::STATE_NO_SETUP : Bot::STATE_ENABLED);
|
||||
$bot->setFeatures($featureFlags);
|
||||
try {
|
||||
$botEntity = $this->botServerMapper->insert($bot);
|
||||
} catch (\Exception $e) {
|
||||
if ($e instanceof Exception && $e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
||||
$output->writeln('<error>Bot with the same secret is already registered</error>');
|
||||
return 3;
|
||||
} else {
|
||||
$output->writeln('<error>' . get_class($e) . ': ' . $e->getMessage() . '</error>');
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
$output->writeln('<info>Bot installed</info>');
|
||||
$output->writeln('ID: ' . $botEntity->getId());
|
||||
|
||||
if ($input->getOption('secret') === null) {
|
||||
$output->writeln('Secret: ' . $secret);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\Talk\Command\Bot;
|
||||
|
||||
use OC\Core\Command\Base;
|
||||
use OCA\Talk\Model\Bot;
|
||||
use OCA\Talk\Model\BotServer;
|
||||
use OCA\Talk\Model\BotServerMapper;
|
||||
use OCA\Talk\Service\BotService;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\DB\Exception;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class Install extends Base {
|
||||
public function __construct(
|
||||
private BotService $botService,
|
||||
private BotServerMapper $botServerMapper,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function configure(): void {
|
||||
parent::configure();
|
||||
$this
|
||||
->setName('talk:bot:install')
|
||||
->setDescription('Install a new bot on the server')
|
||||
->addArgument(
|
||||
'name',
|
||||
InputArgument::REQUIRED,
|
||||
'The name under which the messages will be posted (min. 1 char, max. 64 chars)'
|
||||
)
|
||||
->addArgument(
|
||||
'secret',
|
||||
InputArgument::REQUIRED,
|
||||
'Secret used to validate API calls (min. 40 chars, max. 128 chars)'
|
||||
)
|
||||
->addArgument(
|
||||
'url',
|
||||
InputArgument::REQUIRED,
|
||||
'Webhook endpoint to post messages to (max. 4000 chars)'
|
||||
)
|
||||
->addArgument(
|
||||
'description',
|
||||
InputArgument::OPTIONAL,
|
||||
'Optional description shown in the admin settings (max. 4000 chars)'
|
||||
)
|
||||
->addOption(
|
||||
'no-setup',
|
||||
null,
|
||||
InputOption::VALUE_NONE,
|
||||
'Prevent moderators from setting up the bot in a conversation'
|
||||
)
|
||||
->addOption(
|
||||
'feature',
|
||||
'f',
|
||||
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
|
||||
'Specify the list of features for the bot' . "\n"
|
||||
. ' - webhook: The bot receives posted chat messages as webhooks' . "\n"
|
||||
. ' - response: The bot can post messages and reactions as a response' . "\n"
|
||||
. ' - event: The bot reads posted messages from local events' . "\n"
|
||||
. ' - reaction: The bot is notified about adding and removing of reactions' . "\n"
|
||||
. ' - none: When all features should be disabled for the bot'
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$name = $input->getArgument('name');
|
||||
$secret = $input->getArgument('secret');
|
||||
$url = $input->getArgument('url');
|
||||
$description = $input->getArgument('description') ?? '';
|
||||
$noSetup = $input->getOption('no-setup');
|
||||
|
||||
if (!empty($input->getOption('feature'))) {
|
||||
$featureFlags = Bot::featureLabelsToFlags($input->getOption('feature'));
|
||||
if (str_starts_with($url, Bot::URL_APP_PREFIX)) {
|
||||
$featureFlags &= ~Bot::FEATURE_WEBHOOK;
|
||||
}
|
||||
} elseif (str_starts_with($url, Bot::URL_APP_PREFIX)) {
|
||||
$featureFlags = Bot::FEATURE_EVENT;
|
||||
} else {
|
||||
$featureFlags = Bot::FEATURE_WEBHOOK + Bot::FEATURE_RESPONSE;
|
||||
}
|
||||
|
||||
if ($featureFlags & Bot::FEATURE_EVENT
|
||||
&& ($featureFlags & Bot::FEATURE_WEBHOOK
|
||||
|| $featureFlags & Bot::FEATURE_RESPONSE
|
||||
|| $featureFlags & Bot::FEATURE_REACTION)) {
|
||||
$output->writeln('<error>Bots with feature "event" can not support "webhook", "response" or "reaction" feature. They are mutual exclusive</error>');
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->botService->validateBotParameters($name, $secret, $url, $description);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$output->writeln('<error>' . $e->getMessage() . '</error>');
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->botServerMapper->findByUrl($url);
|
||||
$output->writeln('<error>Bot with the same URL is already registered</error>');
|
||||
return 2;
|
||||
} catch (DoesNotExistException) {
|
||||
}
|
||||
|
||||
$bot = new BotServer();
|
||||
$bot->setName($name);
|
||||
$bot->setSecret($secret);
|
||||
$bot->setUrl($url);
|
||||
$bot->setUrlHash(sha1($url));
|
||||
$bot->setDescription($description);
|
||||
$bot->setState($noSetup ? Bot::STATE_NO_SETUP : Bot::STATE_ENABLED);
|
||||
$bot->setFeatures($featureFlags);
|
||||
try {
|
||||
$botEntity = $this->botServerMapper->insert($bot);
|
||||
} catch (\Exception $e) {
|
||||
if ($e instanceof Exception && $e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
||||
$output->writeln('<error>Bot with the same secret is already registered</error>');
|
||||
return 3;
|
||||
} else {
|
||||
$output->writeln('<error>' . get_class($e) . ': ' . $e->getMessage() . '</error>');
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
$output->writeln('<info>Bot installed</info>');
|
||||
$output->writeln('ID: ' . $botEntity->getId());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\Talk\Command\Bot;
|
||||
|
||||
use OC\Core\Command\Base;
|
||||
use OCA\Talk\Model\Bot;
|
||||
use OCA\Talk\Model\BotConversation;
|
||||
use OCA\Talk\Model\BotConversationMapper;
|
||||
use OCA\Talk\Model\BotServerMapper;
|
||||
use OCA\Talk\Service\BotService;
|
||||
use OCP\App\IAppManager;
|
||||
use OCP\AppFramework\Utility\ITimeFactory;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class ListBots extends Base {
|
||||
public function __construct(
|
||||
private BotConversationMapper $botConversationMapper,
|
||||
private BotServerMapper $botServerMapper,
|
||||
private BotService $botService,
|
||||
private IAppManager $appManager,
|
||||
private ITimeFactory $timeFactory,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function configure(): void {
|
||||
parent::configure();
|
||||
$this
|
||||
->setName('talk:bot:list')
|
||||
->setDescription('List all installed bots of the server or a conversation')
|
||||
->addArgument(
|
||||
'token',
|
||||
InputArgument::OPTIONAL,
|
||||
'Conversation token to limit the bot list for'
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$bots = $this->botServerMapper->getAllBots();
|
||||
$token = $input->getArgument('token');
|
||||
|
||||
if ($token) {
|
||||
$botIds = array_map(static function (BotConversation $bot): int {
|
||||
return $bot->getBotId();
|
||||
}, $this->botConversationMapper->findForToken($token));
|
||||
}
|
||||
|
||||
$data = [];
|
||||
foreach ($bots as $bot) {
|
||||
if ($token && !in_array($bot->getId(), $botIds, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$botData = $bot->jsonSerialize();
|
||||
$botData['features'] = Bot::featureFlagsToLabels($botData['features']);
|
||||
|
||||
if (!$this->botService->isAppForBotEnabled($bot)) {
|
||||
$botData['state'] = Bot::STATE_UNAVAILABLE;
|
||||
if ($input->getOption('output') === 'plain') {
|
||||
$botData['error_count'] = '<error>' . 1 . '</error>';
|
||||
} else {
|
||||
$botData['error_count'] = 1;
|
||||
}
|
||||
$botData['last_error_date'] = $this->timeFactory->getTime();
|
||||
if ($input->getOption('output') === 'plain') {
|
||||
$botData['last_error_message'] = '<error>App disabled</error>';
|
||||
} else {
|
||||
$botData['last_error_message'] = 'App disabled';
|
||||
}
|
||||
}
|
||||
|
||||
if (!$output->isVerbose()) {
|
||||
unset($botData['url']);
|
||||
unset($botData['url_hash']);
|
||||
unset($botData['secret']);
|
||||
unset($botData['last_error_date']);
|
||||
unset($botData['last_error_message']);
|
||||
}
|
||||
|
||||
$data[] = $botData;
|
||||
}
|
||||
|
||||
$this->writeTableInOutputFormat($input, $output, $data);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\Talk\Command\Bot;
|
||||
|
||||
use OC\Core\Command\Base;
|
||||
use OCA\Talk\Events\BotDisabledEvent;
|
||||
use OCA\Talk\Exceptions\RoomNotFoundException;
|
||||
use OCA\Talk\Manager;
|
||||
use OCA\Talk\Model\BotConversationMapper;
|
||||
use OCA\Talk\Model\BotServerMapper;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\EventDispatcher\IEventDispatcher;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class Remove extends Base {
|
||||
public function __construct(
|
||||
private BotConversationMapper $botConversationMapper,
|
||||
private BotServerMapper $botServerMapper,
|
||||
private IEventDispatcher $dispatcher,
|
||||
private Manager $roomManager,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function configure(): void {
|
||||
parent::configure();
|
||||
$this
|
||||
->setName('talk:bot:remove')
|
||||
->setDescription('Remove a bot from a conversation')
|
||||
->addArgument(
|
||||
'bot-id',
|
||||
InputArgument::REQUIRED,
|
||||
'The ID of the bot to remove in a conversation'
|
||||
)
|
||||
->addArgument(
|
||||
'token',
|
||||
InputArgument::IS_ARRAY,
|
||||
'Conversation tokens to remove bot up for'
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$botId = (int)$input->getArgument('bot-id');
|
||||
$tokens = $input->getArgument('token');
|
||||
|
||||
try {
|
||||
$botServer = $this->botServerMapper->findById($botId);
|
||||
} catch (DoesNotExistException) {
|
||||
$output->writeln('<error>Bot could not be found by id: ' . $botId . '</error>');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$this->botConversationMapper->deleteByBotIdAndTokens($botId, $tokens);
|
||||
$output->writeln('<info>Remove bot from given conversations</info>');
|
||||
|
||||
foreach ($tokens as $token) {
|
||||
try {
|
||||
$room = $this->roomManager->getRoomByToken($token);
|
||||
} catch (RoomNotFoundException) {
|
||||
continue;
|
||||
}
|
||||
$event = new BotDisabledEvent($room, $botServer);
|
||||
$this->dispatcher->dispatchTyped($event);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\Talk\Command\Bot;
|
||||
|
||||
use OC\Core\Command\Base;
|
||||
use OCA\Talk\Events\BotEnabledEvent;
|
||||
use OCA\Talk\Exceptions\RoomNotFoundException;
|
||||
use OCA\Talk\Manager;
|
||||
use OCA\Talk\Model\Bot;
|
||||
use OCA\Talk\Model\BotConversation;
|
||||
use OCA\Talk\Model\BotConversationMapper;
|
||||
use OCA\Talk\Model\BotServerMapper;
|
||||
use OCA\Talk\Service\BotService;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\DB\Exception;
|
||||
use OCP\EventDispatcher\IEventDispatcher;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class Setup extends Base {
|
||||
public function __construct(
|
||||
private Manager $roomManager,
|
||||
private BotServerMapper $botServerMapper,
|
||||
private BotConversationMapper $botConversationMapper,
|
||||
private BotService $botService,
|
||||
private IEventDispatcher $dispatcher,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function configure(): void {
|
||||
parent::configure();
|
||||
$this
|
||||
->setName('talk:bot:setup')
|
||||
->setDescription('Add a bot to a conversation')
|
||||
->addArgument(
|
||||
'bot-id',
|
||||
InputArgument::REQUIRED,
|
||||
'The ID of the bot to set up in a conversation'
|
||||
)
|
||||
->addArgument(
|
||||
'token',
|
||||
InputArgument::IS_ARRAY,
|
||||
'Conversation tokens to set the bot up for'
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$botId = (int)$input->getArgument('bot-id');
|
||||
$tokens = $input->getArgument('token');
|
||||
|
||||
try {
|
||||
$botServer = $this->botServerMapper->findById($botId);
|
||||
} catch (DoesNotExistException) {
|
||||
$output->writeln('<error>Bot could not be found by id: ' . $botId . '</error>');
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!$this->botService->isAppForBotEnabled($botServer)) {
|
||||
$output->writeln('<error>Bot app is disabled: ' . $botServer->getUrl() . '</error>');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$returnCode = 0;
|
||||
foreach ($tokens as $token) {
|
||||
try {
|
||||
$room = $this->roomManager->getRoomByToken($token);
|
||||
|
||||
if ($room->isFederatedConversation()) {
|
||||
$output->writeln('<error>Federated conversations can not have bots: ' . $token . '</error>');
|
||||
$returnCode = 2;
|
||||
continue;
|
||||
}
|
||||
} catch (RoomNotFoundException) {
|
||||
$output->writeln('<error>Conversation could not be found by token: ' . $token . '</error>');
|
||||
$returnCode = 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
$bot = new BotConversation();
|
||||
$bot->setBotId($botId);
|
||||
$bot->setToken($token);
|
||||
$bot->setState(Bot::STATE_ENABLED);
|
||||
|
||||
try {
|
||||
$this->botConversationMapper->insert($bot);
|
||||
$output->writeln('<info>Successfully set up for conversation ' . $token . '</info>');
|
||||
|
||||
$event = new BotEnabledEvent($room, $botServer);
|
||||
$this->dispatcher->dispatchTyped($event);
|
||||
} catch (\Exception $e) {
|
||||
if ($e instanceof Exception && $e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
||||
$output->writeln('<error>Bot is already set up for the conversation ' . $token . '</error>');
|
||||
$returnCode = 3;
|
||||
} else {
|
||||
$output->writeln('<error>' . get_class($e) . ': ' . $e->getMessage() . '</error>');
|
||||
$returnCode = 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $returnCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\Talk\Command\Bot;
|
||||
|
||||
use OC\Core\Command\Base;
|
||||
use OCA\Talk\Model\Bot;
|
||||
use OCA\Talk\Model\BotServerMapper;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class State extends Base {
|
||||
public function __construct(
|
||||
private BotServerMapper $botServerMapper,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function configure(): void {
|
||||
parent::configure();
|
||||
$this
|
||||
->setName('talk:bot:state')
|
||||
->setDescription('Change the state or feature list for a bot')
|
||||
->addArgument(
|
||||
'bot-id',
|
||||
InputArgument::REQUIRED,
|
||||
'Bot ID to change the state for'
|
||||
)
|
||||
->addArgument(
|
||||
'state',
|
||||
InputArgument::REQUIRED,
|
||||
'New state for the bot (0 = disabled, 1 = enabled, 2 = no setup via GUI)'
|
||||
)
|
||||
->addOption(
|
||||
'feature',
|
||||
'f',
|
||||
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
|
||||
'Specify the list of features for the bot' . "\n"
|
||||
. ' - webhook: The bot receives posted chat messages as webhooks' . "\n"
|
||||
. ' - response: The bot can post messages and reactions as a response' . "\n"
|
||||
. ' - event: The bot reads posted messages from local events' . "\n"
|
||||
. ' - reaction: The bot is notified about adding and removing of reactions' . "\n"
|
||||
. ' - none: When all features should be disabled for the bot'
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$botId = (int)$input->getArgument('bot-id');
|
||||
$state = (int)$input->getArgument('state');
|
||||
|
||||
$featureFlags = null;
|
||||
if (!empty($input->getOption('feature'))) {
|
||||
$featureFlags = Bot::featureLabelsToFlags($input->getOption('feature'));
|
||||
}
|
||||
|
||||
if (!in_array($state, [Bot::STATE_DISABLED, Bot::STATE_ENABLED, Bot::STATE_NO_SETUP], true)) {
|
||||
$output->writeln('<error>Provided state is invalid</error>');
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
$bot = $this->botServerMapper->findById($botId);
|
||||
} catch (DoesNotExistException) {
|
||||
$output->writeln('<error>Bot could not be found by id: ' . $botId . '</error>');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$bot->setState($state);
|
||||
if ($featureFlags !== null) {
|
||||
if (str_starts_with($bot->getUrl(), Bot::URL_RESPONSE_ONLY_PREFIX)) {
|
||||
$output->writeln('<error>Feature flags of response-only bots cannot be changed</error>');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$bot->setFeatures($featureFlags);
|
||||
}
|
||||
$this->botServerMapper->update($bot);
|
||||
|
||||
if ($featureFlags !== null) {
|
||||
$output->writeln('<info>Bot state set to ' . $state . ' with features: ' . Bot::featureFlagsToLabels($featureFlags) . '</info>');
|
||||
} else {
|
||||
$output->writeln('<info>Bot state set to ' . $state . '</info>');
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\Talk\Command\Bot;
|
||||
|
||||
use OC\Core\Command\Base;
|
||||
use OCA\Talk\Model\BotConversationMapper;
|
||||
use OCA\Talk\Model\BotServerMapper;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class Uninstall extends Base {
|
||||
public function __construct(
|
||||
private BotConversationMapper $botConversationMapper,
|
||||
private BotServerMapper $botServerMapper,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function configure(): void {
|
||||
parent::configure();
|
||||
$this
|
||||
->setName('talk:bot:uninstall')
|
||||
->setDescription('Uninstall a bot from the server')
|
||||
->addArgument(
|
||||
'id',
|
||||
InputArgument::OPTIONAL,
|
||||
'The ID of the bot'
|
||||
)
|
||||
->addOption(
|
||||
'url',
|
||||
null,
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'The URL of the bot (required when no ID is given, ignored otherwise)'
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$botId = (int)$input->getArgument('id');
|
||||
|
||||
try {
|
||||
if ($botId === 0) {
|
||||
$url = $input->getOption('url');
|
||||
if ($url === null) {
|
||||
$output->writeln('<error>URL is required when no ID is given</error>');
|
||||
return 1;
|
||||
}
|
||||
$bot = $this->botServerMapper->findByUrl($url);
|
||||
} else {
|
||||
$bot = $this->botServerMapper->findById($botId);
|
||||
}
|
||||
} catch (DoesNotExistException) {
|
||||
$output->writeln('<error>Bot not found</error>');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$this->botConversationMapper->deleteByBotId($bot->getId());
|
||||
$this->botServerMapper->deleteById($bot->getId());
|
||||
|
||||
$output->writeln('<info>Bot uninstalled</info>');
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user