-
Notifications
You must be signed in to change notification settings - Fork 346
fix(contextchat): use a consistent item id for indexed messages #13443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kesselb
wants to merge
1
commit into
main
Choose a base branch
from
fix/context-chat-item-id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| namespace OCA\Mail\ContextChat; | ||
|
|
||
| use OCA\Mail\AppInfo\Application; | ||
| use OCA\Mail\Db\MailboxMapper; | ||
| use OCA\Mail\Db\Message; | ||
| use OCA\Mail\Db\MessageMapper; | ||
| use OCA\Mail\Events\MessageDeletedEvent; | ||
|
|
@@ -34,12 +35,12 @@ 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 MailboxMapper $mailboxMapper, | ||
| private IURLGenerator $urlGenerator, | ||
| private IUserManager $userManager, | ||
| private IContentManager $contentManager, | ||
|
|
@@ -72,7 +73,12 @@ public function handle(Event $event): void { | |
| } | ||
|
|
||
| if ($event instanceof MessageDeletedEvent) { | ||
| $this->contentManager->deleteContent($this->getAppId(), $this->getId(), [strval($event->getUid())]); | ||
| $itemId = self::itemId( | ||
| $event->getAccount()->getId(), | ||
| $event->getMailbox()->getId(), | ||
| $event->getUid(), | ||
| ); | ||
| $this->contentManager->deleteContent($this->getAppId(), $this->getId(), [$itemId]); | ||
| return; | ||
| } | ||
| } | ||
|
|
@@ -105,11 +111,29 @@ public function getAppId(): 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']); | ||
| [, $mailboxId, $uid] = array_pad(explode(':', $id), 3, null); | ||
| if (!$mailboxId || !$uid) { | ||
| return $this->urlGenerator->linkToRouteAbsolute('mail.page.index', []); | ||
| } | ||
|
|
||
| // Context chat calls this when it renders the sources of an answer, so the | ||
| // message id is looked up on demand rather than baked into the item id. | ||
| try { | ||
| $mailbox = $this->mailboxMapper->findById((int)$mailboxId); | ||
| $messageId = $this->messageMapper->getIdForUid($mailbox, (int)$uid); | ||
| } catch (\Throwable) { | ||
| // Context chat fails the whole answer if this throws | ||
|
Comment on lines
+124
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for pointing this out, fixed in nextcloud/context_chat#266 |
||
| $messageId = null; | ||
| } | ||
| return $this->urlGenerator->linkToRouteAbsolute('mail.page.thread', [ 'mailboxId' => $mailboxId, 'id' => $messageId ]); | ||
|
|
||
| if ($messageId === null) { | ||
| return $this->urlGenerator->linkToRouteAbsolute('mail.page.index', []); | ||
| } | ||
|
|
||
| return $this->urlGenerator->linkToRouteAbsolute( | ||
| 'mail.page.thread', | ||
| ['mailboxId' => (int)$mailboxId, 'id' => $messageId], | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -120,4 +144,14 @@ public function getItemUrl(string $id): string { | |
| */ | ||
| public function triggerInitialImport(): void { | ||
| } | ||
|
|
||
| /** | ||
| * Identifier of a message in the context chat knowledge base. | ||
| * | ||
| * Keyed by uid because that is all MessageDeletedEvent carries. | ||
| */ | ||
| public static function itemId(int $accountId, int $mailboxId, int $uid): string { | ||
| return "$accountId:$mailboxId:$uid"; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getItemUrlis called for every item in the sources list when the output is rendered, truebut without this change, it was still not looked up until the link was clicked by the user. Here, every link would be checked even when not clicked.
not sure what's the difference between
uidandidbut the withidapproach, the message was only checked when the user clicked on it and the webpage from'mail.page.thread'opened.I'm not familiar with the mail's code so sorry if the point does not make much sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot, that's a valid concern. The previous version was a cheap call.
id = oc_mail_messages.id
uid = identifier on the imap server, unique per mailbox, also stored as oc_mail_messages.uid
Better would be the id, but some of the events (e.g. MessageDeletedEvent) does not carry it.
But that might change very soon, and hence I will move this PR back to draft for now and continue once the JMAP changes are in. At best we have id everywhere, otherwise I will add a new entrypoint that accepts mailboxId/uid doing the lookup internally.