Completed
Push — master ( 58d870...e50603 )
by
unknown
02:12
created

SearchService::searchRequestCannotBeEmpty()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 3
nc 2
nop 1
1
<?php
2
/**
3
 * FullTextSearch - Full text search framework for Nextcloud
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\FullTextSearch\Service;
28
29
use Exception;
30
use OC\App\AppManager;
31
use OC\User\NoUserException;
32
use OCA\Circles\Api\v1\Circles;
33
use OCA\FullTextSearch\Exceptions\EmptySearchException;
34
use OCA\FullTextSearch\Exceptions\ProviderDoesNotExistException;
35
use OCA\FullTextSearch\IFullTextSearchPlatform;
36
use OCA\FullTextSearch\IFullTextSearchProvider;
37
use OCA\FullTextSearch\Model\DocumentAccess;
38
use OCA\FullTextSearch\Model\SearchRequest;
39
use OCA\FullTextSearch\Model\SearchResult;
40
use OCP\IGroupManager;
41
use OCP\IUser;
42
use OCP\IUserManager;
43
44
45
class SearchService {
46
47
	/** @var string */
48
	private $userId;
49
50
	/** @var AppManager */
51
	private $appManager;
52
53
	/** @var IUserManager */
54
	private $userManager;
55
56
	/** @var IGroupManager */
57
	private $groupManager;
58
59
	/** @var ConfigService */
60
	private $configService;
61
62
	/** @var ProviderService */
63
	private $providerService;
64
65
	/** @var PlatformService */
66
	private $platformService;
67
68
	/** @var MiscService */
69
	private $miscService;
70
71
72
	/**
73
	 * IndexService constructor.
74
	 *
75
	 * @param string $userId
76
	 * @param AppManager $appManager
77
	 * @param IUserManager $userManager
78
	 * @param IGroupManager $groupManager
79
	 * @param ConfigService $configService
80
	 * @param ProviderService $providerService
81
	 * @param PlatformService $platformService
82
	 * @param MiscService $miscService
83
	 */
84
	public function __construct(
85
		$userId, AppManager $appManager, IUserManager $userManager, IGroupManager $groupManager,
86
		ConfigService $configService, ProviderService $providerService,
87
		PlatformService $platformService,
88
		MiscService $miscService
89
	) {
90
		$this->userId = $userId;
91
		$this->appManager = $appManager;
92
		$this->userManager = $userManager;
93
		$this->groupManager = $groupManager;
94
		$this->configService = $configService;
95
		$this->providerService = $providerService;
96
		$this->platformService = $platformService;
97
		$this->miscService = $miscService;
98
	}
99
100
101
	/**
102
	 * @param string $userId
103
	 * @param SearchRequest $request
104
	 *
105
	 * @return SearchResult[]
106
	 * @throws EmptySearchException
107
	 * @throws Exception
108
	 * @throws ProviderDoesNotExistException
109
	 */
110
	public function search($userId, SearchRequest $request) {
111
112
		$this->searchRequestCannotBeEmpty($request);
113
114
		if ($userId === null) {
115
			$userId = $this->userId;
116
		}
117
118
		$user = $this->userManager->get($userId);
119
		if ($user === null) {
120
			throw new NoUserException('User does not exist');
121
		}
122
123
		$request->setAuthor($user->getUID());
124
		$request->cleanSearch();
125
126
		$providers = $this->providerService->getFilteredProviders($request->getProviders());
127
		$platform = $this->platformService->getPlatform();
128
129
		$access = $this->getDocumentAccessFromUser($user);
130
		$result = $this->searchFromProviders($platform, $providers, $access, $request);
131
132
		foreach ($result as $searchResult) {
133
			$searchResult->setPlatform($platform);
134
			$searchResult->setRequest($request);
135
		}
136
137
		return $result;
138
	}
139
140
141
	/**
142
	 * @param SearchRequest $request
143
	 *
144
	 * @throws EmptySearchException
145
	 */
146
	private function searchRequestCannotBeEmpty(SearchRequest $request) {
147
		if ($request === null || strlen($request->getSearch()) < 1) {
148
			throw new EmptySearchException('search cannot be empty');
149
		}
150
	}
151
152
153
	/**
154
	 * @param IFullTextSearchPlatform $platform
155
	 * @param DocumentAccess $access
156
	 * @param IFullTextSearchProvider[] $providers
157
	 * @param SearchRequest $request
158
	 *
159
	 * @return SearchResult[]
160
	 */
161
	private function searchFromProviders(
162
		IFullTextSearchPlatform $platform, $providers, DocumentAccess $access,
163
		SearchRequest $request
164
	) {
165
		$result = [];
166
		foreach ($providers AS $provider) {
167
			$provider->improveSearchRequest($request);
168
			$searchResult = $platform->searchDocuments($provider, $access, $request);
169
			$searchResult->setProvider($provider);
170
			$searchResult->setPlatform($platform);
171
172
			$provider->improveSearchResult($searchResult);
173
			$result[] = $searchResult;
174
		}
175
176
		return $result;
177
	}
178
179
180
	/**
181
	 * @param IUser $user
182
	 *
183
	 * @return DocumentAccess
184
	 */
185
	private function getDocumentAccessFromUser(IUser $user) {
186
		$rights = new DocumentAccess();
187
188
		$rights->setViewerId($user->getUID());
189
		$rights->setGroups($this->groupManager->getUserGroupIds($user));
190
191
		if ($this->appManager->isEnabledForUser('circles', $user)) {
192
			try {
193
				$rights->setCircles(Circles::joinedCircleIds($user->getUID()));
194
			} catch (Exception $e) {
195
				$this->miscService->log('Circles is set as enabled but: ' . $e->getMessage());
196
			}
197
		}
198
199
		return $rights;
200
	}
201
202
203
}