|
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\BackgroundJob\FederatedCircle; |
|
31
|
|
|
use OCA\Circles\Db\CirclesMapper; |
|
32
|
|
|
use OCA\Circles\Db\MembersMapper; |
|
33
|
|
|
use OCA\Circles\Exceptions\CircleTypeDisabledException; |
|
34
|
|
|
use OCA\Circles\Exceptions\FederatedCircleLinkFormatException; |
|
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
|
|
|
use Punic\Exception; |
|
41
|
|
|
|
|
42
|
|
|
class CirclesService { |
|
43
|
|
|
|
|
44
|
|
|
/** @var string */ |
|
45
|
|
|
private $userId; |
|
46
|
|
|
|
|
47
|
|
|
/** @var IL10N */ |
|
48
|
|
|
private $l10n; |
|
49
|
|
|
|
|
50
|
|
|
/** @var ConfigService */ |
|
51
|
|
|
private $configService; |
|
52
|
|
|
|
|
53
|
|
|
/** @var CirclesMapper */ |
|
54
|
|
|
private $dbCircles; |
|
55
|
|
|
|
|
56
|
|
|
/** @var MembersMapper */ |
|
57
|
|
|
private $dbMembers; |
|
58
|
|
|
|
|
59
|
|
|
/** @var MiscService */ |
|
60
|
|
|
private $miscService; |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* CirclesService constructor. |
|
65
|
|
|
* |
|
66
|
|
|
* @param $userId |
|
67
|
|
|
* @param IL10N $l10n |
|
68
|
|
|
* @param ConfigService $configService |
|
69
|
|
|
* @param DatabaseService $databaseService |
|
70
|
|
|
* @param MiscService $miscService |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __construct( |
|
73
|
|
|
$userId, |
|
74
|
|
|
IL10N $l10n, |
|
75
|
|
|
ConfigService $configService, |
|
76
|
|
|
DatabaseService $databaseService, |
|
77
|
|
|
MiscService $miscService |
|
78
|
|
|
) { |
|
79
|
|
|
$this->userId = $userId; |
|
80
|
|
|
$this->l10n = $l10n; |
|
81
|
|
|
$this->configService = $configService; |
|
82
|
|
|
$this->miscService = $miscService; |
|
83
|
|
|
|
|
84
|
|
|
$this->dbCircles = $databaseService->getCirclesMapper(); |
|
85
|
|
|
$this->dbMembers = $databaseService->getMembersMapper(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Create circle using this->userId as owner |
|
91
|
|
|
* |
|
92
|
|
|
* @param int $type |
|
93
|
|
|
* @param string $name |
|
94
|
|
|
* |
|
95
|
|
|
* @return Circle |
|
96
|
|
|
* @throws CircleTypeDisabledException |
|
97
|
|
|
* @throws \Exception |
|
98
|
|
|
*/ |
|
99
|
|
|
public function createCircle($type, $name) { |
|
100
|
|
|
self::convertTypeStringToBitValue($type); |
|
101
|
|
|
|
|
102
|
|
|
if ($type === "") { |
|
103
|
|
|
throw new CircleTypeDisabledException( |
|
104
|
|
|
$this->l10n->t('You need a specify a type of circle') |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (!$this->configService->isCircleAllowed($type)) { |
|
109
|
|
|
throw new CircleTypeDisabledException( |
|
110
|
|
|
$this->l10n->t('You cannot create this type of circle') |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$owner = new Member($this->l10n, $this->userId); |
|
115
|
|
|
$owner->setStatus(Member::STATUS_MEMBER); |
|
116
|
|
|
$circle = new Circle($this->l10n, $type, $name); |
|
117
|
|
|
$circle->setMembers([$owner]); |
|
118
|
|
|
|
|
119
|
|
|
try { |
|
120
|
|
|
$this->dbCircles->create($circle, $owner); |
|
121
|
|
|
$this->dbMembers->add($owner); |
|
122
|
|
|
} catch (\Exception $e) { |
|
123
|
|
|
$this->dbCircles->destroy($circle->getId()); |
|
124
|
|
|
throw $e; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $circle; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* list Circles depends on type (or all) and name (parts) and minimum level. |
|
133
|
|
|
* |
|
134
|
|
|
* @param $type |
|
135
|
|
|
* @param string $name |
|
136
|
|
|
* @param int $level |
|
137
|
|
|
* |
|
138
|
|
|
* @return array |
|
139
|
|
|
* @throws CircleTypeDisabledException |
|
140
|
|
|
*/ |
|
141
|
|
|
public function listCircles($type, $name = '', $level = 0) { |
|
142
|
|
|
self::convertTypeStringToBitValue($type); |
|
143
|
|
|
|
|
144
|
|
|
if (!$this->configService->isCircleAllowed((int)$type)) { |
|
145
|
|
|
throw new CircleTypeDisabledException( |
|
146
|
|
|
$this->l10n->t('You cannot display this type of circle') |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$data = []; |
|
151
|
|
|
$result = $this->dbCircles->findCirclesByUser($this->userId, $type, $name, $level); |
|
152
|
|
|
foreach ($result as $item) { |
|
153
|
|
|
$data[] = $item; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $data; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* returns details on circle and its members if this->userId is a member itself. |
|
162
|
|
|
* |
|
163
|
|
|
* @param $circleId |
|
164
|
|
|
* |
|
165
|
|
|
* @return Circle |
|
166
|
|
|
* @throws \Exception |
|
167
|
|
|
* @internal param $circleId |
|
168
|
|
|
* @internal param string $iError |
|
169
|
|
|
*/ |
|
170
|
|
|
public function detailsCircle($circleId) { |
|
171
|
|
|
|
|
172
|
|
|
try { |
|
173
|
|
|
$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); |
|
174
|
|
|
if ($circle->getUser() |
|
175
|
|
|
->isLevel(Member::LEVEL_MEMBER) |
|
176
|
|
|
) { |
|
177
|
|
|
$members = $this->dbMembers->getMembersFromCircle( |
|
178
|
|
|
$circleId, $circle->getUser() |
|
179
|
|
|
); |
|
180
|
|
|
$circle->setMembers($members); |
|
181
|
|
|
} |
|
182
|
|
|
} catch (\Exception $e) { |
|
183
|
|
|
throw $e; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $circle; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Join a circle. |
|
192
|
|
|
* |
|
193
|
|
|
* @param $circleId |
|
194
|
|
|
* |
|
195
|
|
|
* @return null|Member |
|
196
|
|
|
* @throws \Exception |
|
197
|
|
|
*/ |
|
198
|
|
View Code Duplication |
public function joinCircle($circleId) { |
|
199
|
|
|
|
|
200
|
|
|
try { |
|
201
|
|
|
$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); |
|
202
|
|
|
|
|
203
|
|
|
try { |
|
204
|
|
|
$member = $this->dbMembers->getMemberFromCircle($circle->getId(), $this->userId); |
|
205
|
|
|
} catch (MemberDoesNotExistException $m) { |
|
206
|
|
|
$member = new Member($this->l10n, $this->userId, $circle->getId()); |
|
207
|
|
|
$this->dbMembers->add($member); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$member->hasToBeAbleToJoinTheCircle(); |
|
211
|
|
|
$member->joinCircle($circle->getType()); |
|
212
|
|
|
$this->dbMembers->editMember($member); |
|
213
|
|
|
|
|
214
|
|
|
} catch (\Exception $e) { |
|
215
|
|
|
throw $e; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
return $member; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Leave a circle. |
|
224
|
|
|
* |
|
225
|
|
|
* @param $circleId |
|
226
|
|
|
* |
|
227
|
|
|
* @return null|Member |
|
228
|
|
|
* @throws \Exception |
|
229
|
|
|
*/ |
|
230
|
|
|
public function leaveCircle($circleId) { |
|
231
|
|
|
|
|
232
|
|
|
try { |
|
233
|
|
|
$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId); |
|
234
|
|
|
$member = $this->dbMembers->getMemberFromCircle($circle->getId(), $this->userId, false); |
|
235
|
|
|
|
|
236
|
|
|
if (!$member->isAlmostMember()) { |
|
237
|
|
|
$member->hasToBeMember(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
$member->cantBeOwner(); |
|
241
|
|
|
$member->setStatus(Member::STATUS_NONMEMBER); |
|
242
|
|
|
$member->setLevel(Member::LEVEL_NONE); |
|
243
|
|
|
$this->dbMembers->editMember($member); |
|
244
|
|
|
|
|
245
|
|
|
} catch (\Exception $e) { |
|
246
|
|
|
throw $e; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return $member; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* destroy a circle. |
|
255
|
|
|
* |
|
256
|
|
|
* @param int $circleId |
|
257
|
|
|
* |
|
258
|
|
|
* @throws MemberIsNotOwnerException |
|
259
|
|
|
*/ |
|
260
|
|
|
public function removeCircle($circleId) { |
|
261
|
|
|
|
|
262
|
|
|
try { |
|
263
|
|
|
$member = $this->dbMembers->getMemberFromCircle($circleId, $this->userId, false); |
|
264
|
|
|
$member->hasToBeOwner(); |
|
265
|
|
|
|
|
266
|
|
|
$this->dbMembers->removeAllFromCircle($circleId); |
|
267
|
|
|
$this->dbCircles->destroy($circleId); |
|
268
|
|
|
|
|
269
|
|
|
} catch (MemberIsNotOwnerException $e) { |
|
270
|
|
|
throw $e; |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @param $circleName |
|
277
|
|
|
* |
|
278
|
|
|
* @return Circle|null |
|
279
|
|
|
*/ |
|
280
|
|
|
public function infoCircleByName($circleName) { |
|
281
|
|
|
return $this->dbCircles->getDetailsFromCircleByName($circleName); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Convert a Type in String to its Bit Value |
|
286
|
|
|
* |
|
287
|
|
|
* @param $type |
|
288
|
|
|
* |
|
289
|
|
|
* @return int |
|
|
|
|
|
|
290
|
|
|
*/ |
|
291
|
|
|
public static function convertTypeStringToBitValue(& $type) { |
|
292
|
|
|
if (strtolower($type) === 'personal') { |
|
293
|
|
|
$type = Circle::CIRCLES_PERSONAL; |
|
294
|
|
|
} |
|
295
|
|
|
if (strtolower($type) === 'hidden') { |
|
296
|
|
|
$type = Circle::CIRCLES_HIDDEN; |
|
297
|
|
|
} |
|
298
|
|
|
if (strtolower($type) === 'private') { |
|
299
|
|
|
$type = Circle::CIRCLES_PRIVATE; |
|
300
|
|
|
} |
|
301
|
|
|
if (strtolower($type) === 'public') { |
|
302
|
|
|
$type = Circle::CIRCLES_PUBLIC; |
|
303
|
|
|
} |
|
304
|
|
|
if (strtolower($type) === 'all') { |
|
305
|
|
|
$type = Circle::CIRCLES_ALL; |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
} |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.