diff --git a/lib/Command/Bot/Install.php b/lib/Command/Bot/Install.php index 4aed46e14ba..a36e2d54ee2 100644 --- a/lib/Command/Bot/Install.php +++ b/lib/Command/Bot/Install.php @@ -81,10 +81,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int if (!empty($input->getOption('feature'))) { $featureFlags = Bot::featureLabelsToFlags($input->getOption('feature')); - if (str_starts_with($url, 'nextcloudapp://')) { + if (str_starts_with($url, Bot::URL_APP_PREFIX)) { $featureFlags &= ~Bot::FEATURE_WEBHOOK; } - } elseif (str_starts_with($url, 'nextcloudapp://')) { + } elseif (str_starts_with($url, Bot::URL_APP_PREFIX)) { $featureFlags = Bot::FEATURE_EVENT; } else { $featureFlags = Bot::FEATURE_WEBHOOK + Bot::FEATURE_RESPONSE; diff --git a/lib/Events/BotInstallEvent.php b/lib/Events/BotInstallEvent.php index 9d305730486..7bdd4d7cc84 100644 --- a/lib/Events/BotInstallEvent.php +++ b/lib/Events/BotInstallEvent.php @@ -17,7 +17,7 @@ public function __construct( protected string $secret, protected string $url, protected string $description = '', - protected int $features = Bot::FEATURE_WEBHOOK | Bot::FEATURE_RESPONSE, + protected ?int $features = null, ) { parent::__construct(); } @@ -39,6 +39,12 @@ public function getDescription(): string { } public function getFeatures(): int { - return $this->features; + if ($this->features !== null) { + return $this->features; + } + if (str_starts_with($this->url, Bot::URL_APP_PREFIX)) { + return Bot::FEATURE_EVENT; + } + return Bot::FEATURE_WEBHOOK | Bot::FEATURE_RESPONSE; } } diff --git a/lib/Listener/BotListener.php b/lib/Listener/BotListener.php index d08c64a4f0b..f1fa92ee454 100644 --- a/lib/Listener/BotListener.php +++ b/lib/Listener/BotListener.php @@ -93,6 +93,11 @@ protected function handleBotInstallEvent(BotInstallEvent $event): void { } catch (DoesNotExistException) { } + $features = $event->getFeatures(); + if (str_starts_with($event->getUrl(), Bot::URL_APP_PREFIX)) { + $features &= ~Bot::FEATURE_WEBHOOK; + } + $bot = new BotServer(); $bot->setName($event->getName()); $bot->setDescription($event->getDescription()); @@ -100,7 +105,7 @@ protected function handleBotInstallEvent(BotInstallEvent $event): void { $bot->setUrl($event->getUrl()); $bot->setUrlHash(sha1($event->getUrl())); $bot->setState(Bot::STATE_ENABLED); - $bot->setFeatures($event->getFeatures()); + $bot->setFeatures($features); $this->botServerMapper->insert($bot); } } diff --git a/lib/Model/Bot.php b/lib/Model/Bot.php index ce900fc32d6..c035fc29b1c 100644 --- a/lib/Model/Bot.php +++ b/lib/Model/Bot.php @@ -23,6 +23,7 @@ class Bot { public const FEATURE_LABEL_WEBHOOK = 'webhook'; public const FEATURE_LABEL_RESPONSE = 'response'; public const FEATURE_LABEL_EVENT = 'event'; + public const URL_APP_PREFIX = 'nextcloudapp://'; public const FEATURE_MAP = [ self::FEATURE_NONE => self::FEATURE_LABEL_NONE, diff --git a/lib/Service/BotService.php b/lib/Service/BotService.php index 0725c042aed..ba3f0e85684 100644 --- a/lib/Service/BotService.php +++ b/lib/Service/BotService.php @@ -406,7 +406,7 @@ public function validateBotParameters(string $name, string $secret, string $url, throw new \InvalidArgumentException('The provided secret is too short (min. 40 chars, max. 128 chars)'); } - if (!$url || strlen($url) > 4000 || !(str_starts_with($url, 'http://') || str_starts_with($url, 'https://') || str_starts_with($url, 'nextcloudapp://'))) { + if (!$url || strlen($url) > 4000 || !(str_starts_with($url, 'http://') || str_starts_with($url, 'https://') || str_starts_with($url, Bot::URL_APP_PREFIX))) { throw new \InvalidArgumentException('The provided URL is not a valid URL'); }