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