Skip to content

Commit

Permalink
initial integration of Search Provider
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hartmann <[email protected]>
  • Loading branch information
Chartman123 committed Jan 3, 2025
1 parent 7afb7e2 commit e984b35
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/Db/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Search\ISearchQuery;
use OCP\Share\IShare;

/**
Expand Down Expand Up @@ -180,6 +181,16 @@ public function findAllByOwnerId(string $ownerId): array {
return $this->findEntities($qb);
}

/**
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
* @return Form[]
*/
public function search(ISearchQuery $query): array {
$qb = $this->db->getQueryBuilder();

Check warning on line 189 in lib/Db/FormMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/FormMapper.php#L188-L189

Added lines #L188 - L189 were not covered by tests
// TODO: implement search for Query Builder
return $this->findEntities($qb);

Check warning on line 191 in lib/Db/FormMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/FormMapper.php#L191

Added line #L191 was not covered by tests
}

/**
* Delete a Form including connected Questions, Submissions and shares.
* @param Form $form The form instance to delete
Expand Down
22 changes: 22 additions & 0 deletions lib/Search/FormsSearchResultEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Forms\Search;

use OCA\Forms\Controller\PageController;
use OCA\Forms\Db\Form;
use OCP\IURLGenerator;
use OCP\Search\SearchResultEntry;

class FormsSearchResultEntry extends SearchResultEntry {
public function __construct(Form $form) {
$urlGenerator = new IURLGenerator();

Check failure on line 18 in lib/Search/FormsSearchResultEntry.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable30

InterfaceInstantiation

lib/Search/FormsSearchResultEntry.php:18:23: InterfaceInstantiation: Interface OCP\IURLGenerator cannot be instantiated (see https://psalm.dev/158)

Check failure on line 18 in lib/Search/FormsSearchResultEntry.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InterfaceInstantiation

lib/Search/FormsSearchResultEntry.php:18:23: InterfaceInstantiation: Interface OCP\IURLGenerator cannot be instantiated (see https://psalm.dev/158)
$formUrl = $urlGenerator->linkToRoute('forms.page.views', ['hash' => $form->getHash(), 'view' => 'submit']);
parent::__construct('', $form->getTitle(), $form->getDescription(), $formUrl, 'forms-dark');

Check warning on line 20 in lib/Search/FormsSearchResultEntry.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/FormsSearchResultEntry.php#L17-L20

Added lines #L17 - L20 were not covered by tests
}
}
65 changes: 65 additions & 0 deletions lib/Search/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Forms\Search;

use OCA\Forms\Db\Form;
use OCA\Forms\Search\FormsSearchResultEntry;
use OCA\Forms\Service\FormsService;
use OCP\IL10N;
use OCP\IUser;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;

class SearchProvider implements IProvider {
/**
* @psalm-suppress PossiblyUnusedMethod
*/
public function __construct(

Check warning on line 24 in lib/Search/Provider.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L24

Added line #L24 was not covered by tests
private IL10N $l10n,
private FormsService $formsService,
) {
}

Check warning on line 28 in lib/Search/Provider.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L28

Added line #L28 was not covered by tests

public function getId(): string {
return 'forms';

Check warning on line 31 in lib/Search/Provider.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L30-L31

Added lines #L30 - L31 were not covered by tests
}

public function getName(): string {
return $this->l10n->t('Forms');

Check warning on line 35 in lib/Search/Provider.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L34-L35

Added lines #L34 - L35 were not covered by tests
}

public function search(IUser $user, ISearchQuery $query): SearchResult {
$forms = $this->formsService->search($query);

Check warning on line 39 in lib/Search/Provider.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L38-L39

Added lines #L38 - L39 were not covered by tests

$results = array_map(function (Form $form) {
return [
'object' => $form,
'entry' => new FormsSearchResultEntry($form)
];
}, $forms);

Check warning on line 46 in lib/Search/Provider.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L41-L46

Added lines #L41 - L46 were not covered by tests

$resultEntries = array_map(function (array $result) {
return $result['entry'];
}, $results);

Check warning on line 50 in lib/Search/Provider.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L48-L50

Added lines #L48 - L50 were not covered by tests

return SearchResult::complete(
$this->l10n->t('Forms'),
$resultEntries
);

Check warning on line 55 in lib/Search/Provider.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L52-L55

Added lines #L52 - L55 were not covered by tests
}

public function getOrder(string $route, array $routeParameters): int {
if (str_contains($route, Application::APP_ID)) {

Check failure on line 59 in lib/Search/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable30

UndefinedClass

lib/Search/Provider.php:59:34: UndefinedClass: Class, interface or enum named OCA\Forms\Search\Application does not exist (see https://psalm.dev/019)

Check failure on line 59 in lib/Search/Provider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedClass

lib/Search/Provider.php:59:34: UndefinedClass: Class, interface or enum named OCA\Forms\Search\Application does not exist (see https://psalm.dev/019)

Check warning on line 59 in lib/Search/Provider.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L58-L59

Added lines #L58 - L59 were not covered by tests
// Active app, prefer my results
return -1;

Check warning on line 61 in lib/Search/Provider.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L61

Added line #L61 was not covered by tests
}
return 77;

Check warning on line 63 in lib/Search/Provider.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L63

Added line #L63 was not covered by tests
}
}
26 changes: 26 additions & 0 deletions lib/Service/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Search\ISearchQuery;
use OCP\Security\ISecureRandom;
use OCP\Share\IShare;

Expand Down Expand Up @@ -698,6 +699,31 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType
return true;
}

/**
* Get list of forms
*
* @param ISearchQuery $query the query to search the forms
* @return array list of forms that match the query
*/
public function search(ISearchQuery $query): array {
$formsList = [];

Check warning on line 709 in lib/Service/FormsService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/FormsService.php#L708-L709

Added lines #L708 - L709 were not covered by tests
try {
$forms = $this->formMapper->search($query);

Check warning on line 711 in lib/Service/FormsService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/FormsService.php#L711

Added line #L711 was not covered by tests

foreach ($forms as $form) {

Check warning on line 713 in lib/Service/FormsService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/FormsService.php#L713

Added line #L713 was not covered by tests
try {
// TODO: Check that user has at least read permission
$formsList[] = $form;
} catch (ForbiddenException $e) {

Check failure on line 717 in lib/Service/FormsService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable30

UndefinedClass

lib/Service/FormsService.php:717:14: UndefinedClass: Class, interface or enum named OCA\Forms\Service\ForbiddenException does not exist (see https://psalm.dev/019)

Check failure on line 717 in lib/Service/FormsService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedClass

lib/Service/FormsService.php:717:14: UndefinedClass: Class, interface or enum named OCA\Forms\Service\ForbiddenException does not exist (see https://psalm.dev/019)
continue;

Check warning on line 718 in lib/Service/FormsService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/FormsService.php#L716-L718

Added lines #L716 - L718 were not covered by tests
}
}
} catch (DoesNotExistException $e) {

Check warning on line 721 in lib/Service/FormsService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/FormsService.php#L721

Added line #L721 was not covered by tests
// silent catch
}
return $formsList;

Check warning on line 724 in lib/Service/FormsService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/FormsService.php#L724

Added line #L724 was not covered by tests
}

public function getFilePath(Form $form): ?string {
$fileId = $form->getFileId();

Expand Down

0 comments on commit e984b35

Please sign in to comment.