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