|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Nextcloud - ContextChat |
| 4 | + * |
| 5 | + * This file is licensed under the Affero General Public License version 3 or |
| 6 | + * later. See the COPYING file. |
| 7 | + * |
| 8 | + * @author Anupam Kumar <[email protected]> |
| 9 | + * @copyright Anupam Kumar 2024 |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace OCA\ContextChat\BackgroundJobs; |
| 15 | + |
| 16 | +use OCA\ContextChat\Db\QueueContentItem; |
| 17 | +use OCA\ContextChat\Db\QueueContentItemMapper; |
| 18 | +use OCA\ContextChat\Service\LangRopeService; |
| 19 | +use OCA\ContextChat\Service\ProviderConfigService; |
| 20 | +use OCA\ContextChat\Type\Source; |
| 21 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 22 | +use OCP\BackgroundJob\IJobList; |
| 23 | +use OCP\BackgroundJob\QueuedJob; |
| 24 | + |
| 25 | +class SubmitContentJob extends QueuedJob { |
| 26 | + private const BATCH_SIZE = 20; |
| 27 | + |
| 28 | + public function __construct( |
| 29 | + ITimeFactory $timeFactory, |
| 30 | + private LangRopeService $service, |
| 31 | + private QueueContentItemMapper $mapper, |
| 32 | + private IJobList $jobList, |
| 33 | + ) { |
| 34 | + parent::__construct($timeFactory); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param $argument |
| 39 | + * @return void |
| 40 | + */ |
| 41 | + protected function run($argument): void { |
| 42 | + $entities = $this->mapper->getFromQueue(static::BATCH_SIZE); |
| 43 | + |
| 44 | + if (empty($entities)) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + /** @var array<string, array<QueueContentItem>> */ |
| 49 | + $bucketed = []; |
| 50 | + foreach ($entities as $entity) { |
| 51 | + foreach (explode(',', $entity->getUsers()) as $userId) { |
| 52 | + if (!is_array($bucketed[$userId])) { |
| 53 | + $bucketed[$userId] = []; |
| 54 | + } |
| 55 | + $bucketed[$userId][] = $entity; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + foreach ($bucketed as $userId => $entities) { |
| 60 | + $sources = array_map(function (QueueContentItem $item) use ($userId) { |
| 61 | + $sourceId = ProviderConfigService::getConfigKey($item->getAppId(), $item->getProviderId()) . ': ' . $item->getItemId(); |
| 62 | + return new Source( |
| 63 | + $userId, |
| 64 | + $sourceId, |
| 65 | + $item->getTitle(), |
| 66 | + $item->getContent(), |
| 67 | + $item->getLastModified()->getTimeStamp(), |
| 68 | + $item->getDocumentType(), |
| 69 | + ); |
| 70 | + }, $entities); |
| 71 | + |
| 72 | + $this->service->indexSources($sources); |
| 73 | + } |
| 74 | + |
| 75 | + foreach ($entities as $entity) { |
| 76 | + $this->mapper->removeFromQueue($entity); |
| 77 | + } |
| 78 | + |
| 79 | + $this->jobList->add(static::class); |
| 80 | + } |
| 81 | +} |
0 commit comments