-
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathContextChatProvider.php
More file actions
129 lines (118 loc) · 3.3 KB
/
Copy pathContextChatProvider.php
File metadata and controls
129 lines (118 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/*
* Copyright (c) 2025. The Nextcloud Bookmarks contributors.
*
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
namespace OCA\Bookmarks\ContextChat;
use OCA\Bookmarks\AppInfo\Application;
use OCA\Bookmarks\BackgroundJobs\ContextChatIndexJob;
use OCA\Bookmarks\Db\TreeMapper;
use OCA\Bookmarks\Events\BeforeDeleteEvent;
use OCA\Bookmarks\Events\ChangeEvent;
use OCA\Bookmarks\Events\InsertEvent;
use OCA\Bookmarks\Events\ManipulateEvent;
use OCA\Bookmarks\Service\BookmarkService;
use OCA\Bookmarks\Service\UserSettingsService;
use OCA\ContextChat\Event\ContentProviderRegisterEvent;
use OCA\ContextChat\Public\ContentItem;
use OCA\ContextChat\Public\ContentManager;
use OCA\ContextChat\Public\IContentProvider;
use OCP\BackgroundJob\IJobList;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IUser;
use OCP\IUserManager;
/**
* @implements IEventListener<Event>
*/
class ContextChatProvider implements IContentProvider, IEventListener {
public function __construct(
private BookmarkService $bookmarkService,
private IUserManager $userManager,
private ?ContentManager $contentManager,
private IJobList $jobList,
private UserSettingsService $userSettings,
) {
}
public function handle(Event $event): void {
if ($this->contentManager === null) {
return;
}
if ($event instanceof ContentProviderRegisterEvent) {
$this->register();
return;
}
if (!$event instanceof ChangeEvent) {
return;
}
if ($this->userSettings->get('contextchat.enabled') !== 'true') {
return;
}
if ($event instanceof InsertEvent || $event instanceof ManipulateEvent) {
if ($event->getType() !== TreeMapper::TYPE_BOOKMARK) {
return;
}
$bookmark = $this->bookmarkService->findById($event->getId());
if ($bookmark === null) {
return;
}
$item = new ContentItem(
(string)$event->getId(),
$this->getId(),
$bookmark->getTitle(),
$bookmark->getTextContent(),
'Website',
new \DateTime('@' . $bookmark->getLastmodified()),
[$bookmark->getUserId()]
);
$this->contentManager->submitContent($this->getAppId(), [$item]);
return;
}
if ($event instanceof BeforeDeleteEvent) {
if ($event->getType() !== TreeMapper::TYPE_BOOKMARK) {
return;
}
$this->contentManager->deleteContent($this->getAppId(), $this->getId(), [(string)$event->getId()]);
return;
}
}
public function register(): void {
$this->contentManager->registerContentProvider($this->getAppId(), $this->getId(), self::class);
}
/**
* The ID of the provider
*
* @return string
*/
public function getId(): string {
return 'bookmarks';
}
/**
* The ID of the app making the provider available
*
* @return string
*/
public function getAppId(): string {
return Application::APP_ID;
}
/**
* The absolute URL to the content item
*
* @param string $id
* @return string
*/
public function getItemUrl(string $id): string {
return $this->bookmarkService->findById(intval($id))?->getUrl() ?? '';
}
/**
* Starts the initial import of content items into content chat
*
* @return void
*/
public function triggerInitialImport(): void {
$this->userManager->callForAllUsers(function (IUser $user) {
$this->jobList->add(ContextChatIndexJob::class, ['user' => $user->getUID()]);
});
}
}