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