-
-
Notifications
You must be signed in to change notification settings - Fork 111
feat: unified search #6243
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
Merged
Merged
feat: unified search #6243
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Libresign\Search; | ||
|
|
||
| use OCA\Libresign\AppInfo\Application; | ||
| use OCA\Libresign\Db\File; | ||
| use OCA\Libresign\Db\SignRequestMapper; | ||
| use OCP\App\IAppManager; | ||
| use OCP\Files\IMimeTypeDetector; | ||
| use OCP\Files\IRootFolder; | ||
| use OCP\IL10N; | ||
| use OCP\IURLGenerator; | ||
| use OCP\IUser; | ||
| use OCP\Search\IProvider; | ||
| use OCP\Search\ISearchQuery; | ||
| use OCP\Search\SearchResult; | ||
| use OCP\Search\SearchResultEntry; | ||
|
|
||
| class FileSearchProvider implements IProvider { | ||
| public function __construct( | ||
| private IL10N $l10n, | ||
| private IURLGenerator $urlGenerator, | ||
| private IRootFolder $rootFolder, | ||
| private IAppManager $appManager, | ||
| private IMimeTypeDetector $mimeTypeDetector, | ||
| private SignRequestMapper $fileMapper, | ||
| ) { | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function getId(): string { | ||
| return 'libresign_files'; | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function getName(): string { | ||
| return $this->l10n->t('LibreSign documents'); | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function getOrder(string $route, array $routeParameters): int { | ||
| if (strpos($route, Application::APP_ID . '.') === 0) { | ||
| return 0; | ||
| } | ||
| return 10; | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function search(IUser $user, ISearchQuery $query): SearchResult { | ||
| if (!$this->appManager->isEnabledForUser(Application::APP_ID, $user)) { | ||
| return SearchResult::complete($this->l10n->t('LibreSign documents'), []); | ||
| } | ||
|
|
||
| $term = $query->getTerm(); | ||
| $limit = $query->getLimit(); | ||
| $offset = $query->getCursor(); | ||
|
|
||
| try { | ||
| $files = $this->fileMapper->getFilesToSearchProvider($user, $term, $limit, (int)$offset); | ||
| } catch (\Exception $e) { | ||
| return SearchResult::complete($this->l10n->t('LibreSign documents'), []); | ||
| } | ||
|
|
||
| $results = array_map(function (File $file) use ($user) { | ||
| return $this->formatResult($file, $user); | ||
| }, $files); | ||
|
|
||
| return SearchResult::paginated( | ||
| $this->l10n->t('LibreSign documents'), | ||
| $results, | ||
| $offset + $limit | ||
| ); | ||
| } | ||
|
|
||
| private function formatResult(File $file): SearchResultEntry { | ||
| $userFolder = $this->rootFolder->getUserFolder($file->getUserId()); | ||
| $thumbnailUrl = ''; | ||
| $subline = ''; | ||
| $icon = ''; | ||
| $path = ''; | ||
|
|
||
| try { | ||
| $nodes = $userFolder->getById($file->getNodeId()); | ||
| if (!empty($nodes)) { | ||
| $node = array_shift($nodes); | ||
|
|
||
| $icon = $this->mimeTypeDetector->mimeTypeIcon($node->getMimetype()); | ||
|
|
||
| $thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute( | ||
| 'core.Preview.getPreviewByFileId', | ||
| [ | ||
| 'x' => 32, | ||
| 'y' => 32, | ||
| 'fileId' => $node->getId() | ||
| ] | ||
| ); | ||
|
|
||
| $path = $userFolder->getRelativePath($node->getPath()); | ||
| $subline = $this->formatSubline($path); | ||
| } | ||
| } catch (\Exception $e) { | ||
| } | ||
|
|
||
| $link = $this->urlGenerator->linkToRoute( | ||
| 'files.View.showFile', | ||
| ['fileid' => $file->getNodeId()] | ||
| ); | ||
|
|
||
| $searchResultEntry = new SearchResultEntry( | ||
| $thumbnailUrl, | ||
| $file->getName(), | ||
| $subline, | ||
| $this->urlGenerator->getAbsoluteURL($link), | ||
| $icon, | ||
| ); | ||
|
|
||
| $searchResultEntry->addAttribute('fileId', (string)$file->getNodeId()); | ||
| $searchResultEntry->addAttribute('path', $path); | ||
|
|
||
| return $searchResultEntry; | ||
| } | ||
|
|
||
| private function formatSubline(string $path): string { | ||
| if (strrpos($path, '/') > 0) { | ||
| $path = ltrim(dirname($path), '/'); | ||
| // TRANSLATORS This string indicates the location of a file in a given path. | ||
| return $this->l10n->t('in %s', [$path]); | ||
| } else { | ||
| return ''; | ||
| } | ||
| } | ||
|
vitormattos marked this conversation as resolved.
|
||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.