|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Circles\FederatedItems; |
| 11 | + |
| 12 | +use OCA\Circles\Db\CircleInvitationRequest; |
| 13 | +use OCA\Circles\Exceptions\CircleNameTooShortException; |
| 14 | +use OCA\Circles\Exceptions\RequestBuilderException; |
| 15 | +use OCA\Circles\IFederatedItem; |
| 16 | +use OCA\Circles\Model\CircleInvitation; |
| 17 | +use OCA\Circles\Model\Federated\FederatedEvent; |
| 18 | +use OCA\Circles\Model\Helpers\MemberHelper; |
| 19 | +use OCA\Circles\Service\EventService; |
| 20 | +use OCA\Circles\Tools\Traits\TDeserialize; |
| 21 | +use OCP\Security\ISecureRandom; |
| 22 | + |
| 23 | +/** |
| 24 | + * Class CircleCreateInvitation |
| 25 | + * |
| 26 | + * @package OCA\Circles\FederatedItems |
| 27 | + */ |
| 28 | +class CircleCreateInvitation implements IFederatedItem { |
| 29 | + use TDeserialize; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + private CircleInvitationRequest $circleInvitationRequest, |
| 33 | + private EventService $eventService, |
| 34 | + private ISecureRandom $random, |
| 35 | + ) { |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param FederatedEvent $event |
| 40 | + * |
| 41 | + * @throws RequestBuilderException |
| 42 | + * @throws CircleNameTooShortException |
| 43 | + */ |
| 44 | + public function verify(FederatedEvent $event): void { |
| 45 | + $circle = $event->getCircle(); |
| 46 | + |
| 47 | + $initiatorHelper = new MemberHelper($circle->getInitiator()); |
| 48 | + $initiatorHelper->mustBeAdmin(); |
| 49 | + |
| 50 | + $new = clone $circle; |
| 51 | + |
| 52 | + $invitationCode = $this->random->generate( |
| 53 | + 16, |
| 54 | + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', |
| 55 | + ); |
| 56 | + |
| 57 | + $circleInvitation = new CircleInvitation(); |
| 58 | + $circleInvitation->setCircleId($circle->getSingleId()); |
| 59 | + $circleInvitation->setInvitationCode($invitationCode); |
| 60 | + $circleInvitation->setCreatedBy($circle->getInitiator()->getUserId()); |
| 61 | + |
| 62 | + $new->setCircleInvitation($circleInvitation); |
| 63 | + $event->getData()->sObj('circle_invitation', $circleInvitation); |
| 64 | + |
| 65 | + $event->setOutcome($this->serialize($new)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param FederatedEvent $event |
| 70 | + * |
| 71 | + * @throws RequestBuilderException |
| 72 | + */ |
| 73 | + public function manage(FederatedEvent $event): void { |
| 74 | + /** @var CircleInvitation $circleInvitation */ |
| 75 | + $circleInvitation = $event->getData()->gObj('circle_invitation'); |
| 76 | + $this->circleInvitationRequest->replace($circleInvitation); |
| 77 | + |
| 78 | + // todo: do we need separate event here? |
| 79 | + $this->eventService->circleEditing($event); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param FederatedEvent $event |
| 84 | + * @param array $results |
| 85 | + */ |
| 86 | + public function result(FederatedEvent $event, array $results): void { |
| 87 | + // todo: do we need separate event here? |
| 88 | + $this->eventService->circleEdited($event, $results); |
| 89 | + } |
| 90 | +} |
0 commit comments