Skip to content

Commit

Permalink
add searching also for tables instead of only views
Browse files Browse the repository at this point in the history
- add search for tables
- small code cleanups

Signed-off-by: Florian Steffens <florian.steffens@nextcloud.com>
  • Loading branch information
Florian Steffens committed Sep 11, 2023
1 parent e3cb594 commit 1075a1b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
2 changes: 0 additions & 2 deletions lib/Db/TableMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
42 changes: 33 additions & 9 deletions lib/Search/SearchTablesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -91,36 +96,55 @@ 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
);
}, $views);

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 '';
}
}
}
6 changes: 1 addition & 5 deletions lib/Service/TableService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -388,20 +387,17 @@ 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 {
/** @var string $userId */
$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 [];
}
}
Expand Down

0 comments on commit 1075a1b

Please sign in to comment.