Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OCA\Assistant\Listener\TaskSuccessfulListener;
use OCA\Assistant\Listener\Text2Image\Text2ImageReferenceListener;
use OCA\Assistant\Listener\Text2Image\Text2StickerListener;
use OCA\Assistant\Listener\UserDeletedListener;
use OCA\Assistant\Notification\Notifier;
use OCA\Assistant\Reference\FreePromptReferenceProvider;
use OCA\Assistant\Reference\SpeechToTextReferenceProvider;
Expand All @@ -48,6 +49,7 @@
use OCP\TaskProcessing\Events\TaskFailedEvent;
use OCP\TaskProcessing\Events\TaskSuccessfulEvent;
use OCP\TaskProcessing\IManager;
use OCP\User\Events\UserDeletedEvent;

class Application extends App implements IBootstrap {

Expand Down Expand Up @@ -102,6 +104,8 @@ public function register(IRegistrationContext $context): void {

$context->registerEventListener(AddContentSecurityPolicyEvent::class, CSPListener::class);

$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);

if (class_exists('OCP\\TaskProcessing\\TaskTypes\\AudioToAudioChat')) {
$context->registerTaskProcessingProvider(AudioToAudioChatProvider::class);
}
Expand Down
11 changes: 11 additions & 0 deletions lib/Db/AssignmentMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,15 @@ public function findDueAssignmentsForUser(string $userId): \Generator {
yield $assignment;
}
}

/**
* @throws \OCP\DB\Exception
*/
public function deleteAllForUser(string $userId): void {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)));

$qb->executeStatement();
}
}
24 changes: 24 additions & 0 deletions lib/Db/ChattyLLM/SessionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ public function getUserSessionForAssignment(string $userId, int $assignmentId):
return $this->findEntity($qb);
}

/**
* @return \Generator<array-key, Session>
* @throws \OCP\DB\Exception
*/
public function getAllUserSessions(string $userId): \Generator {
$qb = $this->db->getQueryBuilder();
$qb->select(Session::$columns)
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)));

yield from $this->yieldEntities($qb);
}

/**
* @param string $userId
* @param bool $isAssignment
Expand Down Expand Up @@ -205,6 +218,17 @@ public function deleteSession(string $userId, int $sessionId) {
$qb->executeStatement();
}

/**
* @throws \OCP\DB\Exception
*/
public function deleteAllSessionsForUser(string $userId): void {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId, IQueryBuilder::PARAM_STR)));

$qb->executeStatement();
}

public function updateSessionIsRemembered(?string $userId, int $sessionId, bool $is_remembered) {
$session = $this->getUserSession($userId, $sessionId);
$session->setIsRemembered($is_remembered);
Expand Down
51 changes: 51 additions & 0 deletions lib/Listener/UserDeletedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Assistant\Listener;

use OCA\Assistant\Service\AssignmentsService;
use OCA\Assistant\Service\ChatService;
use OCA\Assistant\Service\InternalException;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\User\Events\UserDeletedEvent;
use Psr\Log\LoggerInterface;

/**
* @template-implements IEventListener<UserDeletedEvent>
*/
class UserDeletedListener implements IEventListener {

public function __construct(
private ChatService $chatService,
private AssignmentsService $assignmentsService,
private LoggerInterface $logger,
) {
}

public function handle(Event $event): void {
if (!($event instanceof UserDeletedEvent)) {
return;
}

$userId = $event->getUid();

try {
$this->assignmentsService->deleteAllForUser($userId);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't cleanup the jobs created by SessionSummaryService. I think we should check if there are some jobs scheduled for the user and remove them from the bg job queue.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a new commit that does both.

} catch (InternalException $e) {
$this->logger->error('Error while deleting assignments for user ' . $userId, ['exception' => $e]);
}

try {
$this->chatService->deleteAllUserChatData($userId);
} catch (InternalException $e) {
$this->logger->error('Error while deleting chat data for user ' . $userId, ['exception' => $e]);
}
}
}
20 changes: 20 additions & 0 deletions lib/Service/AssignmentsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OCP\DB\Exception;
use OCP\IDateTimeZone;
use OCP\IL10N;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;

class AssignmentsService {
Expand All @@ -33,6 +34,7 @@ public function __construct(
private IJobList $jobList,
private IL10N $l10n,
private IDateTimeZone $dateTimeZone,
private IUserManager $userManager,
) {
}

Expand Down Expand Up @@ -84,13 +86,31 @@ public function createAssignment(?string $userId, string $title, string $prompt,
return $assignment;
}

/**
* @throws InternalException
*/
public function deleteAllForUser(string $userId): void {
try {
$this->assignmentMapper->deleteAllForUser($userId);
} catch (Exception $e) {
throw new InternalException(previous: $e);
}
if ($this->jobList->has(RunAssignmentsJob::class, ['userId' => $userId])) {
$this->jobList->remove(RunAssignmentsJob::class, ['userId' => $userId]);
}
}

/**
* @throws InternalException|UnauthorizedException
*/
public function runDueAssignmentsForUser(?string $userId): void {
if ($userId === null) {
throw new UnauthorizedException();
}
if ($this->userManager->get($userId) === null) {
$this->deleteAllForUser($userId);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also clean everything up here? The chat sessions+messages and the summary bg jobs?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean, they should already be cleaned up via listener, right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If some users where deleted before this PR, there can still be data hanging around.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, but maybe that should be a migration step and not solved here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, a repair step seems appropriate.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A repair step makes sense for this.

return;
}
try {
foreach ($this->assignmentMapper->findDueAssignmentsForUser($userId) as $assignment) {
if ($assignment === null) {
Expand Down
15 changes: 15 additions & 0 deletions lib/Service/ChatService.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,21 @@ public function deleteSession(?string $userId, int $sessionId): void {
}
}

/**
* @throws InternalException
*/
public function deleteAllUserChatData(string $userId): void {
try {
$sessions = $this->sessionMapper->getAllUserSessions($userId);
foreach ($sessions as $session) {
$this->messageMapper->deleteMessagesBySession($session->getId());
}
$this->sessionMapper->deleteAllSessionsForUser($userId);
} catch (Exception|\RuntimeException $e) {
throw new InternalException(previous: $e);
}
}

/**
* @return list<Session>
* @throws InternalException
Expand Down
Loading