Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(files): Add new NoteReferenceProvider #1420

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,6 +39,7 @@ public function register(IRegistrationContext $context): void {
BeforeShareCreatedEvent::class,
BeforeShareCreatedListener::class
);
$context->registerReferenceProvider(NoteReferenceProvider::class);
}

public function boot(IBootContext $context): void {
Expand Down
84 changes: 84 additions & 0 deletions lib/Reference/NoteReferenceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace OCA\Notes\Reference;
use OCA\Notes\Service\NotesService;
use OCP\Collaboration\Reference\IReference;
use OCP\Collaboration\Reference\Reference;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\Collaboration\Reference\ADiscoverableReferenceProvider;
use OCP\IUserSession;
use OCP\L10N\IFactory;

class NoteReferenceProvider extends ADiscoverableReferenceProvider {
private const RICH_OBJECT_TYPE = 'notes_note';
private ?string $userId;
private IL10N $l10n;

public function __construct(
private IURLGenerator $urlGenerator,
private NotesService $notesService,
IUserSession $userSession,
IFactory $l10n,
) {
$this->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) {

Check failure on line 49 in lib/Reference/NoteReferenceProvider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

RiskyTruthyFalsyComparison

lib/Reference/NoteReferenceProvider.php:49:7: RiskyTruthyFalsyComparison: Operand of type int|null contains type int, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)

Check failure on line 49 in lib/Reference/NoteReferenceProvider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

RiskyTruthyFalsyComparison

lib/Reference/NoteReferenceProvider.php:49:7: RiskyTruthyFalsyComparison: Operand of type int|null contains type int, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)

Check failure on line 49 in lib/Reference/NoteReferenceProvider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable30

RiskyTruthyFalsyComparison

lib/Reference/NoteReferenceProvider.php:49:7: RiskyTruthyFalsyComparison: Operand of type int|null contains type int, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)

Check failure on line 49 in lib/Reference/NoteReferenceProvider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

RiskyTruthyFalsyComparison

lib/Reference/NoteReferenceProvider.php:49:7: RiskyTruthyFalsyComparison: Operand of type int|null contains type int, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
$note = $this->notesService->get($this->userId, $noteId);

Check failure on line 50 in lib/Reference/NoteReferenceProvider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

PossiblyNullArgument

lib/Reference/NoteReferenceProvider.php:50:38: PossiblyNullArgument: Argument 1 of OCA\Notes\Service\NotesService::get cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 50 in lib/Reference/NoteReferenceProvider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

PossiblyNullArgument

lib/Reference/NoteReferenceProvider.php:50:38: PossiblyNullArgument: Argument 1 of OCA\Notes\Service\NotesService::get cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 50 in lib/Reference/NoteReferenceProvider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable30

PossiblyNullArgument

lib/Reference/NoteReferenceProvider.php:50:38: PossiblyNullArgument: Argument 1 of OCA\Notes\Service\NotesService::get cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 50 in lib/Reference/NoteReferenceProvider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Reference/NoteReferenceProvider.php:50:38: PossiblyNullArgument: Argument 1 of OCA\Notes\Service\NotesService::get cannot be null, possibly null value provided (see https://psalm.dev/078)
$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');
}
}
Loading