Completed
Push — master ( f8c3ab...8554bc )
by Maxence
01:42
created

Circles::addJavascriptAPI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
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\AppFramework\QueryException;
45
use OCP\Util;
46
47
class Circles {
48
49
	const API_VERSION = [0, 10, 0];
50
51
	// Expose circle and member constants via API
52
	const CIRCLES_PERSONAL = Circle::CIRCLES_PERSONAL;
53
	const CIRCLES_SECRET = Circle::CIRCLES_SECRET;
54
	const CIRCLES_CLOSED = Circle::CIRCLES_CLOSED;
55
	const CIRCLES_PUBLIC = Circle::CIRCLES_PUBLIC;
56
	const CIRCLES_ALL = Circle::CIRCLES_ALL;
57
58
	const TYPE_USER = Member::TYPE_USER;
59
	const TYPE_GROUP = Member::TYPE_GROUP;
60
	const TYPE_MAIL = Member::TYPE_MAIL;
61
	const TYPE_CONTACT = Member::TYPE_CONTACT;
62
63
	const LEVEL_NONE = Member::LEVEL_NONE;
64
	const LEVEL_MEMBER = Member::LEVEL_MEMBER;
65
	const LEVEL_MODERATOR = Member::LEVEL_MODERATOR;
66
	const LEVEL_ADMIN = Member::LEVEL_ADMIN;
67
	const LEVEL_OWNER = Member::LEVEL_OWNER;
68
	
69
70
	protected static function getContainer() {
71
		$app = \OC::$server->query(Application::class);
72
73
		return $app->getContainer();
74
	}
75
76
77
	/**
78
	 * Circles::version();
79
	 *
80
	 * returns the current version of the API
81
	 *
82
	 * @return int[]
83
	 */
84
	public static function version() {
85
		return self::API_VERSION;
86
	}
87
88
89
	public static function addJavascriptAPI() {
90
		Util::addScript(Application::APP_NAME, 'circles.v1.circles');
91
		Util::addScript(Application::APP_NAME, 'circles.v1.members');
92
		Util::addScript(Application::APP_NAME, 'circles.v1.links');
93
		Util::addScript(Application::APP_NAME, 'circles.v1');
94
	}
95
96
97
	/**
98
	 * Circles::compareVersion();
99
	 *
100
	 * Compare and return true if version is compatible.
101
	 * Exception otherwise.
102
	 *
103
	 * @param array $apiVersion
104
	 *
105
	 * @return bool
106
	 * @throws ApiVersionIncompatibleException
107
	 */
108
	public static function compareVersion($apiVersion) {
109
		if ((int)$apiVersion[0] !== self::API_VERSION[0]
110
			|| (int)$apiVersion[1] !== self::API_VERSION[1]) {
111
			throw new ApiVersionIncompatibleException('api_not_compatible');
112
		}
113
114
		return true;
115
	}
116
117
118
	/**
119
	 * Circles::createCircle();
120
	 *
121
	 * Create a new circle and make the current user its owner.
122
	 * You must specify type and name. type is one of this value:
123
	 *
124
	 * CIRCLES_PERSONAL is 1 or 'personal'
125
	 * CIRCLES_SECRET is 2 or 'secret'
126
	 * CIRCLES_CLOSED is 4 or 'closed'
127
	 * CIRCLES_PUBLIC is 8 or 'public'
128
	 *
129
	 * @param mixed $type
130
	 * @param string $name
131
	 *
132
	 * @return Circle
133
	 * @throws QueryException
134
	 */
135
	public static function createCircle($type, $name) {
136
		$c = self::getContainer();
137
138
		return $c->query(CirclesService::class)
139
				 ->createCircle($type, $name);
140
	}
141
142
143
	/**
144
	 * Circles::joinCircle();
145
	 *
146
	 * This function will make the current user joining a circle identified by its Id.
147
	 *
148
	 * @param string $circleUniqueId
149
	 *
150
	 * @return Member
151
	 * @throws QueryException
152
	 */
153
	public static function joinCircle($circleUniqueId) {
154
		$c = self::getContainer();
155
156
		return $c->query(CirclesService::class)
157
				 ->joinCircle($circleUniqueId);
158
	}
159
160
161
	/**
162
	 * Circles::leaveCircle();
163
	 *
164
	 * This function will make the current user leaving the circle identified by its Id. Will fail
165
	 * if user is the owner of the circle.
166
	 *
167
	 * @param string $circleUniqueId
168
	 *
169
	 * @return Member
170
	 * @throws QueryException
171
	 */
172
	public static function leaveCircle($circleUniqueId) {
173
		$c = self::getContainer();
174
175
		return $c->query(CirclesService::class)
176
				 ->leaveCircle($circleUniqueId);
177
	}
178
179
180
	/**
181
	 * Circles::listCircles();
182
	 *
183
	 * This function list all circles fitting a search regarding its name and the level and the
184
	 * rights from the current user. In case of Secret circle, name needs to be complete so the
185
	 * circle is included in the list (or if the current user is the owner)
186
	 *
187
	 * example: Circles::listCircles(Circles::CIRCLES_ALL, '', 8, callback); will returns all
188
	 * circles when the current user is at least an Admin.
189
	 *
190
	 * @param mixed $type
191
	 * @param string $name
192
	 * @param int $level
193
	 * @param string $userId
194
	 *
195
	 * @param bool $forceAll
196
	 *
197
	 * @return Circle[]
198
	 * @throws QueryException
199
	 */
200
	public static function listCircles($type, $name = '', $level = 0, $userId = '', $forceAll = false) {
201
		$c = self::getContainer();
202
203
		if ($userId === '') {
204
			$userId = \OC::$server->getUserSession()
205
								  ->getUser()
206
								  ->getUID();
207
		}
208
209
		return $c->query(CirclesService::class)
210
				 ->listCircles($userId, $type, $name, $level, $forceAll);
211
	}
212
213
214
	/**
215
	 * Circles::joinedCircles();
216
	 *
217
	 * Return all the circle the current user is a member.
218
	 *
219
	 * @param string $userId
220
	 * @param bool $forceAll
221
	 *
222
	 * @return Circle[]
223
	 * @throws QueryException
224
	 */
225
	public static function joinedCircles($userId = '', $forceAll = false) {
226
		return self::listCircles(self::CIRCLES_ALL, '', self::LEVEL_MEMBER, $userId, $forceAll);
227
	}
228
229
230
	/**
231
	 * Circles::joinedCircleIds();
232
	 *
233
	 * Return all the circleIds the user is a member, if empty user, using current user.
234
	 *
235
	 * @param $userId
236
	 *
237
	 * @return array
238
	 * @throws QueryException
239
	 */
240
	public static function joinedCircleIds($userId = '') {
241
		$circleIds = [];
242
		$circles = self::listCircles(self::CIRCLES_ALL, '', self::LEVEL_MEMBER, $userId);
243
		foreach ($circles as $circle) {
244
			$circleIds[] = $circle->getUniqueId();
245
		}
246
247
		return $circleIds;
248
	}
249
250
251
	/**
252
	 * Circles::detailsCircle();
253
	 *
254
	 * WARNING - This function is called by the core - WARNING
255
	 *                 Do not change it
256
	 *
257
	 * Returns details on the circle. If the current user is a member, the members list will be
258
	 * return as well.
259
	 *
260
	 * @param string $circleUniqueId
261
	 * @param bool $forceAll
262
	 *
263
	 * @return Circle
264
	 * @throws QueryException
265
	 */
266
	public static function detailsCircle($circleUniqueId, $forceAll = false) {
267
		$c = self::getContainer();
268
269
		return $c->query(CirclesService::class)
270
				 ->detailsCircle($circleUniqueId, $forceAll);
271
	}
272
273
274
	/**
275
	 * Circles::settingsCircle();
276
	 *
277
	 * Save the settings. Settings is an array and current user need to be an admin
278
	 *
279
	 * @param string $circleUniqueId
280
	 * @param array $settings
281
	 *
282
	 * @return Circle
283
	 * @throws QueryException
284
	 */
285
	public static function settingsCircle($circleUniqueId, array $settings) {
286
		$c = self::getContainer();
287
288
		return $c->query(CirclesService::class)
289
				 ->settingsCircle($circleUniqueId, $settings);
290
	}
291
292
293
	/**
294
	 * Circles::destroyCircle();
295
	 *
296
	 * This function will destroy the circle if the current user is the Owner.
297
	 *
298
	 * @param string $circleUniqueId
299
	 *
300
	 * @return mixed
301
	 * @throws QueryException
302
	 */
303
	public static function destroyCircle($circleUniqueId) {
304
		$c = self::getContainer();
305
306
		return $c->query(CirclesService::class)
307
				 ->removeCircle($circleUniqueId);
308
	}
309
310
311
	/**
312
	 * Circles::addMember();
313
	 *
314
	 * This function will add a user as member of the circle. Current user need at least to be
315
	 * Moderator.
316
	 *
317
	 * @param string $circleUniqueId
318
	 * @param string $ident
319
	 * @param int $type
320
	 *
321
	 * @return Member[]
322
	 * @throws QueryException
323
	 */
324
	public static function addMember($circleUniqueId, $ident, $type) {
325
		$c = self::getContainer();
326
327
		return $c->query(MembersService::class)
328
				 ->addMember($circleUniqueId, $ident, $type);
329
	}
330
331
332
	/**
333
	 * Circles::getMember();
334
	 *
335
	 * This function will return information on a member of the circle. Current user need at least
336
	 * to be Member.
337
	 *
338
	 * @param string $circleUniqueId
339
	 * @param string $ident
340
	 * @param int $type
341
	 * @param bool $forceAll
342
	 *
343
	 * @return Member
344
	 * @throws QueryException
345
	 */
346
	public static function getMember($circleUniqueId, $ident, $type, $forceAll = false) {
347
		$c = self::getContainer();
348
349
		return $c->query(MembersService::class)
350
				 ->getMember($circleUniqueId, $ident, $type, $forceAll);
351
	}
352
353
354
	/**
355
	 * Circles::removeMember();
356
	 *
357
	 * This function will remove a member from the circle. Current user needs to be at least
358
	 * Moderator and have a higher level that the targeted member.
359
	 *
360
	 * @param string $circleUniqueId
361
	 * @param string $ident
362
	 * @param int $type
363
	 *
364
	 * @return Member[]
365
	 * @throws QueryException
366
	 */
367
	public static function removeMember($circleUniqueId, $ident, $type) {
368
		$c = self::getContainer();
369
370
		return $c->query(MembersService::class)
371
				 ->removeMember($circleUniqueId, $ident, $type);
372
	}
373
374
375
	/**
376
	 * Circles::levelMember();
377
	 *
378
	 * Edit the level of a member of the circle. The current level of the target needs to be lower
379
	 * than the user that initiate the process (ie. the current user). The new level of the target
380
	 * cannot be the same than the current level of the user that initiate the process (ie. the
381
	 * current user).
382
	 *
383
	 * @param string $circleUniqueId
384
	 * @param string $ident
385
	 * @param int $type
386
	 * @param int $level
387
	 *
388
	 * @return Member[]
389
	 * @throws QueryException
390
	 */
391
	public static function levelMember($circleUniqueId, $ident, $type, $level) {
392
		$c = self::getContainer();
393
394
		return $c->query(MembersService::class)
395
				 ->levelMember($circleUniqueId, $ident, $type, $level);
396
	}
397
398
399
	/**
400
	 * Circles::shareToCircle();
401
	 *
402
	 * This function will share an item (array) to the circle identified by its Id.
403
	 * Source is the app that is sharing the item and type can be used by the app to identified the
404
	 * payload.
405
	 *
406
	 * @param string $circleUniqueId
407
	 * @param string $source
408
	 * @param string $type
409
	 * @param array $payload
410
	 * @param string $broadcaster
411
	 *
412
	 * @return mixed
413
	 * @throws QueryException
414
	 */
415
	public static function shareToCircle(
416
		$circleUniqueId, $source, $type, array $payload, $broadcaster
417
	) {
418
		$c = self::getContainer();
419
420
		$frame = new SharingFrame((string)$source, (string)$type);
421
		$frame->setPayload($payload);
422
423
		return $c->query(SharingFrameService::class)
424
				 ->createFrame($circleUniqueId, $frame, (string)$broadcaster);
425
	}
426
427
428
	/**
429
	 * Circles::getSharesFromCircle();
430
	 *
431
	 * This function will returns all item (array) shared to a specific circle identified by its Id,
432
	 * source and type. Limited to current user session.
433
	 *
434
	 * @param string $circleUniqueId
435
	 *
436
	 * @return mixed
437
	 * @throws QueryException
438
	 */
439
	public static function getSharesFromCircle($circleUniqueId) {
440
		$c = self::getContainer();
441
442
		return $c->query(SharingFrameService::class)
443
				 ->getFrameFromCircle($circleUniqueId);
444
	}
445
446
447
	/**
448
	 * Circles::linkCircle();
449
	 *
450
	 * Initiate a link procedure. Current user must be at least Admin of the circle.
451
	 * circleId is the local circle and remote is the target for the link.
452
	 * Remote format is: <circle_name>@<remote_host> when remote_host must be a valid HTTPS address.
453
	 * Remote format is: <circle_name>@<remote_host> when remote_host must be a valid HTTPS address.
454
	 *
455
	 * @param string $circleUniqueId
456
	 * @param string $remote
457
	 *
458
	 * @return FederatedLink
459
	 * @throws QueryException
460
	 */
461
	public static function linkCircle($circleUniqueId, $remote) {
462
		$c = self::getContainer();
463
464
		return $c->query(FederatedLinkService::class)
465
				 ->linkCircle($circleUniqueId, $remote);
466
	}
467
468
469
	/**
470
	 * Circles::generateLink();
471
	 *
472
	 * Returns the link to get access to a local circle.
473
	 *
474
	 * @param string $circleUniqueId
475
	 *
476
	 * @return string
477
	 */
478
	public static function generateLink($circleUniqueId) {
479
		return \OC::$server->getURLGenerator()
480
						   ->linkToRoute('circles.Navigation.navigate') . '#' . $circleUniqueId;
481
	}
482
483
484
	/**
485
	 * Circles::generateAbsoluteLink();
486
	 *
487
	 * Returns the absolute link to get access to a local circle.
488
	 *
489
	 * @param string $circleUniqueId
490
	 *
491
	 * @return string
492
	 */
493
	public static function generateAbsoluteLink($circleUniqueId) {
494
		return \OC::$server->getURLGenerator()
495
						   ->linkToRouteAbsolute('circles.Navigation.navigate') . '#' . $circleUniqueId;
496
	}
497
498
499
	/**
500
	 * Circles::generateRemoteLink();
501
	 *
502
	 * Returns the link to get access to a remote circle.
503
	 *
504
	 * @param FederatedLink $link
505
	 *
506
	 * @return string
507
	 */
508
	public static function generateRemoteLink(FederatedLink $link) {
509
		return \OC::$server->getURLGenerator()
510
						   ->linkToRoute('circles.Navigation.navigate') . '#' . $link->getUniqueId()
511
			   . '-' . $link->getToken();
512
	}
513
514
515
	/**
516
	 * @param SharingFrame $frame
517
	 *
518
	 * @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...
519
	 */
520
	public static function generateUserParameter(SharingFrame $frame) {
521
522
		if ($frame->getCloudId() !== null) {
523
			$name = $frame->getAuthor() . '@' . $frame->getCloudId();
524
		} else {
525
			$name = MiscService::getDisplay($frame->getAuthor(), self::TYPE_USER);
526
		}
527
528
		return [
529
			'type' => 'user',
530
			'id'   => $frame->getAuthor(),
531
			'name' => $name
532
		];
533
	}
534
535
536
	/**
537
	 * @param SharingFrame $frame
538
	 *
539
	 * @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...
540
	 */
541
	public static function generateCircleParameter(SharingFrame $frame) {
542
		return [
543
			'type' => 'circle',
544
			'id'   => $frame->getCircle()
545
							->getUniqueId(),
546
			'name' => $frame->getCircle()
547
							->getName(),
548
			'link' => self::generateLink(
549
				$frame->getCircle()
550
					  ->getUniqueId()
551
			)
552
		];
553
	}
554
555
	/**
556
	 * Get a list of objects which are shred with $circleUniqueId.
557
	 *
558
	 * @since 0.14.0
559
	 *
560
	 * @param array $circleUniqueIds
561
	 *
562
	 * @return string[] array of object ids or empty array if none found
563
	 * @throws QueryException
564
	 */
565
	public static function getFilesForCircles($circleUniqueIds) {
566
		$c = self::getContainer();
567
568
		return $c->query(CirclesService::class)
569
			->getFilesForCircles($circleUniqueIds);
570
	}
571
}
572