Completed
Push — master ( 149977...f8c3ab )
by Maxence
02:06 queued 15s
created

Circles::generateRemoteLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
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\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
	protected static function getContainer() {
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

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