Completed
Push — master ( 7588c2...f404fc )
by Maxence
03:40
created

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

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...
417
	 */
418
	public static function generateCircleParameter(SharingFrame $frame) {
419
		return [
420
			'type' => 'circle',
421
			'id'   => $frame->getCircleId(),
422
			'name' => $frame->getCircleName(),
423
			'link' => self::generateLink($frame->getCircleId())
424
		];
425
	}
426
}