Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SearchProvider.php « AppInfo « lib - github.com/nextcloud/notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b56094e54fcff4cfda67311181cb5e129353086f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?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 {
	private Util $util;
	private NotesService $notesService;
	private IURLGenerator $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
		);
	}
}