diff options
author | Joas Schilling <coding@schilljs.com> | 2023-08-21 12:11:33 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2023-08-21 13:12:55 +0200 |
commit | dc7064bb4ee5f025f76cf385adf5d2a683a02294 (patch) | |
tree | 48b2b42c230674864ddbf265000e733d67415586 /lib/Command | |
parent | a632fd395bbba8cfb2e35efac2e165820925e814 (diff) |
feat(bots): Add methods to handle the features in one place only
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/Command')
-rw-r--r-- | lib/Command/Bot/Install.php | 16 | ||||
-rw-r--r-- | lib/Command/Bot/ListBots.php | 2 | ||||
-rw-r--r-- | lib/Command/Bot/State.php | 26 |
3 files changed, 25 insertions, 19 deletions
diff --git a/lib/Command/Bot/Install.php b/lib/Command/Bot/Install.php index 1f0ba5160..9d6fa21ac 100644 --- a/lib/Command/Bot/Install.php +++ b/lib/Command/Bot/Install.php @@ -72,6 +72,15 @@ class Install extends Base { InputOption::VALUE_NONE, 'Prevent moderators from setting up the bot in a conversation' ) + ->addOption( + 'features', + '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" + . ' - none: When all features should be disabled for the bot' + ) ; } @@ -82,6 +91,12 @@ class Install extends Base { $description = $input->getArgument('description'); $noSetup = $input->getOption('no-setup'); + if (!empty($input->getOption('feature'))) { + $featureFlags = Bot::featureLabelsToFlags($input->getOption('feature')); + } else { + $featureFlags = Bot::FEATURE_WEBHOOK + Bot::FEATURE_RESPONSE; + } + $bot = new BotServer(); $bot->setName($name); $bot->setSecret($secret); @@ -89,6 +104,7 @@ class Install extends Base { $bot->setUrlHash(sha1($url)); $bot->setDescription($description); $bot->setState($noSetup ? Bot::STATE_NO_SETUP : Bot::STATE_ENABLED); + $bot->setFeatures($featureFlags); try { $this->botServerMapper->insert($bot); } catch (\Exception $e) { diff --git a/lib/Command/Bot/ListBots.php b/lib/Command/Bot/ListBots.php index 4f5251c93..2719ac46e 100644 --- a/lib/Command/Bot/ListBots.php +++ b/lib/Command/Bot/ListBots.php @@ -26,6 +26,7 @@ declare(strict_types=1); 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; @@ -71,6 +72,7 @@ class ListBots extends Base { } $botData = $bot->jsonSerialize(); + $botData['features'] = Bot::featureFlagsToLabels($botData['features']); if (!$output->isVerbose()) { unset($botData['url']); diff --git a/lib/Command/Bot/State.php b/lib/Command/Bot/State.php index 9ae319087..66b605085 100644 --- a/lib/Command/Bot/State.php +++ b/lib/Command/Bot/State.php @@ -71,26 +71,10 @@ class State extends Base { protected function execute(InputInterface $input, OutputInterface $output): int { $botId = (int)$input->getArgument('bot-id'); $state = (int)$input->getArgument('state'); - $features = $input->getOption('feature'); $featureFlags = null; - $clearFeatures = false; - foreach ($features as $feature) { - if ($feature === 'webhook') { - $featureFlags += Bot::FEATURE_WEBHOOK; - } elseif ($feature === 'response') { - $featureFlags += Bot::FEATURE_RESPONSE; - } elseif ($feature === 'none') { - $clearFeatures = true; - } else { - $output->writeln('<error>Feature "' . $feature . '" is not known for bots</error>'); - return 1; - } - } - - if ($clearFeatures) { - $featureFlags = Bot::FEATURE_NONE; - $features = ['none']; + 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)) { @@ -111,7 +95,11 @@ class State extends Base { } $this->botServerMapper->update($bot); - $output->writeln('<info>Bot state set to ' . $state . ' with features: ' . implode(', ', $features) . '</info>'); + 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; } } |