1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Circles - Bring cloud-users closer together. |
8
|
|
|
* |
9
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
10
|
|
|
* later. See the COPYING file. |
11
|
|
|
* |
12
|
|
|
* @author Maxence Lange <[email protected]> |
13
|
|
|
* @copyright 2021 |
14
|
|
|
* @license GNU AGPL version 3 or any later version |
15
|
|
|
* |
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Affero General Public License as |
18
|
|
|
* published by the Free Software Foundation, either version 3 of the |
19
|
|
|
* License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU Affero General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Affero General Public License |
27
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
namespace OCA\Circles\Db; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use OCA\Circles\Exceptions\RemoteNotFoundException; |
36
|
|
|
use OCA\Circles\Exceptions\RemoteUidException; |
37
|
|
|
use OCA\Circles\Exceptions\RequestBuilderException; |
38
|
|
|
use OCA\Circles\Model\Circle; |
39
|
|
|
use OCA\Circles\Model\Federated\RemoteInstance; |
40
|
|
|
use OCA\Circles\Model\Member; |
41
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Class RemoteRequest |
46
|
|
|
* |
47
|
|
|
* @package OCA\Circles\Db |
48
|
|
|
*/ |
49
|
|
|
class RemoteRequest extends RemoteRequestBuilder { |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param RemoteInstance $remote |
54
|
|
|
* |
55
|
|
|
* @throws RemoteUidException |
56
|
|
|
*/ |
57
|
|
|
public function save(RemoteInstance $remote): void { |
58
|
|
|
$remote->mustBeIdentityAuthed(); |
59
|
|
|
$qb = $this->getRemoteInsertSql(); |
60
|
|
|
$qb->setValue('uid', $qb->createNamedParameter($remote->getUid(true))) |
61
|
|
|
->setValue('instance', $qb->createNamedParameter($remote->getInstance())) |
62
|
|
|
->setValue('href', $qb->createNamedParameter($remote->getId())) |
63
|
|
|
->setValue('type', $qb->createNamedParameter($remote->getType())) |
64
|
|
|
->setValue('interface', $qb->createNamedParameter($remote->getInterface())) |
65
|
|
|
->setValue('item', $qb->createNamedParameter(json_encode($remote->getOrigData()))); |
66
|
|
|
|
67
|
|
|
$qb->execute(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param RemoteInstance $remote |
73
|
|
|
* |
74
|
|
|
* @throws RemoteUidException |
75
|
|
|
*/ |
76
|
|
|
public function update(RemoteInstance $remote) { |
77
|
|
|
$remote->mustBeIdentityAuthed(); |
78
|
|
|
$qb = $this->getRemoteUpdateSql(); |
79
|
|
|
$qb->set('uid', $qb->createNamedParameter($remote->getUid(true))) |
80
|
|
|
->set('href', $qb->createNamedParameter($remote->getId())) |
81
|
|
|
->set('type', $qb->createNamedParameter($remote->getType())) |
82
|
|
|
->set('item', $qb->createNamedParameter(json_encode($remote->getOrigData()))); |
83
|
|
|
|
84
|
|
|
$qb->limitToInstance($remote->getInstance()); |
85
|
|
|
|
86
|
|
|
$qb->execute(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param RemoteInstance $remote |
92
|
|
|
* |
93
|
|
|
* @throws RemoteUidException |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function updateItem(RemoteInstance $remote) { |
|
|
|
|
96
|
|
|
$remote->mustBeIdentityAuthed(); |
97
|
|
|
$qb = $this->getRemoteUpdateSql(); |
98
|
|
|
$qb->set('item', $qb->createNamedParameter(json_encode($remote->getOrigData()))); |
99
|
|
|
|
100
|
|
|
$qb->limitToDBField('uid', $remote->getUid(true), false); |
|
|
|
|
101
|
|
|
|
102
|
|
|
$qb->execute(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param RemoteInstance $remote |
108
|
|
|
* |
109
|
|
|
* @throws RemoteUidException |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public function updateInstance(RemoteInstance $remote) { |
|
|
|
|
112
|
|
|
$remote->mustBeIdentityAuthed(); |
113
|
|
|
$qb = $this->getRemoteUpdateSql(); |
114
|
|
|
$qb->set('instance', $qb->createNamedParameter($remote->getInstance())); |
115
|
|
|
|
116
|
|
|
$qb->limitToDBField('uid', $remote->getUid(true), false); |
|
|
|
|
117
|
|
|
|
118
|
|
|
$qb->execute(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param RemoteInstance $remote |
124
|
|
|
* |
125
|
|
|
* @throws RemoteUidException |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function updateType(RemoteInstance $remote) { |
|
|
|
|
128
|
|
|
$remote->mustBeIdentityAuthed(); |
129
|
|
|
$qb = $this->getRemoteUpdateSql(); |
130
|
|
|
$qb->set('type', $qb->createNamedParameter($remote->getType())); |
131
|
|
|
|
132
|
|
|
$qb->limitToDBField('uid', $remote->getUid(true), false); |
|
|
|
|
133
|
|
|
|
134
|
|
|
$qb->execute(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param RemoteInstance $remote |
140
|
|
|
* |
141
|
|
|
* @throws RemoteUidException |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
public function updateHref(RemoteInstance $remote) { |
|
|
|
|
144
|
|
|
$remote->mustBeIdentityAuthed(); |
145
|
|
|
$qb = $this->getRemoteUpdateSql(); |
146
|
|
|
$qb->set('href', $qb->createNamedParameter($remote->getId())); |
147
|
|
|
|
148
|
|
|
$qb->limitToDBField('uid', $remote->getUid(true), false); |
|
|
|
|
149
|
|
|
|
150
|
|
|
$qb->execute(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return RemoteInstance[] |
156
|
|
|
*/ |
157
|
|
|
public function getAllInstances(): array { |
158
|
|
|
$qb = $this->getRemoteSelectSql(); |
159
|
|
|
|
160
|
|
|
return $this->getItemsFromRequest($qb); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return RemoteInstance[] |
165
|
|
|
*/ |
166
|
|
|
public function getKnownInstances(): array { |
167
|
|
|
$qb = $this->getRemoteSelectSql(); |
168
|
|
|
$qb->filterDBField('type', RemoteInstance::TYPE_UNKNOWN, false); |
|
|
|
|
169
|
|
|
|
170
|
|
|
return $this->getItemsFromRequest($qb); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* - returns: |
176
|
|
|
* - all GLOBAL_SCALE |
177
|
|
|
* - TRUSTED if Circle is Federated |
178
|
|
|
* - EXTERNAL if Circle is Federated and a contains a member from instance |
179
|
|
|
* |
180
|
|
|
* @param Circle $circle |
181
|
|
|
* @param bool $broadcastAsFederated |
182
|
|
|
* |
183
|
|
|
* @return RemoteInstance[] |
184
|
|
|
* @throws RequestBuilderException |
185
|
|
|
*/ |
186
|
|
|
public function getOutgoingRecipient(Circle $circle, bool $broadcastAsFederated = false): array { |
187
|
|
|
$qb = $this->getRemoteSelectSql(); |
188
|
|
|
$expr = $qb->expr(); |
189
|
|
|
$orX = $expr->orX(); |
190
|
|
|
|
191
|
|
|
$orX->add($qb->exprLimitToDBField('type', RemoteInstance::TYPE_GLOBALSCALE, true, false)); |
|
|
|
|
192
|
|
|
|
193
|
|
|
if ($circle->isConfig(Circle::CFG_FEDERATED) || $broadcastAsFederated) { |
194
|
|
|
|
195
|
|
|
// get all TRUSTED |
196
|
|
|
$orX->add($qb->exprLimitToDBField('type', RemoteInstance::TYPE_TRUSTED, true, false)); |
|
|
|
|
197
|
|
|
|
198
|
|
|
// get EXTERNAL with Members |
199
|
|
|
$aliasMember = $qb->generateAlias(CoreQueryBuilder::REMOTE, CoreQueryBuilder::MEMBER); |
200
|
|
|
$qb->leftJoin( |
201
|
|
|
CoreQueryBuilder::REMOTE, self::TABLE_MEMBER, $aliasMember, |
202
|
|
|
$expr->andX( |
203
|
|
|
$expr->eq($aliasMember . '.circle_id', $qb->createNamedParameter($circle->getSingleId())), |
204
|
|
|
$expr->eq($aliasMember . '.instance', CoreQueryBuilder::REMOTE . '.instance'), |
205
|
|
|
$expr->gte( |
206
|
|
|
$aliasMember . '.level', |
207
|
|
|
$qb->createNamedParameter(Member::LEVEL_MEMBER, IQueryBuilder::PARAM_INT) |
208
|
|
|
) |
209
|
|
|
) |
210
|
|
|
); |
211
|
|
|
|
212
|
|
|
$external = $expr->andX(); |
213
|
|
|
$external->add($qb->exprLimitToDBField('type', RemoteInstance::TYPE_EXTERNAL, true, false)); |
|
|
|
|
214
|
|
|
$external->add($expr->isNotNull($aliasMember . '.instance')); |
215
|
|
|
$orX->add($external); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$qb->andWhere($orX) |
219
|
|
|
->generateGroupBy(self::$tables[self::TABLE_REMOTE], CoreQueryBuilder::REMOTE); |
|
|
|
|
220
|
|
|
|
221
|
|
|
return $this->getItemsFromRequest($qb); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $host |
227
|
|
|
* |
228
|
|
|
* @return RemoteInstance |
229
|
|
|
* @throws RemoteNotFoundException |
230
|
|
|
*/ |
231
|
|
|
public function getFromInstance(string $host): RemoteInstance { |
232
|
|
|
$qb = $this->getRemoteSelectSql(); |
233
|
|
|
$qb->limitToInstance($host); |
234
|
|
|
|
235
|
|
|
return $this->getItemFromRequest($qb); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param string $href |
241
|
|
|
* |
242
|
|
|
* @return RemoteInstance |
243
|
|
|
* @throws RemoteNotFoundException |
244
|
|
|
*/ |
245
|
|
|
public function getFromHref(string $href): RemoteInstance { |
246
|
|
|
$qb = $this->getRemoteSelectSql(); |
247
|
|
|
$qb->limitToDBField('href', $href, false); |
|
|
|
|
248
|
|
|
|
249
|
|
|
return $this->getItemFromRequest($qb); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param string $status |
255
|
|
|
* |
256
|
|
|
* @return RemoteInstance[] |
257
|
|
|
*/ |
258
|
|
|
public function getFromType(string $status): array { |
259
|
|
|
$qb = $this->getRemoteSelectSql(); |
260
|
|
|
$qb->limitToTypeString($status); |
261
|
|
|
|
262
|
|
|
return $this->getItemsFromRequest($qb); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param RemoteInstance $remoteInstance |
268
|
|
|
* |
269
|
|
|
* @return RemoteInstance |
270
|
|
|
* @throws RemoteNotFoundException |
271
|
|
|
*/ |
272
|
|
|
public function searchDuplicate(RemoteInstance $remoteInstance) { |
273
|
|
|
$qb = $this->getRemoteSelectSql(); |
274
|
|
|
$orX = $qb->expr()->orX(); |
275
|
|
|
$orX->add($qb->exprLimitToDBField('href', $remoteInstance->getId(), true, false)); |
|
|
|
|
276
|
|
|
$orX->add($qb->exprLimitToDBField('uid', $remoteInstance->getUid(true), true)); |
|
|
|
|
277
|
|
|
$orX->add($qb->exprLimitToDBField('instance', $remoteInstance->getInstance(), true, false)); |
|
|
|
|
278
|
|
|
$qb->andWhere($orX); |
279
|
|
|
|
280
|
|
|
return $this->getItemFromRequest($qb); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param RemoteInstance $remoteInstance |
286
|
|
|
*/ |
287
|
|
|
public function deleteById(RemoteInstance $remoteInstance) { |
288
|
|
|
$qb = $this->getRemoteDeleteSql(); |
289
|
|
|
$qb->limitToId($remoteInstance->getDbId()); |
290
|
|
|
$qb->execute(); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
|
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
|
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.