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