|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Assistant\Migration; |
| 11 | + |
| 12 | +use Closure; |
| 13 | +use OCA\Assistant\Service\ChatService; |
| 14 | +use OCA\Assistant\Service\InternalException; |
| 15 | +use OCA\Assistant\Service\SessionSummaryService; |
| 16 | +use OCP\DB\ISchemaWrapper; |
| 17 | +use OCP\IDBConnection; |
| 18 | +use OCP\IUserManager; |
| 19 | +use OCP\Migration\IOutput; |
| 20 | +use OCP\Migration\SimpleMigrationStep; |
| 21 | +use Override; |
| 22 | +use Psr\Log\LoggerInterface; |
| 23 | + |
| 24 | +class Version030500Date20260715083046 extends SimpleMigrationStep { |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + private IDBConnection $db, |
| 28 | + private IUserManager $userManager, |
| 29 | + private ChatService $chatService, |
| 30 | + private SessionSummaryService $sessionSummaryService, |
| 31 | + private LoggerInterface $logger, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Clean up chat and assignment data left behind by users deleted before UserDeletedListener existed. |
| 37 | + * |
| 38 | + * @param IOutput $output |
| 39 | + * @param Closure(): ISchemaWrapper $schemaClosure |
| 40 | + * @param array $options |
| 41 | + */ |
| 42 | + #[Override] |
| 43 | + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { |
| 44 | + $userIds = $this->getDistinctUserIds(); |
| 45 | + $cleaned = 0; |
| 46 | + |
| 47 | + foreach ($userIds as $userId) { |
| 48 | + if ($this->userManager->userExists($userId)) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + $this->chatService->deleteAllUserChatData($userId); |
| 54 | + } catch (InternalException $e) { |
| 55 | + $this->logger->error('Error while deleting chat data for deleted user ' . $userId, ['exception' => $e]); |
| 56 | + } |
| 57 | + |
| 58 | + try { |
| 59 | + $this->sessionSummaryService->deleteSummaryJobsForUser($userId); |
| 60 | + } catch (InternalException $e) { |
| 61 | + $this->logger->error('Error while deleting summary jobs for deleted user ' . $userId, ['exception' => $e]); |
| 62 | + } |
| 63 | + |
| 64 | + $cleaned++; |
| 65 | + } |
| 66 | + |
| 67 | + if ($cleaned > 0) { |
| 68 | + $output->info('Cleaned up assistant data for ' . $cleaned . ' deleted user(s)'); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return array<int, mixed> |
| 74 | + */ |
| 75 | + private function getDistinctUserIds(): array { |
| 76 | + $userIds = []; |
| 77 | + |
| 78 | + $userIdsChat = []; |
| 79 | + $userIdsAssignments = []; |
| 80 | + |
| 81 | + if ($this->db->tableExists('assistant_chat_sns')) { |
| 82 | + $qb = $this->db->getQueryBuilder(); |
| 83 | + $qb->selectDistinct('user_id') |
| 84 | + ->from('assistant_chat_sns'); |
| 85 | + $result = $qb->executeQuery(); |
| 86 | + $userIdsChat = $result->fetchFirstColumn(); |
| 87 | + $result->closeCursor(); |
| 88 | + } |
| 89 | + |
| 90 | + $userIds = array_unique(array_merge($userIdsChat, $userIdsAssignments)); |
| 91 | + return $userIds; |
| 92 | + } |
| 93 | +} |
0 commit comments