Skip to content

Commit 18ce04d

Browse files
committed
feat(contextchat): Users have to opt in to context chat integration
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent af790a6 commit 18ce04d

6 files changed

Lines changed: 74 additions & 7 deletions

File tree

lib/BackgroundJobs/ContextChatIndexJob.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OCA\Bookmarks\AppInfo\Application;
1212
use OCA\Bookmarks\ContextChat\ContextChatProvider;
1313
use OCA\Bookmarks\Service\BookmarkService;
14+
use OCA\Bookmarks\Service\UserSettingsService;
1415
use OCA\ContextChat\Public\ContentItem;
1516
use OCA\ContextChat\Public\ContentManager;
1617
use OCP\AppFramework\Utility\ITimeFactory;
@@ -25,6 +26,7 @@ public function __construct(
2526
private ?ContentManager $contentManager,
2627
private IUserManager $userManager,
2728
private ContextChatProvider $provider,
29+
private UserSettingsService $userSettings,
2830
) {
2931
parent::__construct($timeFactory);
3032
}
@@ -40,6 +42,10 @@ protected function run($argument) {
4042
if ($user === null) {
4143
return;
4244
}
45+
$this->userSettings->setUserId($user->getUID());
46+
if ($this->userSettings->get('contextchat.enabled') !== 'true') {
47+
return;
48+
}
4349
$items = [];
4450
foreach ($this->bookmarkService->getIterator($user->getUID()) as $bookmark) {
4551
$items[] = new ContentItem(

lib/ContextChat/ContextChatProvider.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
use OCA\Bookmarks\BackgroundJobs\ContextChatIndexJob;
1313
use OCA\Bookmarks\Db\TreeMapper;
1414
use OCA\Bookmarks\Events\BeforeDeleteEvent;
15+
use OCA\Bookmarks\Events\ChangeEvent;
1516
use OCA\Bookmarks\Events\InsertEvent;
1617
use OCA\Bookmarks\Events\ManipulateEvent;
1718
use OCA\Bookmarks\Service\BookmarkService;
19+
use OCA\Bookmarks\Service\UserSettingsService;
1820
use OCA\ContextChat\Event\ContentProviderRegisterEvent;
1921
use OCA\ContextChat\Public\ContentItem;
2022
use OCA\ContextChat\Public\ContentManager;
@@ -35,6 +37,7 @@ public function __construct(
3537
private IUserManager $userManager,
3638
private ?ContentManager $contentManager,
3739
private IJobList $jobList,
40+
private UserSettingsService $userSettings,
3841
) {
3942
}
4043

@@ -46,6 +49,12 @@ public function handle(Event $event): void {
4649
$this->register();
4750
return;
4851
}
52+
if (!$event instanceof ChangeEvent) {
53+
return;
54+
}
55+
if ($this->userSettings->get('contextchat.enabled') !== 'true') {
56+
return;
57+
}
4958
if ($event instanceof InsertEvent || $event instanceof ManipulateEvent) {
5059
if ($event->getType() !== TreeMapper::TYPE_BOOKMARK) {
5160
return;
@@ -114,7 +123,7 @@ public function getItemUrl(string $id): string {
114123
*/
115124
public function triggerInitialImport(): void {
116125
$this->userManager->callForAllUsers(function (IUser $user) {
117-
$this->jobList->add(ContextChatIndexJob::class, [$user->getUID()]);
126+
$this->jobList->add(ContextChatIndexJob::class, ['user' => $user->getUID()]);
118127
});
119128
}
120129
}

lib/Controller/WebViewController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OCA\Bookmarks\Db\PublicFolderMapper;
1616
use OCA\Bookmarks\Service\SettingsService;
1717
use OCA\Bookmarks\Service\UserSettingsService;
18+
use OCP\App\IAppManager;
1819
use OCP\AppFramework\Controller;
1920
use OCP\AppFramework\Db\DoesNotExistException;
2021
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
@@ -23,6 +24,7 @@
2324
use OCP\AppFramework\Http\NotFoundResponse;
2425
use OCP\AppFramework\Http\StreamResponse;
2526
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
27+
use OCP\IConfig;
2628
use OCP\IL10N;
2729
use OCP\IRequest;
2830
use OCP\IURLGenerator;
@@ -49,6 +51,8 @@ public function __construct(
4951
private \OCA\Bookmarks\Controller\InternalTagsController $tagsController,
5052
private UserSettingsService $userSettingsService,
5153
private SettingsService $settings,
54+
private IAppManager $appManager,
55+
private IConfig $config,
5256
) {
5357
parent::__construct($appName, $request);
5458
$this->userId = $userId;
@@ -78,6 +82,8 @@ public function index(): AugmentedTemplateResponse {
7882
$this->initialState->provideInitialState($this->appName, 'allClicksCount', $this->bookmarkController->countAllClicks()->getData()['item']);
7983
$this->initialState->provideInitialState($this->appName, 'withClicksCount', $this->bookmarkController->countWithClicks()->getData()['item']);
8084
$this->initialState->provideInitialState($this->appName, 'tags', $this->tagsController->fullTags(true)->getData());
85+
$this->initialState->provideInitialState($this->appName, 'contextChatInstalled', $this->appManager->isEnabledForUser('context_chat'));
86+
$this->initialState->provideInitialState($this->appName, 'appStoreEnabled', $this->config->getSystemValueBool('appstoreenabled', true));
8187

8288
$settings = $this->userSettingsService->toArray();
8389
$settings['shareapi_allow_links'] = $this->settings->getLinkSharingAllowed();

lib/Service/UserSettingsService.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88

99
namespace OCA\Bookmarks\Service;
1010

11+
use OCA\Bookmarks\BackgroundJobs\ContextChatIndexJob;
12+
use OCP\BackgroundJob\IJobList;
1113
use OCP\IConfig;
1214
use OCP\IL10N;
1315

1416
class UserSettingsService {
1517

16-
public const KEYS = ['hasSeenWhatsnew', 'viewMode', 'archive.enabled', 'archive.filePath', 'backup.enabled', 'backup.filePath', 'sorting'];
18+
public const KEYS = ['hasSeenWhatsnew', 'viewMode', 'archive.enabled', 'archive.filePath', 'backup.enabled', 'backup.filePath', 'sorting', 'contextchat.enabled'];
1719

1820
public function __construct(
1921
private ?string $userId,
2022
private string $appName,
2123
private IConfig $config,
2224
private IL10N $l,
25+
private IJobList $jobList,
2326
) {
2427

2528
}
@@ -46,7 +49,7 @@ public function get(string $key): string {
4649
return $this->config->getAppValue('bookmarks', 'performance.maxBookmarksperAccount', '0');
4750
}
4851
if ($key === 'archive.enabled') {
49-
$default = (string)true;
52+
$default = 'true';
5053
}
5154
if ($key === 'privacy.enableScraping') {
5255
return $this->config->getAppValue($this->appName, 'privacy.enableScraping', 'false');
@@ -55,11 +58,17 @@ public function get(string $key): string {
5558
$default = $this->l->t('Bookmarks');
5659
}
5760
if ($key === 'backup.enabled') {
58-
$default = (string)false;
61+
$default = 'false';
5962
}
6063
if ($key === 'backup.filePath') {
6164
$default = $this->l->t('Bookmarks Backups');
6265
}
66+
if ($key === 'contextchat.enabled') {
67+
$default = 'false';
68+
if ($this->get('archive.enabled') !== 'true') {
69+
return 'false';
70+
}
71+
}
6372
return $this->config->getUserValue(
6473
$this->userId,
6574
$this->appName,
@@ -84,6 +93,9 @@ public function set(string $key, string $value): void {
8493
if ($key === 'sorting' && !in_array($value, ['title', 'added', 'clickcount', 'lastmodified', 'index', 'url'], true)) {
8594
throw new \ValueError();
8695
}
96+
if ($key === 'contextchat.enabled' && $value === 'true' && $this->get('contextchat.enabled') !== 'true' && $this->get('archive.enabled') === 'true') {
97+
$this->jobList->add(ContextChatIndexJob::class, ['user' => $this->userId]);
98+
}
8799
$this->config->setUserValue(
88100
$this->userId,
89101
$this->appName,

src/components/Icons.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ import UndeleteIcon from 'vue-material-design-icons/Restore.vue'
6666
import HotnessZero from 'vue-material-design-icons/Minus.vue'
6767
import Hotness from 'vue-material-design-icons/Fire.vue'
6868
import BookmarksIcon from './icons/BookmarksIcon.vue'
69+
import ContextChatIcon from 'vue-material-design-icons/ClipboardSearchOutline.vue'
6970

7071
export {
72+
ContextChatIcon,
7173
FolderPlusIcon,
7274
FolderMoveIcon,
7375
ContentCopyIcon,

src/components/Settings.vue

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@
6161
@click="onChangeBackupPath" />
6262
</NcAppSettingsSection>
6363

64+
<NcAppSettingsSection v-if="contextChatInstalled || isAdmin && appStoreEnabled" id="contextchat" :name="t('bookmarks', 'Context Chat integration')">
65+
<template #icon>
66+
<ContextChatIcon :size="20" />
67+
</template>
68+
<p>{{ t('bookmarks', 'The bookmarks app can automatically make available the textual contents of the websites you bookmark to Context Chat, which allows asking questions about and getting answers based on those contents. This is only available if auto-archiving is enabled.') }}</p>
69+
<p v-if="isAdmin && !contextChatInstalled">
70+
{{ t('bookmarks', 'Context chat is currently not installed, but is required for this feature.') }}
71+
</p>
72+
<NcCheckboxRadioSwitch :checked="contextChatEnabled" :disabled="!archiveEnabled" @update:checked="onChangeContextChatEnabled">
73+
{{ t('bookmarks', 'Enable Context Chat integration') }}
74+
</NcCheckboxRadioSwitch>
75+
</NcAppSettingsSection>
76+
6477
<NcAppSettingsSection id="client-apps" :name="t('bookmarks', 'Client apps')">
6578
<template #icon>
6679
<ApplicationIcon :size="20" />
@@ -124,17 +137,18 @@
124137
<script>
125138
import { generateUrl } from '@nextcloud/router'
126139
import { actions } from '../store/index.js'
127-
import { getRequestToken } from '@nextcloud/auth'
140+
import { getRequestToken, getCurrentUser } from '@nextcloud/auth'
128141
import { getFilePickerBuilder } from '@nextcloud/dialogs'
129142
import { privateRoutes } from '../router.js'
130143
import { NcAppSettingsSection, NcAppSettingsDialog, NcCheckboxRadioSwitch, NcTextField } from '@nextcloud/vue'
131-
import { ImportIcon, ArchiveIcon, BackupIcon, LinkIcon, ApplicationIcon, ApplicationImportIcon } from './Icons.js'
144+
import { ImportIcon, ArchiveIcon, BackupIcon, LinkIcon, ApplicationIcon, ApplicationImportIcon, ContextChatIcon } from './Icons.js'
132145
import HeartIcon from 'vue-material-design-icons/Heart.vue'
133146
import LifebuoyIcon from 'vue-material-design-icons/Lifebuoy.vue'
147+
import { loadState } from '@nextcloud/initial-state'
134148
135149
export default {
136150
name: 'Settings',
137-
components: { LifebuoyIcon, HeartIcon, NcAppSettingsSection, NcAppSettingsDialog, NcCheckboxRadioSwitch, NcTextField, ImportIcon, ArchiveIcon, BackupIcon, LinkIcon, ApplicationIcon, ApplicationImportIcon },
151+
components: { ContextChatIcon, LifebuoyIcon, HeartIcon, NcAppSettingsSection, NcAppSettingsDialog, NcCheckboxRadioSwitch, NcTextField, ImportIcon, ArchiveIcon, BackupIcon, LinkIcon, ApplicationIcon, ApplicationImportIcon },
138152
props: {
139153
settingsOpen: {
140154
type: Boolean,
@@ -186,6 +200,18 @@ export default {
186200
backupEnabled() {
187201
return this.$store.state.settings['backup.enabled'] === 'true'
188202
},
203+
contextChatEnabled() {
204+
return this.$store.state.settings['contextchat.enabled'] === 'true'
205+
},
206+
contextChatInstalled() {
207+
return loadState('bookmarks', 'contextChatInstalled')
208+
},
209+
isAdmin() {
210+
return getCurrentUser()?.isAdmin
211+
},
212+
appStoreEnabled() {
213+
return loadState('bookmarks', 'appStoreEnabled')
214+
},
189215
},
190216
mounted() {
191217
window.addEventListener('beforeinstallprompt', (e) => {
@@ -219,6 +245,12 @@ export default {
219245
value: String(!this.archiveEnabled),
220246
})
221247
},
248+
async onChangeContextChatEnabled(e) {
249+
await this.$store.dispatch(actions.SET_SETTING, {
250+
key: 'contextchat.enabled',
251+
value: String(!this.contextChatEnabled),
252+
})
253+
},
222254
async onChangeArchivePath(e) {
223255
const path = await this.archivePathPicker.pick()
224256
await this.$store.dispatch(actions.SET_SETTING, {

0 commit comments

Comments
 (0)