diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 4b6faa057..29e9dc18f 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -9,6 +9,7 @@ namespace OCA\Notes\AppInfo; +use OCA\Notes\Reference\NoteReferenceProvider; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; @@ -38,6 +39,7 @@ public function register(IRegistrationContext $context): void { BeforeShareCreatedEvent::class, BeforeShareCreatedListener::class ); + $context->registerReferenceProvider(NoteReferenceProvider::class); } public function boot(IBootContext $context): void { diff --git a/lib/Reference/NoteReferenceProvider.php b/lib/Reference/NoteReferenceProvider.php new file mode 100644 index 000000000..0c45ea1a3 --- /dev/null +++ b/lib/Reference/NoteReferenceProvider.php @@ -0,0 +1,84 @@ +userId = $userSession->getUser()?->getUID(); + $this->l10n = $l10n->get('notes'); + } + + public function matchReference(string $referenceText): bool { + return $this->getNoteLinkId($referenceText) !== null; + } + + private function getNoteLinkId(string $referenceText): ?int { + $start = $this->urlGenerator->getAbsoluteURL('/apps/notes/note/'); + $startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/notes/note/'); + + foreach ([$start, $startIndex] as $url) { + preg_match('/^' . preg_quote($url, '/') . '([0-9]+)$/', $referenceText, $matches); + if ($matches && count($matches) > 1) { + return (int)$matches[1]; + } + } + + return null; + } + + public function resolveReference(string $referenceText): ?IReference { + $noteId = $this->getNoteLinkId($referenceText); + $reference = new Reference($referenceText); + if ($noteId) { + $note = $this->notesService->get($this->userId, $noteId); + $reference->setTitle($note->getTitle()); + $reference->setDescription($note->getCategory()); + $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 600, 'y' => 300, 'fileId' => $note->getId()])); + + return $reference; + } + + return null; + } + + public function getCachePrefix(string $referenceId): string { + return $referenceId; + } + + public function getCacheKey(string $referenceId): string { + return $this->userId ?? ''; + } + + public function getId(): string { + return 'notes' ; + } + + public function getTitle(): string { + return $this->l10n->t('Notes'); + } + + public function getOrder(): int { + return 10; + } + + public function getIconUrl(): string { + return $this->urlGenerator->imagePath('notes', 'notes.svg'); + } +}