add provider for Nextcloud's unified search

This commit is contained in:
korelstar 2020-09-15 21:45:28 +02:00
parent 4ff70c7614
commit 46a6bd1bd6
5 changed files with 120 additions and 0 deletions

View File

@ -1 +1,2 @@
@include icon-black-white('notes', 'notes', 1);
@include icon-black-white('notes-trans', 'notes', 1);

1
img/notes-trans.svg Normal file
View File

@ -0,0 +1 @@
<svg width="32" height="32" version="1.0" viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg"><path d="m24.484 3.5156c-1.0237 0-2.0471 0.38887-2.8281 1.1699l5.6582 5.6582c1.5621-1.5621 1.5621-4.0961 0-5.6582-0.78105-0.78105-1.8064-1.1699-2.8301-1.1699zm-4.2422 2.584-12.02 12.021 5.6562 5.6562 12.021-12.02-5.6582-5.6582zm-13.436 13.436-2.1211 7.7793 7.7793-2.1211-5.6582-5.6582z" fill="#fff" style="fill-opacity:0.5" /></svg>

After

Width:  |  Height:  |  Size: 430 B

View File

@ -20,6 +20,7 @@ class Application extends App implements IBootstrap {
public function register(IRegistrationContext $context): void {
$context->registerCapability(Capabilities::class);
$context->registerSearchProvider(SearchProvider::class);
$context->registerDashboardWidget(DashboardWidget::class);
$context->registerEventListener(
BeforeTemplateRenderedEvent::class,

View File

@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace OCA\Notes\AppInfo;
use OCA\Notes\Service\Note;
use OCA\Notes\Service\NotesService;
use OCA\Notes\Service\Util;
use OCP\IUser;
use OCP\IURLGenerator;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
class SearchProvider implements IProvider {
/** @var Util */
private $util;
/** @var NotesService */
private $notesService;
/** @var IURLGenerator */
private $url;
public function __construct(
Util $util,
NotesService $notesService,
IURLGenerator $url
) {
$this->util = $util;
$this->notesService = $notesService;
$this->url = $url;
}
public function getId(): string {
return Application::APP_ID;
}
public function getName(): string {
return $this->util->l10n->t('Notes');
}
public function getOrder(string $route, array $routeParameters): int {
if (strpos($route, 'files' . '.') === 0) {
return 25;
} elseif (strpos($route, Application::APP_ID . '.') === 0) {
return -1;
}
return 4;
}
public function search(IUser $user, ISearchQuery $query): SearchResult {
$notes = $this->notesService->search($user->getUID(), $query->getTerm());
// sort by modified time
usort($notes, function (Note $a, Note $b) {
return $b->getModified() - $a->getModified();
});
// create SearchResultEntry from Note
$result = array_map(
function (Note $note) : SearchResultEntry {
$excerpt = $note->getCategory();
try {
$excerpt = $note->getExcerpt();
} catch (\Throwable $e) {
}
return new SearchResultEntry(
'',
$note->getTitle(),
$excerpt,
$this->url->linkToRouteAbsolute('notes.page.index') . 'note/'.$note->getId(),
'icon-notes-trans'
);
},
$notes
);
return SearchResult::complete(
$this->getName(),
$result
);
}
}

View File

@ -58,6 +58,39 @@ class NotesService {
return $note;
}
public function search(string $userId, string $search) : array {
$terms = preg_split('/\s+/', $search);
$notes = $this->getAll($userId)['notes'];
return array_values(array_filter(
$notes,
function (Note $note) use ($terms) : bool {
return $this->searchTermsInNote($note, $terms);
}
));
}
private function searchTermsInNote(Note $note, array $terms) : bool {
try {
$d = $note->getData();
$strings = [ $d['title'], $d['category'], $d['content'] ];
foreach ($terms as $term) {
if (!$this->searchTermInData($strings, $term)) {
return false;
}
}
return true;
} catch (\Throwable $e) {
return false;
}
}
private function searchTermInData(array $strings, string $term) : bool {
foreach ($strings as $str) {
if (stripos($str, $term) !== false) {
return true;
}
}
return false;
}
/**
* @throws \OCP\Files\NotPermittedException