|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace OCA\Notes\Reference; |
| 9 | + |
| 10 | +use OCA\Notes\Service\NoteDoesNotExistException; |
| 11 | +use OCA\Notes\Service\NotesService; |
| 12 | +use OCP\Collaboration\Reference\ADiscoverableReferenceProvider; |
| 13 | +use OCP\Collaboration\Reference\IReference; |
| 14 | +use OCP\Collaboration\Reference\Reference; |
| 15 | +use OCP\IL10N; |
| 16 | +use OCP\IURLGenerator; |
| 17 | +use OCP\IUserSession; |
| 18 | +use OCP\L10N\IFactory; |
| 19 | +use Psr\Log\LoggerInterface; |
| 20 | + |
| 21 | +class NoteReferenceProvider extends ADiscoverableReferenceProvider { |
| 22 | + private const RICH_OBJECT_TYPE = 'notes_note'; |
| 23 | + private ?string $userId; |
| 24 | + private IL10N $l10n; |
| 25 | + private LoggerInterface $logger; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + private IURLGenerator $urlGenerator, |
| 29 | + private NotesService $notesService, |
| 30 | + IUserSession $userSession, |
| 31 | + IFactory $l10n, |
| 32 | + LoggerInterface $logger, |
| 33 | + ) { |
| 34 | + $this->userId = $userSession->getUser()?->getUID(); |
| 35 | + $this->l10n = $l10n->get('notes'); |
| 36 | + $this->logger = $logger; |
| 37 | + } |
| 38 | + |
| 39 | + public function matchReference(string $referenceText): bool { |
| 40 | + return $this->getNoteLinkId($referenceText) !== null; |
| 41 | + } |
| 42 | + |
| 43 | + private function getNoteLinkId(string $referenceText): ?int { |
| 44 | + $start = $this->urlGenerator->getAbsoluteURL('/apps/notes/note/'); |
| 45 | + $startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/notes/note/'); |
| 46 | + |
| 47 | + foreach ([$start, $startIndex] as $url) { |
| 48 | + preg_match('/^' . preg_quote($url, '/') . '([0-9]+)$/', $referenceText, $matches); |
| 49 | + if ($matches && count($matches) > 1) { |
| 50 | + return (int)$matches[1]; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + public function resolveReference(string $referenceText): ?IReference { |
| 58 | + $noteId = $this->getNoteLinkId($referenceText); |
| 59 | + $reference = new Reference($referenceText); |
| 60 | + |
| 61 | + if ($this->userId !== null && $noteId !== null) { |
| 62 | + try { |
| 63 | + $note = $this->notesService->get($this->userId, $noteId); |
| 64 | + } catch (NoteDoesNotExistException) { |
| 65 | + $this->logger->warning('Could not find a note with id: ' . $noteId); |
| 66 | + return null; |
| 67 | + } |
| 68 | + $reference->setTitle($note->getTitle()); |
| 69 | + $reference->setDescription($note->getCategory()); |
| 70 | + $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 600, 'y' => 300, 'fileId' => $note->getId()])); |
| 71 | + |
| 72 | + return $reference; |
| 73 | + } |
| 74 | + |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + public function getCachePrefix(string $referenceId): string { |
| 79 | + return $referenceId; |
| 80 | + } |
| 81 | + |
| 82 | + public function getCacheKey(string $referenceId): string { |
| 83 | + return $this->userId ?? ''; |
| 84 | + } |
| 85 | + |
| 86 | + public function getId(): string { |
| 87 | + return 'notes' ; |
| 88 | + } |
| 89 | + |
| 90 | + public function getTitle(): string { |
| 91 | + return $this->l10n->t('Notes'); |
| 92 | + } |
| 93 | + |
| 94 | + public function getOrder(): int { |
| 95 | + return 10; |
| 96 | + } |
| 97 | + |
| 98 | + public function getIconUrl(): string { |
| 99 | + return $this->urlGenerator->imagePath('notes', 'notes.svg'); |
| 100 | + } |
| 101 | +} |
0 commit comments