Skip to content

Commit

Permalink
fix(bots): Fix default and restrict features of app bots when install…
Browse files Browse the repository at this point in the history
…ing with event

Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen authored and backportbot[bot] committed Jan 27, 2025
1 parent ff00e8c commit b9209ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/Events/BotInstallEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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;
}
}
7 changes: 6 additions & 1 deletion lib/Listener/BotListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,19 @@ 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());
$bot->setSecret($event->getSecret());
$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);
}
}
Expand Down

0 comments on commit b9209ee

Please sign in to comment.