Merge pull request #25462 from nextcloud/backport/25432/stable21

[stable21] Emit calendar interaction events
This commit is contained in:
Christoph Wurst 2021-02-03 13:55:50 +01:00 committed by GitHub
commit 62f9f4f7fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 130 additions and 0 deletions

View File

@ -204,6 +204,7 @@ return array(
'OCA\\DAV\\Files\\Sharing\\FilesDropPlugin' => $baseDir . '/../lib/Files/Sharing/FilesDropPlugin.php', 'OCA\\DAV\\Files\\Sharing\\FilesDropPlugin' => $baseDir . '/../lib/Files/Sharing/FilesDropPlugin.php',
'OCA\\DAV\\Files\\Sharing\\PublicLinkCheckPlugin' => $baseDir . '/../lib/Files/Sharing/PublicLinkCheckPlugin.php', 'OCA\\DAV\\Files\\Sharing\\PublicLinkCheckPlugin' => $baseDir . '/../lib/Files/Sharing/PublicLinkCheckPlugin.php',
'OCA\\DAV\\HookManager' => $baseDir . '/../lib/HookManager.php', 'OCA\\DAV\\HookManager' => $baseDir . '/../lib/HookManager.php',
'OCA\\DAV\\Listener\\CalendarContactInteractionListener' => $baseDir . '/../lib/Listener/CalendarContactInteractionListener.php',
'OCA\\DAV\\Migration\\BuildCalendarSearchIndex' => $baseDir . '/../lib/Migration/BuildCalendarSearchIndex.php', 'OCA\\DAV\\Migration\\BuildCalendarSearchIndex' => $baseDir . '/../lib/Migration/BuildCalendarSearchIndex.php',
'OCA\\DAV\\Migration\\BuildCalendarSearchIndexBackgroundJob' => $baseDir . '/../lib/Migration/BuildCalendarSearchIndexBackgroundJob.php', 'OCA\\DAV\\Migration\\BuildCalendarSearchIndexBackgroundJob' => $baseDir . '/../lib/Migration/BuildCalendarSearchIndexBackgroundJob.php',
'OCA\\DAV\\Migration\\BuildSocialSearchIndex' => $baseDir . '/../lib/Migration/BuildSocialSearchIndex.php', 'OCA\\DAV\\Migration\\BuildSocialSearchIndex' => $baseDir . '/../lib/Migration/BuildSocialSearchIndex.php',

View File

@ -219,6 +219,7 @@ class ComposerStaticInitDAV
'OCA\\DAV\\Files\\Sharing\\FilesDropPlugin' => __DIR__ . '/..' . '/../lib/Files/Sharing/FilesDropPlugin.php', 'OCA\\DAV\\Files\\Sharing\\FilesDropPlugin' => __DIR__ . '/..' . '/../lib/Files/Sharing/FilesDropPlugin.php',
'OCA\\DAV\\Files\\Sharing\\PublicLinkCheckPlugin' => __DIR__ . '/..' . '/../lib/Files/Sharing/PublicLinkCheckPlugin.php', 'OCA\\DAV\\Files\\Sharing\\PublicLinkCheckPlugin' => __DIR__ . '/..' . '/../lib/Files/Sharing/PublicLinkCheckPlugin.php',
'OCA\\DAV\\HookManager' => __DIR__ . '/..' . '/../lib/HookManager.php', 'OCA\\DAV\\HookManager' => __DIR__ . '/..' . '/../lib/HookManager.php',
'OCA\\DAV\\Listener\\CalendarContactInteractionListener' => __DIR__ . '/..' . '/../lib/Listener/CalendarContactInteractionListener.php',
'OCA\\DAV\\Migration\\BuildCalendarSearchIndex' => __DIR__ . '/..' . '/../lib/Migration/BuildCalendarSearchIndex.php', 'OCA\\DAV\\Migration\\BuildCalendarSearchIndex' => __DIR__ . '/..' . '/../lib/Migration/BuildCalendarSearchIndex.php',
'OCA\\DAV\\Migration\\BuildCalendarSearchIndexBackgroundJob' => __DIR__ . '/..' . '/../lib/Migration/BuildCalendarSearchIndexBackgroundJob.php', 'OCA\\DAV\\Migration\\BuildCalendarSearchIndexBackgroundJob' => __DIR__ . '/..' . '/../lib/Migration/BuildCalendarSearchIndexBackgroundJob.php',
'OCA\\DAV\\Migration\\BuildSocialSearchIndex' => __DIR__ . '/..' . '/../lib/Migration/BuildSocialSearchIndex.php', 'OCA\\DAV\\Migration\\BuildSocialSearchIndex' => __DIR__ . '/..' . '/../lib/Migration/BuildSocialSearchIndex.php',

View File

@ -53,7 +53,11 @@ use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\CardDAV\ContactsManager; use OCA\DAV\CardDAV\ContactsManager;
use OCA\DAV\CardDAV\PhotoCache; use OCA\DAV\CardDAV\PhotoCache;
use OCA\DAV\CardDAV\SyncService; use OCA\DAV\CardDAV\SyncService;
use OCA\DAV\Events\CalendarObjectCreatedEvent;
use OCA\DAV\Events\CalendarObjectUpdatedEvent;
use OCA\DAV\Events\CalendarShareUpdatedEvent;
use OCA\DAV\HookManager; use OCA\DAV\HookManager;
use OCA\DAV\Listener\CalendarContactInteractionListener;
use OCA\DAV\Search\ContactsSearchProvider; use OCA\DAV\Search\ContactsSearchProvider;
use OCA\DAV\Search\EventsSearchProvider; use OCA\DAV\Search\EventsSearchProvider;
use OCA\DAV\Search\TasksSearchProvider; use OCA\DAV\Search\TasksSearchProvider;
@ -106,6 +110,13 @@ class Application extends App implements IBootstrap {
$context->registerSearchProvider(ContactsSearchProvider::class); $context->registerSearchProvider(ContactsSearchProvider::class);
$context->registerSearchProvider(EventsSearchProvider::class); $context->registerSearchProvider(EventsSearchProvider::class);
$context->registerSearchProvider(TasksSearchProvider::class); $context->registerSearchProvider(TasksSearchProvider::class);
/**
* Register event listeners
*/
$context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarContactInteractionListener::class);
$context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarContactInteractionListener::class);
$context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class);
} }
public function boot(IBootContext $context): void { public function boot(IBootContext $context): void {

View File

@ -0,0 +1,117 @@
<?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\CalendarObjectCreatedEvent;
use OCA\DAV\Events\CalendarObjectUpdatedEvent;
use OCA\DAV\Events\CalendarShareUpdatedEvent;
use OCP\Contacts\Events\ContactInteractedWithEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
use OCP\IUser;
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 CalendarObjectCreatedEvent || $event instanceof CalendarObjectUpdatedEvent) {
// users: href => principal:principals/users/admin
// TODO: parse (email) attendees from the VCARD
foreach ($event->getShares() as $share) {
if (!isset($share['href'])) {
continue;
}
$this->emitFromUri($share['href'], $user);
}
}
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;
}
$this->emitFromUri($added['href'], $user);
}
}
}
private function emitFromUri(string $uri, IUser $user): void {
$principal = $this->principalConnector->findByUri(
$uri,
$this->principalConnector->getPrincipalPrefix()
);
if ($principal === null) {
// Invalid principal
return;
}
if (strpos($principal, self::URI_USERS) !== 0) {
// Not a user principal
return;
}
$uid = substr($principal, strlen(self::URI_USERS));
$this->dispatcher->dispatchTyped(
(new ContactInteractedWithEvent($user))->setUid($uid)
);
}
}