|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\DAV\Listener; |
| 11 | + |
| 12 | +use OCA\DAV\CalDAV\Proxy\ProxyMapper; |
| 13 | +use OCA\DAV\CalDAV\Schedule\IMipService; |
| 14 | +use OCP\Calendar\Events\CalendarObjectCreatedEvent; |
| 15 | +use OCP\Calendar\Events\CalendarObjectDeletedEvent; |
| 16 | +use OCP\Calendar\Events\CalendarObjectMovedToTrashEvent; |
| 17 | +use OCP\Calendar\Events\CalendarObjectRestoredEvent; |
| 18 | +use OCP\Calendar\Events\CalendarObjectUpdatedEvent; |
| 19 | +use OCP\EventDispatcher\Event; |
| 20 | +use OCP\EventDispatcher\IEventListener; |
| 21 | +use OCP\IUser; |
| 22 | +use OCP\IUserManager; |
| 23 | +use OCP\IUserSession; |
| 24 | +use OCP\L10N\IFactory as IL10NFactory; |
| 25 | +use OCP\Mail\IMailer; |
| 26 | +use OCP\Util; |
| 27 | +use Psr\Log\LoggerInterface; |
| 28 | +use Sabre\VObject\Component\VCalendar; |
| 29 | +use Sabre\VObject\Component\VEvent; |
| 30 | +use Sabre\VObject\Reader; |
| 31 | +use Throwable; |
| 32 | + |
| 33 | +/** |
| 34 | + * Sends an iMIP-style notification email to a calendar owner whenever one |
| 35 | + * of their calendar-proxy delegates creates, modifies, deletes, trashes, |
| 36 | + * or restores an event on their behalf. |
| 37 | + * |
| 38 | + * The email body is built with IMipService so the owner sees the same rich |
| 39 | + * bullet-list rendering used for regular invitations, including diff |
| 40 | + * strike-throughs on update. |
| 41 | + * |
| 42 | + * @template-implements IEventListener<CalendarObjectCreatedEvent|CalendarObjectUpdatedEvent|CalendarObjectDeletedEvent|CalendarObjectMovedToTrashEvent|CalendarObjectRestoredEvent> |
| 43 | + */ |
| 44 | +class CalendarDelegateActionListener implements IEventListener { |
| 45 | + |
| 46 | + private const ACTION_CREATE = 'create'; |
| 47 | + private const ACTION_UPDATE = 'update'; |
| 48 | + private const ACTION_DELETE = 'delete'; |
| 49 | + private const ACTION_TRASH = 'trash'; |
| 50 | + private const ACTION_RESTORE = 'restore'; |
| 51 | + |
| 52 | + public function __construct( |
| 53 | + private readonly IUserSession $userSession, |
| 54 | + private readonly IUserManager $userManager, |
| 55 | + private readonly ProxyMapper $proxyMapper, |
| 56 | + private readonly IMailer $mailer, |
| 57 | + private readonly IL10NFactory $l10nFactory, |
| 58 | + private readonly IMipService $imipService, |
| 59 | + private readonly LoggerInterface $logger, |
| 60 | + ) { |
| 61 | + } |
| 62 | + |
| 63 | + #[\Override] |
| 64 | + public function handle(Event $event): void { |
| 65 | + $action = match (true) { |
| 66 | + $event instanceof CalendarObjectCreatedEvent => self::ACTION_CREATE, |
| 67 | + $event instanceof CalendarObjectUpdatedEvent => self::ACTION_UPDATE, |
| 68 | + $event instanceof CalendarObjectDeletedEvent => self::ACTION_DELETE, |
| 69 | + $event instanceof CalendarObjectMovedToTrashEvent => self::ACTION_TRASH, |
| 70 | + $event instanceof CalendarObjectRestoredEvent => self::ACTION_RESTORE, |
| 71 | + default => null, |
| 72 | + }; |
| 73 | + if ($action === null) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + $actor = $this->userSession->getUser(); |
| 78 | + if ($actor === null) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + $calendarInfo = $event->getCalendarData(); |
| 83 | + $ownerPrincipalUri = $calendarInfo['principaluri'] ?? null; |
| 84 | + if (!is_string($ownerPrincipalUri) || !str_starts_with($ownerPrincipalUri, 'principals/users/')) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + [, $ownerUid] = \Sabre\Uri\split($ownerPrincipalUri); |
| 89 | + if ($ownerUid === $actor->getUID()) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + if (!$this->actorIsProxyOf($actor->getUID(), $ownerPrincipalUri)) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + $owner = $this->userManager->get($ownerUid); |
| 98 | + if ($owner === null) { |
| 99 | + return; |
| 100 | + } |
| 101 | + $ownerEmail = $owner->getEMailAddress(); |
| 102 | + if ($ownerEmail === null || $ownerEmail === '') { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + // Only an update carries a meaningful previous version to diff against. |
| 107 | + $oldObjectData = $event instanceof CalendarObjectUpdatedEvent ? $event->getOldObjectData() : []; |
| 108 | + |
| 109 | + try { |
| 110 | + $this->sendNotification($action, $actor, $owner, $ownerEmail, $calendarInfo, $event->getObjectData(), $oldObjectData); |
| 111 | + } catch (Throwable $e) { |
| 112 | + $this->logger->warning('Could not send delegate-action notification to calendar owner', [ |
| 113 | + 'app' => 'dav', |
| 114 | + 'owner' => $ownerUid, |
| 115 | + 'actor' => $actor->getUID(), |
| 116 | + 'action' => $action, |
| 117 | + 'exception' => $e, |
| 118 | + ]); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private function actorIsProxyOf(string $actorUid, string $ownerPrincipalUri): bool { |
| 123 | + $actorPrincipalUri = 'principals/users/' . $actorUid; |
| 124 | + foreach ($this->proxyMapper->getProxiesOf($ownerPrincipalUri) as $proxy) { |
| 125 | + if ($proxy->getProxyId() === $actorPrincipalUri) { |
| 126 | + return true; |
| 127 | + } |
| 128 | + } |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + private function sendNotification( |
| 133 | + string $action, |
| 134 | + IUser $actor, |
| 135 | + IUser $owner, |
| 136 | + string $ownerEmail, |
| 137 | + array $calendarInfo, |
| 138 | + array $objectData, |
| 139 | + array $oldObjectData, |
| 140 | + ): void { |
| 141 | + $l = $this->l10nFactory->get('dav', $this->l10nFactory->getUserLanguage($owner)); |
| 142 | + |
| 143 | + $newVCalendar = $this->readVCalendar($objectData['calendardata'] ?? null); |
| 144 | + $newVEvent = $this->firstVEvent($newVCalendar); |
| 145 | + if ($newVEvent === null) { |
| 146 | + // Without a VEVENT there is nothing meaningful to describe. |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + $oldVCalendar = $this->readVCalendar($oldObjectData['calendardata'] ?? null); |
| 151 | + $oldVEvent = $this->firstVEvent($oldVCalendar); |
| 152 | + |
| 153 | + $actorName = $actor->getDisplayName() ?: $actor->getUID(); |
| 154 | + $calendarName = (string)($calendarInfo['{DAV:}displayname'] ?? $calendarInfo['uri'] ?? 'calendar'); |
| 155 | + |
| 156 | + // Build the same data payload IMipPlugin uses, so addBulletList renders |
| 157 | + // the familiar title/when/location/url/description list — with diff |
| 158 | + // strikethroughs when an old version is available. |
| 159 | + $isCancellation = $action === self::ACTION_DELETE || $action === self::ACTION_TRASH; |
| 160 | + $data = $isCancellation |
| 161 | + ? $this->imipService->buildCancelledBodyData($newVEvent) |
| 162 | + : $this->imipService->buildBodyData($newVEvent, $action === self::ACTION_UPDATE ? $oldVEvent : null); |
| 163 | + |
| 164 | + $summary = (string)($newVEvent->SUMMARY ?? $l->t('Untitled event')); |
| 165 | + |
| 166 | + [$subject, $heading] = $this->subjectAndHeading($l, $action, $actorName, $summary, $calendarName); |
| 167 | + |
| 168 | + $template = $this->mailer->createEMailTemplate('dav.delegateAction.' . $action, [ |
| 169 | + 'actor' => $actorName, |
| 170 | + 'calendar' => $calendarName, |
| 171 | + 'event' => $summary, |
| 172 | + ]); |
| 173 | + $template->addHeader(); |
| 174 | + $template->setSubject($subject); |
| 175 | + $template->addHeading($heading); |
| 176 | + |
| 177 | + // Attribution row (who did it, on which calendar) — sits above the |
| 178 | + // event details so the owner immediately sees the responsible delegate. |
| 179 | + $template->addBodyListItem($actorName, $l->t('Delegate:')); |
| 180 | + $template->addBodyListItem($calendarName, $l->t('Calendar:')); |
| 181 | + |
| 182 | + $this->imipService->addBulletList($template, $newVEvent, $data); |
| 183 | + |
| 184 | + $template->addFooter(); |
| 185 | + |
| 186 | + $message = $this->mailer->createMessage(); |
| 187 | + $message->setFrom([Util::getDefaultEmailAddress('invitations-noreply') => $actorName]); |
| 188 | + $message->setTo([$ownerEmail => $owner->getDisplayName() ?: $owner->getUID()]); |
| 189 | + $message->setSubject($subject); |
| 190 | + $message->useTemplate($template); |
| 191 | + |
| 192 | + // Attach the raw iCalendar so the owner's client can pick up the change. |
| 193 | + if ($action !== self::ACTION_DELETE) { |
| 194 | + $calendarData = $objectData['calendardata'] ?? null; |
| 195 | + if (is_resource($calendarData)) { |
| 196 | + $calendarData = stream_get_contents($calendarData); |
| 197 | + } |
| 198 | + if (is_string($calendarData) && $calendarData !== '') { |
| 199 | + $message->attachInline( |
| 200 | + $calendarData, |
| 201 | + 'event.ics', |
| 202 | + 'text/calendar; charset="utf-8"', |
| 203 | + ); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + $this->mailer->send($message); |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * @return array{0: string, 1: string} [subject, heading] |
| 212 | + */ |
| 213 | + private function subjectAndHeading(\OCP\IL10N $l, string $action, string $actorName, string $summary, string $calendarName): array { |
| 214 | + return match ($action) { |
| 215 | + self::ACTION_CREATE => [ |
| 216 | + $l->t('%1$s created "%2$s" on your behalf', [$actorName, $summary]), |
| 217 | + $l->t('%1$s created "%2$s" on your calendar "%3$s"', [$actorName, $summary, $calendarName]), |
| 218 | + ], |
| 219 | + self::ACTION_UPDATE => [ |
| 220 | + $l->t('%1$s updated "%2$s" on your behalf', [$actorName, $summary]), |
| 221 | + $l->t('%1$s updated "%2$s" on your calendar "%3$s"', [$actorName, $summary, $calendarName]), |
| 222 | + ], |
| 223 | + self::ACTION_DELETE => [ |
| 224 | + $l->t('%1$s deleted "%2$s" on your behalf', [$actorName, $summary]), |
| 225 | + $l->t('%1$s permanently deleted "%2$s" from your calendar "%3$s"', [$actorName, $summary, $calendarName]), |
| 226 | + ], |
| 227 | + self::ACTION_TRASH => [ |
| 228 | + $l->t('%1$s moved "%2$s" to the trash on your behalf', [$actorName, $summary]), |
| 229 | + $l->t('%1$s moved "%2$s" to the trash on your calendar "%3$s"', [$actorName, $summary, $calendarName]), |
| 230 | + ], |
| 231 | + self::ACTION_RESTORE => [ |
| 232 | + $l->t('%1$s restored "%2$s" on your behalf', [$actorName, $summary]), |
| 233 | + $l->t('%1$s restored "%2$s" on your calendar "%3$s"', [$actorName, $summary, $calendarName]), |
| 234 | + ], |
| 235 | + }; |
| 236 | + } |
| 237 | + |
| 238 | + private function readVCalendar(mixed $calendarData): ?VCalendar { |
| 239 | + if (is_resource($calendarData)) { |
| 240 | + $calendarData = stream_get_contents($calendarData); |
| 241 | + } |
| 242 | + if (!is_string($calendarData) || $calendarData === '') { |
| 243 | + return null; |
| 244 | + } |
| 245 | + try { |
| 246 | + $vCalendar = Reader::read($calendarData); |
| 247 | + } catch (Throwable) { |
| 248 | + return null; |
| 249 | + } |
| 250 | + return $vCalendar instanceof VCalendar ? $vCalendar : null; |
| 251 | + } |
| 252 | + |
| 253 | + private function firstVEvent(?VCalendar $vCalendar): ?VEvent { |
| 254 | + if ($vCalendar === null) { |
| 255 | + return null; |
| 256 | + } |
| 257 | + foreach ($vCalendar->VEVENT ?? [] as $vEvent) { |
| 258 | + if ($vEvent instanceof VEvent) { |
| 259 | + return $vEvent; |
| 260 | + } |
| 261 | + } |
| 262 | + return null; |
| 263 | + } |
| 264 | +} |
0 commit comments