Completed
Pull Request — master (#246)
by Joas
12:22
created

Circles::generateAbsoluteLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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