@@ -58,375 +58,375 @@ |
||
58 | 58 | |
59 | 59 | class ShareesAPIController extends OCSController { |
60 | 60 | |
61 | - /** @var string */ |
|
62 | - protected $userId; |
|
63 | - |
|
64 | - /** @var IConfig */ |
|
65 | - protected $config; |
|
66 | - |
|
67 | - /** @var IURLGenerator */ |
|
68 | - protected $urlGenerator; |
|
69 | - |
|
70 | - /** @var IManager */ |
|
71 | - protected $shareManager; |
|
72 | - |
|
73 | - /** @var int */ |
|
74 | - protected $offset = 0; |
|
75 | - |
|
76 | - /** @var int */ |
|
77 | - protected $limit = 10; |
|
78 | - |
|
79 | - /** @var array */ |
|
80 | - protected $result = [ |
|
81 | - 'exact' => [ |
|
82 | - 'users' => [], |
|
83 | - 'groups' => [], |
|
84 | - 'remotes' => [], |
|
85 | - 'remote_groups' => [], |
|
86 | - 'emails' => [], |
|
87 | - 'circles' => [], |
|
88 | - 'rooms' => [], |
|
89 | - ], |
|
90 | - 'users' => [], |
|
91 | - 'groups' => [], |
|
92 | - 'remotes' => [], |
|
93 | - 'remote_groups' => [], |
|
94 | - 'emails' => [], |
|
95 | - 'lookup' => [], |
|
96 | - 'circles' => [], |
|
97 | - 'rooms' => [], |
|
98 | - 'lookupEnabled' => false, |
|
99 | - ]; |
|
100 | - |
|
101 | - protected $reachedEndFor = []; |
|
102 | - /** @var ISearch */ |
|
103 | - private $collaboratorSearch; |
|
104 | - |
|
105 | - /** |
|
106 | - * @param string $UserId |
|
107 | - * @param string $appName |
|
108 | - * @param IRequest $request |
|
109 | - * @param IConfig $config |
|
110 | - * @param IURLGenerator $urlGenerator |
|
111 | - * @param IManager $shareManager |
|
112 | - * @param ISearch $collaboratorSearch |
|
113 | - */ |
|
114 | - public function __construct( |
|
115 | - $UserId, |
|
116 | - string $appName, |
|
117 | - IRequest $request, |
|
118 | - IConfig $config, |
|
119 | - IURLGenerator $urlGenerator, |
|
120 | - IManager $shareManager, |
|
121 | - ISearch $collaboratorSearch |
|
122 | - ) { |
|
123 | - parent::__construct($appName, $request); |
|
124 | - $this->userId = $UserId; |
|
125 | - $this->config = $config; |
|
126 | - $this->urlGenerator = $urlGenerator; |
|
127 | - $this->shareManager = $shareManager; |
|
128 | - $this->collaboratorSearch = $collaboratorSearch; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * @NoAdminRequired |
|
133 | - * |
|
134 | - * @param string $search |
|
135 | - * @param string $itemType |
|
136 | - * @param int $page |
|
137 | - * @param int $perPage |
|
138 | - * @param int|int[] $shareType |
|
139 | - * @param bool $lookup |
|
140 | - * @return DataResponse |
|
141 | - * @throws OCSBadRequestException |
|
142 | - */ |
|
143 | - public function search(string $search = '', string $itemType = null, int $page = 1, int $perPage = 200, $shareType = null, bool $lookup = false): DataResponse { |
|
144 | - |
|
145 | - // only search for string larger than a given threshold |
|
146 | - $threshold = $this->config->getSystemValueInt('sharing.minSearchStringLength', 0); |
|
147 | - if (strlen($search) < $threshold) { |
|
148 | - return new DataResponse($this->result); |
|
149 | - } |
|
150 | - |
|
151 | - // never return more than the max. number of results configured in the config.php |
|
152 | - $maxResults = $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT); |
|
153 | - if ($maxResults > 0) { |
|
154 | - $perPage = min($perPage, $maxResults); |
|
155 | - } |
|
156 | - if ($perPage <= 0) { |
|
157 | - throw new OCSBadRequestException('Invalid perPage argument'); |
|
158 | - } |
|
159 | - if ($page <= 0) { |
|
160 | - throw new OCSBadRequestException('Invalid page'); |
|
161 | - } |
|
162 | - |
|
163 | - $shareTypes = [ |
|
164 | - IShare::TYPE_USER, |
|
165 | - ]; |
|
166 | - |
|
167 | - if ($itemType === null) { |
|
168 | - throw new OCSBadRequestException('Missing itemType'); |
|
169 | - } elseif ($itemType === 'file' || $itemType === 'folder') { |
|
170 | - if ($this->shareManager->allowGroupSharing()) { |
|
171 | - $shareTypes[] = IShare::TYPE_GROUP; |
|
172 | - } |
|
173 | - |
|
174 | - if ($this->isRemoteSharingAllowed($itemType)) { |
|
175 | - $shareTypes[] = IShare::TYPE_REMOTE; |
|
176 | - } |
|
177 | - |
|
178 | - if ($this->isRemoteGroupSharingAllowed($itemType)) { |
|
179 | - $shareTypes[] = IShare::TYPE_REMOTE_GROUP; |
|
180 | - } |
|
181 | - |
|
182 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) { |
|
183 | - $shareTypes[] = IShare::TYPE_EMAIL; |
|
184 | - } |
|
185 | - |
|
186 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_ROOM)) { |
|
187 | - $shareTypes[] = IShare::TYPE_ROOM; |
|
188 | - } |
|
189 | - } else { |
|
190 | - if ($this->shareManager->allowGroupSharing()) { |
|
191 | - $shareTypes[] = IShare::TYPE_GROUP; |
|
192 | - } |
|
193 | - $shareTypes[] = IShare::TYPE_EMAIL; |
|
194 | - } |
|
195 | - |
|
196 | - // FIXME: DI |
|
197 | - if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
198 | - $shareTypes[] = IShare::TYPE_CIRCLE; |
|
199 | - } |
|
200 | - |
|
201 | - if ($shareType !== null && is_array($shareType)) { |
|
202 | - $shareTypes = array_intersect($shareTypes, $shareType); |
|
203 | - } elseif (is_numeric($shareType)) { |
|
204 | - $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
|
205 | - } |
|
206 | - sort($shareTypes); |
|
207 | - |
|
208 | - $this->limit = $perPage; |
|
209 | - $this->offset = $perPage * ($page - 1); |
|
210 | - |
|
211 | - // In global scale mode we always search the loogup server |
|
212 | - if ($this->config->getSystemValueBool('gs.enabled', false)) { |
|
213 | - $lookup = true; |
|
214 | - $this->result['lookupEnabled'] = true; |
|
215 | - } else { |
|
216 | - $this->result['lookupEnabled'] = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'yes') === 'yes'; |
|
217 | - } |
|
218 | - |
|
219 | - [$result, $hasMoreResults] = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset); |
|
220 | - |
|
221 | - // extra treatment for 'exact' subarray, with a single merge expected keys might be lost |
|
222 | - if (isset($result['exact'])) { |
|
223 | - $result['exact'] = array_merge($this->result['exact'], $result['exact']); |
|
224 | - } |
|
225 | - $this->result = array_merge($this->result, $result); |
|
226 | - $response = new DataResponse($this->result); |
|
227 | - |
|
228 | - if ($hasMoreResults) { |
|
229 | - $response->addHeader('Link', $this->getPaginationLink($page, [ |
|
230 | - 'search' => $search, |
|
231 | - 'itemType' => $itemType, |
|
232 | - 'shareType' => $shareTypes, |
|
233 | - 'perPage' => $perPage, |
|
234 | - ])); |
|
235 | - } |
|
236 | - |
|
237 | - return $response; |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @param string $user |
|
242 | - * @param int $shareType |
|
243 | - * |
|
244 | - * @return Generator<array<string>> |
|
245 | - */ |
|
246 | - private function getAllShareesByType(string $user, int $shareType): Generator { |
|
247 | - $offset = 0; |
|
248 | - $pageSize = 50; |
|
249 | - |
|
250 | - while (count($page = $this->shareManager->getSharesBy( |
|
251 | - $user, |
|
252 | - $shareType, |
|
253 | - null, |
|
254 | - false, |
|
255 | - $pageSize, |
|
256 | - $offset |
|
257 | - ))) { |
|
258 | - foreach ($page as $share) { |
|
259 | - yield [$share->getSharedWith(), $share->getSharedWithDisplayName() ?? $share->getSharedWith()]; |
|
260 | - } |
|
261 | - |
|
262 | - $offset += $pageSize; |
|
263 | - } |
|
264 | - } |
|
265 | - |
|
266 | - private function sortShareesByFrequency(array $sharees): array { |
|
267 | - usort($sharees, function (array $s1, array $s2): int { |
|
268 | - return $s2['count'] - $s1['count']; |
|
269 | - }); |
|
270 | - return $sharees; |
|
271 | - } |
|
272 | - |
|
273 | - private $searchResultTypeMap = [ |
|
274 | - IShare::TYPE_USER => 'users', |
|
275 | - IShare::TYPE_GROUP => 'groups', |
|
276 | - IShare::TYPE_REMOTE => 'remotes', |
|
277 | - IShare::TYPE_REMOTE_GROUP => 'remote_groups', |
|
278 | - IShare::TYPE_EMAIL => 'emails', |
|
279 | - ]; |
|
280 | - |
|
281 | - private function getAllSharees(string $user, array $shareTypes): ISearchResult { |
|
282 | - $result = []; |
|
283 | - foreach ($shareTypes as $shareType) { |
|
284 | - $sharees = $this->getAllShareesByType($user, $shareType); |
|
285 | - $shareTypeResults = []; |
|
286 | - foreach ($sharees as [$sharee, $displayname]) { |
|
287 | - if (!isset($this->searchResultTypeMap[$shareType])) { |
|
288 | - continue; |
|
289 | - } |
|
290 | - |
|
291 | - if (!isset($shareTypeResults[$sharee])) { |
|
292 | - $shareTypeResults[$sharee] = [ |
|
293 | - 'count' => 1, |
|
294 | - 'label' => $displayname, |
|
295 | - 'value' => [ |
|
296 | - 'shareType' => $shareType, |
|
297 | - 'shareWith' => $sharee, |
|
298 | - ], |
|
299 | - ]; |
|
300 | - } else { |
|
301 | - $shareTypeResults[$sharee]['count']++; |
|
302 | - } |
|
303 | - } |
|
304 | - $result = array_merge($result, array_values($shareTypeResults)); |
|
305 | - } |
|
306 | - |
|
307 | - $top5 = array_slice( |
|
308 | - $this->sortShareesByFrequency($result), |
|
309 | - 0, |
|
310 | - 5 |
|
311 | - ); |
|
312 | - |
|
313 | - $searchResult = new SearchResult(); |
|
314 | - foreach ($this->searchResultTypeMap as $int => $str) { |
|
315 | - $searchResult->addResultSet(new SearchResultType($str), [], []); |
|
316 | - foreach ($top5 as $x) { |
|
317 | - if ($x['value']['shareType'] === $int) { |
|
318 | - $searchResult->addResultSet(new SearchResultType($str), [], [$x]); |
|
319 | - } |
|
320 | - } |
|
321 | - } |
|
322 | - return $searchResult; |
|
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * @NoAdminRequired |
|
327 | - * |
|
328 | - * @param string $itemType |
|
329 | - * @return DataResponse |
|
330 | - * @throws OCSBadRequestException |
|
331 | - */ |
|
332 | - public function findRecommended(string $itemType = null, $shareType = null): DataResponse { |
|
333 | - $shareTypes = [ |
|
334 | - IShare::TYPE_USER, |
|
335 | - ]; |
|
336 | - |
|
337 | - if ($itemType === null) { |
|
338 | - throw new OCSBadRequestException('Missing itemType'); |
|
339 | - } elseif ($itemType === 'file' || $itemType === 'folder') { |
|
340 | - if ($this->shareManager->allowGroupSharing()) { |
|
341 | - $shareTypes[] = IShare::TYPE_GROUP; |
|
342 | - } |
|
343 | - |
|
344 | - if ($this->isRemoteSharingAllowed($itemType)) { |
|
345 | - $shareTypes[] = IShare::TYPE_REMOTE; |
|
346 | - } |
|
347 | - |
|
348 | - if ($this->isRemoteGroupSharingAllowed($itemType)) { |
|
349 | - $shareTypes[] = IShare::TYPE_REMOTE_GROUP; |
|
350 | - } |
|
351 | - |
|
352 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) { |
|
353 | - $shareTypes[] = IShare::TYPE_EMAIL; |
|
354 | - } |
|
355 | - |
|
356 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_ROOM)) { |
|
357 | - $shareTypes[] = IShare::TYPE_ROOM; |
|
358 | - } |
|
359 | - } else { |
|
360 | - $shareTypes[] = IShare::TYPE_GROUP; |
|
361 | - $shareTypes[] = IShare::TYPE_EMAIL; |
|
362 | - } |
|
363 | - |
|
364 | - // FIXME: DI |
|
365 | - if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
366 | - $shareTypes[] = IShare::TYPE_CIRCLE; |
|
367 | - } |
|
368 | - |
|
369 | - if (isset($_GET['shareType']) && is_array($_GET['shareType'])) { |
|
370 | - $shareTypes = array_intersect($shareTypes, $_GET['shareType']); |
|
371 | - sort($shareTypes); |
|
372 | - } elseif (is_numeric($shareType)) { |
|
373 | - $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
|
374 | - sort($shareTypes); |
|
375 | - } |
|
376 | - |
|
377 | - return new DataResponse( |
|
378 | - $this->getAllSharees($this->userId, $shareTypes)->asArray() |
|
379 | - ); |
|
380 | - } |
|
381 | - |
|
382 | - /** |
|
383 | - * Method to get out the static call for better testing |
|
384 | - * |
|
385 | - * @param string $itemType |
|
386 | - * @return bool |
|
387 | - */ |
|
388 | - protected function isRemoteSharingAllowed(string $itemType): bool { |
|
389 | - try { |
|
390 | - // FIXME: static foo makes unit testing unnecessarily difficult |
|
391 | - $backend = \OC\Share\Share::getBackend($itemType); |
|
392 | - return $backend->isShareTypeAllowed(IShare::TYPE_REMOTE); |
|
393 | - } catch (\Exception $e) { |
|
394 | - return false; |
|
395 | - } |
|
396 | - } |
|
397 | - |
|
398 | - protected function isRemoteGroupSharingAllowed(string $itemType): bool { |
|
399 | - try { |
|
400 | - // FIXME: static foo makes unit testing unnecessarily difficult |
|
401 | - $backend = \OC\Share\Share::getBackend($itemType); |
|
402 | - return $backend->isShareTypeAllowed(IShare::TYPE_REMOTE_GROUP); |
|
403 | - } catch (\Exception $e) { |
|
404 | - return false; |
|
405 | - } |
|
406 | - } |
|
407 | - |
|
408 | - |
|
409 | - /** |
|
410 | - * Generates a bunch of pagination links for the current page |
|
411 | - * |
|
412 | - * @param int $page Current page |
|
413 | - * @param array $params Parameters for the URL |
|
414 | - * @return string |
|
415 | - */ |
|
416 | - protected function getPaginationLink(int $page, array $params): string { |
|
417 | - if ($this->isV2()) { |
|
418 | - $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
419 | - } else { |
|
420 | - $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
421 | - } |
|
422 | - $params['page'] = $page + 1; |
|
423 | - return '<' . $url . http_build_query($params) . '>; rel="next"'; |
|
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * @return bool |
|
428 | - */ |
|
429 | - protected function isV2(): bool { |
|
430 | - return $this->request->getScriptName() === '/ocs/v2.php'; |
|
431 | - } |
|
61 | + /** @var string */ |
|
62 | + protected $userId; |
|
63 | + |
|
64 | + /** @var IConfig */ |
|
65 | + protected $config; |
|
66 | + |
|
67 | + /** @var IURLGenerator */ |
|
68 | + protected $urlGenerator; |
|
69 | + |
|
70 | + /** @var IManager */ |
|
71 | + protected $shareManager; |
|
72 | + |
|
73 | + /** @var int */ |
|
74 | + protected $offset = 0; |
|
75 | + |
|
76 | + /** @var int */ |
|
77 | + protected $limit = 10; |
|
78 | + |
|
79 | + /** @var array */ |
|
80 | + protected $result = [ |
|
81 | + 'exact' => [ |
|
82 | + 'users' => [], |
|
83 | + 'groups' => [], |
|
84 | + 'remotes' => [], |
|
85 | + 'remote_groups' => [], |
|
86 | + 'emails' => [], |
|
87 | + 'circles' => [], |
|
88 | + 'rooms' => [], |
|
89 | + ], |
|
90 | + 'users' => [], |
|
91 | + 'groups' => [], |
|
92 | + 'remotes' => [], |
|
93 | + 'remote_groups' => [], |
|
94 | + 'emails' => [], |
|
95 | + 'lookup' => [], |
|
96 | + 'circles' => [], |
|
97 | + 'rooms' => [], |
|
98 | + 'lookupEnabled' => false, |
|
99 | + ]; |
|
100 | + |
|
101 | + protected $reachedEndFor = []; |
|
102 | + /** @var ISearch */ |
|
103 | + private $collaboratorSearch; |
|
104 | + |
|
105 | + /** |
|
106 | + * @param string $UserId |
|
107 | + * @param string $appName |
|
108 | + * @param IRequest $request |
|
109 | + * @param IConfig $config |
|
110 | + * @param IURLGenerator $urlGenerator |
|
111 | + * @param IManager $shareManager |
|
112 | + * @param ISearch $collaboratorSearch |
|
113 | + */ |
|
114 | + public function __construct( |
|
115 | + $UserId, |
|
116 | + string $appName, |
|
117 | + IRequest $request, |
|
118 | + IConfig $config, |
|
119 | + IURLGenerator $urlGenerator, |
|
120 | + IManager $shareManager, |
|
121 | + ISearch $collaboratorSearch |
|
122 | + ) { |
|
123 | + parent::__construct($appName, $request); |
|
124 | + $this->userId = $UserId; |
|
125 | + $this->config = $config; |
|
126 | + $this->urlGenerator = $urlGenerator; |
|
127 | + $this->shareManager = $shareManager; |
|
128 | + $this->collaboratorSearch = $collaboratorSearch; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * @NoAdminRequired |
|
133 | + * |
|
134 | + * @param string $search |
|
135 | + * @param string $itemType |
|
136 | + * @param int $page |
|
137 | + * @param int $perPage |
|
138 | + * @param int|int[] $shareType |
|
139 | + * @param bool $lookup |
|
140 | + * @return DataResponse |
|
141 | + * @throws OCSBadRequestException |
|
142 | + */ |
|
143 | + public function search(string $search = '', string $itemType = null, int $page = 1, int $perPage = 200, $shareType = null, bool $lookup = false): DataResponse { |
|
144 | + |
|
145 | + // only search for string larger than a given threshold |
|
146 | + $threshold = $this->config->getSystemValueInt('sharing.minSearchStringLength', 0); |
|
147 | + if (strlen($search) < $threshold) { |
|
148 | + return new DataResponse($this->result); |
|
149 | + } |
|
150 | + |
|
151 | + // never return more than the max. number of results configured in the config.php |
|
152 | + $maxResults = $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT); |
|
153 | + if ($maxResults > 0) { |
|
154 | + $perPage = min($perPage, $maxResults); |
|
155 | + } |
|
156 | + if ($perPage <= 0) { |
|
157 | + throw new OCSBadRequestException('Invalid perPage argument'); |
|
158 | + } |
|
159 | + if ($page <= 0) { |
|
160 | + throw new OCSBadRequestException('Invalid page'); |
|
161 | + } |
|
162 | + |
|
163 | + $shareTypes = [ |
|
164 | + IShare::TYPE_USER, |
|
165 | + ]; |
|
166 | + |
|
167 | + if ($itemType === null) { |
|
168 | + throw new OCSBadRequestException('Missing itemType'); |
|
169 | + } elseif ($itemType === 'file' || $itemType === 'folder') { |
|
170 | + if ($this->shareManager->allowGroupSharing()) { |
|
171 | + $shareTypes[] = IShare::TYPE_GROUP; |
|
172 | + } |
|
173 | + |
|
174 | + if ($this->isRemoteSharingAllowed($itemType)) { |
|
175 | + $shareTypes[] = IShare::TYPE_REMOTE; |
|
176 | + } |
|
177 | + |
|
178 | + if ($this->isRemoteGroupSharingAllowed($itemType)) { |
|
179 | + $shareTypes[] = IShare::TYPE_REMOTE_GROUP; |
|
180 | + } |
|
181 | + |
|
182 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) { |
|
183 | + $shareTypes[] = IShare::TYPE_EMAIL; |
|
184 | + } |
|
185 | + |
|
186 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_ROOM)) { |
|
187 | + $shareTypes[] = IShare::TYPE_ROOM; |
|
188 | + } |
|
189 | + } else { |
|
190 | + if ($this->shareManager->allowGroupSharing()) { |
|
191 | + $shareTypes[] = IShare::TYPE_GROUP; |
|
192 | + } |
|
193 | + $shareTypes[] = IShare::TYPE_EMAIL; |
|
194 | + } |
|
195 | + |
|
196 | + // FIXME: DI |
|
197 | + if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
198 | + $shareTypes[] = IShare::TYPE_CIRCLE; |
|
199 | + } |
|
200 | + |
|
201 | + if ($shareType !== null && is_array($shareType)) { |
|
202 | + $shareTypes = array_intersect($shareTypes, $shareType); |
|
203 | + } elseif (is_numeric($shareType)) { |
|
204 | + $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
|
205 | + } |
|
206 | + sort($shareTypes); |
|
207 | + |
|
208 | + $this->limit = $perPage; |
|
209 | + $this->offset = $perPage * ($page - 1); |
|
210 | + |
|
211 | + // In global scale mode we always search the loogup server |
|
212 | + if ($this->config->getSystemValueBool('gs.enabled', false)) { |
|
213 | + $lookup = true; |
|
214 | + $this->result['lookupEnabled'] = true; |
|
215 | + } else { |
|
216 | + $this->result['lookupEnabled'] = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'yes') === 'yes'; |
|
217 | + } |
|
218 | + |
|
219 | + [$result, $hasMoreResults] = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset); |
|
220 | + |
|
221 | + // extra treatment for 'exact' subarray, with a single merge expected keys might be lost |
|
222 | + if (isset($result['exact'])) { |
|
223 | + $result['exact'] = array_merge($this->result['exact'], $result['exact']); |
|
224 | + } |
|
225 | + $this->result = array_merge($this->result, $result); |
|
226 | + $response = new DataResponse($this->result); |
|
227 | + |
|
228 | + if ($hasMoreResults) { |
|
229 | + $response->addHeader('Link', $this->getPaginationLink($page, [ |
|
230 | + 'search' => $search, |
|
231 | + 'itemType' => $itemType, |
|
232 | + 'shareType' => $shareTypes, |
|
233 | + 'perPage' => $perPage, |
|
234 | + ])); |
|
235 | + } |
|
236 | + |
|
237 | + return $response; |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @param string $user |
|
242 | + * @param int $shareType |
|
243 | + * |
|
244 | + * @return Generator<array<string>> |
|
245 | + */ |
|
246 | + private function getAllShareesByType(string $user, int $shareType): Generator { |
|
247 | + $offset = 0; |
|
248 | + $pageSize = 50; |
|
249 | + |
|
250 | + while (count($page = $this->shareManager->getSharesBy( |
|
251 | + $user, |
|
252 | + $shareType, |
|
253 | + null, |
|
254 | + false, |
|
255 | + $pageSize, |
|
256 | + $offset |
|
257 | + ))) { |
|
258 | + foreach ($page as $share) { |
|
259 | + yield [$share->getSharedWith(), $share->getSharedWithDisplayName() ?? $share->getSharedWith()]; |
|
260 | + } |
|
261 | + |
|
262 | + $offset += $pageSize; |
|
263 | + } |
|
264 | + } |
|
265 | + |
|
266 | + private function sortShareesByFrequency(array $sharees): array { |
|
267 | + usort($sharees, function (array $s1, array $s2): int { |
|
268 | + return $s2['count'] - $s1['count']; |
|
269 | + }); |
|
270 | + return $sharees; |
|
271 | + } |
|
272 | + |
|
273 | + private $searchResultTypeMap = [ |
|
274 | + IShare::TYPE_USER => 'users', |
|
275 | + IShare::TYPE_GROUP => 'groups', |
|
276 | + IShare::TYPE_REMOTE => 'remotes', |
|
277 | + IShare::TYPE_REMOTE_GROUP => 'remote_groups', |
|
278 | + IShare::TYPE_EMAIL => 'emails', |
|
279 | + ]; |
|
280 | + |
|
281 | + private function getAllSharees(string $user, array $shareTypes): ISearchResult { |
|
282 | + $result = []; |
|
283 | + foreach ($shareTypes as $shareType) { |
|
284 | + $sharees = $this->getAllShareesByType($user, $shareType); |
|
285 | + $shareTypeResults = []; |
|
286 | + foreach ($sharees as [$sharee, $displayname]) { |
|
287 | + if (!isset($this->searchResultTypeMap[$shareType])) { |
|
288 | + continue; |
|
289 | + } |
|
290 | + |
|
291 | + if (!isset($shareTypeResults[$sharee])) { |
|
292 | + $shareTypeResults[$sharee] = [ |
|
293 | + 'count' => 1, |
|
294 | + 'label' => $displayname, |
|
295 | + 'value' => [ |
|
296 | + 'shareType' => $shareType, |
|
297 | + 'shareWith' => $sharee, |
|
298 | + ], |
|
299 | + ]; |
|
300 | + } else { |
|
301 | + $shareTypeResults[$sharee]['count']++; |
|
302 | + } |
|
303 | + } |
|
304 | + $result = array_merge($result, array_values($shareTypeResults)); |
|
305 | + } |
|
306 | + |
|
307 | + $top5 = array_slice( |
|
308 | + $this->sortShareesByFrequency($result), |
|
309 | + 0, |
|
310 | + 5 |
|
311 | + ); |
|
312 | + |
|
313 | + $searchResult = new SearchResult(); |
|
314 | + foreach ($this->searchResultTypeMap as $int => $str) { |
|
315 | + $searchResult->addResultSet(new SearchResultType($str), [], []); |
|
316 | + foreach ($top5 as $x) { |
|
317 | + if ($x['value']['shareType'] === $int) { |
|
318 | + $searchResult->addResultSet(new SearchResultType($str), [], [$x]); |
|
319 | + } |
|
320 | + } |
|
321 | + } |
|
322 | + return $searchResult; |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * @NoAdminRequired |
|
327 | + * |
|
328 | + * @param string $itemType |
|
329 | + * @return DataResponse |
|
330 | + * @throws OCSBadRequestException |
|
331 | + */ |
|
332 | + public function findRecommended(string $itemType = null, $shareType = null): DataResponse { |
|
333 | + $shareTypes = [ |
|
334 | + IShare::TYPE_USER, |
|
335 | + ]; |
|
336 | + |
|
337 | + if ($itemType === null) { |
|
338 | + throw new OCSBadRequestException('Missing itemType'); |
|
339 | + } elseif ($itemType === 'file' || $itemType === 'folder') { |
|
340 | + if ($this->shareManager->allowGroupSharing()) { |
|
341 | + $shareTypes[] = IShare::TYPE_GROUP; |
|
342 | + } |
|
343 | + |
|
344 | + if ($this->isRemoteSharingAllowed($itemType)) { |
|
345 | + $shareTypes[] = IShare::TYPE_REMOTE; |
|
346 | + } |
|
347 | + |
|
348 | + if ($this->isRemoteGroupSharingAllowed($itemType)) { |
|
349 | + $shareTypes[] = IShare::TYPE_REMOTE_GROUP; |
|
350 | + } |
|
351 | + |
|
352 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) { |
|
353 | + $shareTypes[] = IShare::TYPE_EMAIL; |
|
354 | + } |
|
355 | + |
|
356 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_ROOM)) { |
|
357 | + $shareTypes[] = IShare::TYPE_ROOM; |
|
358 | + } |
|
359 | + } else { |
|
360 | + $shareTypes[] = IShare::TYPE_GROUP; |
|
361 | + $shareTypes[] = IShare::TYPE_EMAIL; |
|
362 | + } |
|
363 | + |
|
364 | + // FIXME: DI |
|
365 | + if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
366 | + $shareTypes[] = IShare::TYPE_CIRCLE; |
|
367 | + } |
|
368 | + |
|
369 | + if (isset($_GET['shareType']) && is_array($_GET['shareType'])) { |
|
370 | + $shareTypes = array_intersect($shareTypes, $_GET['shareType']); |
|
371 | + sort($shareTypes); |
|
372 | + } elseif (is_numeric($shareType)) { |
|
373 | + $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
|
374 | + sort($shareTypes); |
|
375 | + } |
|
376 | + |
|
377 | + return new DataResponse( |
|
378 | + $this->getAllSharees($this->userId, $shareTypes)->asArray() |
|
379 | + ); |
|
380 | + } |
|
381 | + |
|
382 | + /** |
|
383 | + * Method to get out the static call for better testing |
|
384 | + * |
|
385 | + * @param string $itemType |
|
386 | + * @return bool |
|
387 | + */ |
|
388 | + protected function isRemoteSharingAllowed(string $itemType): bool { |
|
389 | + try { |
|
390 | + // FIXME: static foo makes unit testing unnecessarily difficult |
|
391 | + $backend = \OC\Share\Share::getBackend($itemType); |
|
392 | + return $backend->isShareTypeAllowed(IShare::TYPE_REMOTE); |
|
393 | + } catch (\Exception $e) { |
|
394 | + return false; |
|
395 | + } |
|
396 | + } |
|
397 | + |
|
398 | + protected function isRemoteGroupSharingAllowed(string $itemType): bool { |
|
399 | + try { |
|
400 | + // FIXME: static foo makes unit testing unnecessarily difficult |
|
401 | + $backend = \OC\Share\Share::getBackend($itemType); |
|
402 | + return $backend->isShareTypeAllowed(IShare::TYPE_REMOTE_GROUP); |
|
403 | + } catch (\Exception $e) { |
|
404 | + return false; |
|
405 | + } |
|
406 | + } |
|
407 | + |
|
408 | + |
|
409 | + /** |
|
410 | + * Generates a bunch of pagination links for the current page |
|
411 | + * |
|
412 | + * @param int $page Current page |
|
413 | + * @param array $params Parameters for the URL |
|
414 | + * @return string |
|
415 | + */ |
|
416 | + protected function getPaginationLink(int $page, array $params): string { |
|
417 | + if ($this->isV2()) { |
|
418 | + $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
419 | + } else { |
|
420 | + $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?'; |
|
421 | + } |
|
422 | + $params['page'] = $page + 1; |
|
423 | + return '<' . $url . http_build_query($params) . '>; rel="next"'; |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * @return bool |
|
428 | + */ |
|
429 | + protected function isV2(): bool { |
|
430 | + return $this->request->getScriptName() === '/ocs/v2.php'; |
|
431 | + } |
|
432 | 432 | } |