Merge pull request #26913 from nextcloud/chore/dav-calendar-object-untyped-events

Drop some more untyped events from the dav code
This commit is contained in:
Christoph Wurst 2021-05-07 16:47:44 +02:00 committed by GitHub
commit f8c519f5f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 340 additions and 287 deletions

View File

@ -205,10 +205,10 @@ 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\\ActivityUpdaterListener' => $baseDir . '/../lib/Listener/ActivityUpdaterListener.php',
'OCA\\DAV\\Listener\\CalendarContactInteractionListener' => $baseDir . '/../lib/Listener/CalendarContactInteractionListener.php', 'OCA\\DAV\\Listener\\CalendarContactInteractionListener' => $baseDir . '/../lib/Listener/CalendarContactInteractionListener.php',
'OCA\\DAV\\Listener\\CalendarDeletionActivityUpdaterListener' => $baseDir . '/../lib/Listener/CalendarDeletionActivityUpdaterListener.php',
'OCA\\DAV\\Listener\\CalendarDeletionDefaultUpdaterListener' => $baseDir . '/../lib/Listener/CalendarDeletionDefaultUpdaterListener.php', 'OCA\\DAV\\Listener\\CalendarDeletionDefaultUpdaterListener' => $baseDir . '/../lib/Listener/CalendarDeletionDefaultUpdaterListener.php',
'OCA\\DAV\\Listener\\CalendarDeletionReminderUpdaterListener' => $baseDir . '/../lib/Listener/CalendarDeletionReminderUpdaterListener.php', 'OCA\\DAV\\Listener\\CalendarObjectReminderUpdaterListener' => $baseDir . '/../lib/Listener/CalendarObjectReminderUpdaterListener.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

@ -220,10 +220,10 @@ 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\\ActivityUpdaterListener' => __DIR__ . '/..' . '/../lib/Listener/ActivityUpdaterListener.php',
'OCA\\DAV\\Listener\\CalendarContactInteractionListener' => __DIR__ . '/..' . '/../lib/Listener/CalendarContactInteractionListener.php', 'OCA\\DAV\\Listener\\CalendarContactInteractionListener' => __DIR__ . '/..' . '/../lib/Listener/CalendarContactInteractionListener.php',
'OCA\\DAV\\Listener\\CalendarDeletionActivityUpdaterListener' => __DIR__ . '/..' . '/../lib/Listener/CalendarDeletionActivityUpdaterListener.php',
'OCA\\DAV\\Listener\\CalendarDeletionDefaultUpdaterListener' => __DIR__ . '/..' . '/../lib/Listener/CalendarDeletionDefaultUpdaterListener.php', 'OCA\\DAV\\Listener\\CalendarDeletionDefaultUpdaterListener' => __DIR__ . '/..' . '/../lib/Listener/CalendarDeletionDefaultUpdaterListener.php',
'OCA\\DAV\\Listener\\CalendarDeletionReminderUpdaterListener' => __DIR__ . '/..' . '/../lib/Listener/CalendarDeletionReminderUpdaterListener.php', 'OCA\\DAV\\Listener\\CalendarObjectReminderUpdaterListener' => __DIR__ . '/..' . '/../lib/Listener/CalendarObjectReminderUpdaterListener.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

@ -36,7 +36,6 @@ namespace OCA\DAV\AppInfo;
use Exception; use Exception;
use OCA\DAV\BackgroundJob\UpdateCalendarResourcesRoomsBackgroundJob; use OCA\DAV\BackgroundJob\UpdateCalendarResourcesRoomsBackgroundJob;
use OCA\DAV\CalDAV\Activity\Backend; use OCA\DAV\CalDAV\Activity\Backend;
use OCA\DAV\CalDAV\Activity\Provider\Event;
use OCA\DAV\CalDAV\BirthdayService; use OCA\DAV\CalDAV\BirthdayService;
use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\CalendarManager; use OCA\DAV\CalDAV\CalendarManager;
@ -45,7 +44,7 @@ use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider;
use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider; use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider;
use OCA\DAV\CalDAV\Reminder\NotificationProviderManager; use OCA\DAV\CalDAV\Reminder\NotificationProviderManager;
use OCA\DAV\CalDAV\Reminder\Notifier; use OCA\DAV\CalDAV\Reminder\Notifier;
use OCA\DAV\CalDAV\Reminder\ReminderService;
use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService; use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
use OCA\DAV\Capabilities; use OCA\DAV\Capabilities;
use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\CardDAV\CardDavBackend;
@ -54,13 +53,14 @@ use OCA\DAV\CardDAV\PhotoCache;
use OCA\DAV\CardDAV\SyncService; use OCA\DAV\CardDAV\SyncService;
use OCA\DAV\Events\CalendarDeletedEvent; use OCA\DAV\Events\CalendarDeletedEvent;
use OCA\DAV\Events\CalendarObjectCreatedEvent; use OCA\DAV\Events\CalendarObjectCreatedEvent;
use OCA\DAV\Events\CalendarObjectDeletedEvent;
use OCA\DAV\Events\CalendarObjectUpdatedEvent; use OCA\DAV\Events\CalendarObjectUpdatedEvent;
use OCA\DAV\Events\CalendarShareUpdatedEvent; use OCA\DAV\Events\CalendarShareUpdatedEvent;
use OCA\DAV\HookManager; use OCA\DAV\HookManager;
use OCA\DAV\Listener\ActivityUpdaterListener;
use OCA\DAV\Listener\CalendarContactInteractionListener; use OCA\DAV\Listener\CalendarContactInteractionListener;
use OCA\DAV\Listener\CalendarDeletionActivityUpdaterListener;
use OCA\DAV\Listener\CalendarDeletionDefaultUpdaterListener; use OCA\DAV\Listener\CalendarDeletionDefaultUpdaterListener;
use OCA\DAV\Listener\CalendarDeletionReminderUpdaterListener; use OCA\DAV\Listener\CalendarObjectReminderUpdaterListener;
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;
@ -114,11 +114,17 @@ class Application extends App implements IBootstrap {
/** /**
* Register event listeners * Register event listeners
*/ */
$context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionActivityUpdaterListener::class); $context->registerEventListener(CalendarDeletedEvent::class, ActivityUpdaterListener::class);
$context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionReminderUpdaterListener::class); $context->registerEventListener(CalendarDeletedEvent::class, CalendarObjectReminderUpdaterListener::class);
$context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionDefaultUpdaterListener::class); $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionDefaultUpdaterListener::class);
$context->registerEventListener(CalendarObjectCreatedEvent::class, ActivityUpdaterListener::class);
$context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarContactInteractionListener::class); $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarContactInteractionListener::class);
$context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectReminderUpdaterListener::class);
$context->registerEventListener(CalendarObjectUpdatedEvent::class, ActivityUpdaterListener::class);
$context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarContactInteractionListener::class); $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarContactInteractionListener::class);
$context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarObjectReminderUpdaterListener::class);
$context->registerEventListener(CalendarObjectDeletedEvent::class, ActivityUpdaterListener::class);
$context->registerEventListener(CalendarObjectDeletedEvent::class, CalendarObjectReminderUpdaterListener::class);
$context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class); $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class);
$context->registerNotifierService(Notifier::class); $context->registerNotifierService(Notifier::class);
@ -230,34 +236,6 @@ class Application extends App implements IBootstrap {
); );
}); });
$listener = function (GenericEvent $event, $eventName) use ($container): void {
/** @var Backend $backend */
$backend = $container->query(Backend::class);
$subject = Event::SUBJECT_OBJECT_ADD;
if ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject') {
$subject = Event::SUBJECT_OBJECT_UPDATE;
} elseif ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject') {
$subject = Event::SUBJECT_OBJECT_DELETE;
}
$backend->onTouchCalendarObject(
$subject,
$event->getArgument('calendarData'),
$event->getArgument('shares'),
$event->getArgument('objectData')
);
/** @var ReminderService $reminderBackend */
$reminderService = $container->query(ReminderService::class);
$reminderService->onTouchCalendarObject(
$eventName,
$event->getArgument('objectData')
);
};
$dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', $listener);
$dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', $listener);
$dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', $listener);
$dispatcher->addListener('OCP\Federation\TrustedServerEvent::remove', $dispatcher->addListener('OCP\Federation\TrustedServerEvent::remove',
function (GenericEvent $event) { function (GenericEvent $event) {

View File

@ -125,7 +125,7 @@ class BuildReminderIndexBackgroundJob extends QueuedJob {
$row['component'] = $row['componenttype']; $row['component'] = $row['componenttype'];
try { try {
$this->reminderService->onTouchCalendarObject('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', $row); $this->reminderService->onCalendarObjectCreate($row);
} catch (\Exception $ex) { } catch (\Exception $ex) {
$this->logger->logException($ex); $this->logger->logException($ex);
} }

View File

@ -1141,15 +1141,6 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
$shares = $this->getShares($calendarId); $shares = $this->getShares($calendarId);
$this->dispatcher->dispatchTyped(new CalendarObjectCreatedEvent((int)$calendarId, $calendarRow, $shares, $objectRow)); $this->dispatcher->dispatchTyped(new CalendarObjectCreatedEvent((int)$calendarId, $calendarRow, $shares, $objectRow));
$this->legacyDispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', new GenericEvent(
'\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject',
[
'calendarId' => $calendarId,
'calendarData' => $calendarRow,
'shares' => $shares,
'objectData' => $objectRow,
]
));
} else { } else {
$subscriptionRow = $this->getSubscriptionById($calendarId); $subscriptionRow = $this->getSubscriptionById($calendarId);
@ -1215,15 +1206,6 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
$shares = $this->getShares($calendarId); $shares = $this->getShares($calendarId);
$this->dispatcher->dispatchTyped(new CalendarObjectUpdatedEvent((int)$calendarId, $calendarRow, $shares, $objectRow)); $this->dispatcher->dispatchTyped(new CalendarObjectUpdatedEvent((int)$calendarId, $calendarRow, $shares, $objectRow));
$this->legacyDispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', new GenericEvent(
'\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject',
[
'calendarId' => $calendarId,
'calendarData' => $calendarRow,
'shares' => $shares,
'objectData' => $objectRow,
]
));
} else { } else {
$subscriptionRow = $this->getSubscriptionById($calendarId); $subscriptionRow = $this->getSubscriptionById($calendarId);
@ -1278,15 +1260,6 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
$shares = $this->getShares($calendarId); $shares = $this->getShares($calendarId);
$this->dispatcher->dispatchTyped(new CalendarObjectDeletedEvent((int)$calendarId, $calendarRow, $shares, $data)); $this->dispatcher->dispatchTyped(new CalendarObjectDeletedEvent((int)$calendarId, $calendarRow, $shares, $data));
$this->legacyDispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', new GenericEvent(
'\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject',
[
'calendarId' => $calendarId,
'calendarData' => $calendarRow,
'shares' => $shares,
'objectData' => $data,
]
));
} else { } else {
$subscriptionRow = $this->getSubscriptionById($calendarId); $subscriptionRow = $this->getSubscriptionById($calendarId);

View File

@ -44,6 +44,7 @@ use Sabre\VObject\InvalidDataException;
use Sabre\VObject\ParseException; use Sabre\VObject\ParseException;
use Sabre\VObject\Recur\EventIterator; use Sabre\VObject\Recur\EventIterator;
use Sabre\VObject\Recur\NoInstancesException; use Sabre\VObject\Recur\NoInstancesException;
use function strcasecmp;
class ReminderService { class ReminderService {
@ -154,39 +155,15 @@ class ReminderService {
} }
/** /**
* @param string $action
* @param array $objectData * @param array $objectData
* @throws VObject\InvalidDataException * @throws VObject\InvalidDataException
*/ */
public function onTouchCalendarObject(string $action, public function onCalendarObjectCreate(array $objectData):void {
array $objectData):void {
// We only support VEvents for now // We only support VEvents for now
if (strcasecmp($objectData['component'], 'vevent') !== 0) { if (strcasecmp($objectData['component'], 'vevent') !== 0) {
return; return;
} }
switch ($action) {
case '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject':
$this->onCalendarObjectCreate($objectData);
break;
case '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject':
$this->onCalendarObjectEdit($objectData);
break;
case '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject':
$this->onCalendarObjectDelete($objectData);
break;
default:
break;
}
}
/**
* @param array $objectData
*/
private function onCalendarObjectCreate(array $objectData):void {
$calendarData = is_resource($objectData['calendardata']) $calendarData = is_resource($objectData['calendardata'])
? stream_get_contents($objectData['calendardata']) ? stream_get_contents($objectData['calendardata'])
: $objectData['calendardata']; : $objectData['calendardata'];
@ -307,8 +284,9 @@ class ReminderService {
/** /**
* @param array $objectData * @param array $objectData
* @throws VObject\InvalidDataException
*/ */
private function onCalendarObjectEdit(array $objectData):void { public function onCalendarObjectEdit(array $objectData):void {
// TODO - this can be vastly improved // TODO - this can be vastly improved
// - get cached reminders // - get cached reminders
// - ... // - ...
@ -319,8 +297,14 @@ class ReminderService {
/** /**
* @param array $objectData * @param array $objectData
* @throws VObject\InvalidDataException
*/ */
private function onCalendarObjectDelete(array $objectData):void { public function onCalendarObjectDelete(array $objectData):void {
// We only support VEvents for now
if (strcasecmp($objectData['component'], 'vevent') !== 0) {
return;
}
$this->backend->cleanRemindersForEvent((int) $objectData['id']); $this->backend->cleanRemindersForEvent((int) $objectData['id']);
} }

View File

@ -0,0 +1,126 @@
<?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\CalDAV\Activity\Backend as ActivityBackend;
use OCA\DAV\Events\CalendarDeletedEvent;
use OCA\DAV\Events\CalendarObjectCreatedEvent;
use OCA\DAV\Events\CalendarObjectDeletedEvent;
use OCA\DAV\Events\CalendarObjectUpdatedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use Psr\Log\LoggerInterface;
use Throwable;
use function sprintf;
class ActivityUpdaterListener implements IEventListener {
/** @var ActivityBackend */
private $activityBackend;
/** @var LoggerInterface */
private $logger;
public function __construct(ActivityBackend $activityBackend,
LoggerInterface $logger) {
$this->activityBackend = $activityBackend;
$this->logger = $logger;
}
public function handle(Event $event): void {
if ($event instanceof CalendarDeletedEvent) {
try {
$this->activityBackend->onCalendarDelete(
$event->getCalendarData(),
$event->getShares()
);
$this->logger->debug(
sprintf('Activity generated for deleted calendar %d', $event->getCalendarId())
);
} catch (Throwable $e) {
// Any error with activities shouldn't abort the calendar deletion, so we just log it
$this->logger->error('Error generating activities for a deleted calendar: ' . $e->getMessage(), [
'exception' => $e,
]);
}
} elseif ($event instanceof CalendarObjectCreatedEvent) {
try {
$this->activityBackend->onTouchCalendarObject(
\OCA\DAV\CalDAV\Activity\Provider\Event::SUBJECT_OBJECT_ADD,
$event->getCalendarData(),
$event->getShares(),
$event->getObjectData()
);
$this->logger->debug(
sprintf('Activity generated for new calendar object in calendar %d', $event->getCalendarId())
);
} catch (Throwable $e) {
// Any error with activities shouldn't abort the calendar object creation, so we just log it
$this->logger->error('Error generating activity for a new calendar object: ' . $e->getMessage(), [
'exception' => $e,
]);
}
} elseif ($event instanceof CalendarObjectUpdatedEvent) {
try {
$this->activityBackend->onTouchCalendarObject(
\OCA\DAV\CalDAV\Activity\Provider\Event::SUBJECT_OBJECT_UPDATE,
$event->getCalendarData(),
$event->getShares(),
$event->getObjectData()
);
$this->logger->debug(
sprintf('Activity generated for deleted calendar object %d', $event->getCalendarId())
);
} catch (Throwable $e) {
// Any error with activities shouldn't abort the calendar deletion, so we just log it
$this->logger->error('Error generating activity for a deleted calendar object: ' . $e->getMessage(), [
'exception' => $e,
]);
}
} elseif ($event instanceof CalendarObjectDeletedEvent) {
try {
$this->activityBackend->onTouchCalendarObject(
\OCA\DAV\CalDAV\Activity\Provider\Event::SUBJECT_OBJECT_DELETE,
$event->getCalendarData(),
$event->getShares(),
$event->getObjectData()
);
$this->logger->debug(
sprintf('Activity generated for deleted calendar object %d', $event->getCalendarId())
);
} catch (Throwable $e) {
// Any error with activities shouldn't abort the calendar deletion, so we just log it
$this->logger->error('Error generating activity for a deleted calendar object: ' . $e->getMessage(), [
'exception' => $e,
]);
}
}
}
}

View File

@ -1,75 +0,0 @@
<?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\CalDAV\Activity\Backend as ActivityBackend;
use OCA\DAV\Events\CalendarDeletedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use Psr\Log\LoggerInterface;
use Throwable;
use function sprintf;
/**
* @template-implements IEventListener<\OCA\DAV\Events\CalendarDeletedEvent>
*/
class CalendarDeletionActivityUpdaterListener implements IEventListener {
/** @var ActivityBackend */
private $activityBackend;
/** @var LoggerInterface */
private $logger;
public function __construct(ActivityBackend $activityBackend,
LoggerInterface $logger) {
$this->activityBackend = $activityBackend;
$this->logger = $logger;
}
public function handle(Event $event): void {
if (!($event instanceof CalendarDeletedEvent)) {
// Not what we subscribed to
return;
}
try {
$this->activityBackend->onCalendarDelete(
$event->getCalendarData(),
$event->getShares()
);
$this->logger->debug(
sprintf('Activity generated for deleted calendar %d', $event->getCalendarId())
);
} catch (Throwable $e) {
// Any error with activities shouldn't abort the calendar deletion, so we just log it
$this->logger->error('Error generating activities for a deleted calendar: ' . $e->getMessage(), [
'exception' => $e,
]);
}
}
}

View File

@ -1,74 +0,0 @@
<?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\CalDAV\Reminder\Backend as ReminderBackend;
use OCA\DAV\Events\CalendarDeletedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use Psr\Log\LoggerInterface;
use Throwable;
use function sprintf;
/**
* @template-implements IEventListener<\OCA\DAV\Events\CalendarDeletedEvent>
*/
class CalendarDeletionReminderUpdaterListener implements IEventListener {
/** @var ReminderBackend */
private $reminderBackend;
/** @var LoggerInterface */
private $logger;
public function __construct(ReminderBackend $reminderBackend,
LoggerInterface $logger) {
$this->reminderBackend = $reminderBackend;
$this->logger = $logger;
}
public function handle(Event $event): void {
if (!($event instanceof CalendarDeletedEvent)) {
// Not what we subscribed to
return;
}
try {
$this->reminderBackend->cleanRemindersForCalendar(
$event->getCalendarId()
);
$this->logger->debug(
sprintf('Reminders of calendar %d cleaned up', $event->getCalendarId())
);
} catch (Throwable $e) {
// Any error with activities shouldn't abort the calendar deletion, so we just log it
$this->logger->error('Error cleaning up reminders of a deleted calendar: ' . $e->getMessage(), [
'exception' => $e,
]);
}
}
}

View File

@ -0,0 +1,122 @@
<?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\CalDAV\Reminder\Backend as ReminderBackend;
use OCA\DAV\CalDAV\Reminder\ReminderService;
use OCA\DAV\Events\CalendarDeletedEvent;
use OCA\DAV\Events\CalendarObjectCreatedEvent;
use OCA\DAV\Events\CalendarObjectDeletedEvent;
use OCA\DAV\Events\CalendarObjectUpdatedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use Psr\Log\LoggerInterface;
use Throwable;
use function sprintf;
class CalendarObjectReminderUpdaterListener implements IEventListener {
/** @var ReminderBackend */
private $reminderBackend;
/** @var ReminderService */
private $reminderService;
/** @var LoggerInterface */
private $logger;
public function __construct(ReminderBackend $reminderBackend,
ReminderService $reminderService,
LoggerInterface $logger) {
$this->reminderBackend = $reminderBackend;
$this->reminderService = $reminderService;
$this->logger = $logger;
}
public function handle(Event $event): void {
if ($event instanceof CalendarDeletedEvent) {
try {
$this->reminderBackend->cleanRemindersForCalendar(
$event->getCalendarId()
);
$this->logger->debug(
sprintf('Reminders of calendar %d cleaned up', $event->getCalendarId())
);
} catch (Throwable $e) {
// Any error with activities shouldn't abort the calendar deletion, so we just log it
$this->logger->error('Error cleaning up reminders of a deleted calendar: ' . $e->getMessage(), [
'exception' => $e,
]);
}
} elseif ($event instanceof CalendarObjectCreatedEvent) {
try {
$this->reminderService->onCalendarObjectCreate(
$event->getObjectData()
);
$this->logger->debug(
sprintf('Reminders of calendar object of calendar %d created', $event->getCalendarId())
);
} catch (Throwable $e) {
// Any error with reminders shouldn't abort the calendar object creation, so we just log it
$this->logger->error('Error creating reminders of a calendar object: ' . $e->getMessage(), [
'exception' => $e,
]);
}
} elseif ($event instanceof CalendarObjectUpdatedEvent) {
try {
$this->reminderService->onCalendarObjectEdit(
$event->getObjectData()
);
$this->logger->debug(
sprintf('Reminders of calendar object of calendar %d cleaned up', $event->getCalendarId())
);
} catch (Throwable $e) {
// Any error with activities shouldn't abort the calendar object deletion, so we just log it
$this->logger->error('Error cleaning up reminders of a calendar object: ' . $e->getMessage(), [
'exception' => $e,
]);
}
} elseif ($event instanceof CalendarObjectDeletedEvent) {
try {
$this->reminderService->onCalendarObjectDelete(
$event->getObjectData()
);
$this->logger->debug(
sprintf('Reminders of calendar object of calendar %d cleaned up', $event->getCalendarId())
);
} catch (Throwable $e) {
// Any error with activities shouldn't abort the calendar object deletion, so we just log it
$this->logger->error('Error cleaning up reminders of a deleted calendar object: ' . $e->getMessage(), [
'exception' => $e,
]);
}
}
}
}

View File

@ -32,6 +32,7 @@ use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\Proxy\ProxyMapper; use OCA\DAV\CalDAV\Proxy\ProxyMapper;
use OCA\DAV\Connector\Sabre\Principal; use OCA\DAV\Connector\Sabre\Principal;
use OCA\DAV\Events\CalendarDeletedEvent; use OCA\DAV\Events\CalendarDeletedEvent;
use OCA\DAV\Events\CalendarObjectCreatedEvent;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig; use OCP\IConfig;
@ -206,9 +207,11 @@ END:VCALENDAR
EOD; EOD;
$uri0 = $this->getUniqueID('event'); $uri0 = $this->getUniqueID('event');
$this->legacyDispatcher->expects($this->at(0)) $this->dispatcher->expects(self::once())
->method('dispatch') ->method('dispatchTyped')
->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject'); ->with(self::callback(function ($event) {
return $event instanceof CalendarObjectCreatedEvent;
}));
$this->backend->createCalendarObject($calendarId, $uri0, $calData); $this->backend->createCalendarObject($calendarId, $uri0, $calData);

View File

@ -37,6 +37,8 @@ use DateTimeZone;
use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\Calendar; use OCA\DAV\CalDAV\Calendar;
use OCA\DAV\Events\CalendarDeletedEvent; use OCA\DAV\Events\CalendarDeletedEvent;
use OCA\DAV\Events\CalendarObjectCreatedEvent;
use OCA\DAV\Events\CalendarUpdatedEvent;
use OCP\IConfig; use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
use Sabre\DAV\Exception\NotFound; use Sabre\DAV\Exception\NotFound;
@ -186,9 +188,11 @@ END:VEVENT
END:VCALENDAR END:VCALENDAR
EOD; EOD;
$this->legacyDispatcher->expects($this->at(0)) $this->dispatcher->expects(self::once())
->method('dispatch') ->method('dispatchTyped')
->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject'); ->with(self::callback(function ($event) {
return $event instanceof CalendarObjectCreatedEvent;
}));
$this->backend->createCalendarObject($calendarId, $uri, $calData); $this->backend->createCalendarObject($calendarId, $uri, $calData);
/** @var IACL $child */ /** @var IACL $child */
@ -232,9 +236,11 @@ END:VEVENT
END:VCALENDAR END:VCALENDAR
EOD; EOD;
$this->legacyDispatcher->expects($this->at(0)) $this->dispatcher->expects(self::once())
->method('dispatch') ->method('dispatchTyped')
->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject'); ->with(self::callback(function ($event) {
return $event instanceof CalendarObjectCreatedEvent;
}));
$this->backend->createCalendarObject($calendarId, $uri, $calData); $this->backend->createCalendarObject($calendarId, $uri, $calData);
// get all the cards // get all the cards
@ -270,17 +276,21 @@ DTEND;VALUE=DATE-TIME:20130912T140000Z
END:VEVENT END:VEVENT
END:VCALENDAR END:VCALENDAR
EOD; EOD;
$this->legacyDispatcher->expects($this->at(0)) $this->dispatcher->expects(self::once())
->method('dispatch') ->method('dispatchTyped')
->with('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject'); ->with(self::callback(function ($event) {
return $event instanceof CalendarUpdatedEvent;
}));
$this->backend->updateCalendarObject($calendarId, $uri, $calData); $this->backend->updateCalendarObject($calendarId, $uri, $calData);
$calendarObject = $this->backend->getCalendarObject($calendarId, $uri); $calendarObject = $this->backend->getCalendarObject($calendarId, $uri);
$this->assertEquals($calData, $calendarObject['calendardata']); $this->assertEquals($calData, $calendarObject['calendardata']);
// delete the card // delete the card
$this->legacyDispatcher->expects($this->at(0)) $this->dispatcher->expects(self::once())
->method('dispatch') ->method('dispatchTyped')
->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject'); ->with(self::callback(function ($event) {
return $event instanceof CalendarDeletedEvent;
}));
$this->backend->deleteCalendarObject($calendarId, $uri); $this->backend->deleteCalendarObject($calendarId, $uri);
$calendarObjects = $this->backend->getCalendarObjects($calendarId); $calendarObjects = $this->backend->getCalendarObjects($calendarId);
$this->assertCount(0, $calendarObjects); $this->assertCount(0, $calendarObjects);
@ -373,19 +383,25 @@ END:VCALENDAR
EOD; EOD;
$uri0 = static::getUniqueID('card'); $uri0 = static::getUniqueID('card');
$this->legacyDispatcher->expects($this->at(0)) $this->dispatcher->expects(self::once())
->method('dispatch') ->method('dispatchTyped')
->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject'); ->with(self::callback(function ($event) {
return $event instanceof CalendarObjectCreatedEvent;
}));
$this->backend->createCalendarObject($calendarId, $uri0, $calData[0]); $this->backend->createCalendarObject($calendarId, $uri0, $calData[0]);
$uri1 = static::getUniqueID('card'); $uri1 = static::getUniqueID('card');
$this->legacyDispatcher->expects($this->at(0)) $this->dispatcher->expects(self::once())
->method('dispatch') ->method('dispatchTyped')
->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject'); ->with(self::callback(function ($event) {
return $event instanceof CalendarObjectCreatedEvent;
}));
$this->backend->createCalendarObject($calendarId, $uri1, $calData[1]); $this->backend->createCalendarObject($calendarId, $uri1, $calData[1]);
$uri2 = static::getUniqueID('card'); $uri2 = static::getUniqueID('card');
$this->legacyDispatcher->expects($this->at(0)) $this->dispatcher->expects(self::once())
->method('dispatch') ->method('dispatchTyped')
->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject'); ->with(self::callback(function ($event) {
return $event instanceof CalendarObjectCreatedEvent;
}));
$this->backend->createCalendarObject($calendarId, $uri2, $calData[2]); $this->backend->createCalendarObject($calendarId, $uri2, $calData[2]);
// get all the cards // get all the cards
@ -412,17 +428,23 @@ EOD;
$this->assertEquals($calData[2], $calendarObjects[1]['calendardata']); $this->assertEquals($calData[2], $calendarObjects[1]['calendardata']);
// delete the card // delete the card
$this->legacyDispatcher->expects($this->at(0)) $this->dispatcher->expects(self::once())
->method('dispatch') ->method('dispatchTyped')
->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject'); ->with(self::callback(function ($event) {
return $event instanceof CalendarDeletedEvent;
}));
$this->backend->deleteCalendarObject($calendarId, $uri0); $this->backend->deleteCalendarObject($calendarId, $uri0);
$this->legacyDispatcher->expects($this->at(0)) $this->dispatcher->expects(self::once())
->method('dispatch') ->method('dispatchTyped')
->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject'); ->with(self::callback(function ($event) {
return $event instanceof CalendarDeletedEvent;
}));
$this->backend->deleteCalendarObject($calendarId, $uri1); $this->backend->deleteCalendarObject($calendarId, $uri1);
$this->legacyDispatcher->expects($this->at(0)) $this->dispatcher->expects(self::once())
->method('dispatch') ->method('dispatchTyped')
->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject'); ->with(self::callback(function ($event) {
return $event instanceof CalendarDeletedEvent;
}));
$this->backend->deleteCalendarObject($calendarId, $uri2); $this->backend->deleteCalendarObject($calendarId, $uri2);
$calendarObjects = $this->backend->getCalendarObjects($calendarId); $calendarObjects = $this->backend->getCalendarObjects($calendarId);
$this->assertCount(0, $calendarObjects); $this->assertCount(0, $calendarObjects);

View File

@ -211,17 +211,15 @@ EOD;
->method('cleanRemindersForEvent') ->method('cleanRemindersForEvent')
->with(44); ->with(44);
$action = '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject';
$objectData = [ $objectData = [
'id' => '44', 'id' => '44',
'component' => 'vevent', 'component' => 'vevent',
]; ];
$this->reminderService->onTouchCalendarObject($action, $objectData); $this->reminderService->onCalendarObjectDelete($objectData);
} }
public function testOnCalendarObjectCreateSingleEntry():void { public function testOnCalendarObjectCreateSingleEntry():void {
$action = '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject';
$objectData = [ $objectData = [
'calendardata' => self::CALENDAR_DATA, 'calendardata' => self::CALENDAR_DATA,
'id' => '42', 'id' => '42',
@ -242,11 +240,10 @@ EOD;
->with() ->with()
->willReturn(\DateTime::createFromFormat(\DateTime::ATOM, '2016-06-08T00:00:00+00:00')); ->willReturn(\DateTime::createFromFormat(\DateTime::ATOM, '2016-06-08T00:00:00+00:00'));
$this->reminderService->onTouchCalendarObject($action, $objectData); $this->reminderService->onCalendarObjectCreate($objectData);
} }
public function testOnCalendarObjectCreateSingleEntryWithRepeat(): void { public function testOnCalendarObjectCreateSingleEntryWithRepeat(): void {
$action = '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject';
$objectData = [ $objectData = [
'calendardata' => self::CALENDAR_DATA_REPEAT, 'calendardata' => self::CALENDAR_DATA_REPEAT,
'id' => '42', 'id' => '42',
@ -270,11 +267,10 @@ EOD;
->with() ->with()
->willReturn(\DateTime::createFromFormat(\DateTime::ATOM, '2016-06-08T00:00:00+00:00')); ->willReturn(\DateTime::createFromFormat(\DateTime::ATOM, '2016-06-08T00:00:00+00:00'));
$this->reminderService->onTouchCalendarObject($action, $objectData); $this->reminderService->onCalendarObjectCreate($objectData);
} }
public function testOnCalendarObjectCreateRecurringEntry(): void { public function testOnCalendarObjectCreateRecurringEntry(): void {
$action = '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject';
$objectData = [ $objectData = [
'calendardata' => self::CALENDAR_DATA_RECURRING, 'calendardata' => self::CALENDAR_DATA_RECURRING,
'id' => '42', 'id' => '42',
@ -295,11 +291,10 @@ EOD;
->with() ->with()
->willReturn(\DateTime::createFromFormat(\DateTime::ATOM, '2016-06-29T00:00:00+00:00')); ->willReturn(\DateTime::createFromFormat(\DateTime::ATOM, '2016-06-29T00:00:00+00:00'));
$this->reminderService->onTouchCalendarObject($action, $objectData); $this->reminderService->onCalendarObjectCreate($objectData);
} }
public function testOnCalendarObjectCreateEmpty():void { public function testOnCalendarObjectCreateEmpty():void {
$action = '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject';
$objectData = [ $objectData = [
'calendardata' => self::CALENDAR_DATA_NO_ALARM, 'calendardata' => self::CALENDAR_DATA_NO_ALARM,
'id' => '42', 'id' => '42',
@ -310,11 +305,10 @@ EOD;
$this->backend->expects($this->never()) $this->backend->expects($this->never())
->method('insertReminder'); ->method('insertReminder');
$this->reminderService->onTouchCalendarObject($action, $objectData); $this->reminderService->onCalendarObjectCreate($objectData);
} }
public function testOnCalendarObjectCreateRecurringEntryWithRepeat():void { public function testOnCalendarObjectCreateRecurringEntryWithRepeat():void {
$action = '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject';
$objectData = [ $objectData = [
'calendardata' => self::CALENDAR_DATA_RECURRING_REPEAT, 'calendardata' => self::CALENDAR_DATA_RECURRING_REPEAT,
'id' => '42', 'id' => '42',
@ -339,7 +333,7 @@ EOD;
->with() ->with()
->willReturn(\DateTime::createFromFormat(\DateTime::ATOM, '2016-06-29T00:00:00+00:00')); ->willReturn(\DateTime::createFromFormat(\DateTime::ATOM, '2016-06-29T00:00:00+00:00'));
$this->reminderService->onTouchCalendarObject($action, $objectData); $this->reminderService->onCalendarObjectCreate($objectData);
} }
public function testProcessReminders():void { public function testProcessReminders():void {