|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Circles - Bring cloud-users closer together. |
|
5
|
|
|
* |
|
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
7
|
|
|
* later. See the COPYING file. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Maxence Lange <[email protected]> |
|
10
|
|
|
* @copyright 2017 |
|
11
|
|
|
* @license GNU AGPL version 3 or any later version |
|
12
|
|
|
* |
|
13
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
14
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
15
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
16
|
|
|
* License, or (at your option) any later version. |
|
17
|
|
|
* |
|
18
|
|
|
* This program is distributed in the hope that it will be useful, |
|
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21
|
|
|
* GNU Affero General Public License for more details. |
|
22
|
|
|
* |
|
23
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace OCA\Circles\Db; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
|
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 OCP\DB\QueryBuilder\IQueryBuilder; |
|
37
|
|
|
|
|
38
|
|
|
class CirclesRequestBuilder extends CoreRequestBuilder { |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Join the Circles table |
|
42
|
|
|
* |
|
43
|
|
|
* @param IQueryBuilder $qb |
|
44
|
|
|
* @param string $field |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function joinCircles(& $qb, $field) { |
|
47
|
|
|
$expr = $qb->expr(); |
|
48
|
|
|
|
|
49
|
|
|
$qb->from(self::TABLE_CIRCLES, 'c') |
|
50
|
|
|
->andWhere($expr->eq('c.id', $field)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* add a request to the members list, using the current user ID. |
|
56
|
|
|
* will returns level and stuff. |
|
57
|
|
|
* |
|
58
|
|
|
* @param IQueryBuilder $qb |
|
59
|
|
|
* @param string $userId |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function leftJoinUserIdAsViewer(IQueryBuilder & $qb, $userId) { |
|
62
|
|
|
|
|
63
|
|
|
if ($qb->getType() !== QueryBuilder::SELECT) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$expr = $qb->expr(); |
|
68
|
|
|
$pf = $this->default_select_alias . '.'; |
|
69
|
|
|
|
|
70
|
|
|
$qb->selectAlias('u.user_id', 'viewer_userid'); |
|
71
|
|
|
$qb->selectAlias('u.level', 'viewer_level'); |
|
72
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
73
|
|
|
$qb->leftJoin( |
|
74
|
|
|
$this->default_select_alias, CoreRequestBuilder::TABLE_MEMBERS, 'u', |
|
75
|
|
|
$expr->andX( |
|
76
|
|
|
$expr->eq($pf . 'id', 'u.circle_id'), |
|
77
|
|
|
$expr->eq('u.user_id', $qb->createNamedParameter($userId)) |
|
78
|
|
|
) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param IQueryBuilder $qb |
|
84
|
|
|
* |
|
85
|
|
|
* @deprecated |
|
86
|
|
|
* never used in fact. |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function leftJoinOwner(IQueryBuilder & $qb) { |
|
89
|
|
|
|
|
90
|
|
|
if ($qb->getType() !== QueryBuilder::SELECT) { |
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$expr = $qb->expr(); |
|
95
|
|
|
$pf = $this->default_select_alias . '.'; |
|
96
|
|
|
|
|
97
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
98
|
|
|
$qb->leftJoin( |
|
99
|
|
|
$this->default_select_alias, MembersMapper::TABLENAME, 'o', |
|
100
|
|
|
$expr->andX( |
|
101
|
|
|
$expr->eq($pf . 'id', 'o.circle_id'), |
|
102
|
|
|
$expr->eq('o.level', $qb->createNamedParameter(Member::LEVEL_OWNER)) |
|
103
|
|
|
) |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Base of the Sql Select request for Shares |
|
110
|
|
|
* |
|
111
|
|
|
* @return IQueryBuilder |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function getLinksSelectSql() { |
|
114
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
115
|
|
|
|
|
116
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
117
|
|
|
$qb->select('id', 'status', 'address', 'token', 'circle_id', 'unique_id', 'creation') |
|
118
|
|
|
->from('circles_links', 's'); |
|
119
|
|
|
|
|
120
|
|
|
$this->default_select_alias = 's'; |
|
121
|
|
|
|
|
122
|
|
|
return $qb; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Base of the Sql Select request for Shares |
|
128
|
|
|
* |
|
129
|
|
|
* @return IQueryBuilder |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function getSharesSelectSql() { |
|
132
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
133
|
|
|
|
|
134
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
135
|
|
|
$qb->select( |
|
136
|
|
|
'circle_id', 'source', 'type', 'author', 'cloud_id', 'payload', 'creation', 'headers', |
|
137
|
|
|
'unique_id' |
|
138
|
|
|
) |
|
139
|
|
|
->from('circles_shares', 's'); |
|
140
|
|
|
|
|
141
|
|
|
$this->default_select_alias = 's'; |
|
142
|
|
|
|
|
143
|
|
|
return $qb; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Base of the Sql Insert request for Shares |
|
148
|
|
|
* |
|
149
|
|
|
* @return IQueryBuilder |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function getSharesInsertSql() { |
|
152
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
153
|
|
|
$qb->insert('circles_shares') |
|
154
|
|
|
->setValue('creation', $qb->createFunction('NOW()')); |
|
155
|
|
|
|
|
156
|
|
|
return $qb; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Base of the Sql Update request for Shares |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $uniqueId |
|
164
|
|
|
* |
|
165
|
|
|
* @return IQueryBuilder |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function getSharesUpdateSql($uniqueId) { |
|
168
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
169
|
|
|
$qb->update('circles_shares') |
|
170
|
|
|
->where( |
|
171
|
|
|
$qb->expr() |
|
172
|
|
|
->eq('unique_id', $qb->createNamedParameter((string)$uniqueId)) |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
return $qb; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Base of the Sql Update request for Shares |
|
181
|
|
|
* |
|
182
|
|
|
* @param int $circleId |
|
183
|
|
|
* |
|
184
|
|
|
* @return IQueryBuilder |
|
185
|
|
|
*/ |
|
186
|
|
View Code Duplication |
protected function getCirclesUpdateSql($circleId) { |
|
|
|
|
|
|
187
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
188
|
|
|
$qb->update('circles_circles') |
|
189
|
|
|
->where( |
|
190
|
|
|
$qb->expr() |
|
191
|
|
|
->eq('id', $qb->createNamedParameter($circleId)) |
|
192
|
|
|
); |
|
193
|
|
|
|
|
194
|
|
|
return $qb; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @return IQueryBuilder |
|
200
|
|
|
*/ |
|
201
|
|
|
protected function getMembersSelectSql() { |
|
202
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
203
|
|
|
|
|
204
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
205
|
|
|
$qb->select('user_id', 'circle_id', 'level', 'status', 'joined') |
|
206
|
|
|
->from('circles_members', 'm'); |
|
207
|
|
|
|
|
208
|
|
|
$this->default_select_alias = 'm'; |
|
209
|
|
|
|
|
210
|
|
|
return $qb; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @return IQueryBuilder |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function getCirclesSelectSql() { |
|
218
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
219
|
|
|
|
|
220
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
221
|
|
|
$qb->select( |
|
222
|
|
|
'c.id', 'c.unique_id', 'c.name', 'c.description', 'c.settings', 'c.type', 'c.creation' |
|
223
|
|
|
) |
|
224
|
|
|
->from('circles_circles', 'c'); |
|
225
|
|
|
$this->default_select_alias = 'c'; |
|
226
|
|
|
|
|
227
|
|
|
return $qb; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param array $data |
|
232
|
|
|
* |
|
233
|
|
|
* @return Member |
|
234
|
|
|
*/ |
|
235
|
|
|
protected function parseMembersSelectSql(array $data) { |
|
236
|
|
|
$member = new Member($this->l10n); |
|
237
|
|
|
$member->setUserId($data['user_id']); |
|
238
|
|
|
$member->setCircleId($data['circle_id']); |
|
239
|
|
|
$member->setLevel($data['level']); |
|
240
|
|
|
$member->setStatus($data['status']); |
|
241
|
|
|
$member->setJoined($data['joined']); |
|
242
|
|
|
|
|
243
|
|
|
return $member; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @param array $data |
|
249
|
|
|
* |
|
250
|
|
|
* @return Circle |
|
|
|
|
|
|
251
|
|
|
*/ |
|
252
|
|
|
protected function parseCirclesSelectSql($data) { |
|
253
|
|
|
if ($data === false || $data === null) { |
|
254
|
|
|
return null; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
$circle = new Circle($this->l10n); |
|
258
|
|
|
$circle->setId($data['id']); |
|
259
|
|
|
$circle->setUniqueId($data['unique_id']); |
|
260
|
|
|
$circle->setName($data['name']); |
|
261
|
|
|
$circle->setDescription($data['description']); |
|
262
|
|
|
$circle->setSettings($data['settings']); |
|
263
|
|
|
$circle->setType($data['type']); |
|
264
|
|
|
$circle->setCreation($data['creation']); |
|
265
|
|
|
|
|
266
|
|
|
if (key_exists('viewer_level', $data)) { |
|
267
|
|
|
$user = new Member($this->l10n); |
|
268
|
|
|
$user->setUserId($data['viewer_userid']); |
|
269
|
|
|
$user->setLevel($data['viewer_level']); |
|
270
|
|
|
$circle->setViewer($user); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
return $circle; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @param array $data |
|
279
|
|
|
* |
|
280
|
|
|
* @return SharingFrame |
|
|
|
|
|
|
281
|
|
|
*/ |
|
282
|
|
|
protected function parseSharesSelectSql($data) { |
|
283
|
|
|
if ($data === false || $data === null) { |
|
284
|
|
|
return null; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
$frame = new SharingFrame($data['source'], $data['type']); |
|
288
|
|
|
$frame->setCircleId($data['circle_id']); |
|
289
|
|
|
$frame->setAuthor($data['author']); |
|
290
|
|
|
$frame->setCloudId($data['cloud_id']); |
|
291
|
|
|
$frame->setPayload(json_decode($data['payload'], true)); |
|
292
|
|
|
$frame->setCreation($data['creation']); |
|
293
|
|
|
$frame->setHeaders(json_decode($data['headers'], true)); |
|
294
|
|
|
$frame->setUniqueId($data['unique_id']); |
|
295
|
|
|
|
|
296
|
|
|
return $frame; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* @param array $data |
|
302
|
|
|
* |
|
303
|
|
|
* @return FederatedLink |
|
|
|
|
|
|
304
|
|
|
*/ |
|
305
|
|
View Code Duplication |
public function parseLinksSelectSql($data) { |
|
|
|
|
|
|
306
|
|
|
if ($data === false || $data === null) { |
|
307
|
|
|
return null; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
$link = new FederatedLink(); |
|
311
|
|
|
$link->setId($data['id']) |
|
312
|
|
|
->setUniqueId($data['unique_id']) |
|
313
|
|
|
->setStatus($data['status']) |
|
314
|
|
|
->setCreation($data['creation']) |
|
315
|
|
|
->setAddress($data['address']) |
|
316
|
|
|
->setToken($data['token']) |
|
317
|
|
|
->setCircleId($data['circle_id']); |
|
318
|
|
|
|
|
319
|
|
|
return $link; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
|
|
323
|
|
|
} |
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.