Skip to content

Commit 48703bc

Browse files
committed
clarify message mapper, use sessionId in deleteMessageById and getMessageById
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 7bdbfe7 commit 48703bc

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

lib/Controller/ChattyLLMController.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,7 @@ public function getMessage(int $sessionId, int $messageId): JSONResponse {
402402
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
403403
}
404404

405-
$message = $this->messageMapper->getMessageById($messageId);
406-
if ($message->getSessionId() !== $sessionId) {
407-
return new JSONResponse(['error' => $this->l10n->t('Message not found')], Http::STATUS_NOT_FOUND);
408-
}
405+
$message = $this->messageMapper->getMessageById($sessionId, $messageId);
409406

410407
return new JSONResponse($message->jsonSerialize());
411408
} catch (\OCP\DB\Exception $e) {
@@ -439,23 +436,19 @@ public function deleteMessage(int $messageId, int $sessionId): JSONResponse {
439436
if (!$sessionExists) {
440437
return new JSONResponse(['error' => $this->l10n->t('Session not found')], Http::STATUS_NOT_FOUND);
441438
}
442-
$message = $this->messageMapper->getMessageById($messageId);
443-
// otherwise one can delete a message from other sessions
444-
if ($message->getSessionId() !== $sessionId) {
445-
return new JSONResponse(['error' => $this->l10n->t('Message not found')], Http::STATUS_NOT_FOUND);
446-
}
439+
$message = $this->messageMapper->getMessageById($sessionId, $messageId);
440+
$ocpTaskId = $message->getOcpTaskId();
441+
442+
$this->messageMapper->deleteMessageById($sessionId, $messageId);
447443

448444
// delete the related task
449-
$ocpTaskId = $message->getOcpTaskId();
450445
if ($ocpTaskId !== 0) {
451446
try {
452447
$task = $this->taskProcessingManager->getTask($ocpTaskId);
453448
$this->taskProcessingManager->deleteTask($task);
454449
} catch (\OCP\TaskProcessing\Exception\Exception) {
455450
}
456451
}
457-
458-
$this->messageMapper->deleteMessageById($messageId);
459452
return new JSONResponse();
460453
} catch (\OCP\DB\Exception|\RuntimeException $e) {
461454
$this->logger->warning('Failed to delete a chat message', ['exception' => $e]);
@@ -633,7 +626,7 @@ public function regenerateForSession(int $sessionId, int $messageId): JSONRespon
633626
}
634627

635628
try {
636-
$this->messageMapper->deleteMessageById($messageId);
629+
$this->messageMapper->deleteMessageById($sessionId, $messageId);
637630
} catch (\OCP\DB\Exception|\RuntimeException $e) {
638631
$this->logger->warning('Failed to delete the last message', ['exception' => $e]);
639632
return new JSONResponse(['error' => $this->l10n->t('Failed to delete the last message')], Http::STATUS_INTERNAL_SERVER_ERROR);

lib/Db/ChattyLLM/MessageMapper.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCP\AppFramework\Db\DoesNotExistException;
1313
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
1414
use OCP\AppFramework\Db\QBMapper;
15+
use OCP\DB\Exception;
1516
use OCP\DB\QueryBuilder\IQueryBuilder;
1617
use OCP\IDBConnection;
1718

@@ -99,17 +100,19 @@ public function getMessages(int $sessionId, int $cursor, int $limit): array {
99100
}
100101

101102
/**
103+
* @param int $sessionId
102104
* @param integer $messageId
103105
* @return Message
104-
* @throws \OCP\DB\Exception
105-
* @throws MultipleObjectsReturnedException
106106
* @throws DoesNotExistException
107+
* @throws Exception
108+
* @throws MultipleObjectsReturnedException
107109
*/
108-
public function getMessageById(int $messageId): Message {
110+
public function getMessageById(int $sessionId, int $messageId): Message {
109111
$qb = $this->db->getQueryBuilder();
110112
$qb->select(Message::$columns)
111113
->from($this->getTableName())
112-
->where($qb->expr()->eq('id', $qb->createPositionalParameter($messageId, IQueryBuilder::PARAM_INT)));
114+
->where($qb->expr()->eq('id', $qb->createPositionalParameter($messageId, IQueryBuilder::PARAM_INT)))
115+
->andWhere($qb->expr()->eq('session_id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)));
113116

114117
return $this->findEntity($qb);
115118
}
@@ -147,15 +150,16 @@ public function deleteMessagesBySession(int $sessionId): void {
147150
}
148151

149152
/**
153+
* @param int $sessionId
150154
* @param integer $messageId
151-
* @throws \OCP\DB\Exception
152-
* @throws \RuntimeException
153155
* @return void
156+
* @throws Exception
154157
*/
155-
public function deleteMessageById(int $messageId): void {
158+
public function deleteMessageById(int $sessionId, int $messageId): void {
156159
$qb = $this->db->getQueryBuilder();
157160
$qb->delete($this->getTableName())
158-
->where($qb->expr()->eq('id', $qb->createPositionalParameter($messageId, IQueryBuilder::PARAM_INT)));
161+
->where($qb->expr()->eq('id', $qb->createPositionalParameter($messageId, IQueryBuilder::PARAM_INT)))
162+
->andWhere($qb->expr()->eq('session_id', $qb->createPositionalParameter($sessionId, IQueryBuilder::PARAM_INT)));
159163

160164
$qb->executeStatement();
161165
}

lib/Listener/ChattyLLMTaskListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function handle(Event $event): void {
9292
// now we have the transcription of the user audio input
9393
if (preg_match('/^chatty-llm:\d+:(\d+)$/', $customId, $matches)) {
9494
$queryMessageId = (int)$matches[1];
95-
$queryMessage = $this->messageMapper->getMessageById($queryMessageId);
95+
$queryMessage = $this->messageMapper->getMessageById($sessionId, $queryMessageId);
9696
$queryMessage->setContent(trim($taskOutput['input_transcript'] ?? ''));
9797
$this->messageMapper->update($queryMessage);
9898
// TODO update session title if it's the first message

0 commit comments

Comments
 (0)