-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathContextChatProvider.php
More file actions
123 lines (105 loc) · 3.17 KB
/
ContextChatProvider.php
File metadata and controls
123 lines (105 loc) · 3.17 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\ContextChat;
use OCA\Mail\AppInfo\Application;
use OCA\Mail\Db\Message;
use OCA\Mail\Db\MessageMapper;
use OCA\Mail\Events\MessageDeletedEvent;
use OCA\Mail\Events\NewMessagesSynchronized;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\ContextChat\TaskService;
use OCA\Mail\Service\MailManager;
use OCP\BackgroundJob\IJobList;
use OCP\ContextChat\Events\ContentProviderRegisterEvent;
use OCP\ContextChat\IContentManager;
use OCP\ContextChat\IContentProvider;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IURLGenerator;
use OCP\IUserManager;
/**
* @implements IEventListener<Event>
*/
class ContextChatProvider implements IContentProvider, IEventListener {
public const CONTEXT_CHAT_MESSAGE_MAX_AGE = 31557600; // 60 * 60 * 24 * 365.25 (1 year)
public const CONTEXT_CHAT_IMPORT_MAX_ITEMS = 1000;
public const CONTEXT_CHAT_JOB_INTERVAL = 300; // 60 * 5 (5 minutes)
public function __construct(
private TaskService $taskService,
private AccountService $accountService,
private MailManager $mailManager,
private MessageMapper $messageMapper,
private IURLGenerator $urlGenerator,
private IUserManager $userManager,
private IContentManager $contentManager,
private IJobList $jobList,
) {
}
public function handle(Event $event): void {
if (!$this->contentManager->isContextChatAvailable()) {
return;
}
if ($event instanceof ContentProviderRegisterEvent) {
$this->contentManager->registerContentProvider($this->getAppId(), $this->getId(), self::class);
return;
}
if ($event instanceof NewMessagesSynchronized) {
$messageIds = array_map(static fn (Message $m): int => $m->getId(), $event->getMessages());
// Ensure that there are messages to sync
if ($messageIds === []) {
return;
}
$mailboxId = $event->getMailbox()->getId();
$this->taskService->updateOrCreate($mailboxId, min($messageIds));
return;
}
if ($event instanceof MessageDeletedEvent) {
$this->contentManager->deleteContent($this->getAppId(), $this->getId(), [strval($event->getMessageId())]);
return;
}
}
/**
* The ID of the provider
*
* @return string
* @since 5.2.0
*/
public function getId(): string {
return 'mail';
}
/**
* The ID of the app making the provider avaialble
*
* @return string
* @since 5.2.0
*/
public function getAppId(): string {
return Application::APP_ID;
}
/**
* The absolute URL to the content item
*
* @param string $id
* @return string
* @since 5.2.0
*/
public function getItemUrl(string $id): string {
[$mailboxId, $messageId] = explode(':', $id);
if (!$mailboxId || !$messageId) {
return $this->urlGenerator->linkToRouteAbsolute('mail.page.thread', [ 'mailboxId' => $mailboxId, 'id' => 'error']);
}
return $this->urlGenerator->linkToRouteAbsolute('mail.page.thread', [ 'mailboxId' => $mailboxId, 'id' => $messageId ]);
}
/**
* Starts the initial import of content items into context chat
*
* @return void
* @since 5.2.0
*/
public function triggerInitialImport(): void {
}
}