diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2021-02-02 10:08:44 +0100 |
---|---|---|
committer | backportbot[bot] <backportbot[bot]@users.noreply.github.com> | 2021-02-03 11:53:20 +0000 |
commit | b5fd6950a18302a75b64c80038097201bf666469 (patch) | |
tree | 6fb6b9d56b09eab5f3cb5369d4e46cb15e09c13e /apps/dav/lib | |
parent | 207b7d54fbb1516ddd093961f350f717a3565cbf (diff) | |
download | nextcloud-server-b5fd6950a18302a75b64c80038097201bf666469.tar.gz nextcloud-server-b5fd6950a18302a75b64c80038097201bf666469.zip |
Emit an interaction event for calendar share recipients
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'apps/dav/lib')
-rw-r--r-- | apps/dav/lib/AppInfo/Application.php | 7 | ||||
-rw-r--r-- | apps/dav/lib/Listener/CalendarContactInteractionListener.php | 96 |
2 files changed, 103 insertions, 0 deletions
diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php index 1bad3cb1eba..09081403140 100644 --- a/apps/dav/lib/AppInfo/Application.php +++ b/apps/dav/lib/AppInfo/Application.php @@ -53,7 +53,9 @@ use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\CardDAV\ContactsManager; use OCA\DAV\CardDAV\PhotoCache; use OCA\DAV\CardDAV\SyncService; +use OCA\DAV\Events\CalendarShareUpdatedEvent; use OCA\DAV\HookManager; +use OCA\DAV\Listener\CalendarContactInteractionListener; use OCA\DAV\Search\ContactsSearchProvider; use OCA\DAV\Search\EventsSearchProvider; use OCA\DAV\Search\TasksSearchProvider; @@ -106,6 +108,11 @@ class Application extends App implements IBootstrap { $context->registerSearchProvider(ContactsSearchProvider::class); $context->registerSearchProvider(EventsSearchProvider::class); $context->registerSearchProvider(TasksSearchProvider::class); + + /** + * Register event listeners + */ + $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class); } public function boot(IBootContext $context): void { diff --git a/apps/dav/lib/Listener/CalendarContactInteractionListener.php b/apps/dav/lib/Listener/CalendarContactInteractionListener.php new file mode 100644 index 00000000000..a0b94c34f1f --- /dev/null +++ b/apps/dav/lib/Listener/CalendarContactInteractionListener.php @@ -0,0 +1,96 @@ +<?php + +declare(strict_types=1); + +/* + * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCA\DAV\Listener; + +use OCA\DAV\Connector\Sabre\Principal; +use OCA\DAV\Events\CalendarShareUpdatedEvent; +use OCP\Contacts\Events\ContactInteractedWithEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\EventDispatcher\IEventListener; +use OCP\IUserSession; +use Psr\Log\LoggerInterface; +use function strlen; +use function strpos; +use function substr; + +class CalendarContactInteractionListener implements IEventListener { + private const URI_USERS = 'principals/users/'; + + /** @var IEventDispatcher */ + private $dispatcher; + + /** @var IUserSession */ + private $userManager; + + /** @var Principal */ + private $principalConnector; + + /** @var LoggerInterface */ + private $logger; + + public function __construct(IEventDispatcher $dispatcher, + IUserSession $userManager, + Principal $principalConnector, + LoggerInterface $logger) { + $this->dispatcher = $dispatcher; + $this->userManager = $userManager; + $this->principalConnector = $principalConnector; + $this->logger = $logger; + } + + public function handle(Event $event): void { + if (($user = $this->userManager->getUser()) === null) { + // Without user context we can't do anything + return; + } + + if ($event instanceof CalendarShareUpdatedEvent && !empty($event->getAdded())) { + // group: href => principal:principals/groups/admin + // users: href => principal:principals/users/admin + foreach ($event->getAdded() as $added) { + if (!isset($added['href'])) { + // Nothing to work with + continue; + } + $principal = $this->principalConnector->findByUri( + $added['href'], + $this->principalConnector->getPrincipalPrefix() + ); + if ($principal === null) { + // Invalid principal + continue; + } + if (strpos($principal, self::URI_USERS) === 0) { + $uid = substr($principal, strlen(self::URI_USERS)); + $this->dispatcher->dispatchTyped( + (new ContactInteractedWithEvent($user))->setUid($uid) + ); + } + } + } + } +} |