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 ab8047a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/Db/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\Search\ISearchQuery;
use OCP\Share\IShare;

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

/**
* @param IUser $user the user that performs the search
* @param ISearchQuery $query the query to search the forms
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
* @return Form[] array of Forms
*/
public function search(IUser $user, ISearchQuery $query): array {
$qb = $this->db->getQueryBuilder();

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

View check run for this annotation

Codecov / codecov/patch

lib/Db/FormMapper.php#L191-L192

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Db/FormMapper.php#L194

Added line #L194 was not covered by tests
}

/**
* Delete a Form including connected Questions, Submissions and shares.
* @param Form $form The form instance to delete
Expand Down
18 changes: 18 additions & 0 deletions lib/Search/FormsSearchResultEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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 OCP\Search\SearchResultEntry;

class FormsSearchResultEntry extends SearchResultEntry {
public function __construct(Form $form, string $formUrl) {
parent::__construct('', $form->getTitle(), $form->getDescription(), $formUrl, 'forms-dark');

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

View check run for this annotation

Codecov / codecov/patch

lib/Search/FormsSearchResultEntry.php#L15-L16

Added lines #L15 - L16 were not covered by tests
}
}
68 changes: 68 additions & 0 deletions lib/Search/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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\AppInfo\Application;
use OCA\Forms\Db\Form;
use OCA\Forms\Service\FormsService;
use OCP\IL10N;
use OCP\IURLGenerator;
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 25 in lib/Search/Provider.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L25

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L30

Added line #L30 was not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L32-L33

Added lines #L32 - L33 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L36-L37

Added lines #L36 - L37 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L40-L41

Added lines #L40 - L41 were not covered by tests

$results = array_map(function (Form $form) {
$formUrl = $this->urlGenerator->linkToRoute('forms.page.views', ['hash' => $form->getHash(), 'view' => 'submit']);
return [
'object' => $form,
'entry' => new FormsSearchResultEntry($form, $formUrl)
];
}, $forms);

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

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L43-L49

Added lines #L43 - L49 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L51-L53

Added lines #L51 - L53 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L55-L58

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

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L61-L62

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L64

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L66

Added line #L66 was not covered by tests
}
}
18 changes: 18 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,23 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType
return true;
}

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Service/FormsService.php#L709-L710

Added lines #L709 - L710 were not covered by tests
try {
$formsList = $this->formMapper->search($user, $query);
} catch (DoesNotExistException $e) {

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

View check run for this annotation

Codecov / codecov/patch

lib/Service/FormsService.php#L712-L713

Added lines #L712 - L713 were not covered by tests
// silent catch
}
return $formsList;

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

View check run for this annotation

Codecov / codecov/patch

lib/Service/FormsService.php#L716

Added line #L716 was not covered by tests
}

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

Expand Down

0 comments on commit ab8047a

Please sign in to comment.