Skip to content

Commit 9be6b66

Browse files
committed
fix: Run ContextChat index in per-user bg jobs
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent 6cadd1d commit 9be6b66

3 files changed

Lines changed: 72 additions & 24 deletions

File tree

lib/AppInfo/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,15 @@ public function register(IRegistrationContext $context): void {
111111
* @throws \Throwable
112112
*/
113113
public function boot(IBootContext $context): void {
114+
// Register with ContextChat
114115
if (class_exists(ContentProviderRegisterEvent::class)) {
115116
$this->getContainer()->get(ContextChatProvider::class)->register();
116117
$eventDispatcher = $this->getContainer()->get(IEventDispatcher::class);
117118
$eventDispatcher->addServiceListener(InsertEvent::class, ContextChatProvider::class);
118119
$eventDispatcher->addServiceListener(ManipulateEvent::class, ContextChatProvider::class);
119120
$eventDispatcher->addServiceListener(BeforeDeleteEvent::class, ContextChatProvider::class);
120121
}
122+
// Register with Nextcloud Flow
121123
$container = $context->getServerContainer();
122124
CreateBookmark::register($container->get(IEventDispatcher::class));
123125
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) 2020-2025. The Nextcloud Bookmarks contributors.
5+
*
6+
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
7+
*/
8+
9+
namespace OCA\Bookmarks\BackgroundJobs;
10+
11+
use OCA\Bookmarks\AppInfo\Application;
12+
use OCA\Bookmarks\Service\BookmarkService;
13+
use OCA\ContextChat\Public\ContentItem;
14+
use OCA\ContextChat\Public\ContentManager;
15+
use OCP\AppFramework\Utility\ITimeFactory;
16+
use OCP\BackgroundJob\QueuedJob;
17+
use OCP\IUserManager;
18+
19+
class ContextChatIndexJob extends QueuedJob {
20+
21+
public function __construct(
22+
ITimeFactory $timeFactory,
23+
private BookmarkService $bookmarkService,
24+
private ?ContentManager $contentManager,
25+
private IUserManager $userManager,
26+
) {
27+
parent::__construct($timeFactory);
28+
}
29+
30+
protected function run($argument) {
31+
if ($this->contentManager === null) {
32+
return;
33+
}
34+
if (!isset($argument['user'])) {
35+
return;
36+
}
37+
$user = $this->userManager->get($argument['user']);
38+
if ($user === null) {
39+
return;
40+
}
41+
$items = [];
42+
foreach ($this->bookmarkService->getIterator($user->getUID()) as $bookmark) {
43+
$items[] = new ContentItem(
44+
(string)$bookmark->getId(),
45+
$this->getId(),
46+
$bookmark->getTitle(),
47+
$bookmark->getTextContent(),
48+
'Website',
49+
new \DateTime('@' . $bookmark->getLastmodified()),
50+
[$user->getUID()]
51+
);
52+
if (count($items) < 25) {
53+
continue;
54+
}
55+
$this->contentManager->submitContent(Application::APP_ID, $items);
56+
}
57+
}
58+
}

lib/ContextChat/ContextChatProvider.php

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace OCA\Bookmarks\ContextChat;
1010

1111
use OCA\Bookmarks\AppInfo\Application;
12+
use OCA\Bookmarks\BackgroundJobs\ContextChatIndexJob;
1213
use OCA\Bookmarks\Db\TreeMapper;
1314
use OCA\Bookmarks\Events\BeforeDeleteEvent;
1415
use OCA\Bookmarks\Events\InsertEvent;
@@ -18,8 +19,8 @@
1819
use OCA\ContextChat\Public\ContentItem;
1920
use OCA\ContextChat\Public\ContentManager;
2021
use OCA\ContextChat\Public\IContentProvider;
22+
use OCP\BackgroundJob\IJobList;
2123
use OCP\EventDispatcher\Event;
22-
use OCP\EventDispatcher\IEventDispatcher;
2324
use OCP\EventDispatcher\IEventListener;
2425
use OCP\IUser;
2526
use OCP\IUserManager;
@@ -32,12 +33,15 @@ class ContextChatProvider implements IContentProvider, IEventListener {
3233
public function __construct(
3334
private BookmarkService $bookmarkService,
3435
private IUserManager $userManager,
35-
private ContentManager $contentManager,
36-
private IEventDispatcher $eventDispatcher,
36+
private ?ContentManager $contentManager,
37+
private IJobList $jobList,
3738
) {
3839
}
3940

4041
public function handle(Event $event): void {
42+
if ($this->contentManager === null) {
43+
return;
44+
}
4145
if ($event instanceof ContentProviderRegisterEvent) {
4246
$this->register();
4347
return;
@@ -47,6 +51,9 @@ public function handle(Event $event): void {
4751
return;
4852
}
4953
$bookmark = $this->bookmarkService->findById($event->getId());
54+
if ($bookmark === null) {
55+
return;
56+
}
5057
$item = new ContentItem(
5158
(string)$event->getId(),
5259
$this->getId(),
@@ -76,17 +83,15 @@ public function register(): void {
7683
* The ID of the provider
7784
*
7885
* @return string
79-
* @since 1.1.0
8086
*/
8187
public function getId(): string {
8288
return 'bookmarks';
8389
}
8490

8591
/**
86-
* The ID of the app making the provider avaialble
92+
* The ID of the app making the provider available
8793
*
8894
* @return string
89-
* @since 1.1.0
9095
*/
9196
public function getAppId(): string {
9297
return Application::APP_ID;
@@ -97,7 +102,6 @@ public function getAppId(): string {
97102
*
98103
* @param string $id
99104
* @return string
100-
* @since 1.1.0
101105
*/
102106
public function getItemUrl(string $id): string {
103107
return $this->bookmarkService->findById(intval($id))?->getUrl() ?? '';
@@ -107,26 +111,10 @@ public function getItemUrl(string $id): string {
107111
* Starts the initial import of content items into content chat
108112
*
109113
* @return void
110-
* @since 1.1.0
111114
*/
112115
public function triggerInitialImport(): void {
113116
$this->userManager->callForAllUsers(function (IUser $user) {
114-
$items = [];
115-
foreach ($this->bookmarkService->getIterator($user->getUID()) as $bookmark) {
116-
$items[] = new ContentItem(
117-
(string)$bookmark->getId(),
118-
$this->getId(),
119-
$bookmark->getTitle(),
120-
$bookmark->getTextContent(),
121-
'Website',
122-
new \DateTime('@' . $bookmark->getLastmodified()),
123-
[$user->getUID()]
124-
);
125-
if (count($items) < 25) {
126-
continue;
127-
}
128-
$this->contentManager->submitContent($this->getAppId(), $items);
129-
}
117+
$this->jobList->add(ContextChatIndexJob::class, [$user->getUID()]);
130118
});
131119
}
132120
}

0 commit comments

Comments
 (0)