Completed
Pull Request — master (#551)
by Maxence
03:10 queued 01:14
created

FederatedEventService::configureEvent()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 4
nc 8
nop 2
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
namespace OCA\Circles\Service;
32
33
34
use daita\MySmallPhpTools\ActivityPub\Nextcloud\nc21\NC21Signature;
35
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
36
use daita\MySmallPhpTools\Exceptions\SignatoryException;
37
use daita\MySmallPhpTools\Model\Nextcloud\nc21\NC21Request;
38
use daita\MySmallPhpTools\Model\Request;
39
use daita\MySmallPhpTools\Model\SimpleDataStore;
40
use daita\MySmallPhpTools\Traits\Nextcloud\nc21\TNC21Request;
41
use daita\MySmallPhpTools\Traits\TStringTools;
42
use OC;
43
use OCA\Circles\Db\MemberRequest;
44
use OCA\Circles\Db\RemoteRequest;
45
use OCA\Circles\Db\RemoteWrapperRequest;
46
use OCA\Circles\Exceptions\FederatedEventDSyncException;
47
use OCA\Circles\Exceptions\FederatedEventException;
48
use OCA\Circles\Exceptions\FederatedItemException;
49
use OCA\Circles\Exceptions\InitiatorNotConfirmedException;
50
use OCA\Circles\Exceptions\JsonException;
51
use OCA\Circles\Exceptions\ModelException;
52
use OCA\Circles\Exceptions\OwnerNotFoundException;
53
use OCA\Circles\Exceptions\RemoteNotFoundException;
54
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
55
use OCA\Circles\Exceptions\UnknownRemoteException;
56
use OCA\Circles\IFederatedItem;
57
use OCA\Circles\IFederatedItemAsyncProcess;
58
use OCA\Circles\IFederatedItemCircleCheckNotRequired;
59
use OCA\Circles\IFederatedItemDataRequestOnly;
60
use OCA\Circles\IFederatedItemInitiatorCheckNotRequired;
61
use OCA\Circles\IFederatedItemInitiatorMembershipNotRequired;
62
use OCA\Circles\IFederatedItemInitiatorMustBeLocal;
63
use OCA\Circles\IFederatedItemLimitedToInstanceWithMembership;
64
use OCA\Circles\IFederatedItemMemberCheckNotRequired;
65
use OCA\Circles\IFederatedItemMemberEmpty;
66
use OCA\Circles\IFederatedItemMemberOptional;
67
use OCA\Circles\IFederatedItemMemberRequired;
68
use OCA\Circles\Model\Circle;
69
use OCA\Circles\Model\Federated\FederatedEvent;
70
use OCA\Circles\Model\Federated\RemoteInstance;
71
use OCA\Circles\Model\Federated\RemoteWrapper;
72
use OCA\Circles\Model\GlobalScale\GSWrapper;
73
use OCA\Circles\Model\Member;
74
use OCP\IL10N;
75
use ReflectionClass;
76
use ReflectionException;
77
78
79
/**
80
 * Class FederatedEventService
81
 *
82
 * @package OCA\Circles\Service
83
 */
84
class FederatedEventService extends NC21Signature {
85
86
87
	use TNC21Request;
88
	use TStringTools;
89
90
91
	/** @var IL10N */
92
	private $l10n;
93
94
	/** @var RemoteWrapperRequest */
95
	private $remoteWrapperRequest;
96
97
	/** @var RemoteRequest */
98
	private $remoteRequest;
99
100
	/** @var MemberRequest */
101
	private $memberRequest;
102
103
	/** @var RemoteUpstreamService */
104
	private $remoteUpstreamService;
105
106
	/** @var ConfigService */
107
	private $configService;
108
109
	/** @var MiscService */
110
	private $miscService;
111
112
113
	/**
114
	 * FederatedEventService constructor.
115
	 *
116
	 * @param IL10N $l10n
117
	 * @param RemoteWrapperRequest $remoteWrapperRequest
118
	 * @param RemoteRequest $remoteRequest
119
	 * @param MemberRequest $memberRequest
120
	 * @param RemoteUpstreamService $remoteUpstreamService
121
	 * @param ConfigService $configService
122
	 */
123
	public function __construct(
124
		IL10N $l10n, RemoteWrapperRequest $remoteWrapperRequest, RemoteRequest $remoteRequest,
125
		MemberRequest $memberRequest, RemoteUpstreamService $remoteUpstreamService,
126
		ConfigService $configService
127
	) {
128
		$this->l10n = $l10n;
129
		$this->remoteWrapperRequest = $remoteWrapperRequest;
130
		$this->remoteRequest = $remoteRequest;
131
		$this->memberRequest = $memberRequest;
132
		$this->remoteUpstreamService = $remoteUpstreamService;
133
		$this->configService = $configService;
134
	}
135
136
137
	/**
138
	 * Called when creating a new Event.
139
	 * This method will manage the event locally and upstream the payload if needed.
140
	 *
141
	 * @param FederatedEvent $event
142
	 *
143
	 * @return SimpleDataStore
144
	 * @throws InitiatorNotConfirmedException
145
	 * @throws OwnerNotFoundException
146
	 * @throws FederatedEventException
147
	 * @throws RequestNetworkException
148
	 * @throws RemoteNotFoundException
149
	 * @throws RemoteResourceNotFoundException
150
	 * @throws UnknownRemoteException
151
	 * @throws SignatoryException
152
	 * @throws FederatedItemException
153
	 * @throws FederatedEventDSyncException
154
	 */
155
	public function newEvent(FederatedEvent $event): SimpleDataStore {
156
		$event->setSource($this->configService->getLocalInstance());
157
158
		try {
159
			$federatedItem = $this->getFederatedItem($event, false);
160
		} catch (FederatedEventException $e) {
161
			$this->e($e);
162
			throw $e;
163
		}
164
165
		$this->confirmInitiator($event, true);
166
		if ($this->configService->isLocalInstance($event->getCircle()->getInstance())) {
167
			try {
168
				$federatedItem->verify($event);
169
				$reading = $event->getReadingOutcome();
170
				$reading->s('translated', $this->l10n->t($reading->g('message'), $reading->gArray('params')));
171
			} catch (FederatedItemException $e) {
172
				throw new FederatedItemException($this->l10n->t($e->getMessage(), $e->getParams()));
173
			}
174
175
			if ($event->isDataRequestOnly()) {
176
				return $event->getDataOutcome();
177
			}
178
179
			if (!$event->isAsync()) {
180
				$federatedItem->manage($event);
181
			}
182
183
			$this->initBroadcast($event);
184
		} else {
185
			$this->remoteUpstreamService->confirmEvent($event);
186
			if ($event->isDataRequestOnly()) {
187
				return $event->getDataOutcome();
188
			}
189
190
			if (!$event->isAsync()) {
191
				$federatedItem->manage($event);
192
			}
193
		}
194
195
		return $event->getOutcome();
196
	}
197
198
199
	/**
200
	 * This confirmation is optional, method is just here to avoid going too far away on the process
201
	 *
202
	 * @param FederatedEvent $event
203
	 * @param bool $local
204
	 *
205
	 * @throws InitiatorNotConfirmedException
206
	 */
207
	public function confirmInitiator(FederatedEvent $event, bool $local = false): void {
208
		if ($event->canBypass(FederatedEvent::BYPASS_INITIATORCHECK)) {
209
			return;
210
		}
211
212
		$circle = $event->getCircle();
213
		if (!$circle->hasInitiator()) {
214
			throw new InitiatorNotConfirmedException('Initiator does not exist');
215
		}
216
217
		if ($local) {
218
			if (!$this->configService->isLocalInstance($circle->getInitiator()->getInstance())) {
219
				throw new InitiatorNotConfirmedException(
220
					'Initiator is not from the instance at the origin of the request'
221
				);
222
			}
223
		} else {
224
			if ($circle->getInitiator()->getInstance() !== $event->getIncomingOrigin()) {
225
				throw new InitiatorNotConfirmedException(
226
					'Initiator must belong to the instance at the origin of the request'
227
				);
228
			}
229
		}
230
231
		if (!$event->canBypass(FederatedEvent::BYPASS_INITIATORMEMBERSHIP)
232
			&& $circle->getInitiator()->getLevel() < Member::LEVEL_MEMBER) {
233
			throw new InitiatorNotConfirmedException('Initiator must be a member of the Circle');
234
		}
235
	}
236
237
238
	/**
239
	 * @param FederatedEvent $event
240
	 * @param bool $checkLocalOnly
241
	 *
242
	 * @return IFederatedItem
243
	 * @throws FederatedEventException
244
	 */
245
	public function getFederatedItem(FederatedEvent $event, bool $checkLocalOnly = true): IFederatedItem {
246
		$class = $event->getClass();
247
		try {
248
			$test = new ReflectionClass($class);
249
		} catch (ReflectionException $e) {
250
			throw new FederatedEventException('ReflectionException with ' . $class . ': ' . $e->getMessage());
251
		}
252
253
		if (!in_array(IFederatedItem::class, $test->getInterfaceNames())) {
254
			throw new FederatedEventException($class . ' does not implements IFederatedItem');
255
		}
256
257
		$item = OC::$server->get($class);
258
		if (!($item instanceof IFederatedItem)) {
259
			throw new FederatedEventException($class . ' not an IFederatedItem');
260
		}
261
262
		$this->setFederatedEventBypass($event, $item);
263
		$this->confirmRequiredCondition($event, $item, $checkLocalOnly);
264
		$this->configureEvent($event, $item);
265
266
		return $item;
267
	}
268
269
270
	/**
271
	 * Some event might need to bypass some checks
272
	 *
273
	 * @param FederatedEvent $event
274
	 * @param IFederatedItem $item
275
	 */
276
	private function setFederatedEventBypass(FederatedEvent $event, IFederatedItem $item) {
277
		if ($item instanceof IFederatedItemCircleCheckNotRequired) {
278
			$event->bypass(FederatedEvent::BYPASS_LOCALCIRCLECHECK);
279
		}
280
		if ($item instanceof IFederatedItemMemberCheckNotRequired) {
281
			$event->bypass(FederatedEvent::BYPASS_LOCALMEMBERCHECK);
282
		}
283
		if ($item instanceof IFederatedItemInitiatorCheckNotRequired) {
284
			$event->bypass(FederatedEvent::BYPASS_INITIATORCHECK);
285
		}
286
		if ($item instanceof IFederatedItemInitiatorMembershipNotRequired) {
287
			$event->bypass(FederatedEvent::BYPASS_INITIATORMEMBERSHIP);
288
		}
289
	}
290
291
	/**
292
	 * Some event might require additional check
293
	 *
294
	 * @param FederatedEvent $event
295
	 * @param IFederatedItem $item
296
	 * @param bool $checkLocalOnly
297
	 *
298
	 * @throws FederatedEventException
299
	 */
300
	private function confirmRequiredCondition(
301
		FederatedEvent $event,
302
		IFederatedItem $item,
303
		bool $checkLocalOnly = true
304
	) {
305
		if (!$event->hasCircle()) {
306
			throw new FederatedEventException('FederatedEvent has no Circle linked');
307
		}
308
		if ($item instanceof IFederatedItemMemberEmpty) {
309
			$event->setMember(null);
310
		}
311
		if ($item instanceof IFederatedItemMemberRequired && !$event->hasMember()) {
312
			throw new FederatedEventException('FederatedEvent has no Member linked');
313
		}
314
		if ($event->hasMember()
315
			&& !($item instanceof IFederatedItemMemberRequired)
316
			&& !($item instanceof IFederatedItemMemberOptional)) {
317
			throw new FederatedEventException(
318
				get_class($item)
319
				. ' does not implements IFederatedItemMemberOptional nor IFederatedItemMemberRequired'
320
			);
321
		}
322
		if ($item instanceof IFederatedItemInitiatorMustBeLocal && $checkLocalOnly) {
323
			throw new FederatedEventException('FederatedItem must be executed locally');
324
		}
325
	}
326
327
328
	/**
329
	 * @param FederatedEvent $event
330
	 * @param IFederatedItem $item
331
	 */
332
	private function configureEvent(FederatedEvent $event, IFederatedItem $item) {
333
		if ($item instanceof IFederatedItemAsyncProcess) {
334
			$event->setAsync(true);
335
		}
336
		if ($item instanceof IFederatedItemLimitedToInstanceWithMembership) {
337
			$event->setLimitedToInstanceWithMember(true);
338
		}
339
		if ($item instanceof IFederatedItemDataRequestOnly) {
340
			$event->setDataRequestOnly(true);
341
		}
342
	}
343
344
345
	/**
346
	 * async the process, generate a local request that will be closed.
347
	 *
348
	 * @param FederatedEvent $event
349
	 * @param array $filter
350
	 */
351
	public function initBroadcast(FederatedEvent $event, array $filter = []): void {
352
		$instances = array_diff($this->getInstances($event), $filter);
353
		if (empty($instances)) {
354
			return;
355
		}
356
357
		$wrapper = new RemoteWrapper();
358
		$wrapper->setEvent($event);
359
		$wrapper->setToken($this->uuid());
360
		$wrapper->setCreation(time());
361
		$wrapper->setSeverity($event->getSeverity());
362
363
		foreach ($instances as $instance) {
364
			$wrapper->setInstance($instance);
365
			$this->remoteWrapperRequest->create($wrapper);
366
		}
367
368
		$request = new NC21Request('', Request::TYPE_POST);
369
		$this->configService->configureRequest(
370
			$request, 'circles.RemoteWrapper.asyncBroadcast', ['token' => $wrapper->getToken()]
371
		);
372
373
		$event->setWrapperToken($wrapper->getToken());
374
375
		try {
376
			$this->doRequest($request);
377
		} catch (RequestNetworkException $e) {
378
			$this->e($e, ['wrapper' => $wrapper]);
379
		}
380
	}
381
382
383
	/**
384
	 * @param FederatedEvent $event
385
	 * @param Circle|null $circle
386
	 * // TODO: use of $circle ??
387
	 *
388
	 * @return array
389
	 */
390
	public function getInstances(FederatedEvent $event, ?Circle $circle = null): array {
391
		$local = $this->configService->getLocalInstance();
392
		$instances = $this->remoteRequest->getOutgoingRecipient($circle);
393
394
		$instances = array_map(
395
			function(RemoteInstance $instance): string {
396
				return $instance->getInstance();
397
			}, $instances
398
		);
399
400
		if ($event->isLimitedToInstanceWithMember()) {
401
			$instances =
402
				array_intersect(
403
					$instances, $this->memberRequest->getMemberInstances($event->getCircle()->getId())
404
				);
405
		}
406
407
		$instances = array_merge([$local], $instances);
408
409
		if ($event->isAsync()) {
410
			return $instances;
411
		}
412
413
		return array_values(
414
			array_diff($instances, array_merge($this->configService->getTrustedDomains(), [$local]))
415
		);
416
	}
417
418
419
	/**
420
	 * @param array $current
421
	 */
422
	private function updateGlobalScaleInstances(array $current): void {
0 ignored issues
show
Unused Code introduced by
The parameter $current is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
423
//		$known = $this->remoteRequest->getFromType(RemoteInstance::TYPE_GLOBAL_SCALE);
424
	}
425
426
	/**
427
	 * @return array
428
	 */
429
	private function getRemoteInstances(): array {
430
		return [];
431
	}
432
433
434
	/**
435
	 * should be used to manage results from events, like sending mails on user creation
436
	 *
437
	 * @param string $token
438
	 */
439
	public function manageResults(string $token): void {
440
		try {
441
			$wrappers = $this->remoteWrapperRequest->getByToken($token);
442
		} catch (JsonException | ModelException $e) {
443
			return;
444
		}
445
446
		$event = null;
447
		$events = [];
448
		foreach ($wrappers as $wrapper) {
449
			if ($wrapper->getStatus() !== GSWrapper::STATUS_DONE) {
450
				return;
451
			}
452
453
			$events[$wrapper->getInstance()] = $event = $wrapper->getEvent();
454
		}
455
456
		if ($event === null) {
457
			return;
458
		}
459
460
		try {
461
			$gs = $this->getFederatedItem($event, false);
462
			$gs->result($events);
463
		} catch (FederatedEventException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
464
		}
465
	}
466
467
}
468
469