|
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\Service; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
use OCA\Circles\Db\CirclesRequest; |
|
31
|
|
|
use OCA\Circles\Db\MembersRequest; |
|
32
|
|
|
use OCA\Circles\Exceptions\CircleAlreadyExistsException; |
|
33
|
|
|
use OCA\Circles\Exceptions\CircleTypeDisabledException; |
|
34
|
|
|
use OCA\Circles\Exceptions\FederatedCircleNotAllowedException; |
|
35
|
|
|
use OCA\Circles\Exceptions\MemberDoesNotExistException; |
|
36
|
|
|
use OCA\Circles\Exceptions\MemberIsNotOwnerException; |
|
37
|
|
|
use OCA\Circles\Model\Circle; |
|
38
|
|
|
use OCA\Circles\Model\Member; |
|
39
|
|
|
use OCP\IL10N; |
|
40
|
|
|
|
|
41
|
|
|
class CirclesService { |
|
42
|
|
|
|
|
43
|
|
|
/** @var string */ |
|
44
|
|
|
private $userId; |
|
45
|
|
|
|
|
46
|
|
|
/** @var IL10N */ |
|
47
|
|
|
private $l10n; |
|
48
|
|
|
|
|
49
|
|
|
/** @var ConfigService */ |
|
50
|
|
|
private $configService; |
|
51
|
|
|
|
|
52
|
|
|
/** @var CirclesRequest */ |
|
53
|
|
|
private $circlesRequest; |
|
54
|
|
|
|
|
55
|
|
|
/** @var MembersRequest */ |
|
56
|
|
|
private $membersRequest; |
|
57
|
|
|
|
|
58
|
|
|
/** @var EventsService */ |
|
59
|
|
|
private $eventsService; |
|
60
|
|
|
|
|
61
|
|
|
/** @var MiscService */ |
|
62
|
|
|
private $miscService; |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* CirclesService constructor. |
|
67
|
|
|
* |
|
68
|
|
|
* @param $userId |
|
69
|
|
|
* @param IL10N $l10n |
|
70
|
|
|
* @param ConfigService $configService |
|
71
|
|
|
* @param CirclesRequest $circlesRequest |
|
72
|
|
|
* @param MembersRequest $membersRequest |
|
73
|
|
|
* @param EventsService $eventsService |
|
74
|
|
|
* @param MiscService $miscService |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
77
|
|
|
$userId, |
|
78
|
|
|
IL10N $l10n, |
|
79
|
|
|
ConfigService $configService, |
|
80
|
|
|
CirclesRequest $circlesRequest, |
|
81
|
|
|
MembersRequest $membersRequest, |
|
82
|
|
|
EventsService $eventsService, |
|
83
|
|
|
MiscService $miscService |
|
84
|
|
|
) { |
|
85
|
|
|
$this->userId = $userId; |
|
86
|
|
|
$this->l10n = $l10n; |
|
87
|
|
|
$this->configService = $configService; |
|
88
|
|
|
$this->circlesRequest = $circlesRequest; |
|
89
|
|
|
$this->membersRequest = $membersRequest; |
|
90
|
|
|
$this->eventsService = $eventsService; |
|
91
|
|
|
$this->miscService = $miscService; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Create circle using this->userId as owner |
|
97
|
|
|
* |
|
98
|
|
|
* @param int|string $type |
|
99
|
|
|
* @param string $name |
|
100
|
|
|
* |
|
101
|
|
|
* @return Circle |
|
102
|
|
|
* @throws CircleTypeDisabledException |
|
103
|
|
|
* @throws \Exception |
|
104
|
|
|
*/ |
|
105
|
|
|
public function createCircle($type, $name) { |
|
106
|
|
|
self::convertTypeStringToBitValue($type); |
|
107
|
|
|
$type = (int)$type; |
|
108
|
|
|
|
|
109
|
|
|
if ($type === '') { |
|
|
|
|
|
|
110
|
|
|
throw new CircleTypeDisabledException( |
|
111
|
|
|
$this->l10n->t('You need a specify a type of circle') |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (!$this->configService->isCircleAllowed($type)) { |
|
116
|
|
|
throw new CircleTypeDisabledException( |
|
117
|
|
|
$this->l10n->t('You cannot create this type of circle') |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$circle = new Circle($this->l10n, $type, $name); |
|
122
|
|
|
|
|
123
|
|
|
try { |
|
124
|
|
|
$this->circlesRequest->createCircle($circle, $this->userId); |
|
125
|
|
|
$this->membersRequest->createMember($circle->getOwner()); |
|
126
|
|
|
} catch (CircleAlreadyExistsException $e) { |
|
127
|
|
|
throw $e; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$this->eventsService->onCircleCreation($circle); |
|
131
|
|
|
|
|
132
|
|
|
return $circle; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* list Circles depends on type (or all) and name (parts) and minimum level. |
|
138
|
|
|
* |
|
139
|
|
|
* @param mixed $type |
|
140
|
|
|
* @param string $name |
|
141
|
|
|
* @param int $level |
|
142
|
|
|
* |
|
143
|
|
|
* @return Circle[] |
|
144
|
|
|
* @throws CircleTypeDisabledException |
|
145
|
|
|
*/ |
|
146
|
|
|
public function listCircles($type, $name = '', $level = 0) { |
|
147
|
|
|
self::convertTypeStringToBitValue($type); |
|
148
|
|
|
|
|
149
|
|
|
if (!$this->configService->isCircleAllowed((int)$type)) { |
|
150
|
|
|
throw new CircleTypeDisabledException( |
|
151
|
|
|
$this->l10n->t('You cannot display this type of circle') |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$data = []; |
|
156
|
|
|
$result = $this->circlesRequest->getCircles($this->userId, $type, $name, $level); |
|
157
|
|
|
foreach ($result as $item) { |
|
158
|
|
|
$data[] = $item; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $data; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* returns details on circle and its members if this->userId is a member itself. |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $circleUniqueId |
|
169
|
|
|
* |
|
170
|
|
|
* @return Circle |
|
171
|
|
|
* @throws \Exception |
|
172
|
|
|
] */ |
|
173
|
|
|
public function detailsCircle($circleUniqueId) { |
|
174
|
|
|
|
|
175
|
|
|
try { |
|
176
|
|
|
$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
|
177
|
|
|
if ($circle->getHigherViewer() |
|
178
|
|
|
->isLevel(Member::LEVEL_MEMBER) |
|
179
|
|
|
) { |
|
180
|
|
|
$this->detailsCircleMembers($circle); |
|
181
|
|
|
$this->detailsCircleLinkedGroups($circle); |
|
182
|
|
|
$this->detailsCircleFederatedCircles($circle); |
|
183
|
|
|
} |
|
184
|
|
|
} catch (\Exception $e) { |
|
185
|
|
|
throw $e; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return $circle; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* get the Members list and add the result to the Circle. |
|
194
|
|
|
* |
|
195
|
|
|
* @param Circle $circle |
|
196
|
|
|
*/ |
|
197
|
|
|
private function detailsCircleMembers(Circle &$circle) { |
|
198
|
|
|
$members = |
|
199
|
|
|
$this->membersRequest->getMembers($circle->getUniqueId(), $circle->getHigherViewer()); |
|
200
|
|
|
|
|
201
|
|
|
$circle->setMembers($members); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* get the Linked Group list and add the result to the Circle. |
|
207
|
|
|
* |
|
208
|
|
|
* @param Circle $circle |
|
209
|
|
|
*/ |
|
210
|
|
|
private function detailsCircleLinkedGroups(Circle &$circle) { |
|
211
|
|
|
$groups = []; |
|
212
|
|
|
if ($this->configService->isLinkedGroupsAllowed()) { |
|
213
|
|
|
$groups = |
|
214
|
|
|
$this->membersRequest->getGroupsFromCircle( |
|
215
|
|
|
$circle->getUniqueId(), $circle->getHigherViewer() |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$circle->setGroups($groups); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* get the Federated Circles list and add the result to the Circle. |
|
225
|
|
|
* |
|
226
|
|
|
* @param Circle $circle |
|
227
|
|
|
*/ |
|
228
|
|
|
private function detailsCircleFederatedCircles(Circle &$circle) { |
|
229
|
|
|
$links = []; |
|
230
|
|
|
|
|
231
|
|
|
try { |
|
232
|
|
|
if ($this->configService->isFederatedCirclesAllowed()) { |
|
233
|
|
|
$circle->hasToBeFederated(); |
|
234
|
|
|
$links = $this->circlesRequest->getLinksFromCircle($circle->getUniqueId()); |
|
235
|
|
|
} |
|
236
|
|
|
} catch (FederatedCircleNotAllowedException $e) { |
|
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
$circle->setLinks($links); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* save new settings if current user is admin. |
|
245
|
|
|
* |
|
246
|
|
|
* @param string $circleUniqueId |
|
247
|
|
|
* @param array $settings |
|
248
|
|
|
* |
|
249
|
|
|
* @return Circle |
|
250
|
|
|
* @throws \Exception |
|
251
|
|
|
*/ |
|
252
|
|
|
public function settingsCircle($circleUniqueId, $settings) { |
|
253
|
|
|
|
|
254
|
|
|
try { |
|
255
|
|
|
$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
|
256
|
|
|
$circle->getHigherViewer() |
|
257
|
|
|
->hasToBeOwner(); |
|
258
|
|
|
|
|
259
|
|
|
$ak = array_keys($settings); |
|
260
|
|
|
foreach ($ak AS $k) { |
|
261
|
|
|
$circle->setSetting($k, $settings[$k]); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
$this->circlesRequest->updateCircle($circle); |
|
265
|
|
|
} catch (\Exception $e) { |
|
266
|
|
|
throw $e; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
return $circle; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Join a circle. |
|
275
|
|
|
* |
|
276
|
|
|
* @param string $circleUniqueId |
|
277
|
|
|
* |
|
278
|
|
|
* @return null|Member |
|
279
|
|
|
* @throws \Exception |
|
280
|
|
|
*/ |
|
281
|
|
|
public function joinCircle($circleUniqueId) { |
|
282
|
|
|
|
|
283
|
|
|
try { |
|
284
|
|
|
$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
|
285
|
|
|
|
|
286
|
|
|
try { |
|
287
|
|
|
$member = |
|
288
|
|
|
$this->membersRequest->forceGetMember( |
|
289
|
|
|
$circle->getUniqueId(), $this->userId, Member::TYPE_USER |
|
290
|
|
|
); |
|
291
|
|
|
} catch (MemberDoesNotExistException $m) { |
|
292
|
|
|
$member = new Member($this->userId, Member::TYPE_USER, $circle->getUniqueId()); |
|
293
|
|
|
$this->membersRequest->createMember($member); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
$member->hasToBeAbleToJoinTheCircle(); |
|
297
|
|
|
$member->joinCircle($circle->getType()); |
|
298
|
|
|
$this->membersRequest->updateMember($member); |
|
299
|
|
|
$this->eventsService->onMemberNew($circle, $member); |
|
300
|
|
|
} catch (\Exception $e) { |
|
301
|
|
|
throw $e; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
return $member; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Leave a circle. |
|
310
|
|
|
* |
|
311
|
|
|
* @param string $circleUniqueId |
|
312
|
|
|
* |
|
313
|
|
|
* @return null|Member |
|
314
|
|
|
* @throws \Exception |
|
315
|
|
|
*/ |
|
316
|
|
View Code Duplication |
public function leaveCircle($circleUniqueId) { |
|
|
|
|
|
|
317
|
|
|
|
|
318
|
|
|
try { |
|
319
|
|
|
$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
|
320
|
|
|
$member = $circle->getViewer(); |
|
321
|
|
|
|
|
322
|
|
|
$member->hasToBeMemberOrAlmost(); |
|
323
|
|
|
$member->cantBeOwner(); |
|
324
|
|
|
|
|
325
|
|
|
$this->eventsService->onMemberLeaving($circle, $member); |
|
326
|
|
|
|
|
327
|
|
|
$member->setStatus(Member::STATUS_NONMEMBER); |
|
328
|
|
|
$member->setLevel(Member::LEVEL_NONE); |
|
329
|
|
|
$this->membersRequest->updateMember($member); |
|
330
|
|
|
} catch (\Exception $e) { |
|
331
|
|
|
throw $e; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
return $member; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* destroy a circle. |
|
340
|
|
|
* |
|
341
|
|
|
* @param string $circleUniqueId |
|
342
|
|
|
* |
|
343
|
|
|
* @throws MemberIsNotOwnerException |
|
344
|
|
|
*/ |
|
345
|
|
|
public function removeCircle($circleUniqueId) { |
|
346
|
|
|
|
|
347
|
|
|
try { |
|
348
|
|
|
$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
|
349
|
|
|
$circle->getHigherViewer() |
|
350
|
|
|
->hasToBeOwner(); |
|
351
|
|
|
|
|
352
|
|
|
$this->eventsService->onCircleDestruction($circle); |
|
353
|
|
|
|
|
354
|
|
|
$this->membersRequest->removeAllFromCircle($circleUniqueId); |
|
355
|
|
|
$this->circlesRequest->destroyCircle($circleUniqueId); |
|
356
|
|
|
|
|
357
|
|
|
} catch (MemberIsNotOwnerException $e) { |
|
358
|
|
|
throw $e; |
|
359
|
|
|
} |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* @param $circleName |
|
365
|
|
|
* |
|
366
|
|
|
* @return Circle|null |
|
367
|
|
|
*/ |
|
368
|
|
|
public function infoCircleByName($circleName) { |
|
369
|
|
|
return $this->circlesRequest->forceGetCircleByName($circleName); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* Convert a Type in String to its Bit Value |
|
374
|
|
|
* |
|
375
|
|
|
* @param string $type |
|
376
|
|
|
*/ |
|
377
|
|
|
public static function convertTypeStringToBitValue(&$type) { |
|
378
|
|
|
if (strtolower($type) === 'personal') { |
|
379
|
|
|
$type = Circle::CIRCLES_PERSONAL; |
|
380
|
|
|
} |
|
381
|
|
|
if (strtolower($type) === 'secret') { |
|
382
|
|
|
$type = Circle::CIRCLES_SECRET; |
|
383
|
|
|
} |
|
384
|
|
|
if (strtolower($type) === 'closed') { |
|
385
|
|
|
$type = Circle::CIRCLES_CLOSED; |
|
386
|
|
|
} |
|
387
|
|
|
if (strtolower($type) === 'public') { |
|
388
|
|
|
$type = Circle::CIRCLES_PUBLIC; |
|
389
|
|
|
} |
|
390
|
|
|
if (strtolower($type) === 'all') { |
|
391
|
|
|
$type = Circle::CIRCLES_ALL; |
|
392
|
|
|
} |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* getCircleIcon() |
|
398
|
|
|
* |
|
399
|
|
|
* Return the right imagePath for a type of circle. |
|
400
|
|
|
* |
|
401
|
|
|
* @param string $type |
|
402
|
|
|
* @param bool $png |
|
403
|
|
|
* |
|
404
|
|
|
* @return string |
|
405
|
|
|
*/ |
|
406
|
|
|
public static function getCircleIcon($type, $png = false) { |
|
407
|
|
|
|
|
408
|
|
|
$ext = '.svg'; |
|
409
|
|
|
if ($png === true) { |
|
410
|
|
|
$ext = '.png'; |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
$urlGen = \OC::$server->getURLGenerator(); |
|
414
|
|
|
switch ($type) { |
|
415
|
|
|
case Circle::CIRCLES_PERSONAL: |
|
416
|
|
|
return $urlGen->getAbsoluteURL( |
|
417
|
|
|
$urlGen->imagePath('circles', 'personal' . $ext) |
|
418
|
|
|
); |
|
419
|
|
|
case Circle::CIRCLES_CLOSED: |
|
420
|
|
|
return $urlGen->getAbsoluteURL( |
|
421
|
|
|
$urlGen->imagePath('circles', 'closed' . $ext) |
|
422
|
|
|
); |
|
423
|
|
|
case Circle::CIRCLES_SECRET: |
|
424
|
|
|
return $urlGen->getAbsoluteURL( |
|
425
|
|
|
$urlGen->imagePath('circles', 'secret' . $ext) |
|
426
|
|
|
); |
|
427
|
|
|
case Circle::CIRCLES_PUBLIC: |
|
428
|
|
|
return $urlGen->getAbsoluteURL( |
|
429
|
|
|
$urlGen->imagePath('circles', 'public' . $ext) |
|
430
|
|
|
); |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
return $urlGen->getAbsoluteURL($urlGen->imagePath('circles', 'black_circle' . $ext)); |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.