Skip to content

Commit 12ddbbb

Browse files
authored
Merge pull request #1420 from nextcloud/enh/1167-Notes-reference-widget
fix(files): Add new NoteReferenceProvider
2 parents 838b079 + ab19ef2 commit 12ddbbb

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

lib/AppInfo/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace OCA\Notes\AppInfo;
1111

12+
use OCA\Notes\Reference\NoteReferenceProvider;
1213
use OCP\AppFramework\App;
1314
use OCP\AppFramework\Bootstrap\IBootContext;
1415
use OCP\AppFramework\Bootstrap\IBootstrap;
@@ -38,6 +39,7 @@ public function register(IRegistrationContext $context): void {
3839
BeforeShareCreatedEvent::class,
3940
BeforeShareCreatedListener::class
4041
);
42+
$context->registerReferenceProvider(NoteReferenceProvider::class);
4143
}
4244

4345
public function boot(IBootContext $context): void {
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

lib/Service/NotesService.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public function countNotes(string $userId) : int {
7373
}
7474
}
7575

76+
/**
77+
* @throws NoteDoesNotExistException
78+
*/
7679
public function get(string $userId, int $id) : Note {
7780
$customExtension = $this->getCustomExtension($userId);
7881
$notesFolder = $this->getNotesFolder($userId);

0 commit comments

Comments
 (0)