Passed
Push — master ( acdc68...f0ad46 )
by Morris
11:28 queued 11s
created
apps/files_sharing/lib/Controller/ShareesAPIController.php 1 patch
Indentation   +369 added lines, -369 removed lines patch added patch discarded remove patch
@@ -54,373 +54,373 @@
 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 int */
70
-	protected $offset = 0;
71
-
72
-	/** @var int */
73
-	protected $limit = 10;
74
-
75
-	/** @var array */
76
-	protected $result = [
77
-		'exact' => [
78
-			'users' => [],
79
-			'groups' => [],
80
-			'remotes' => [],
81
-			'remote_groups' => [],
82
-			'emails' => [],
83
-			'circles' => [],
84
-			'rooms' => [],
85
-		],
86
-		'users' => [],
87
-		'groups' => [],
88
-		'remotes' => [],
89
-		'remote_groups' => [],
90
-		'emails' => [],
91
-		'lookup' => [],
92
-		'circles' => [],
93
-		'rooms' => [],
94
-		'lookupEnabled' => false,
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
-			IShare::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[] = IShare::TYPE_GROUP;
168
-			}
169
-
170
-			if ($this->isRemoteSharingAllowed($itemType)) {
171
-				$shareTypes[] = IShare::TYPE_REMOTE;
172
-			}
173
-
174
-			if ($this->isRemoteGroupSharingAllowed($itemType)) {
175
-				$shareTypes[] = IShare::TYPE_REMOTE_GROUP;
176
-			}
177
-
178
-			if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) {
179
-				$shareTypes[] = IShare::TYPE_EMAIL;
180
-			}
181
-
182
-			if ($this->shareManager->shareProviderExists(IShare::TYPE_ROOM)) {
183
-				$shareTypes[] = IShare::TYPE_ROOM;
184
-			}
185
-		} else {
186
-			$shareTypes[] = IShare::TYPE_GROUP;
187
-			$shareTypes[] = IShare::TYPE_EMAIL;
188
-		}
189
-
190
-		// FIXME: DI
191
-		if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) {
192
-			$shareTypes[] = IShare::TYPE_CIRCLE;
193
-		}
194
-
195
-		if ($shareType !== null && is_array($shareType)) {
196
-			$shareTypes = array_intersect($shareTypes, $shareType);
197
-		} elseif (is_numeric($shareType)) {
198
-			$shareTypes = array_intersect($shareTypes, [(int) $shareType]);
199
-		}
200
-		sort($shareTypes);
201
-
202
-		$this->limit = (int) $perPage;
203
-		$this->offset = $perPage * ($page - 1);
204
-
205
-		// In global scale mode we always search the loogup server
206
-		if ($this->config->getSystemValueBool('gs.enabled', false)) {
207
-			$lookup = true;
208
-			$this->result['lookupEnabled'] = true;
209
-		} else {
210
-			$this->result['lookupEnabled'] = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'yes') === 'yes';
211
-		}
212
-
213
-		list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset);
214
-
215
-		// extra treatment for 'exact' subarray, with a single merge expected keys might be lost
216
-		if (isset($result['exact'])) {
217
-			$result['exact'] = array_merge($this->result['exact'], $result['exact']);
218
-		}
219
-		$this->result = array_merge($this->result, $result);
220
-		$response = new DataResponse($this->result);
221
-
222
-		if ($hasMoreResults) {
223
-			$response->addHeader('Link', $this->getPaginationLink($page, [
224
-				'search' => $search,
225
-				'itemType' => $itemType,
226
-				'shareType' => $shareTypes,
227
-				'perPage' => $perPage,
228
-			]));
229
-		}
230
-
231
-		return $response;
232
-	}
233
-
234
-	/**
235
-	 * @param string $user
236
-	 * @param int $shareType
237
-	 *
238
-	 * @return Generator<array<string>>
239
-	 */
240
-	private function getAllShareesByType(string $user, int $shareType): Generator {
241
-		$offset = 0;
242
-		$pageSize = 50;
243
-
244
-		while (count($page = $this->shareManager->getSharesBy(
245
-			$user,
246
-			$shareType,
247
-			null,
248
-			false,
249
-			$pageSize,
250
-			$offset
251
-		))) {
252
-			foreach ($page as $share) {
253
-				yield [$share->getSharedWith(), $share->getSharedWithDisplayName() ?? $share->getSharedWith()];
254
-			}
255
-
256
-			$offset += $pageSize;
257
-		}
258
-	}
259
-
260
-	private function sortShareesByFrequency(array $sharees): array {
261
-		usort($sharees, function (array $s1, array $s2) {
262
-			return $s2['count'] - $s1['count'];
263
-		});
264
-		return $sharees;
265
-	}
266
-
267
-	private $searchResultTypeMap = [
268
-		IShare::TYPE_USER => 'users',
269
-		IShare::TYPE_GROUP => 'groups',
270
-		IShare::TYPE_REMOTE => 'remotes',
271
-		IShare::TYPE_REMOTE_GROUP => 'remote_groups',
272
-		IShare::TYPE_EMAIL => 'emails',
273
-	];
274
-
275
-	private function getAllSharees(string $user, array $shareTypes): ISearchResult {
276
-		$result = [];
277
-		foreach ($shareTypes as $shareType) {
278
-			$sharees = $this->getAllShareesByType($user, $shareType);
279
-			$shareTypeResults = [];
280
-			foreach ($sharees as list($sharee, $displayname)) {
281
-				if (!isset($this->searchResultTypeMap[$shareType])) {
282
-					continue;
283
-				}
284
-
285
-				if (!isset($shareTypeResults[$sharee])) {
286
-					$shareTypeResults[$sharee] = [
287
-						'count' => 1,
288
-						'label' => $displayname,
289
-						'value' => [
290
-							'shareType' => $shareType,
291
-							'shareWith' => $sharee,
292
-						],
293
-					];
294
-				} else {
295
-					$shareTypeResults[$sharee]['count']++;
296
-				}
297
-			}
298
-			$result = array_merge($result, array_values($shareTypeResults));
299
-		}
300
-
301
-		$top5 = array_slice(
302
-			$this->sortShareesByFrequency($result),
303
-			0,
304
-			5
305
-		);
306
-
307
-		$searchResult = new SearchResult();
308
-		foreach ($this->searchResultTypeMap as $int => $str) {
309
-			$searchResult->addResultSet(new SearchResultType($str), [], []);
310
-			foreach ($top5 as $x) {
311
-				if ($x['value']['shareType'] === $int) {
312
-					$searchResult->addResultSet(new SearchResultType($str), [], [$x]);
313
-				}
314
-			}
315
-		}
316
-		return $searchResult;
317
-	}
318
-
319
-	/**
320
-	 * @NoAdminRequired
321
-	 *
322
-	 * @param string $itemType
323
-	 * @return DataResponse
324
-	 * @throws OCSBadRequestException
325
-	 */
326
-	public function findRecommended(string $itemType = null, $shareType = null): DataResponse {
327
-		$shareTypes = [
328
-			IShare::TYPE_USER,
329
-		];
330
-
331
-		if ($itemType === null) {
332
-			throw new OCSBadRequestException('Missing itemType');
333
-		} elseif ($itemType === 'file' || $itemType === 'folder') {
334
-			if ($this->shareManager->allowGroupSharing()) {
335
-				$shareTypes[] = IShare::TYPE_GROUP;
336
-			}
337
-
338
-			if ($this->isRemoteSharingAllowed($itemType)) {
339
-				$shareTypes[] = IShare::TYPE_REMOTE;
340
-			}
341
-
342
-			if ($this->isRemoteGroupSharingAllowed($itemType)) {
343
-				$shareTypes[] = IShare::TYPE_REMOTE_GROUP;
344
-			}
345
-
346
-			if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) {
347
-				$shareTypes[] = IShare::TYPE_EMAIL;
348
-			}
349
-
350
-			if ($this->shareManager->shareProviderExists(IShare::TYPE_ROOM)) {
351
-				$shareTypes[] = IShare::TYPE_ROOM;
352
-			}
353
-		} else {
354
-			$shareTypes[] = IShare::TYPE_GROUP;
355
-			$shareTypes[] = IShare::TYPE_EMAIL;
356
-		}
357
-
358
-		// FIXME: DI
359
-		if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) {
360
-			$shareTypes[] = IShare::TYPE_CIRCLE;
361
-		}
362
-
363
-		if (isset($_GET['shareType']) && is_array($_GET['shareType'])) {
364
-			$shareTypes = array_intersect($shareTypes, $_GET['shareType']);
365
-			sort($shareTypes);
366
-		} elseif (is_numeric($shareType)) {
367
-			$shareTypes = array_intersect($shareTypes, [(int) $shareType]);
368
-			sort($shareTypes);
369
-		}
370
-
371
-		return new DataResponse(
372
-			$this->getAllSharees($this->userId, $shareTypes)->asArray()
373
-		);
374
-	}
375
-
376
-	/**
377
-	 * Method to get out the static call for better testing
378
-	 *
379
-	 * @param string $itemType
380
-	 * @return bool
381
-	 */
382
-	protected function isRemoteSharingAllowed(string $itemType): bool {
383
-		try {
384
-			// FIXME: static foo makes unit testing unnecessarily difficult
385
-			$backend = \OC\Share\Share::getBackend($itemType);
386
-			return $backend->isShareTypeAllowed(IShare::TYPE_REMOTE);
387
-		} catch (\Exception $e) {
388
-			return false;
389
-		}
390
-	}
391
-
392
-	protected function isRemoteGroupSharingAllowed(string $itemType): bool {
393
-		try {
394
-			// FIXME: static foo makes unit testing unnecessarily difficult
395
-			$backend = \OC\Share\Share::getBackend($itemType);
396
-			return $backend->isShareTypeAllowed(IShare::TYPE_REMOTE_GROUP);
397
-		} catch (\Exception $e) {
398
-			return false;
399
-		}
400
-	}
401
-
402
-
403
-	/**
404
-	 * Generates a bunch of pagination links for the current page
405
-	 *
406
-	 * @param int $page Current page
407
-	 * @param array $params Parameters for the URL
408
-	 * @return string
409
-	 */
410
-	protected function getPaginationLink(int $page, array $params): string {
411
-		if ($this->isV2()) {
412
-			$url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?';
413
-		} else {
414
-			$url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?';
415
-		}
416
-		$params['page'] = $page + 1;
417
-		return '<' . $url . http_build_query($params) . '>; rel="next"';
418
-	}
419
-
420
-	/**
421
-	 * @return bool
422
-	 */
423
-	protected function isV2(): bool {
424
-		return $this->request->getScriptName() === '/ocs/v2.php';
425
-	}
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 int */
70
+    protected $offset = 0;
71
+
72
+    /** @var int */
73
+    protected $limit = 10;
74
+
75
+    /** @var array */
76
+    protected $result = [
77
+        'exact' => [
78
+            'users' => [],
79
+            'groups' => [],
80
+            'remotes' => [],
81
+            'remote_groups' => [],
82
+            'emails' => [],
83
+            'circles' => [],
84
+            'rooms' => [],
85
+        ],
86
+        'users' => [],
87
+        'groups' => [],
88
+        'remotes' => [],
89
+        'remote_groups' => [],
90
+        'emails' => [],
91
+        'lookup' => [],
92
+        'circles' => [],
93
+        'rooms' => [],
94
+        'lookupEnabled' => false,
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
+            IShare::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[] = IShare::TYPE_GROUP;
168
+            }
169
+
170
+            if ($this->isRemoteSharingAllowed($itemType)) {
171
+                $shareTypes[] = IShare::TYPE_REMOTE;
172
+            }
173
+
174
+            if ($this->isRemoteGroupSharingAllowed($itemType)) {
175
+                $shareTypes[] = IShare::TYPE_REMOTE_GROUP;
176
+            }
177
+
178
+            if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) {
179
+                $shareTypes[] = IShare::TYPE_EMAIL;
180
+            }
181
+
182
+            if ($this->shareManager->shareProviderExists(IShare::TYPE_ROOM)) {
183
+                $shareTypes[] = IShare::TYPE_ROOM;
184
+            }
185
+        } else {
186
+            $shareTypes[] = IShare::TYPE_GROUP;
187
+            $shareTypes[] = IShare::TYPE_EMAIL;
188
+        }
189
+
190
+        // FIXME: DI
191
+        if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) {
192
+            $shareTypes[] = IShare::TYPE_CIRCLE;
193
+        }
194
+
195
+        if ($shareType !== null && is_array($shareType)) {
196
+            $shareTypes = array_intersect($shareTypes, $shareType);
197
+        } elseif (is_numeric($shareType)) {
198
+            $shareTypes = array_intersect($shareTypes, [(int) $shareType]);
199
+        }
200
+        sort($shareTypes);
201
+
202
+        $this->limit = (int) $perPage;
203
+        $this->offset = $perPage * ($page - 1);
204
+
205
+        // In global scale mode we always search the loogup server
206
+        if ($this->config->getSystemValueBool('gs.enabled', false)) {
207
+            $lookup = true;
208
+            $this->result['lookupEnabled'] = true;
209
+        } else {
210
+            $this->result['lookupEnabled'] = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'yes') === 'yes';
211
+        }
212
+
213
+        list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset);
214
+
215
+        // extra treatment for 'exact' subarray, with a single merge expected keys might be lost
216
+        if (isset($result['exact'])) {
217
+            $result['exact'] = array_merge($this->result['exact'], $result['exact']);
218
+        }
219
+        $this->result = array_merge($this->result, $result);
220
+        $response = new DataResponse($this->result);
221
+
222
+        if ($hasMoreResults) {
223
+            $response->addHeader('Link', $this->getPaginationLink($page, [
224
+                'search' => $search,
225
+                'itemType' => $itemType,
226
+                'shareType' => $shareTypes,
227
+                'perPage' => $perPage,
228
+            ]));
229
+        }
230
+
231
+        return $response;
232
+    }
233
+
234
+    /**
235
+     * @param string $user
236
+     * @param int $shareType
237
+     *
238
+     * @return Generator<array<string>>
239
+     */
240
+    private function getAllShareesByType(string $user, int $shareType): Generator {
241
+        $offset = 0;
242
+        $pageSize = 50;
243
+
244
+        while (count($page = $this->shareManager->getSharesBy(
245
+            $user,
246
+            $shareType,
247
+            null,
248
+            false,
249
+            $pageSize,
250
+            $offset
251
+        ))) {
252
+            foreach ($page as $share) {
253
+                yield [$share->getSharedWith(), $share->getSharedWithDisplayName() ?? $share->getSharedWith()];
254
+            }
255
+
256
+            $offset += $pageSize;
257
+        }
258
+    }
259
+
260
+    private function sortShareesByFrequency(array $sharees): array {
261
+        usort($sharees, function (array $s1, array $s2) {
262
+            return $s2['count'] - $s1['count'];
263
+        });
264
+        return $sharees;
265
+    }
266
+
267
+    private $searchResultTypeMap = [
268
+        IShare::TYPE_USER => 'users',
269
+        IShare::TYPE_GROUP => 'groups',
270
+        IShare::TYPE_REMOTE => 'remotes',
271
+        IShare::TYPE_REMOTE_GROUP => 'remote_groups',
272
+        IShare::TYPE_EMAIL => 'emails',
273
+    ];
274
+
275
+    private function getAllSharees(string $user, array $shareTypes): ISearchResult {
276
+        $result = [];
277
+        foreach ($shareTypes as $shareType) {
278
+            $sharees = $this->getAllShareesByType($user, $shareType);
279
+            $shareTypeResults = [];
280
+            foreach ($sharees as list($sharee, $displayname)) {
281
+                if (!isset($this->searchResultTypeMap[$shareType])) {
282
+                    continue;
283
+                }
284
+
285
+                if (!isset($shareTypeResults[$sharee])) {
286
+                    $shareTypeResults[$sharee] = [
287
+                        'count' => 1,
288
+                        'label' => $displayname,
289
+                        'value' => [
290
+                            'shareType' => $shareType,
291
+                            'shareWith' => $sharee,
292
+                        ],
293
+                    ];
294
+                } else {
295
+                    $shareTypeResults[$sharee]['count']++;
296
+                }
297
+            }
298
+            $result = array_merge($result, array_values($shareTypeResults));
299
+        }
300
+
301
+        $top5 = array_slice(
302
+            $this->sortShareesByFrequency($result),
303
+            0,
304
+            5
305
+        );
306
+
307
+        $searchResult = new SearchResult();
308
+        foreach ($this->searchResultTypeMap as $int => $str) {
309
+            $searchResult->addResultSet(new SearchResultType($str), [], []);
310
+            foreach ($top5 as $x) {
311
+                if ($x['value']['shareType'] === $int) {
312
+                    $searchResult->addResultSet(new SearchResultType($str), [], [$x]);
313
+                }
314
+            }
315
+        }
316
+        return $searchResult;
317
+    }
318
+
319
+    /**
320
+     * @NoAdminRequired
321
+     *
322
+     * @param string $itemType
323
+     * @return DataResponse
324
+     * @throws OCSBadRequestException
325
+     */
326
+    public function findRecommended(string $itemType = null, $shareType = null): DataResponse {
327
+        $shareTypes = [
328
+            IShare::TYPE_USER,
329
+        ];
330
+
331
+        if ($itemType === null) {
332
+            throw new OCSBadRequestException('Missing itemType');
333
+        } elseif ($itemType === 'file' || $itemType === 'folder') {
334
+            if ($this->shareManager->allowGroupSharing()) {
335
+                $shareTypes[] = IShare::TYPE_GROUP;
336
+            }
337
+
338
+            if ($this->isRemoteSharingAllowed($itemType)) {
339
+                $shareTypes[] = IShare::TYPE_REMOTE;
340
+            }
341
+
342
+            if ($this->isRemoteGroupSharingAllowed($itemType)) {
343
+                $shareTypes[] = IShare::TYPE_REMOTE_GROUP;
344
+            }
345
+
346
+            if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) {
347
+                $shareTypes[] = IShare::TYPE_EMAIL;
348
+            }
349
+
350
+            if ($this->shareManager->shareProviderExists(IShare::TYPE_ROOM)) {
351
+                $shareTypes[] = IShare::TYPE_ROOM;
352
+            }
353
+        } else {
354
+            $shareTypes[] = IShare::TYPE_GROUP;
355
+            $shareTypes[] = IShare::TYPE_EMAIL;
356
+        }
357
+
358
+        // FIXME: DI
359
+        if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) {
360
+            $shareTypes[] = IShare::TYPE_CIRCLE;
361
+        }
362
+
363
+        if (isset($_GET['shareType']) && is_array($_GET['shareType'])) {
364
+            $shareTypes = array_intersect($shareTypes, $_GET['shareType']);
365
+            sort($shareTypes);
366
+        } elseif (is_numeric($shareType)) {
367
+            $shareTypes = array_intersect($shareTypes, [(int) $shareType]);
368
+            sort($shareTypes);
369
+        }
370
+
371
+        return new DataResponse(
372
+            $this->getAllSharees($this->userId, $shareTypes)->asArray()
373
+        );
374
+    }
375
+
376
+    /**
377
+     * Method to get out the static call for better testing
378
+     *
379
+     * @param string $itemType
380
+     * @return bool
381
+     */
382
+    protected function isRemoteSharingAllowed(string $itemType): bool {
383
+        try {
384
+            // FIXME: static foo makes unit testing unnecessarily difficult
385
+            $backend = \OC\Share\Share::getBackend($itemType);
386
+            return $backend->isShareTypeAllowed(IShare::TYPE_REMOTE);
387
+        } catch (\Exception $e) {
388
+            return false;
389
+        }
390
+    }
391
+
392
+    protected function isRemoteGroupSharingAllowed(string $itemType): bool {
393
+        try {
394
+            // FIXME: static foo makes unit testing unnecessarily difficult
395
+            $backend = \OC\Share\Share::getBackend($itemType);
396
+            return $backend->isShareTypeAllowed(IShare::TYPE_REMOTE_GROUP);
397
+        } catch (\Exception $e) {
398
+            return false;
399
+        }
400
+    }
401
+
402
+
403
+    /**
404
+     * Generates a bunch of pagination links for the current page
405
+     *
406
+     * @param int $page Current page
407
+     * @param array $params Parameters for the URL
408
+     * @return string
409
+     */
410
+    protected function getPaginationLink(int $page, array $params): string {
411
+        if ($this->isV2()) {
412
+            $url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/files_sharing/api/v1/sharees') . '?';
413
+        } else {
414
+            $url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?';
415
+        }
416
+        $params['page'] = $page + 1;
417
+        return '<' . $url . http_build_query($params) . '>; rel="next"';
418
+    }
419
+
420
+    /**
421
+     * @return bool
422
+     */
423
+    protected function isV2(): bool {
424
+        return $this->request->getScriptName() === '/ocs/v2.php';
425
+    }
426 426
 }
Please login to merge, or discard this patch.