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\Service; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use daita\MySmallPhpTools\Model\Request; |
36
|
|
|
use daita\MySmallPhpTools\Model\SimpleDataStore; |
37
|
|
|
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Request; |
38
|
|
|
use OCA\Circles\Db\EventWrapperRequest; |
39
|
|
|
use OCA\Circles\Exceptions\FederatedItemException; |
40
|
|
|
use OCA\Circles\Exceptions\OwnerNotFoundException; |
41
|
|
|
use OCA\Circles\Exceptions\RemoteInstanceException; |
42
|
|
|
use OCA\Circles\Exceptions\RemoteNotFoundException; |
43
|
|
|
use OCA\Circles\Exceptions\RemoteResourceNotFoundException; |
44
|
|
|
use OCA\Circles\Exceptions\UnknownRemoteException; |
45
|
|
|
use OCA\Circles\Model\Federated\EventWrapper; |
46
|
|
|
use OCA\Circles\Model\Federated\FederatedEvent; |
47
|
|
|
use OCA\Circles\Model\Federated\RemoteInstance; |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Class RemoteUpstreamService |
52
|
|
|
* |
53
|
|
|
* @package OCA\Circles\Service |
54
|
|
|
*/ |
55
|
|
|
class RemoteUpstreamService { |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
use TNC22Request; |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** @var EventWrapperRequest */ |
62
|
|
|
private $eventWrapperRequest; |
63
|
|
|
|
64
|
|
|
/** @var RemoteStreamService */ |
65
|
|
|
private $remoteStreamService; |
66
|
|
|
|
67
|
|
|
/** @var InterfaceService */ |
68
|
|
|
private $interfaceService; |
69
|
|
|
|
70
|
|
|
/** @var ConfigService */ |
71
|
|
|
private $configService; |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* RemoteUpstreamService constructor. |
76
|
|
|
* |
77
|
|
|
* @param EventWrapperRequest $eventWrapperRequest |
78
|
|
|
* @param RemoteStreamService $remoteStreamService |
79
|
|
|
* @param InterfaceService $interfaceService |
80
|
|
|
* @param ConfigService $configService |
81
|
|
|
*/ |
82
|
|
|
public function __construct( |
83
|
|
|
EventWrapperRequest $eventWrapperRequest, |
84
|
|
|
RemoteStreamService $remoteStreamService, |
85
|
|
|
InterfaceService $interfaceService, |
86
|
|
|
ConfigService $configService |
87
|
|
|
) { |
88
|
|
|
$this->eventWrapperRequest = $eventWrapperRequest; |
89
|
|
|
$this->remoteStreamService = $remoteStreamService; |
90
|
|
|
$this->interfaceService = $interfaceService; |
91
|
|
|
$this->configService = $configService; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $token |
97
|
|
|
* |
98
|
|
|
* @return EventWrapper[] |
99
|
|
|
*/ |
100
|
|
|
public function getEventsByToken(string $token): array { |
101
|
|
|
return $this->eventWrapperRequest->getByToken($token); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param EventWrapper $wrapper |
107
|
|
|
* |
108
|
|
|
* @throws FederatedItemException |
109
|
|
|
* @throws RemoteInstanceException |
110
|
|
|
* @throws RemoteNotFoundException |
111
|
|
|
* @throws RemoteResourceNotFoundException |
112
|
|
|
* @throws UnknownRemoteException |
113
|
|
|
*/ |
114
|
|
|
public function broadcastEvent(EventWrapper $wrapper): void { |
115
|
|
|
$this->interfaceService->setCurrentInterface($wrapper->getInterface()); |
116
|
|
|
$data = $this->remoteStreamService->resultRequestRemoteInstance( |
117
|
|
|
$wrapper->getInstance(), |
118
|
|
|
RemoteInstance::INCOMING, |
119
|
|
|
Request::TYPE_POST, |
120
|
|
|
$wrapper->getEvent() |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$wrapper->getEvent()->setResult(new SimpleDataStore($data)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param FederatedEvent $event |
129
|
|
|
* |
130
|
|
|
* @throws FederatedItemException |
131
|
|
|
* @throws OwnerNotFoundException |
132
|
|
|
* @throws RemoteInstanceException |
133
|
|
|
* @throws RemoteNotFoundException |
134
|
|
|
* @throws RemoteResourceNotFoundException |
135
|
|
|
* @throws UnknownRemoteException |
136
|
|
|
*/ |
137
|
|
|
public function confirmEvent(FederatedEvent $event): void { |
138
|
|
|
$data = $this->remoteStreamService->resultRequestRemoteInstance( |
139
|
|
|
$event->getCircle()->getInstance(), |
140
|
|
|
RemoteInstance::EVENT, |
141
|
|
|
Request::TYPE_POST, |
142
|
|
|
$event |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$event->setOutcome($data); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
// |
150
|
|
|
// |
151
|
|
|
// |
152
|
|
|
|
153
|
|
|
// /** |
154
|
|
|
// * @param array $sync |
155
|
|
|
// * |
156
|
|
|
// * @throws GSStatusException |
157
|
|
|
// */ |
158
|
|
|
// public function synchronize(array $sync) { |
159
|
|
|
// $this->configService->getGSStatus(); |
160
|
|
|
// |
161
|
|
|
// $this->synchronizeCircles($sync); |
162
|
|
|
// $this->removeDeprecatedCircles(); |
163
|
|
|
// $this->removeDeprecatedEvents(); |
164
|
|
|
// } |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
// /** |
168
|
|
|
// * @param array $circles |
169
|
|
|
// */ |
170
|
|
|
// public function synchronizeCircles(array $circles): void { |
171
|
|
|
// $event = new GSEvent(GSEvent::GLOBAL_SYNC, true); |
172
|
|
|
// $event->setSource($this->configService->getLocalInstance()); |
173
|
|
|
// $event->setData(new SimpleDataStore($circles)); |
174
|
|
|
// |
175
|
|
|
// foreach ($this->federatedEventService->getInstances() as $instance) { |
176
|
|
|
// try { |
177
|
|
|
// $this->broadcastEvent($event, $instance); |
178
|
|
|
// } catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException | RequestResultNotJsonException $e) { |
179
|
|
|
// } |
180
|
|
|
// } |
181
|
|
|
// } |
182
|
|
|
// |
183
|
|
|
// |
184
|
|
|
// /** |
185
|
|
|
// * |
186
|
|
|
// */ |
187
|
|
|
// private function removeDeprecatedCircles() { |
188
|
|
|
// $knownCircles = $this->circlesRequest->forceGetCircles(); |
189
|
|
|
// foreach ($knownCircles as $knownItem) { |
190
|
|
|
// if ($knownItem->getOwner() |
191
|
|
|
// ->getInstance() === '') { |
192
|
|
|
// continue; |
193
|
|
|
// } |
194
|
|
|
// |
195
|
|
|
// try { |
196
|
|
|
// $this->checkCircle($knownItem); |
197
|
|
|
// } catch (GSStatusException $e) { |
198
|
|
|
// } |
199
|
|
|
// } |
200
|
|
|
// } |
201
|
|
|
// |
202
|
|
|
// |
203
|
|
|
// /** |
204
|
|
|
// * @param DeprecatedCircle $circle |
205
|
|
|
// * |
206
|
|
|
// * @throws GSStatusException |
207
|
|
|
// */ |
208
|
|
|
// private function checkCircle(DeprecatedCircle $circle): void { |
209
|
|
|
// $status = $this->confirmCircleStatus($circle); |
210
|
|
|
// |
211
|
|
|
// if (!$status) { |
212
|
|
|
// $this->circlesRequest->destroyCircle($circle->getUniqueId()); |
213
|
|
|
// $this->membersRequest->removeAllFromCircle($circle->getUniqueId()); |
214
|
|
|
// } |
215
|
|
|
// } |
216
|
|
|
// |
217
|
|
|
// |
218
|
|
|
// /** |
219
|
|
|
// * @param DeprecatedCircle $circle |
220
|
|
|
// * |
221
|
|
|
// * @return bool |
222
|
|
|
// * @throws GSStatusException |
223
|
|
|
// */ |
224
|
|
|
// public function confirmCircleStatus(DeprecatedCircle $circle): bool { |
225
|
|
|
// $event = new GSEvent(GSEvent::CIRCLE_STATUS, true); |
226
|
|
|
// $event->setSource($this->configService->getLocalInstance()); |
227
|
|
|
// $event->setDeprecatedCircle($circle); |
228
|
|
|
// |
229
|
|
|
// $this->signEvent($event); |
230
|
|
|
// |
231
|
|
|
// $path = $this->urlGenerator->linkToRoute('circles.EventWrapper.status'); |
232
|
|
|
// $request = new NC22Request($path, Request::TYPE_POST); |
233
|
|
|
// $this->configService->configureRequest($request); |
234
|
|
|
// $request->setDataSerialize($event); |
235
|
|
|
// |
236
|
|
|
// $requestIssue = false; |
237
|
|
|
// $notFound = false; |
238
|
|
|
// $foundWithNoOwner = false; |
239
|
|
|
// foreach ($this->federatedEventService->getInstances() as $instance) { |
240
|
|
|
// $request->setHost($instance); |
241
|
|
|
// |
242
|
|
|
// try { |
243
|
|
|
// $result = $this->retrieveJson($request); |
244
|
|
|
// if ($this->getInt('status', $result, 0) !== 1) { |
245
|
|
|
// throw new RequestContentException('result status is not good'); |
246
|
|
|
// } |
247
|
|
|
// |
248
|
|
|
// $status = $this->getInt('success.data.status', $result); |
249
|
|
|
// |
250
|
|
|
// // if error, we assume the circle might still exist. |
251
|
|
|
// if ($status === CircleStatus::STATUS_ERROR) { |
252
|
|
|
// return true; |
253
|
|
|
// } |
254
|
|
|
// |
255
|
|
|
// if ($status === CircleStatus::STATUS_OK) { |
256
|
|
|
// return true; |
257
|
|
|
// } |
258
|
|
|
// |
259
|
|
|
// // TODO: check the data.supposedOwner entry. |
260
|
|
|
// if ($status === CircleStatus::STATUS_NOT_OWNER) { |
261
|
|
|
// $foundWithNoOwner = true; |
262
|
|
|
// } |
263
|
|
|
// |
264
|
|
|
// if ($status === CircleStatus::STATUS_NOT_FOUND) { |
265
|
|
|
// $notFound = true; |
266
|
|
|
// } |
267
|
|
|
// |
268
|
|
|
// } catch (RequestContentException |
269
|
|
|
// | RequestNetworkException |
270
|
|
|
// | RequestResultNotJsonException |
271
|
|
|
// | RequestResultSizeException |
272
|
|
|
// | RequestServerException $e) { |
273
|
|
|
// $requestIssue = true; |
274
|
|
|
// // TODO: log instances that have network issue, after too many tries (7d), remove this circle. |
275
|
|
|
// continue; |
276
|
|
|
// } |
277
|
|
|
// } |
278
|
|
|
// |
279
|
|
|
// // if no request issue, we can imagine that the instance that owns the circle is down. |
280
|
|
|
// // We'll wait for more information (cf request exceptions management); |
281
|
|
|
// if ($requestIssue) { |
282
|
|
|
// return true; |
283
|
|
|
// } |
284
|
|
|
// |
285
|
|
|
// // circle were not found in any other instances, we can easily says that the circle does not exists anymore |
286
|
|
|
// if ($notFound && !$foundWithNoOwner) { |
287
|
|
|
// return false; |
288
|
|
|
// } |
289
|
|
|
// |
290
|
|
|
// // circle were found everywhere but with no owner on every instance. we need to assign a new owner. |
291
|
|
|
// // This should be done by checking admin rights. if no admin rights, let's assume that circle should be removed. |
292
|
|
|
// if (!$notFound && $foundWithNoOwner) { |
293
|
|
|
// // TODO: assign a new owner and check that when changing owner, we do check that the destination instance is updated FOR SURE! |
294
|
|
|
// return true; |
295
|
|
|
// } |
296
|
|
|
// |
297
|
|
|
// // some instances returned notFound, some returned circle with no owner. let's assume the circle is deprecated. |
298
|
|
|
// return false; |
299
|
|
|
// } |
300
|
|
|
// |
301
|
|
|
// /** |
302
|
|
|
// * @throws GSStatusException |
303
|
|
|
// */ |
304
|
|
|
// public function syncEvents() { |
305
|
|
|
// |
306
|
|
|
// } |
307
|
|
|
// |
308
|
|
|
// /** |
309
|
|
|
// * |
310
|
|
|
// */ |
311
|
|
|
// private function removeDeprecatedEvents() { |
312
|
|
|
//// $this->deprecatedEvents(); |
313
|
|
|
// |
314
|
|
|
// } |
315
|
|
|
|
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
|