@@ -47,131 +47,131 @@ |
||
47 | 47 | use function substr; |
48 | 48 | |
49 | 49 | class CalendarContactInteractionListener implements IEventListener { |
50 | - private const URI_USERS = 'principals/users/'; |
|
51 | - |
|
52 | - /** @var IEventDispatcher */ |
|
53 | - private $dispatcher; |
|
54 | - |
|
55 | - /** @var IUserSession */ |
|
56 | - private $userSession; |
|
57 | - |
|
58 | - /** @var Principal */ |
|
59 | - private $principalConnector; |
|
60 | - |
|
61 | - /** @var IMailer */ |
|
62 | - private $mailer; |
|
63 | - |
|
64 | - /** @var LoggerInterface */ |
|
65 | - private $logger; |
|
66 | - |
|
67 | - public function __construct(IEventDispatcher $dispatcher, |
|
68 | - IUserSession $userSession, |
|
69 | - Principal $principalConnector, |
|
70 | - IMailer $mailer, |
|
71 | - LoggerInterface $logger) { |
|
72 | - $this->dispatcher = $dispatcher; |
|
73 | - $this->userSession = $userSession; |
|
74 | - $this->principalConnector = $principalConnector; |
|
75 | - $this->mailer = $mailer; |
|
76 | - $this->logger = $logger; |
|
77 | - } |
|
78 | - |
|
79 | - public function handle(Event $event): void { |
|
80 | - if (($user = $this->userSession->getUser()) === null) { |
|
81 | - // Without user context we can't do anything |
|
82 | - return; |
|
83 | - } |
|
84 | - |
|
85 | - if ($event instanceof CalendarObjectCreatedEvent || $event instanceof CalendarObjectUpdatedEvent) { |
|
86 | - // users: href => principal:principals/users/admin |
|
87 | - foreach ($event->getShares() as $share) { |
|
88 | - if (!isset($share['href'])) { |
|
89 | - continue; |
|
90 | - } |
|
91 | - $this->emitFromUri($share['href'], $user); |
|
92 | - } |
|
93 | - |
|
94 | - // emit interaction for email attendees as well |
|
95 | - if (isset($event->getObjectData()['calendardata'])) { |
|
96 | - try { |
|
97 | - $calendar = Reader::read($event->getObjectData()['calendardata']); |
|
98 | - if ($calendar->VEVENT) { |
|
99 | - foreach ($calendar->VEVENT as $calendarEvent) { |
|
100 | - $this->emitFromObject($calendarEvent, $user); |
|
101 | - } |
|
102 | - } |
|
103 | - } catch (Throwable $e) { |
|
104 | - $this->logger->warning('Could not read calendar data for interaction events: ' . $e->getMessage(), [ |
|
105 | - 'exception' => $e, |
|
106 | - ]); |
|
107 | - } |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - if ($event instanceof CalendarShareUpdatedEvent && !empty($event->getAdded())) { |
|
112 | - // group: href => principal:principals/groups/admin |
|
113 | - // users: href => principal:principals/users/admin |
|
114 | - foreach ($event->getAdded() as $added) { |
|
115 | - if (!isset($added['href'])) { |
|
116 | - // Nothing to work with |
|
117 | - continue; |
|
118 | - } |
|
119 | - $this->emitFromUri($added['href'], $user); |
|
120 | - } |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - private function emitFromUri(string $uri, IUser $user): void { |
|
125 | - $principal = $this->principalConnector->findByUri( |
|
126 | - $uri, |
|
127 | - $this->principalConnector->getPrincipalPrefix() |
|
128 | - ); |
|
129 | - if ($principal === null) { |
|
130 | - // Invalid principal |
|
131 | - return; |
|
132 | - } |
|
133 | - if (strpos($principal, self::URI_USERS) !== 0) { |
|
134 | - // Not a user principal |
|
135 | - return; |
|
136 | - } |
|
137 | - |
|
138 | - $uid = substr($principal, strlen(self::URI_USERS)); |
|
139 | - $this->dispatcher->dispatchTyped( |
|
140 | - (new ContactInteractedWithEvent($user))->setUid($uid) |
|
141 | - ); |
|
142 | - } |
|
143 | - |
|
144 | - private function emitFromObject(VEvent $vevent, IUser $user): void { |
|
145 | - if (!$vevent->ATTENDEE) { |
|
146 | - // Nothing left to do |
|
147 | - return; |
|
148 | - } |
|
149 | - |
|
150 | - foreach ($vevent->ATTENDEE as $attendee) { |
|
151 | - if (!($attendee instanceof Property)) { |
|
152 | - continue; |
|
153 | - } |
|
154 | - |
|
155 | - $cuType = $attendee->offsetGet('CUTYPE'); |
|
156 | - if ($cuType instanceof Parameter && $cuType->getValue() !== 'INDIVIDUAL') { |
|
157 | - // Don't care about those |
|
158 | - continue; |
|
159 | - } |
|
160 | - |
|
161 | - $mailTo = $attendee->getValue(); |
|
162 | - if (strpos($mailTo, 'mailto:') !== 0) { |
|
163 | - // Doesn't look like an email |
|
164 | - continue; |
|
165 | - } |
|
166 | - $email = substr($mailTo, strlen('mailto:')); |
|
167 | - if (!$this->mailer->validateMailAddress($email)) { |
|
168 | - // This really isn't a valid email |
|
169 | - continue; |
|
170 | - } |
|
171 | - |
|
172 | - $this->dispatcher->dispatchTyped( |
|
173 | - (new ContactInteractedWithEvent($user))->setEmail($email) |
|
174 | - ); |
|
175 | - } |
|
176 | - } |
|
50 | + private const URI_USERS = 'principals/users/'; |
|
51 | + |
|
52 | + /** @var IEventDispatcher */ |
|
53 | + private $dispatcher; |
|
54 | + |
|
55 | + /** @var IUserSession */ |
|
56 | + private $userSession; |
|
57 | + |
|
58 | + /** @var Principal */ |
|
59 | + private $principalConnector; |
|
60 | + |
|
61 | + /** @var IMailer */ |
|
62 | + private $mailer; |
|
63 | + |
|
64 | + /** @var LoggerInterface */ |
|
65 | + private $logger; |
|
66 | + |
|
67 | + public function __construct(IEventDispatcher $dispatcher, |
|
68 | + IUserSession $userSession, |
|
69 | + Principal $principalConnector, |
|
70 | + IMailer $mailer, |
|
71 | + LoggerInterface $logger) { |
|
72 | + $this->dispatcher = $dispatcher; |
|
73 | + $this->userSession = $userSession; |
|
74 | + $this->principalConnector = $principalConnector; |
|
75 | + $this->mailer = $mailer; |
|
76 | + $this->logger = $logger; |
|
77 | + } |
|
78 | + |
|
79 | + public function handle(Event $event): void { |
|
80 | + if (($user = $this->userSession->getUser()) === null) { |
|
81 | + // Without user context we can't do anything |
|
82 | + return; |
|
83 | + } |
|
84 | + |
|
85 | + if ($event instanceof CalendarObjectCreatedEvent || $event instanceof CalendarObjectUpdatedEvent) { |
|
86 | + // users: href => principal:principals/users/admin |
|
87 | + foreach ($event->getShares() as $share) { |
|
88 | + if (!isset($share['href'])) { |
|
89 | + continue; |
|
90 | + } |
|
91 | + $this->emitFromUri($share['href'], $user); |
|
92 | + } |
|
93 | + |
|
94 | + // emit interaction for email attendees as well |
|
95 | + if (isset($event->getObjectData()['calendardata'])) { |
|
96 | + try { |
|
97 | + $calendar = Reader::read($event->getObjectData()['calendardata']); |
|
98 | + if ($calendar->VEVENT) { |
|
99 | + foreach ($calendar->VEVENT as $calendarEvent) { |
|
100 | + $this->emitFromObject($calendarEvent, $user); |
|
101 | + } |
|
102 | + } |
|
103 | + } catch (Throwable $e) { |
|
104 | + $this->logger->warning('Could not read calendar data for interaction events: ' . $e->getMessage(), [ |
|
105 | + 'exception' => $e, |
|
106 | + ]); |
|
107 | + } |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + if ($event instanceof CalendarShareUpdatedEvent && !empty($event->getAdded())) { |
|
112 | + // group: href => principal:principals/groups/admin |
|
113 | + // users: href => principal:principals/users/admin |
|
114 | + foreach ($event->getAdded() as $added) { |
|
115 | + if (!isset($added['href'])) { |
|
116 | + // Nothing to work with |
|
117 | + continue; |
|
118 | + } |
|
119 | + $this->emitFromUri($added['href'], $user); |
|
120 | + } |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + private function emitFromUri(string $uri, IUser $user): void { |
|
125 | + $principal = $this->principalConnector->findByUri( |
|
126 | + $uri, |
|
127 | + $this->principalConnector->getPrincipalPrefix() |
|
128 | + ); |
|
129 | + if ($principal === null) { |
|
130 | + // Invalid principal |
|
131 | + return; |
|
132 | + } |
|
133 | + if (strpos($principal, self::URI_USERS) !== 0) { |
|
134 | + // Not a user principal |
|
135 | + return; |
|
136 | + } |
|
137 | + |
|
138 | + $uid = substr($principal, strlen(self::URI_USERS)); |
|
139 | + $this->dispatcher->dispatchTyped( |
|
140 | + (new ContactInteractedWithEvent($user))->setUid($uid) |
|
141 | + ); |
|
142 | + } |
|
143 | + |
|
144 | + private function emitFromObject(VEvent $vevent, IUser $user): void { |
|
145 | + if (!$vevent->ATTENDEE) { |
|
146 | + // Nothing left to do |
|
147 | + return; |
|
148 | + } |
|
149 | + |
|
150 | + foreach ($vevent->ATTENDEE as $attendee) { |
|
151 | + if (!($attendee instanceof Property)) { |
|
152 | + continue; |
|
153 | + } |
|
154 | + |
|
155 | + $cuType = $attendee->offsetGet('CUTYPE'); |
|
156 | + if ($cuType instanceof Parameter && $cuType->getValue() !== 'INDIVIDUAL') { |
|
157 | + // Don't care about those |
|
158 | + continue; |
|
159 | + } |
|
160 | + |
|
161 | + $mailTo = $attendee->getValue(); |
|
162 | + if (strpos($mailTo, 'mailto:') !== 0) { |
|
163 | + // Doesn't look like an email |
|
164 | + continue; |
|
165 | + } |
|
166 | + $email = substr($mailTo, strlen('mailto:')); |
|
167 | + if (!$this->mailer->validateMailAddress($email)) { |
|
168 | + // This really isn't a valid email |
|
169 | + continue; |
|
170 | + } |
|
171 | + |
|
172 | + $this->dispatcher->dispatchTyped( |
|
173 | + (new ContactInteractedWithEvent($user))->setEmail($email) |
|
174 | + ); |
|
175 | + } |
|
176 | + } |
|
177 | 177 | } |
@@ -101,7 +101,7 @@ |
||
101 | 101 | } |
102 | 102 | } |
103 | 103 | } catch (Throwable $e) { |
104 | - $this->logger->warning('Could not read calendar data for interaction events: ' . $e->getMessage(), [ |
|
104 | + $this->logger->warning('Could not read calendar data for interaction events: '.$e->getMessage(), [ |
|
105 | 105 | 'exception' => $e, |
106 | 106 | ]); |
107 | 107 | } |