From 1075a1b7848e185a6503098d31d91edd3fe19e81 Mon Sep 17 00:00:00 2001 From: Florian Steffens Date: Mon, 11 Sep 2023 12:31:42 +0200 Subject: [PATCH] add searching also for tables instead of only views - add search for tables - small code cleanups Signed-off-by: Florian Steffens --- lib/Db/TableMapper.php | 2 -- lib/Search/SearchTablesProvider.php | 42 ++++++++++++++++++++++------- lib/Service/TableService.php | 6 +---- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/Db/TableMapper.php b/lib/Db/TableMapper.php index 5d3093f6e..c51fb429c 100644 --- a/lib/Db/TableMapper.php +++ b/lib/Db/TableMapper.php @@ -102,8 +102,6 @@ public function search(string $term = null, ?string $userId = null, ?int $limit $qb->setFirstResult($offset); } - $sql = $qb->getSQL(); - return $this->findEntities($qb); } } diff --git a/lib/Search/SearchTablesProvider.php b/lib/Search/SearchTablesProvider.php index 0fa53109f..0ae9b351a 100644 --- a/lib/Search/SearchTablesProvider.php +++ b/lib/Search/SearchTablesProvider.php @@ -25,7 +25,9 @@ namespace OCA\Tables\Search; use OCA\Tables\AppInfo\Application; +use OCA\Tables\Db\Table; use OCA\Tables\Db\View; +use OCA\Tables\Service\TableService; use OCA\Tables\Service\ViewService; use OCP\App\IAppManager; use OCP\IL10N; @@ -40,15 +42,18 @@ class SearchTablesProvider implements IProvider { private IAppManager $appManager; private IL10N $l10n; private ViewService $viewService; + private TableService $tableService; private IURLGenerator $urlGenerator; public function __construct(IAppManager $appManager, IL10N $l10n, ViewService $viewService, + TableService $tableService, IURLGenerator $urlGenerator) { $this->appManager = $appManager; $this->l10n = $l10n; $this->viewService = $viewService; + $this->tableService = $tableService; $this->urlGenerator = $urlGenerator; } @@ -91,18 +96,31 @@ public function search(IUser $user, ISearchQuery $query): SearchResult { $offset = $query->getCursor(); $offset = $offset ? (int) $offset : 0; - $views = $this->viewService->search($term, $limit, $offset); - $appIconUrl = $this->urlGenerator->getAbsoluteURL( $this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg') ); - $formattedResults = array_map(function (View $view) use ($appIconUrl): SearchResultEntry { + // look for tables + $tables = $this->tableService->search($term, $limit, $offset); + $formattedTablesResults = array_map(function (Table $table) use ($appIconUrl): SearchResultEntry { + return new SearchResultEntry( + $appIconUrl, + $table->getEmoji() .' '. $table->getTitle(), + ($table->getOwnerDisplayName() ?? $table->getOwnership()) . ', ' . $this->l10n->n('%n row', '%n rows', $table->getRowsCount()), + $this->getInternalLink('table', $table->getId()), + '', + false + ); + }, $tables); + + // look for views + $views = $this->viewService->search($term, $limit, $offset); + $formattedViewResults = array_map(function (View $view) use ($appIconUrl): SearchResultEntry { return new SearchResultEntry( $appIconUrl, $view->getEmoji() .' '. $view->getTitle(), ($view->getOwnerDisplayName() ?? $view->getOwnership()) . ', ' . $this->l10n->n('%n row', '%n rows', $view->getRowsCount()), - $this->getInternalLink($view), + $this->getInternalLink('view', $view->getId()), '', false ); @@ -110,17 +128,23 @@ public function search(IUser $user, ISearchQuery $query): SearchResult { return SearchResult::paginated( $this->getName(), - $formattedResults, + array_merge($formattedViewResults, $formattedTablesResults), $offset + $limit ); } /** - * @param View $view + * @param string $nodeType + * @param int $nodeId * @return string */ - protected function getInternalLink(View $view): string { - return $this->urlGenerator->linkToRouteAbsolute(Application::APP_ID . '.page.index') - . '#/view/' . $view->getId(); + protected function getInternalLink(string $nodeType = 'table', int $nodeId): string { + $allowedNodeTypes = ['table', 'view']; + if(in_array($nodeType, $allowedNodeTypes)) { + return $this->urlGenerator->linkToRouteAbsolute(Application::APP_ID . '.page.index') + . '#/'.$nodeType.'/' . $nodeId; + } else { + return ''; + } } } diff --git a/lib/Service/TableService.php b/lib/Service/TableService.php index d9d9ce28d..599cdd3ef 100644 --- a/lib/Service/TableService.php +++ b/lib/Service/TableService.php @@ -132,7 +132,6 @@ public function findAll(?string $userId = null, bool $skipTableEnhancement = fal * @param Table $table * @param string $userId * @throws InternalError - * @throws MultipleObjectsReturnedException * @throws PermissionError */ private function enhanceTable(Table $table, string $userId): void { @@ -388,8 +387,6 @@ public function update(int $id, ?string $title, ?string $emoji, ?string $userId * @param int $offset * @param string|null $userId * @return array - * @throws MultipleObjectsReturnedException - * @throws PermissionError */ public function search(string $term, int $limit = 100, int $offset = 0, ?string $userId = null): array { try { @@ -397,11 +394,10 @@ public function search(string $term, int $limit = 100, int $offset = 0, ?string $userId = $this->permissionsService->preCheckUserId($userId); $tables = $this->mapper->search($term, $userId, $limit, $offset); foreach ($tables as &$table) { - /** @var string $userId */ $this->enhanceTable($table, $userId); } return $tables; - } catch (InternalError | \OCP\DB\Exception $e) { + } catch (InternalError | PermissionError | \OCP\DB\Exception $e) { return []; } }