-
Notifications
You must be signed in to change notification settings - Fork 6
Adding search files functionality in Paperless from Nextcloud #49
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
base: main
Are you sure you want to change the base?
Changes from all commits
0e90eb5
0b2be22
d9f6a88
80c192c
49254f7
feb3dc7
72bea30
f6b0bd1
e392558
96dd592
dcb65fa
2e576db
a636dfe
6f597e6
559302b
f22e30f
b46d32b
78fcaa2
4efb291
3fb8924
e2028b3
19eb4b8
c045eb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file contains code derived from Nextcloud - Zulip | ||
| * @copyright Copyright (c) 2024, Edward Ly | ||
| * | ||
| * @author Edward Ly <contact@edward.ly> | ||
| * @author Goh Jin Di <jdgoh334@gmail.com> | ||
| * @license AGPL-3.0 | ||
| * | ||
| * This code is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License, version 3, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License, version 3, | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/> | ||
| * | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OCA\Paperless\Search; | ||
|
|
||
| use OCA\Paperless\AppInfo\Application; | ||
| use OCA\Paperless\Service\ApiService; | ||
| use OCA\Paperless\Service\ConfigService; | ||
| use OCP\App\IAppManager; | ||
| use OCP\IConfig; | ||
| use OCP\IDateTimeFormatter; | ||
| use OCP\IDateTimeZone; | ||
| 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; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| class SearchProvider implements IProvider { | ||
|
|
||
| public function __construct( | ||
| private LoggerInterface $logger, | ||
| private IAppManager $appManager, | ||
| private IL10N $l10n, | ||
| private IConfig $config, | ||
| private IURLGenerator $urlGenerator, | ||
| private IDateTimeFormatter $dateTimeFormatter, | ||
| private IDateTimeZone $dateTimeZone, | ||
| private ConfigService $configService, | ||
| private ApiService $apiService, | ||
| ) { | ||
| } | ||
|
|
||
| public function getId(): string { | ||
| return 'paperless-search-documents'; | ||
| } | ||
|
|
||
| public function getName(): string { | ||
| return $this->l10n->t('Paperless document search result'); | ||
| } | ||
|
|
||
| public function getOrder(string $route, array $routeParameters): int { | ||
| return 30; // Adjust priority as needed | ||
| } | ||
|
|
||
| public function search(IUser $user, ISearchQuery $query): SearchResult { | ||
| $offset = ($query->getCursor() ?? 0); | ||
| $limit = $query->getLimit(); | ||
|
|
||
| $term = $query->getTerm(); | ||
| $url = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'url'); | ||
| $apiKey = $this->configService->getConfig($user->getUID(), 'token'); | ||
|
|
||
| if ($url === '' || $apiKey === '') { | ||
| return SearchResult::paginated($this->getName(), [], 0); | ||
| } | ||
|
|
||
| $searchResult = $this->apiService->searchDocuments($user->getUID(), $term, $offset, $limit); | ||
|
|
||
| if (isset($searchResult['html'])) { | ||
| return SearchResult::paginated($this->getName(), [], 0); | ||
| } | ||
|
|
||
| $formattedResults = array_map(function (array $entry) use ($url): SearchResultEntry { | ||
| $finalThumbnailUrl = ''; | ||
| $title = $entry['title'] ?? 'Untitled'; | ||
| $context = strip_tags($entry['__search_hit__']['highlights'] ?? ''); | ||
| $link = $this->getLinkToPaperless($entry, $url); | ||
| return new SearchResultEntry( | ||
| $finalThumbnailUrl, | ||
| $title, | ||
| $context, | ||
| $link, | ||
| $finalThumbnailUrl, | ||
| true | ||
| ); | ||
| }, $searchResult); | ||
|
|
||
| return SearchResult::paginated( | ||
| $this->getName(), | ||
| $formattedResults, | ||
| $offset + $limit | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @param array $entry | ||
| * @param string $url | ||
| * @return string | ||
| */ | ||
| protected function getLinkToPaperless(array $entry, string $url): string { | ||
| return rtrim($url, '/') . '/documents/' . ($entry['id'] ?? '#'); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ | |
| class SettingsSection implements IIconSection { | ||
| public function __construct( | ||
| private IURLGenerator $urlGenerator, | ||
| private IL10N $l | ||
| private IL10N $l, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary change, you're not even changing anything else in this file
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this is the change made from the |
||
| ) { | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also add yourself to this authors list