Completed
Push — master ( 1990ee...212490 )
by Maxence
04:38 queued 01:45
created

Circles::addJavascriptAPI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

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