Completed
Pull Request — master (#141)
by Maxence
02:27
created

Circles::getFilesForCircles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @author Vinicius Cubas Brand <[email protected]>
10
 * @author Daniel Tygel <[email protected]>
11
 *
12
 * @copyright 2017
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
namespace OCA\Circles\Api\v1;
31
32
33
use OCA\Circles\AppInfo\Application;
34
use OCA\Circles\Exceptions\ApiVersionIncompatibleException;
35
use OCA\Circles\Model\Circle;
36
use OCA\Circles\Model\FederatedLink;
37
use OCA\Circles\Model\Member;
38
use OCA\Circles\Model\SharingFrame;
39
use OCA\Circles\Service\CirclesService;
40
use OCA\Circles\Service\FederatedLinkService;
41
use OCA\Circles\Service\MembersService;
42
use OCA\Circles\Service\MiscService;
43
use OCA\Circles\Service\SharingFrameService;
44
use OCP\Util;
45
46
class Circles {
47
48
	const API_VERSION = [0, 10, 0];
49
50
	protected static function getContainer() {
51
		$app = new Application();
52
53
		return $app->getContainer();
54
	}
55
56
57
	/**
58
	 * Circles::version();
59
	 *
60
	 * returns the current version of the API
61
	 *
62
	 * @return int[]
63
	 */
64
	public static function version() {
65
		return self::API_VERSION;
66
	}
67
68
69
	public static function addJavascriptAPI() {
70
		Util::addScript(Application::APP_NAME, 'circles.v1.circles');
71
		Util::addScript(Application::APP_NAME, 'circles.v1.members');
72
		Util::addScript(Application::APP_NAME, 'circles.v1.links');
73
		Util::addScript(Application::APP_NAME, 'circles.v1');
74
	}
75
76
77
	/**
78
	 * Circles::compareVersion();
79
	 *
80
	 * Compare and return true if version is compatible.
81
	 * Exception otherwise.
82
	 *
83
	 * @param array $apiVersion
84
	 *
85
	 * @return bool
86
	 * @throws ApiVersionIncompatibleException
87
	 */
88
	public static function compareVersion($apiVersion) {
89
		if ((int)$apiVersion[0] !== Circles::API_VERSION[0]
90
			|| (int)$apiVersion[1] !== Circles::API_VERSION[1]) {
91
			throw new ApiVersionIncompatibleException('api_not_compatible');
92
		}
93
94
		return true;
95
	}
96
97
98
	/**
99
	 * Circles::createCircle();
100
	 *
101
	 * Create a new circle and make the current user its owner.
102
	 * You must specify type and name. type is one of this value:
103
	 *
104
	 * CIRCLES_PERSONAL is 1 or 'personal'
105
	 * CIRCLES_SECRET is 2 or 'secret'
106
	 * CIRCLES_CLOSED is 4 or 'closed'
107
	 * CIRCLES_PUBLIC is 8 or 'public'
108
	 *
109
	 * @param mixed $type
110
	 * @param string $name
111
	 *
112
	 * @return Circle
113
	 */
114
	public static function createCircle($type, $name) {
115
		$c = self::getContainer();
116
117
		return $c->query(CirclesService::class)
118
				 ->createCircle($type, $name);
119
	}
120
121
122
	/**
123
	 * Circles::joinCircle();
124
	 *
125
	 * This function will make the current user joining a circle identified by its Id.
126
	 *
127
	 * @param string $circleUniqueId
128
	 *
129
	 * @return Member
130
	 */
131
	public static function joinCircle($circleUniqueId) {
132
		$c = self::getContainer();
133
134
		return $c->query(CirclesService::class)
135
				 ->joinCircle($circleUniqueId);
136
	}
137
138
139
	/**
140
	 * Circles::leaveCircle();
141
	 *
142
	 * This function will make the current user leaving the circle identified by its Id. Will fail
143
	 * if user is the owner of the circle.
144
	 *
145
	 * @param string $circleUniqueId
146
	 *
147
	 * @return Member
148
	 */
149
	public static function leaveCircle($circleUniqueId) {
150
		$c = self::getContainer();
151
152
		return $c->query(CirclesService::class)
153
				 ->leaveCircle($circleUniqueId);
154
	}
155
156
157
	/**
158
	 * Circles::listCircles();
159
	 *
160
	 * This function list all circles fitting a search regarding its name and the level and the
161
	 * rights from the current user. In case of Secret circle, name needs to be complete so the
162
	 * circle is included in the list (or if the current user is the owner)
163
	 *
164
	 * example: Circles::listCircles(Circle::CIRCLES_ALL, '', 8, callback); will returns all
165
	 * circles when the current user is at least an Admin.
166
	 *
167
	 * @param mixed $type
168
	 * @param string $name
169
	 * @param int $level
170
	 * @param string $userId
171
	 *
172
	 * @return Circle[]
173
	 */
174
	public static function listCircles($type, $name = '', $level = 0, $userId = '') {
175
		$c = self::getContainer();
176
177
		if ($userId === '') {
178
			$userId = \OC::$server->getUserSession()
179
								  ->getUser()
180
								  ->getUID();
181
		}
182
183
		return $c->query(CirclesService::class)
184
				 ->listCircles($userId, $type, $name, $level);
185
	}
186
187
188
	/**
189
	 * Circles::joinedCircles();
190
	 *
191
	 * Return all the circle the current user is a member.
192
	 *
193
	 * @param string $userId
194
	 *
195
	 * @return Circle[]
196
	 */
197
	public static function joinedCircles($userId = '') {
198
		return self::listCircles(Circle::CIRCLES_ALL, '', Member::LEVEL_MEMBER, $userId);
199
	}
200
201
202
	/**
203
	 * Circles::joinedCircleIds();
204
	 *
205
	 * Return all the circleIds the user is a member, if empty user, using current user.
206
	 *
207
	 * @param $userId
208
	 *
209
	 * @return array
210
	 */
211
	public static function joinedCircleIds($userId = '') {
212
		$circleIds = [];
213
		$circles = self::listCircles(Circle::CIRCLES_ALL, '', Member::LEVEL_MEMBER, $userId);
214
		foreach ($circles as $circle) {
215
			$circleIds[] = $circle->getUniqueId();
216
		}
217
218
		return $circleIds;
219
	}
220
221
222
	/**
223
	 * Circles::detailsCircle();
224
	 *
225
	 * WARNING - This function is called by the core - WARNING
226
	 *                 Do not change it
227
	 *
228
	 * Returns details on the circle. If the current user is a member, the members list will be
229
	 * return as well.
230
	 *
231
	 * @param string $circleUniqueId
232
	 *
233
	 * @return Circle
234
	 */
235
	public static function detailsCircle($circleUniqueId) {
236
		$c = self::getContainer();
237
238
		return $c->query(CirclesService::class)
239
				 ->detailsCircle($circleUniqueId);
240
	}
241
242
243
	/**
244
	 * Circles::settingsCircle();
245
	 *
246
	 * Save the settings. Settings is an array and current user need to be an admin
247
	 *
248
	 * @param string $circleUniqueId
249
	 * @param array $settings
250
	 *
251
	 * @return Circle
252
	 */
253
	public static function settingsCircle($circleUniqueId, array $settings) {
254
		$c = self::getContainer();
255
256
		return $c->query(CirclesService::class)
257
				 ->settingsCircle($circleUniqueId, $settings);
258
	}
259
260
261
	/**
262
	 * Circles::destroyCircle();
263
	 *
264
	 * This function will destroy the circle if the current user is the Owner.
265
	 *
266
	 * @param string $circleUniqueId
267
	 *
268
	 * @return mixed
269
	 */
270
	public static function destroyCircle($circleUniqueId) {
271
		$c = self::getContainer();
272
273
		return $c->query(CirclesService::class)
274
				 ->removeCircle($circleUniqueId);
275
	}
276
277
278
	/**
279
	 * Circles::addMember();
280
	 *
281
	 * This function will add a user as member of the circle. Current user need at least to be
282
	 * Moderator.
283
	 *
284
	 * @param string $circleUniqueId
285
	 * @param string $ident
286
	 * @param int $type
287
	 *
288
	 * @return Member[]
289
	 */
290
	public static function addMember($circleUniqueId, $ident, $type) {
291
		$c = self::getContainer();
292
293
		return $c->query(MembersService::class)
294
				 ->addMember($circleUniqueId, $ident, $type);
295
	}
296
297
298
	/**
299
	 * Circles::getMember();
300
	 *
301
	 * This function will return information on a member of the circle. Current user need at least
302
	 * to be Member.
303
	 *
304
	 * @param string $circleUniqueId
305
	 * @param string $ident
306
	 * @param int $type
307
	 *
308
	 * @return Member
309
	 */
310
	public static function getMember($circleUniqueId, $ident, $type) {
311
		$c = self::getContainer();
312
313
		return $c->query(MembersService::class)
314
				 ->getMember($circleUniqueId, $ident, $type);
315
	}
316
317
318
	/**
319
	 * Circles::removeMember();
320
	 *
321
	 * This function will remove a member from the circle. Current user needs to be at least
322
	 * Moderator and have a higher level that the targeted member.
323
	 *
324
	 * @param string $circleUniqueId
325
	 * @param string $ident
326
	 * @param int $type
327
	 *
328
	 * @return Member[]
329
	 */
330
	public static function removeMember($circleUniqueId, $ident, $type) {
331
		$c = self::getContainer();
332
333
		return $c->query(MembersService::class)
334
				 ->removeMember($circleUniqueId, $ident, $type);
335
	}
336
337
338
	/**
339
	 * Circles::levelMember();
340
	 *
341
	 * Edit the level of a member of the circle. The current level of the target needs to be lower
342
	 * than the user that initiate the process (ie. the current user). The new level of the target
343
	 * cannot be the same than the current level of the user that initiate the process (ie. the
344
	 * current user).
345
	 *
346
	 * @param string $circleUniqueId
347
	 * @param string $ident
348
	 * @param int $type
349
	 * @param int $level
350
	 *
351
	 * @return Member[]
352
	 */
353
	public static function levelMember($circleUniqueId, $ident, $type, $level) {
354
		$c = self::getContainer();
355
356
		return $c->query(MembersService::class)
357
				 ->levelMember($circleUniqueId, $ident, $type, $level);
358
	}
359
360
361
	/**
362
	 * Circles::shareToCircle();
363
	 *
364
	 * This function will share an item (array) to the circle identified by its Id.
365
	 * Source is the app that is sharing the item and type can be used by the app to identified the
366
	 * payload.
367
	 *
368
	 * @param string $circleUniqueId
369
	 * @param string $source
370
	 * @param string $type
371
	 * @param array $payload
372
	 * @param string $broadcaster
373
	 *
374
	 * @return mixed
375
	 */
376
	public static function shareToCircle(
377
		$circleUniqueId, $source, $type, array $payload, $broadcaster
378
	) {
379
		$c = self::getContainer();
380
381
		$frame = new SharingFrame((string)$source, (string)$type);
382
		$frame->setPayload($payload);
383
384
		return $c->query(SharingFrameService::class)
385
				 ->createFrame($circleUniqueId, $frame, (string)$broadcaster);
386
	}
387
388
389
	/**
390
	 * Circles::getSharesFromCircle();
391
	 *
392
	 * This function will returns all item (array) shared to a specific circle identified by its Id,
393
	 * source and type. Limited to current user session.
394
	 *
395
	 * @param string $circleUniqueId
396
	 *
397
	 * @return mixed
398
	 */
399
	public static function getSharesFromCircle($circleUniqueId) {
400
		$c = self::getContainer();
401
402
		return $c->query(SharingFrameService::class)
403
				 ->getFrameFromCircle($circleUniqueId);
404
	}
405
406
407
	/**
408
	 * Circles::linkCircle();
409
	 *
410
	 * Initiate a link procedure. Current user must be at least Admin of the circle.
411
	 * circleId is the local circle and remote is the target for the link.
412
	 * Remote format is: <circle_name>@<remote_host> when remote_host must be a valid HTTPS address.
413
	 * Remote format is: <circle_name>@<remote_host> when remote_host must be a valid HTTPS address.
414
	 *
415
	 * @param string $circleUniqueId
416
	 * @param string $remote
417
	 *
418
	 * @return FederatedLink
419
	 */
420
	public static function linkCircle($circleUniqueId, $remote) {
421
		$c = self::getContainer();
422
423
		return $c->query(FederatedLinkService::class)
424
				 ->linkCircle($circleUniqueId, $remote);
425
	}
426
427
428
	/**
429
	 * Circles::generateLink();
430
	 *
431
	 * Returns the link to get access to a local circle.
432
	 *
433
	 * @param string $circleUniqueId
434
	 *
435
	 * @return string
436
	 */
437
	public static function generateLink($circleUniqueId) {
438
		return \OC::$server->getURLGenerator()
439
						   ->linkToRoute('circles.Navigation.navigate') . '#' . $circleUniqueId;
440
	}
441
442
443
	/**
444
	 * Circles::generateLink();
445
	 *
446
	 * Returns the link to get access to a remote circle.
447
	 *
448
	 * @param FederatedLink $link
449
	 *
450
	 * @return string
451
	 */
452
	public static function generateRemoteLink(FederatedLink $link) {
453
		return \OC::$server->getURLGenerator()
454
						   ->linkToRoute('circles.Navigation.navigate') . '#' . $link->getUniqueId()
455
			   . '-' . $link->getToken();
456
	}
457
458
459
	/**
460
	 * @param SharingFrame $frame
461
	 *
462
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
463
	 */
464
	public static function generateUserParameter(SharingFrame $frame) {
465
466
		if ($frame->getCloudId() !== null) {
467
			$name = $frame->getAuthor() . '@' . $frame->getCloudId();
468
		} else {
469
			$name = MiscService::getDisplay($frame->getAuthor(), Member::TYPE_USER);
470
		}
471
472
		return [
473
			'type' => 'user',
474
			'id'   => $frame->getAuthor(),
475
			'name' => $name
476
		];
477
	}
478
479
480
	/**
481
	 * @param SharingFrame $frame
482
	 *
483
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
484
	 */
485
	public static function generateCircleParameter(SharingFrame $frame) {
486
		return [
487
			'type' => 'circle',
488
			'id'   => $frame->getCircle()
489
							->getUniqueId(),
490
			'name' => $frame->getCircle()
491
							->getName(),
492
			'link' => self::generateLink(
493
				$frame->getCircle()
494
					  ->getUniqueId()
495
			)
496
		];
497
	}
498
499
	/**
500
	 * Get a list of objects which are shred with $circleUniqueId.
501
	 *
502
	 * @since 0.14.0
503
	 *
504
	 * @param array $circleUniqueIds
505
	 *
506
	 * @return string[] array of object ids or empty array if none found
507
	 */
508
	public static function getFilesForCircles($circleUniqueIds) {
509
		$c = self::getContainer();
510
511
		return $c->query(CirclesService::class)
512
			->getFilesForCircles($circleUniqueIds);
513
	}
514
}