Completed
Push — master ( fc6227...6ab97c )
by Maxence
04:01
created

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