summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2023-07-27 09:25:53 +0200
committerJoas Schilling <coding@schilljs.com>2023-08-08 10:46:55 +0200
commitaa320083a6341a5db98d6335977e1b7bd70bae17 (patch)
treef4981b45bd13d6275a3e77637cc2b69291e667d3
parentbf86dcab036262db25f6069a7f7d3170d30bf8b6 (diff)
fix(bots): Update name, description and state when reinstalling only
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r--lib/Listener/BotListener.php14
-rw-r--r--lib/Model/BotServerMapper.php15
2 files changed, 29 insertions, 0 deletions
diff --git a/lib/Listener/BotListener.php b/lib/Listener/BotListener.php
index 7a56e976e..c6e73f509 100644
--- a/lib/Listener/BotListener.php
+++ b/lib/Listener/BotListener.php
@@ -35,6 +35,7 @@ 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\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
@@ -75,8 +76,21 @@ class BotListener implements IEventListener {
public function handle(Event $event): void {
if ($event instanceof BotInstallEvent) {
+ $this->handleBotInstallEvent($event);
+ }
+ }
+
+ protected function handleBotInstallEvent(BotInstallEvent $event): void {
+ try {
+ $bot = $this->botServerMapper->findByUrlAndSecret($event->getUrl(), $event->getSecret());
+
+ $bot->setName($event->getName());
+ $bot->setDescription($event->getDescription());
+ $this->botServerMapper->update($bot);
+ } catch (DoesNotExistException) {
$bot = new BotServer();
$bot->setName($event->getName());
+ $bot->setDescription($event->getDescription());
$bot->setSecret($event->getSecret());
$bot->setUrl($event->getUrl());
$bot->setUrlHash(sha1($event->getUrl()));
diff --git a/lib/Model/BotServerMapper.php b/lib/Model/BotServerMapper.php
index 66cbbea42..caee0e779 100644
--- a/lib/Model/BotServerMapper.php
+++ b/lib/Model/BotServerMapper.php
@@ -56,6 +56,21 @@ class BotServerMapper extends QBMapper {
return $this->findEntity($query);
}
+ /**
+ * @throws DoesNotExistException
+ */
+ public function findByUrlAndSecret(string $url, string $secret): BotServer {
+ $urlHash = sha1($url);
+
+ $query = $this->db->getQueryBuilder();
+ $query->select('*')
+ ->from($this->getTableName())
+ ->where($query->expr()->eq('url_hash', $query->createNamedParameter($urlHash)))
+ ->andWhere($query->expr()->eq('secret', $query->createNamedParameter($secret)));
+
+ return $this->findEntity($query);
+ }
+
public function deleteById(int $botId): int {
$query = $this->db->getQueryBuilder();
$query->delete($this->getTableName())