|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at> |
| 7 | + * |
| 8 | + * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + */ |
| 25 | + |
| 26 | +namespace OCA\DAV\Listener; |
| 27 | + |
| 28 | +use OCA\DAV\Connector\Sabre\Principal; |
| 29 | +use OCA\DAV\Events\CalendarObjectCreatedEvent; |
| 30 | +use OCA\DAV\Events\CalendarObjectUpdatedEvent; |
| 31 | +use OCA\DAV\Events\CalendarShareUpdatedEvent; |
| 32 | +use OCP\Contacts\Events\ContactInteractedWithEvent; |
| 33 | +use OCP\EventDispatcher\Event; |
| 34 | +use OCP\EventDispatcher\IEventDispatcher; |
| 35 | +use OCP\EventDispatcher\IEventListener; |
| 36 | +use OCP\IUser; |
| 37 | +use OCP\IUserSession; |
| 38 | +use Psr\Log\LoggerInterface; |
| 39 | +use function strlen; |
| 40 | +use function strpos; |
| 41 | +use function substr; |
| 42 | + |
| 43 | +class CalendarContactInteractionListener implements IEventListener { |
| 44 | + private const URI_USERS = 'principals/users/'; |
| 45 | + |
| 46 | + /** @var IEventDispatcher */ |
| 47 | + private $dispatcher; |
| 48 | + |
| 49 | + /** @var IUserSession */ |
| 50 | + private $userManager; |
| 51 | + |
| 52 | + /** @var Principal */ |
| 53 | + private $principalConnector; |
| 54 | + |
| 55 | + /** @var LoggerInterface */ |
| 56 | + private $logger; |
| 57 | + |
| 58 | + public function __construct(IEventDispatcher $dispatcher, |
| 59 | + IUserSession $userManager, |
| 60 | + Principal $principalConnector, |
| 61 | + LoggerInterface $logger) { |
| 62 | + $this->dispatcher = $dispatcher; |
| 63 | + $this->userManager = $userManager; |
| 64 | + $this->principalConnector = $principalConnector; |
| 65 | + $this->logger = $logger; |
| 66 | + } |
| 67 | + |
| 68 | + public function handle(Event $event): void { |
| 69 | + if (($user = $this->userManager->getUser()) === null) { |
| 70 | + // Without user context we can't do anything |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if ($event instanceof CalendarObjectCreatedEvent || $event instanceof CalendarObjectUpdatedEvent) { |
| 75 | + // users: href => principal:principals/users/admin |
| 76 | + // TODO: parse (email) attendees from the VCARD |
| 77 | + foreach ($event->getShares() as $share) { |
| 78 | + if (!isset($share['href'])) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + $this->emitFromUri($share['href'], $user); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if ($event instanceof CalendarShareUpdatedEvent && !empty($event->getAdded())) { |
| 86 | + // group: href => principal:principals/groups/admin |
| 87 | + // users: href => principal:principals/users/admin |
| 88 | + foreach ($event->getAdded() as $added) { |
| 89 | + if (!isset($added['href'])) { |
| 90 | + // Nothing to work with |
| 91 | + continue; |
| 92 | + } |
| 93 | + $this->emitFromUri($added['href'], $user); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private function emitFromUri(string $uri, IUser $user): void { |
| 99 | + $principal = $this->principalConnector->findByUri( |
| 100 | + $uri, |
| 101 | + $this->principalConnector->getPrincipalPrefix() |
| 102 | + ); |
| 103 | + if ($principal === null) { |
| 104 | + // Invalid principal |
| 105 | + return; |
| 106 | + } |
| 107 | + if (strpos($principal, self::URI_USERS) !== 0) { |
| 108 | + // Not a user principal |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + $uid = substr($principal, strlen(self::URI_USERS)); |
| 113 | + $this->dispatcher->dispatchTyped( |
| 114 | + (new ContactInteractedWithEvent($user))->setUid($uid) |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments