@@ -42,127 +42,127 @@ |
||
| 42 | 42 | |
| 43 | 43 | class ContactInteractionListener implements IEventListener { |
| 44 | 44 | |
| 45 | - /** @var RecentContactMapper */ |
|
| 46 | - private $mapper; |
|
| 47 | - |
|
| 48 | - /** @var CardSearchDao */ |
|
| 49 | - private $cardSearchDao; |
|
| 50 | - |
|
| 51 | - /** @var IUserManager */ |
|
| 52 | - private $userManager; |
|
| 53 | - |
|
| 54 | - /** @var ITimeFactory */ |
|
| 55 | - private $timeFactory; |
|
| 56 | - |
|
| 57 | - /** @var IL10N */ |
|
| 58 | - private $l10n; |
|
| 59 | - |
|
| 60 | - /** @var LoggerInterface */ |
|
| 61 | - private $logger; |
|
| 62 | - |
|
| 63 | - public function __construct(RecentContactMapper $mapper, |
|
| 64 | - CardSearchDao $cardSearchDao, |
|
| 65 | - IUserManager $userManager, |
|
| 66 | - ITimeFactory $timeFactory, |
|
| 67 | - IL10N $l10nFactory, |
|
| 68 | - LoggerInterface $logger) { |
|
| 69 | - $this->mapper = $mapper; |
|
| 70 | - $this->cardSearchDao = $cardSearchDao; |
|
| 71 | - $this->userManager = $userManager; |
|
| 72 | - $this->timeFactory = $timeFactory; |
|
| 73 | - $this->l10n = $l10nFactory; |
|
| 74 | - $this->logger = $logger; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function handle(Event $event): void { |
|
| 78 | - if (!($event instanceof ContactInteractedWithEvent)) { |
|
| 79 | - return; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - if ($event->getUid() === null && $event->getEmail() === null && $event->getFederatedCloudId() === null) { |
|
| 83 | - $this->logger->warning("Contact interaction event has no user identifier set"); |
|
| 84 | - return; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - $existing = $this->mapper->findMatch( |
|
| 88 | - $event->getActor(), |
|
| 89 | - $event->getUid(), |
|
| 90 | - $event->getEmail(), |
|
| 91 | - $event->getFederatedCloudId() |
|
| 92 | - ); |
|
| 93 | - if (!empty($existing)) { |
|
| 94 | - $now = $this->timeFactory->getTime(); |
|
| 95 | - foreach ($existing as $c) { |
|
| 96 | - $c->setLastContact($now); |
|
| 97 | - $this->mapper->update($c); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - return; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $contact = new RecentContact(); |
|
| 104 | - $contact->setActorUid($event->getActor()->getUID()); |
|
| 105 | - if ($event->getUid() !== null) { |
|
| 106 | - $contact->setUid($event->getUid()); |
|
| 107 | - } |
|
| 108 | - if ($event->getEmail() !== null) { |
|
| 109 | - $contact->setEmail($event->getEmail()); |
|
| 110 | - } |
|
| 111 | - if ($event->getFederatedCloudId() !== null) { |
|
| 112 | - $contact->setFederatedCloudId($event->getFederatedCloudId()); |
|
| 113 | - } |
|
| 114 | - $contact->setLastContact($this->timeFactory->getTime()); |
|
| 115 | - |
|
| 116 | - $copy = $this->cardSearchDao->findExisting( |
|
| 117 | - $event->getActor(), |
|
| 118 | - $event->getUid(), |
|
| 119 | - $event->getEmail(), |
|
| 120 | - $event->getFederatedCloudId() |
|
| 121 | - ); |
|
| 122 | - if ($copy !== null) { |
|
| 123 | - try { |
|
| 124 | - $parsed = Reader::read($copy, Reader::OPTION_FORGIVING); |
|
| 125 | - $parsed->CATEGORIES = $this->l10n->t('Recently contacted'); |
|
| 126 | - $contact->setCard($parsed->serialize()); |
|
| 127 | - } catch (Throwable $e) { |
|
| 128 | - $this->logger->warning( |
|
| 129 | - 'Could not parse card to add recent category: ' . $e->getMessage(), |
|
| 130 | - [ |
|
| 131 | - 'exception' => $e, |
|
| 132 | - ]); |
|
| 133 | - $contact->setCard($copy); |
|
| 134 | - } |
|
| 135 | - } else { |
|
| 136 | - $contact->setCard($this->generateCard($contact)); |
|
| 137 | - } |
|
| 138 | - $this->mapper->insert($contact); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - private function getDisplayName(?string $uid): ?string { |
|
| 142 | - if ($uid === null) { |
|
| 143 | - return null; |
|
| 144 | - } |
|
| 145 | - if (($user = $this->userManager->get($uid)) === null) { |
|
| 146 | - return null; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - return $user->getDisplayName(); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - private function generateCard(RecentContact $contact): string { |
|
| 153 | - $props = [ |
|
| 154 | - 'URI' => UUIDUtil::getUUID(), |
|
| 155 | - 'FN' => $this->getDisplayName($contact->getUid()) ?? $contact->getEmail() ?? $contact->getFederatedCloudId(), |
|
| 156 | - 'CATEGORIES' => $this->l10n->t('Recently contacted'), |
|
| 157 | - ]; |
|
| 158 | - |
|
| 159 | - if ($contact->getEmail() !== null) { |
|
| 160 | - $props['EMAIL'] = $contact->getEmail(); |
|
| 161 | - } |
|
| 162 | - if ($contact->getFederatedCloudId() !== null) { |
|
| 163 | - $props['CLOUD'] = $contact->getFederatedCloudId(); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - return (new VCard($props))->serialize(); |
|
| 167 | - } |
|
| 45 | + /** @var RecentContactMapper */ |
|
| 46 | + private $mapper; |
|
| 47 | + |
|
| 48 | + /** @var CardSearchDao */ |
|
| 49 | + private $cardSearchDao; |
|
| 50 | + |
|
| 51 | + /** @var IUserManager */ |
|
| 52 | + private $userManager; |
|
| 53 | + |
|
| 54 | + /** @var ITimeFactory */ |
|
| 55 | + private $timeFactory; |
|
| 56 | + |
|
| 57 | + /** @var IL10N */ |
|
| 58 | + private $l10n; |
|
| 59 | + |
|
| 60 | + /** @var LoggerInterface */ |
|
| 61 | + private $logger; |
|
| 62 | + |
|
| 63 | + public function __construct(RecentContactMapper $mapper, |
|
| 64 | + CardSearchDao $cardSearchDao, |
|
| 65 | + IUserManager $userManager, |
|
| 66 | + ITimeFactory $timeFactory, |
|
| 67 | + IL10N $l10nFactory, |
|
| 68 | + LoggerInterface $logger) { |
|
| 69 | + $this->mapper = $mapper; |
|
| 70 | + $this->cardSearchDao = $cardSearchDao; |
|
| 71 | + $this->userManager = $userManager; |
|
| 72 | + $this->timeFactory = $timeFactory; |
|
| 73 | + $this->l10n = $l10nFactory; |
|
| 74 | + $this->logger = $logger; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function handle(Event $event): void { |
|
| 78 | + if (!($event instanceof ContactInteractedWithEvent)) { |
|
| 79 | + return; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + if ($event->getUid() === null && $event->getEmail() === null && $event->getFederatedCloudId() === null) { |
|
| 83 | + $this->logger->warning("Contact interaction event has no user identifier set"); |
|
| 84 | + return; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + $existing = $this->mapper->findMatch( |
|
| 88 | + $event->getActor(), |
|
| 89 | + $event->getUid(), |
|
| 90 | + $event->getEmail(), |
|
| 91 | + $event->getFederatedCloudId() |
|
| 92 | + ); |
|
| 93 | + if (!empty($existing)) { |
|
| 94 | + $now = $this->timeFactory->getTime(); |
|
| 95 | + foreach ($existing as $c) { |
|
| 96 | + $c->setLastContact($now); |
|
| 97 | + $this->mapper->update($c); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + return; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $contact = new RecentContact(); |
|
| 104 | + $contact->setActorUid($event->getActor()->getUID()); |
|
| 105 | + if ($event->getUid() !== null) { |
|
| 106 | + $contact->setUid($event->getUid()); |
|
| 107 | + } |
|
| 108 | + if ($event->getEmail() !== null) { |
|
| 109 | + $contact->setEmail($event->getEmail()); |
|
| 110 | + } |
|
| 111 | + if ($event->getFederatedCloudId() !== null) { |
|
| 112 | + $contact->setFederatedCloudId($event->getFederatedCloudId()); |
|
| 113 | + } |
|
| 114 | + $contact->setLastContact($this->timeFactory->getTime()); |
|
| 115 | + |
|
| 116 | + $copy = $this->cardSearchDao->findExisting( |
|
| 117 | + $event->getActor(), |
|
| 118 | + $event->getUid(), |
|
| 119 | + $event->getEmail(), |
|
| 120 | + $event->getFederatedCloudId() |
|
| 121 | + ); |
|
| 122 | + if ($copy !== null) { |
|
| 123 | + try { |
|
| 124 | + $parsed = Reader::read($copy, Reader::OPTION_FORGIVING); |
|
| 125 | + $parsed->CATEGORIES = $this->l10n->t('Recently contacted'); |
|
| 126 | + $contact->setCard($parsed->serialize()); |
|
| 127 | + } catch (Throwable $e) { |
|
| 128 | + $this->logger->warning( |
|
| 129 | + 'Could not parse card to add recent category: ' . $e->getMessage(), |
|
| 130 | + [ |
|
| 131 | + 'exception' => $e, |
|
| 132 | + ]); |
|
| 133 | + $contact->setCard($copy); |
|
| 134 | + } |
|
| 135 | + } else { |
|
| 136 | + $contact->setCard($this->generateCard($contact)); |
|
| 137 | + } |
|
| 138 | + $this->mapper->insert($contact); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + private function getDisplayName(?string $uid): ?string { |
|
| 142 | + if ($uid === null) { |
|
| 143 | + return null; |
|
| 144 | + } |
|
| 145 | + if (($user = $this->userManager->get($uid)) === null) { |
|
| 146 | + return null; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + return $user->getDisplayName(); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + private function generateCard(RecentContact $contact): string { |
|
| 153 | + $props = [ |
|
| 154 | + 'URI' => UUIDUtil::getUUID(), |
|
| 155 | + 'FN' => $this->getDisplayName($contact->getUid()) ?? $contact->getEmail() ?? $contact->getFederatedCloudId(), |
|
| 156 | + 'CATEGORIES' => $this->l10n->t('Recently contacted'), |
|
| 157 | + ]; |
|
| 158 | + |
|
| 159 | + if ($contact->getEmail() !== null) { |
|
| 160 | + $props['EMAIL'] = $contact->getEmail(); |
|
| 161 | + } |
|
| 162 | + if ($contact->getFederatedCloudId() !== null) { |
|
| 163 | + $props['CLOUD'] = $contact->getFederatedCloudId(); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + return (new VCard($props))->serialize(); |
|
| 167 | + } |
|
| 168 | 168 | } |
@@ -37,62 +37,62 @@ |
||
| 37 | 37 | use function in_array; |
| 38 | 38 | |
| 39 | 39 | class ShareInteractionListener implements IEventListener { |
| 40 | - private const SUPPORTED_SHARE_TYPES = [ |
|
| 41 | - IShare::TYPE_USER, |
|
| 42 | - IShare::TYPE_EMAIL, |
|
| 43 | - IShare::TYPE_REMOTE, |
|
| 44 | - ]; |
|
| 40 | + private const SUPPORTED_SHARE_TYPES = [ |
|
| 41 | + IShare::TYPE_USER, |
|
| 42 | + IShare::TYPE_EMAIL, |
|
| 43 | + IShare::TYPE_REMOTE, |
|
| 44 | + ]; |
|
| 45 | 45 | |
| 46 | - /** @var IEventDispatcher */ |
|
| 47 | - private $dispatcher; |
|
| 46 | + /** @var IEventDispatcher */ |
|
| 47 | + private $dispatcher; |
|
| 48 | 48 | |
| 49 | - /** @var IUserManager */ |
|
| 50 | - private $userManager; |
|
| 49 | + /** @var IUserManager */ |
|
| 50 | + private $userManager; |
|
| 51 | 51 | |
| 52 | - /** @var ILogger */ |
|
| 53 | - private $logger; |
|
| 52 | + /** @var ILogger */ |
|
| 53 | + private $logger; |
|
| 54 | 54 | |
| 55 | - public function __construct(IEventDispatcher $dispatcher, |
|
| 56 | - IUserManager $userManager, |
|
| 57 | - ILogger $logger) { |
|
| 58 | - $this->dispatcher = $dispatcher; |
|
| 59 | - $this->userManager = $userManager; |
|
| 60 | - $this->logger = $logger; |
|
| 61 | - } |
|
| 55 | + public function __construct(IEventDispatcher $dispatcher, |
|
| 56 | + IUserManager $userManager, |
|
| 57 | + ILogger $logger) { |
|
| 58 | + $this->dispatcher = $dispatcher; |
|
| 59 | + $this->userManager = $userManager; |
|
| 60 | + $this->logger = $logger; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - public function handle(Event $event): void { |
|
| 64 | - if (!($event instanceof ShareCreatedEvent)) { |
|
| 65 | - // Unrelated |
|
| 66 | - return; |
|
| 67 | - } |
|
| 63 | + public function handle(Event $event): void { |
|
| 64 | + if (!($event instanceof ShareCreatedEvent)) { |
|
| 65 | + // Unrelated |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - $share = $event->getShare(); |
|
| 70 | - if (!in_array($share->getShareType(), self::SUPPORTED_SHARE_TYPES, true)) { |
|
| 71 | - $this->logger->debug('Share type does not allow to emit interaction event'); |
|
| 72 | - return; |
|
| 73 | - } |
|
| 74 | - $actor = $this->userManager->get($share->getSharedBy()); |
|
| 75 | - $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
| 76 | - if ($actor === null) { |
|
| 77 | - $this->logger->warning('Share was not created by a user, can\'t emit interaction event'); |
|
| 78 | - return; |
|
| 79 | - } |
|
| 80 | - $interactionEvent = new ContactInteractedWithEvent($actor); |
|
| 81 | - switch ($share->getShareType()) { |
|
| 82 | - case IShare::TYPE_USER: |
|
| 83 | - $interactionEvent->setUid($share->getSharedWith()); |
|
| 84 | - if ($sharedWith !== null) { |
|
| 85 | - $interactionEvent->setFederatedCloudId($sharedWith->getCloudId()); |
|
| 86 | - } |
|
| 87 | - break; |
|
| 88 | - case IShare::TYPE_EMAIL: |
|
| 89 | - $interactionEvent->setEmail($share->getSharedWith()); |
|
| 90 | - break; |
|
| 91 | - case IShare::TYPE_REMOTE: |
|
| 92 | - $interactionEvent->setFederatedCloudId($share->getSharedWith()); |
|
| 93 | - break; |
|
| 94 | - } |
|
| 69 | + $share = $event->getShare(); |
|
| 70 | + if (!in_array($share->getShareType(), self::SUPPORTED_SHARE_TYPES, true)) { |
|
| 71 | + $this->logger->debug('Share type does not allow to emit interaction event'); |
|
| 72 | + return; |
|
| 73 | + } |
|
| 74 | + $actor = $this->userManager->get($share->getSharedBy()); |
|
| 75 | + $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
| 76 | + if ($actor === null) { |
|
| 77 | + $this->logger->warning('Share was not created by a user, can\'t emit interaction event'); |
|
| 78 | + return; |
|
| 79 | + } |
|
| 80 | + $interactionEvent = new ContactInteractedWithEvent($actor); |
|
| 81 | + switch ($share->getShareType()) { |
|
| 82 | + case IShare::TYPE_USER: |
|
| 83 | + $interactionEvent->setUid($share->getSharedWith()); |
|
| 84 | + if ($sharedWith !== null) { |
|
| 85 | + $interactionEvent->setFederatedCloudId($sharedWith->getCloudId()); |
|
| 86 | + } |
|
| 87 | + break; |
|
| 88 | + case IShare::TYPE_EMAIL: |
|
| 89 | + $interactionEvent->setEmail($share->getSharedWith()); |
|
| 90 | + break; |
|
| 91 | + case IShare::TYPE_REMOTE: |
|
| 92 | + $interactionEvent->setFederatedCloudId($share->getSharedWith()); |
|
| 93 | + break; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - $this->dispatcher->dispatchTyped($interactionEvent); |
|
| 97 | - } |
|
| 96 | + $this->dispatcher->dispatchTyped($interactionEvent); |
|
| 97 | + } |
|
| 98 | 98 | } |