Skip to content

Commit 66bd48f

Browse files
committed
adding options 'is:?' to search
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent feceb45 commit 66bd48f

File tree

5 files changed

+464
-13
lines changed

5 files changed

+464
-13
lines changed

lib/AppInfo/Application.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
use OCA\Circles\Notification\Notifier;
7373
use OCA\Circles\Service\ConfigService;
7474
use OCA\Circles\Service\DavService;
75-
use OCA\Circles\UnifiedSearch\UnifiedSearchProvider;
75+
use OCA\Circles\Search\UnifiedSearchProvider;
7676
use OCP\App\ManagerEvent;
7777
use OCP\AppFramework\App;
7878
use OCP\AppFramework\Bootstrap\IBootContext;
@@ -90,7 +90,6 @@
9090
use Symfony\Component\EventDispatcher\GenericEvent;
9191
use Throwable;
9292

93-
//use OCA\Files\App as FilesApp;
9493

9594
/**
9695
* Class Application

lib/Db/CoreQueryBuilder.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,36 @@ public function filterCircle(Circle $circle): void {
393393
return;
394394
}
395395

396+
$expr = $this->expr();
397+
$orX = $expr->orX();
396398
if ($circle->getDisplayName() !== '') {
397-
$this->searchInDBField('display_name', '%' . $circle->getDisplayName() . '%');
399+
$andX = $expr->andX();
400+
foreach (explode(' ', $circle->getDisplayName()) as $word) {
401+
$andX->add(
402+
$expr->iLike(
403+
$this->getDefaultSelectAlias() . '.' . 'display_name',
404+
$this->createNamedParameter('%' . $word . '%')
405+
)
406+
);
407+
}
408+
$orX->add($andX);
398409
}
410+
if ($circle->getDescription() !== '') {
411+
$orDescription = $expr->orX();
412+
foreach (explode(' ', $circle->getDescription()) as $word) {
413+
$orDescription->add(
414+
$expr->iLike(
415+
$this->getDefaultSelectAlias() . '.' . 'description',
416+
$this->createNamedParameter('%' . $word . '%')
417+
)
418+
);
419+
}
420+
$orX->add($orDescription);
421+
}
422+
if ($orX->count() > 0) {
423+
$this->andWhere($orX);
424+
}
425+
399426
if ($circle->getSource() > 0) {
400427
$this->limitInt('source', $circle->getSource());
401428
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
/**
7+
* Circles - Bring cloud-users closer together.
8+
*
9+
* This file is licensed under the Affero General Public License version 3 or
10+
* later. See the COPYING file.
11+
*
12+
* @author Maxence Lange <maxence@artificial-owl.com>
13+
* @copyright 2021
14+
* @license GNU AGPL version 3 or any later version
15+
*
16+
* This program is free software: you can redistribute it and/or modify
17+
* it under the terms of the GNU Affero General Public License as
18+
* published by the Free Software Foundation, either version 3 of the
19+
* License, or (at your option) any later version.
20+
*
21+
* This program is distributed in the hope that it will be useful,
22+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24+
* GNU Affero General Public License for more details.
25+
*
26+
* You should have received a copy of the GNU Affero General Public License
27+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
28+
*
29+
*/
30+
31+
32+
namespace OCA\Circles\Search;
33+
34+
35+
use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
36+
use Exception;
37+
use OCA\Circles\Exceptions\ParseMemberLevelException;
38+
use OCA\Circles\Model\Member;
39+
use OCA\Circles\Service\FederatedUserService;
40+
use OCA\Circles\Service\SearchService;
41+
use OCP\IL10N;
42+
use OCP\IUser;
43+
use OCP\Search\IProvider;
44+
use OCP\Search\ISearchQuery;
45+
use OCP\Search\SearchResult;
46+
47+
class UnifiedSearchProvider implements IProvider {
48+
49+
50+
const PROVIDER_ID = 'circles';
51+
const ORDER = 9;
52+
53+
54+
use TArrayTools;
55+
56+
57+
/** @var IL10N */
58+
private $l10n;
59+
60+
/** @var FederatedUserService */
61+
private $federatedUserService;
62+
63+
/** @var SearchService */
64+
private $searchService;
65+
66+
67+
/**
68+
* @param IL10N $l10n
69+
* @param FederatedUserService $federatedUserService
70+
* @param SearchService $searchService
71+
*/
72+
public function __construct(
73+
IL10N $l10n,
74+
FederatedUserService $federatedUserService,
75+
SearchService $searchService
76+
) {
77+
$this->l10n = $l10n;
78+
$this->federatedUserService = $federatedUserService;
79+
$this->searchService = $searchService;
80+
}
81+
82+
83+
/**
84+
* return unique id of the provider
85+
*/
86+
public function getId(): string {
87+
return self::PROVIDER_ID;
88+
}
89+
90+
91+
/**
92+
* @return string
93+
*/
94+
public function getName(): string {
95+
return $this->l10n->t('Circles');
96+
}
97+
98+
99+
/**
100+
* @param string $route
101+
* @param array $routeParameters
102+
*
103+
* @return int
104+
*/
105+
public function getOrder(string $route, array $routeParameters): int {
106+
return self::ORDER;
107+
}
108+
109+
110+
/**
111+
* @param IUser $user
112+
* @param ISearchQuery $query
113+
*
114+
* @return SearchResult
115+
*/
116+
public function search(IUser $user, ISearchQuery $query): SearchResult {
117+
$term = $query->getTerm();
118+
$options = $this->extractOptions($term);
119+
120+
$result = [];
121+
try {
122+
$this->federatedUserService->setLocalCurrentUser($user);
123+
$result = $this->searchService->unifiedSearch($term, $options);
124+
} catch (Exception $e) {
125+
}
126+
127+
return SearchResult::paginated(
128+
$this->l10n->t('Circles'),
129+
$result,
130+
($query->getCursor() ?? 0) + $query->getLimit()
131+
);
132+
}
133+
134+
135+
/**
136+
* This is temporary, should be handled by core to extract Options from Term
137+
*
138+
* @param string $term
139+
*
140+
* @return array
141+
*/
142+
private function extractOptions(string &$term): array {
143+
$new = $options = [];
144+
foreach (explode(' ', $term) as $word) {
145+
$word = trim($word);
146+
if (strtolower(substr($word, 0, 3)) === 'is:') {
147+
try {
148+
$options['level'] = Member::parseLevelString(substr($word, 3));
149+
} catch (ParseMemberLevelException $e) {
150+
}
151+
} else {
152+
$new[] = $word;
153+
}
154+
}
155+
156+
$term = trim(implode(' ', $new));
157+
158+
return $options;
159+
}
160+
}
161+

lib/Search/UnifiedSearchResult.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
/**
7+
* Circles - Bring cloud-users closer together.
8+
*
9+
* This file is licensed under the Affero General Public License version 3 or
10+
* later. See the COPYING file.
11+
*
12+
* @author Maxence Lange <maxence@artificial-owl.com>
13+
* @copyright 2021
14+
* @license GNU AGPL version 3 or any later version
15+
*
16+
* This program is free software: you can redistribute it and/or modify
17+
* it under the terms of the GNU Affero General Public License as
18+
* published by the Free Software Foundation, either version 3 of the
19+
* License, or (at your option) any later version.
20+
*
21+
* This program is distributed in the hope that it will be useful,
22+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24+
* GNU Affero General Public License for more details.
25+
*
26+
* You should have received a copy of the GNU Affero General Public License
27+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
28+
*
29+
*/
30+
31+
32+
namespace OCA\Circles\Search;
33+
34+
35+
use OCP\Search\SearchResultEntry;
36+
37+
class UnifiedSearchResult extends SearchResultEntry {
38+
39+
40+
/**
41+
* UnifiedSearchResult constructor.
42+
*
43+
* @param string $thumbnailUrl
44+
* @param string $title
45+
* @param string $subline
46+
* @param string $resourceUrl
47+
* @param string $icon
48+
* @param bool $rounded
49+
*/
50+
public function __construct(
51+
string $thumbnailUrl = '',
52+
string $title = '',
53+
string $subline = '',
54+
string $resourceUrl = '',
55+
string $icon = '',
56+
bool $rounded = false
57+
) {
58+
parent::__construct($thumbnailUrl, $title, $subline, $resourceUrl, $icon, $rounded);
59+
}
60+
61+
62+
/**
63+
* @return string
64+
*/
65+
public function getThumbnailUrl(): string {
66+
return $this->thumbnailUrl;
67+
}
68+
69+
/**
70+
* @param string $thumbnailUrl
71+
*
72+
* @return UnifiedSearchResult
73+
*/
74+
public function setThumbnailUrl(string $thumbnailUrl): self {
75+
$this->thumbnailUrl = $thumbnailUrl;
76+
77+
return $this;
78+
}
79+
80+
81+
/**
82+
* @return string
83+
*/
84+
public function getTitle(): string {
85+
return $this->title;
86+
}
87+
88+
/**
89+
* @param string $title
90+
*
91+
* @return UnifiedSearchResult
92+
*/
93+
public function setTitle(string $title): self {
94+
$this->title = $title;
95+
96+
return $this;
97+
}
98+
99+
100+
/**
101+
* @return string
102+
*/
103+
public function getSubline(): string {
104+
return $this->subline;
105+
}
106+
107+
/**
108+
* @param string $subline
109+
*
110+
* @return UnifiedSearchResult
111+
*/
112+
public function setSubline(string $subline): self {
113+
$this->subline = $subline;
114+
115+
return $this;
116+
}
117+
118+
119+
/**
120+
* @return string
121+
*/
122+
public function getResourceUrl(): string {
123+
return $this->resourceUrl;
124+
}
125+
126+
/**
127+
* @param string $resourceUrl
128+
*
129+
* @return UnifiedSearchResult
130+
*/
131+
public function setResourceUrl(string $resourceUrl): self {
132+
$this->resourceUrl = $resourceUrl;
133+
134+
return $this;
135+
}
136+
137+
138+
/**
139+
* @return string
140+
*/
141+
public function getIcon(): string {
142+
return $this->icon;
143+
}
144+
145+
/**
146+
* @param string $icon
147+
*
148+
* @return UnifiedSearchResult
149+
*/
150+
public function setIcon(string $icon): self {
151+
$this->icon = $icon;
152+
153+
return $this;
154+
}
155+
156+
157+
/**
158+
* @return bool
159+
*/
160+
public function isRounded(): bool {
161+
return $this->rounded;
162+
}
163+
164+
/**
165+
* @param bool $rounded
166+
*
167+
* @return UnifiedSearchResult
168+
*/
169+
public function setRounded(bool $rounded): self {
170+
$this->rounded = $rounded;
171+
172+
return $this;
173+
}
174+
175+
}
176+

0 commit comments

Comments
 (0)