Repository navigation
add tag-based indexing with allow and deny lists #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MichelleAntunes
wants to merge
1
commit into
nextcloud:main
Choose a base branch
from
MichelleAntunes:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\ContextChat\BackgroundJobs; | ||
|
|
||
| use OCA\ContextChat\Logger; | ||
| use OCA\ContextChat\Service\FsEventService; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\BackgroundJob\QueuedJob; | ||
| use OCP\Files\IRootFolder; | ||
| use OCP\SystemTag\ISystemTagObjectMapper; | ||
|
|
||
| /** | ||
| * An admin added a tag to, or removed a tag from, the allow or deny list. Only the | ||
| * files and folders that carry that tag can change status, so only those are | ||
| * looked at (a tag on a folder covers everything inside it). | ||
| * | ||
| * ACTION_INDEX: the tag no longer blocks, or now allows: queue what is eligible. | ||
| * ACTION_REMOVE: the tag now blocks, or no longer allows: remove from the index | ||
| * what is no longer eligible. | ||
| * | ||
| * Eligibility is decided with the lists as they are when the job runs. | ||
| */ | ||
| class TagListChangedJob extends QueuedJob { | ||
| public const ACTION_INDEX = 'index'; | ||
| public const ACTION_REMOVE = 'remove'; | ||
|
|
||
| public function __construct( | ||
| ITimeFactory $timeFactory, | ||
| private Logger $logger, | ||
| private IRootFolder $rootFolder, | ||
| private ISystemTagObjectMapper $tagMapper, | ||
| private FsEventService $fsEventService, | ||
| ) { | ||
| parent::__construct($timeFactory); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{tag_id:int|string, action:string} $argument | ||
| */ | ||
| protected function run($argument): void { | ||
| $tagId = (string)($argument['tag_id'] ?? ''); | ||
| $action = $argument['action'] ?? ''; | ||
| if ($tagId === '' || !in_array($action, [self::ACTION_INDEX, self::ACTION_REMOVE], true)) { | ||
| $this->logger->warning('[TagListChangedJob] Invalid arguments, nothing done'); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $objectIds = $this->tagMapper->getObjectIdsForTags([$tagId], 'files'); | ||
| } catch (\Throwable $e) { | ||
| // for example the tag was deleted in the meantime | ||
| $this->logger->warning('[TagListChangedJob] Cannot list files of tag ' . $tagId . ': ' . $e->getMessage(), ['exception' => $e]); | ||
| return; | ||
| } | ||
|
|
||
| foreach ($objectIds as $objectId) { | ||
| try { | ||
| $node = $this->rootFolder->getFirstNodeById((int)$objectId); | ||
| if ($node === null) { | ||
| continue; | ||
| } | ||
| if ($action === self::ACTION_INDEX) { | ||
| $this->fsEventService->onInsert($node); | ||
| } else { | ||
| $this->fsEventService->onTagRemoved($node); | ||
| } | ||
| } catch (\Throwable $e) { | ||
| $this->logger->warning('[TagListChangedJob] Failed for file id ' . $objectId . ': ' . $e->getMessage(), ['exception' => $e]); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\ContextChat\BackgroundJobs; | ||
|
|
||
| use OCA\ContextChat\AppInfo\Application; | ||
| use OCA\ContextChat\Logger; | ||
| use OCA\ContextChat\Notification\Notifier; | ||
| use OCP\AppFramework\Services\IAppConfig; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\BackgroundJob\QueuedJob; | ||
| use OCP\IUser; | ||
| use OCP\IUserManager; | ||
| use OCP\Notification\IManager; | ||
|
|
||
| /** | ||
| * Tells the users that the admin switched to indexing by tags, or takes that notice | ||
| * away again when the admin switches back to indexing everything. | ||
| * | ||
| * It runs in the background because there can be many users. The job looks at the | ||
| * mode as it is when it runs, so quick switches back and forth, and the order in which | ||
| * the jobs run, cannot leave a notice that no longer fits the mode. | ||
| */ | ||
| class TagModeNoticeJob extends QueuedJob { | ||
| public const ACTION_NOTIFY = 'notify'; | ||
| public const ACTION_DISMISS = 'dismiss'; | ||
|
|
||
| private const OBJECT_TYPE = 'tag_mode'; | ||
| private const OBJECT_ID = 'enabled'; | ||
|
|
||
| public function __construct( | ||
| ITimeFactory $timeFactory, | ||
| private Logger $logger, | ||
| private IAppConfig $appConfig, | ||
| private IUserManager $userManager, | ||
| private IManager $notificationManager, | ||
| ) { | ||
| parent::__construct($timeFactory); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{action:string, skip_user?:string|null} $argument | ||
| */ | ||
| protected function run($argument): void { | ||
| $action = $argument['action'] ?? ''; | ||
| if (!in_array($action, [self::ACTION_NOTIFY, self::ACTION_DISMISS], true)) { | ||
| $this->logger->warning('[TagModeNoticeJob] Invalid action, nothing done'); | ||
| return; | ||
| } | ||
|
|
||
| $tagMode = $this->appConfig->getAppValueString('index_mode', 'all', lazy: true) === 'tag_only'; | ||
| if (($action === self::ACTION_NOTIFY) !== $tagMode) { | ||
| // the mode changed again after this job was queued | ||
| return; | ||
| } | ||
|
|
||
| $skipUser = $argument['skip_user'] ?? null; | ||
| $count = 0; | ||
| $this->userManager->callForSeenUsers(function (IUser $user) use ($action, $skipUser, &$count): void { | ||
| if (!$user->isEnabled() || $user->getUID() === $skipUser) { | ||
| return; | ||
| } | ||
|
|
||
| $notification = $this->notificationManager->createNotification(); | ||
| $notification->setApp(Application::APP_ID) | ||
| ->setUser($user->getUID()) | ||
| ->setObject(self::OBJECT_TYPE, self::OBJECT_ID); | ||
| // never leave two of them for one user | ||
| $this->notificationManager->markProcessed($notification); | ||
|
|
||
| if ($action === self::ACTION_NOTIFY) { | ||
| $notification->setDateTime($this->time->getDateTime()) | ||
| ->setSubject(Notifier::SUBJECT_TAG_MODE_ENABLED); | ||
| $this->notificationManager->notify($notification); | ||
| } | ||
| $count++; | ||
| }); | ||
|
|
||
| $this->logger->info('[TagModeNoticeJob] ' . $action . ' done for ' . $count . ' users'); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\ContextChat\BackgroundJobs; | ||
|
|
||
| use OCA\ContextChat\Service\FsEventService; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\BackgroundJob\QueuedJob; | ||
| use OCP\Files\IRootFolder; | ||
|
|
||
| /** | ||
| * Runs after an allow or a deny tag was removed from a file or folder, once the | ||
| * tag rows are consistent again. | ||
| * - allow tag removed: removes from the index what is no longer eligible. | ||
| * - deny tag removed: queues for indexing what is now eligible. | ||
| */ | ||
| class TagRemovedJob extends QueuedJob { | ||
| public const KIND_ALLOW = 'allow'; | ||
| public const KIND_DENY = 'deny'; | ||
|
|
||
| public function __construct( | ||
| ITimeFactory $timeFactory, | ||
| private IRootFolder $rootFolder, | ||
| private FsEventService $fsEventService, | ||
| ) { | ||
| parent::__construct($timeFactory); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{file_id:int, kind?:string} $argument | ||
| */ | ||
| protected function run($argument): void { | ||
| $node = $this->rootFolder->getFirstNodeById($argument['file_id']); | ||
| if ($node === null) { | ||
| return; | ||
| } | ||
| if (($argument['kind'] ?? self::KIND_ALLOW) === self::KIND_DENY) { | ||
| $this->fsEventService->onInsert($node); | ||
| return; | ||
| } | ||
| $this->fsEventService->onTagRemoved($node); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\ContextChat\BackgroundJobs; | ||
|
|
||
| use OCA\ContextChat\Logger; | ||
| use OCA\ContextChat\Service\FsEventService; | ||
| use OCA\ContextChat\Service\IndexCompletionService; | ||
| use OCA\ContextChat\Service\StorageService; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\BackgroundJob\IJobList; | ||
| use OCP\BackgroundJob\QueuedJob; | ||
| use OCP\Files\IRootFolder; | ||
|
|
||
| /** | ||
| * For a single mount, walks all files in batches and removes from the | ||
| * index any file that does not have the "AI knowledge" tag. Mirrors | ||
| * StorageCrawlJob's batching/self-rescheduling shape, but for cleanup | ||
| * instead of indexing. | ||
| */ | ||
| class UntaggedCleanupCrawlJob extends QueuedJob { | ||
| public const BATCH_SIZE = 2000; | ||
| public const JOB_INTERVAL = 60; | ||
|
|
||
| public function __construct( | ||
| ITimeFactory $timeFactory, | ||
| private Logger $logger, | ||
| private IJobList $jobList, | ||
| private StorageService $storageService, | ||
| private FsEventService $fsEventService, | ||
| private IRootFolder $rootFolder, | ||
| private IndexCompletionService $indexCompletionService, | ||
| ) { | ||
| parent::__construct($timeFactory); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{storage_id:int, root_id:int, last_file_id:int|null} $argument | ||
| * @return void | ||
| */ | ||
| protected function run($argument): void { | ||
| $storageId = $argument['storage_id']; | ||
| $rootId = $argument['root_id']; | ||
| $lastFileId = ($argument['last_file_id'] ?? null) === null ? 0 : $argument['last_file_id']; | ||
|
|
||
| // Remove current iteration | ||
| $this->jobList->remove(self::class, $argument); | ||
|
|
||
| $mountFilesCount = 0; | ||
| $lastFileIdSeen = $lastFileId; | ||
| foreach ($this->storageService->getFilesInMount($storageId, $rootId, $lastFileId, self::BATCH_SIZE) as $fileId) { | ||
| $lastFileIdSeen = $fileId; | ||
| $mountFilesCount++; | ||
|
|
||
| $node = $this->rootFolder->getFirstNodeById($fileId); | ||
| if ($node === null) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!$this->fsEventService->hasAiKnowledgeTag($node)) { | ||
| $this->fsEventService->onDelete($node, false); | ||
| } | ||
| } | ||
|
|
||
| if ($mountFilesCount > 0) { | ||
| // Schedule next batch | ||
| $this->jobList->scheduleAfter(self::class, $this->time->getTime() + self::JOB_INTERVAL, [ | ||
| 'storage_id' => $storageId, | ||
| 'root_id' => $rootId, | ||
| 'last_file_id' => $lastFileIdSeen, | ||
| ]); | ||
| } else { | ||
| $this->logger->info('[UntaggedCleanupCrawlJob] Finished mount storage_id=' . $storageId . ' root_id=' . $rootId); | ||
| $this->indexCompletionService->checkAndMarkComplete(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\ContextChat\BackgroundJobs; | ||
|
|
||
| use OCA\ContextChat\Logger; | ||
| use OCA\ContextChat\Service\StorageService; | ||
| use OCP\AppFramework\Services\IAppConfig; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\BackgroundJob\IJobList; | ||
| use OCP\BackgroundJob\QueuedJob; | ||
|
|
||
| /** | ||
| * Discovers all mounts and schedules an UntaggedCleanupCrawlJob for each, | ||
| * mirroring SchedulerJob's shape. | ||
| */ | ||
| class UntaggedCleanupSchedulerJob extends QueuedJob { | ||
| public function __construct( | ||
| ITimeFactory $timeFactory, | ||
| private Logger $logger, | ||
| private IJobList $jobList, | ||
| private StorageService $storageService, | ||
| private IAppConfig $appConfig, | ||
| ) { | ||
| parent::__construct($timeFactory); | ||
| } | ||
|
|
||
| protected function run($argument): void { | ||
| // Mirror SchedulerJob: a fresh cleanup run means the initial-indexing | ||
| // status is no longer settled until this run finishes. | ||
| $this->appConfig->setAppValueInt('last_indexed_time', 0, lazy: true); | ||
| foreach ($this->storageService->getMounts() as $mount) { | ||
| $this->logger->debug('[UntaggedCleanupSchedulerJob] Scheduling cleanup storage_id=' . $mount['storage_id'] . ' root_id=' . $mount['root_id']); | ||
| $this->jobList->add(UntaggedCleanupCrawlJob::class, [ | ||
| 'storage_id' => $mount['storage_id'], | ||
| 'root_id' => $mount['root_id'], | ||
| 'last_file_id' => 0, | ||
| ]); | ||
| } | ||
| $this->jobList->remove(self::class); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is sadly immensely inefficient.