Skip to content

Commit 2f8b8a8

Browse files
fix(files): Add new NoteReferenceProvider
Signed-off-by: julia.kirschenheuter <julia.kirschenheuter@nextcloud.com>
1 parent 83399a9 commit 2f8b8a8

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-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: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace OCA\Notes\Reference;
4+
use OCA\Notes\Service\NotesService;
5+
use OCP\Collaboration\Reference\IReference;
6+
use OCP\Collaboration\Reference\Reference;
7+
use OCP\IL10N;
8+
use OCP\IURLGenerator;
9+
use OCP\Collaboration\Reference\ADiscoverableReferenceProvider;
10+
use OCP\IUserSession;
11+
use OCP\L10N\IFactory;
12+
13+
class NoteReferenceProvider extends ADiscoverableReferenceProvider {
14+
private const RICH_OBJECT_TYPE = 'notes_note';
15+
private ?string $userId;
16+
private IL10N $l10n;
17+
18+
public function __construct(
19+
private IURLGenerator $urlGenerator,
20+
private NotesService $notesService,
21+
IUserSession $userSession,
22+
IFactory $l10n,
23+
) {
24+
$this->userId = $userSession->getUser()?->getUID();
25+
$this->l10n = $l10n->get('notes');
26+
}
27+
28+
public function matchReference(string $referenceText): bool {
29+
return $this->getNoteLinkId($referenceText) !== null;
30+
}
31+
32+
private function getNoteLinkId(string $referenceText): ?int {
33+
$start = $this->urlGenerator->getAbsoluteURL('/apps/notes/note/');
34+
$startIndex = $this->urlGenerator->getAbsoluteURL('/index.php/apps/notes/note/');
35+
36+
foreach ([$start, $startIndex] as $url) {
37+
preg_match('/^' . preg_quote($url, '/') . '([0-9]+)$/', $referenceText, $matches);
38+
if ($matches && count($matches) > 1) {
39+
return (int)$matches[1];
40+
}
41+
}
42+
43+
return null;
44+
}
45+
46+
public function resolveReference(string $referenceText): ?IReference {
47+
$noteId = $this->getNoteLinkId($referenceText);
48+
$reference = new Reference($referenceText);
49+
if ($noteId) {
50+
$note = $this->notesService->get($this->userId, $noteId);
51+
$reference->setTitle($note->getTitle());
52+
$reference->setDescription($note->getCategory());
53+
$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 600, 'y' => 300, 'fileId' => $note->getId()]));
54+
55+
return $reference;
56+
}
57+
58+
return null;
59+
}
60+
61+
public function getCachePrefix(string $referenceId): string {
62+
return $referenceId;
63+
}
64+
65+
public function getCacheKey(string $referenceId): string {
66+
return $this->userId ?? '';
67+
}
68+
69+
public function getId(): string {
70+
return 'notes' ;
71+
}
72+
73+
public function getTitle(): string {
74+
return $this->l10n->t('Notes');
75+
}
76+
77+
public function getOrder(): int {
78+
return 10;
79+
}
80+
81+
public function getIconUrl(): string {
82+
return $this->urlGenerator->imagePath('notes', 'notes.svg');
83+
}
84+
}

0 commit comments

Comments
 (0)