summaryrefslogtreecommitdiffstats
path: root/apps/dav/lib
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@winzerhof-wurst.at>2021-04-29 10:04:08 +0200
committerChristoph Wurst <christoph@winzerhof-wurst.at>2021-04-29 15:31:19 +0200
commit63eb3694e4a2d87342f8b478bd02fff79a8c73e7 (patch)
tree927a85056427fa32254f37ee58ad709d93f1cf4d /apps/dav/lib
parentabb5b5d983be485b7e006085643788e5e007d942 (diff)
downloadnextcloud-server-63eb3694e4a2d87342f8b478bd02fff79a8c73e7.tar.gz
nextcloud-server-63eb3694e4a2d87342f8b478bd02fff79a8c73e7.zip
Exclusively use the typed calendar deletion events for DAV
We had both in places, but the old one isn't used anywhere outside this app, so it's time to migrate the code. Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'apps/dav/lib')
-rw-r--r--apps/dav/lib/AppInfo/Application.php40
-rw-r--r--apps/dav/lib/CalDAV/CalDavBackend.php9
-rw-r--r--apps/dav/lib/Listener/CalendarDeletionActivityUpdaterListener.php75
-rw-r--r--apps/dav/lib/Listener/CalendarDeletionDefaultUpdaterListener.php86
-rw-r--r--apps/dav/lib/Listener/CalendarDeletionReminderUpdaterListener.php74
5 files changed, 243 insertions, 41 deletions
diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php
index 7d5b98199d7..8298a9dd6f7 100644
--- a/apps/dav/lib/AppInfo/Application.php
+++ b/apps/dav/lib/AppInfo/Application.php
@@ -40,7 +40,6 @@ use OCA\DAV\CalDAV\Activity\Provider\Event;
use OCA\DAV\CalDAV\BirthdayService;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\CalendarManager;
-use OCA\DAV\CalDAV\Reminder\Backend as ReminderBackend;
use OCA\DAV\CalDAV\Reminder\NotificationProvider\AudioProvider;
use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider;
use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider;
@@ -53,11 +52,15 @@ use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\CardDAV\ContactsManager;
use OCA\DAV\CardDAV\PhotoCache;
use OCA\DAV\CardDAV\SyncService;
+use OCA\DAV\Events\CalendarDeletedEvent;
use OCA\DAV\Events\CalendarObjectCreatedEvent;
use OCA\DAV\Events\CalendarObjectUpdatedEvent;
use OCA\DAV\Events\CalendarShareUpdatedEvent;
use OCA\DAV\HookManager;
use OCA\DAV\Listener\CalendarContactInteractionListener;
+use OCA\DAV\Listener\CalendarDeletionActivityUpdaterListener;
+use OCA\DAV\Listener\CalendarDeletionDefaultUpdaterListener;
+use OCA\DAV\Listener\CalendarDeletionReminderUpdaterListener;
use OCA\DAV\Search\ContactsSearchProvider;
use OCA\DAV\Search\EventsSearchProvider;
use OCA\DAV\Search\TasksSearchProvider;
@@ -68,7 +71,6 @@ use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\IAppContainer;
use OCP\Calendar\IManager as ICalendarManager;
use OCP\Contacts\IManager as IContactsManager;
-use OCP\IConfig;
use OCP\ILogger;
use OCP\IServerContainer;
use OCP\IUser;
@@ -77,7 +79,6 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Throwable;
use function is_null;
-use function strpos;
class Application extends App implements IBootstrap {
public const APP_ID = 'dav';
@@ -113,6 +114,9 @@ class Application extends App implements IBootstrap {
/**
* Register event listeners
*/
+ $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionActivityUpdaterListener::class);
+ $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionReminderUpdaterListener::class);
+ $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionDefaultUpdaterListener::class);
$context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarContactInteractionListener::class);
$context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarContactInteractionListener::class);
$context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class);
@@ -204,20 +208,6 @@ class Application extends App implements IBootstrap {
$event->getArgument('propertyMutations')
);
});
- $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', function (GenericEvent $event) use ($container) {
- /** @var Backend $backend */
- $backend = $container->query(Backend::class);
- $backend->onCalendarDelete(
- $event->getArgument('calendarData'),
- $event->getArgument('shares')
- );
-
- /** @var ReminderBackend $reminderBackend */
- $reminderBackend = $container->query(ReminderBackend::class);
- $reminderBackend->cleanRemindersForCalendar(
- (int) $event->getArgument('calendarId')
- );
- });
$dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateShares', function (GenericEvent $event) use ($container) {
/** @var Backend $backend */
$backend = $container->query(Backend::class);
@@ -269,22 +259,6 @@ class Application extends App implements IBootstrap {
$dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', $listener);
$dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', $listener);
- /**
- * In case the user has set their default calendar to this one
- */
- $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', function (GenericEvent $event) use ($serverContainer) {
- /** @var IConfig $config */
- $config = $serverContainer->getConfig();
- $principalUri = $event->getArgument('calendarData')['principaluri'];
- if (strpos($principalUri, 'principals/users') === 0) {
- [, $UID] = \Sabre\Uri\split($principalUri);
- $uri = $event->getArgument('calendarData')['uri'];
- if ($config->getUserValue($UID, 'dav', 'defaultCalendar') === $uri) {
- $config->deleteUserValue($UID, 'dav', 'defaultCalendar');
- }
- }
- });
-
$dispatcher->addListener('OCP\Federation\TrustedServerEvent::remove',
function (GenericEvent $event) {
/** @var CardDavBackend $cardDavBackend */
diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php
index 0a57ce17454..2196505b701 100644
--- a/apps/dav/lib/CalDAV/CalDavBackend.php
+++ b/apps/dav/lib/CalDAV/CalDavBackend.php
@@ -890,14 +890,6 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
$calendarData = $this->getCalendarById($calendarId);
$shares = $this->getShares($calendarId);
- $this->legacyDispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', new GenericEvent(
- '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar',
- [
- 'calendarId' => $calendarId,
- 'calendarData' => $calendarData,
- 'shares' => $shares,
- ]));
-
$stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `calendartype` = ?');
$stmt->execute([$calendarId, self::CALENDAR_TYPE_CALENDAR]);
@@ -915,6 +907,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_CALENDAR)))
->executeUpdate();
+ // Only dispatch if we actually deleted anything
if ($calendarData) {
$this->dispatcher->dispatchTyped(new CalendarDeletedEvent((int)$calendarId, $calendarData, $shares));
}
diff --git a/apps/dav/lib/Listener/CalendarDeletionActivityUpdaterListener.php b/apps/dav/lib/Listener/CalendarDeletionActivityUpdaterListener.php
new file mode 100644
index 00000000000..4139aba226e
--- /dev/null
+++ b/apps/dav/lib/Listener/CalendarDeletionActivityUpdaterListener.php
@@ -0,0 +1,75 @@
+<?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,
+ ]);
+ }
+ }
+}
diff --git a/apps/dav/lib/Listener/CalendarDeletionDefaultUpdaterListener.php b/apps/dav/lib/Listener/CalendarDeletionDefaultUpdaterListener.php
new file mode 100644
index 00000000000..8ffe112628d
--- /dev/null
+++ b/apps/dav/lib/Listener/CalendarDeletionDefaultUpdaterListener.php
@@ -0,0 +1,86 @@
+<?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\Events\CalendarDeletedEvent;
+use OCP\EventDispatcher\Event;
+use OCP\EventDispatcher\IEventListener;
+use OCP\IConfig;
+use Psr\Log\LoggerInterface;
+use Throwable;
+use function strpos;
+
+/**
+ * @template-implements IEventListener<\OCA\DAV\Events\CalendarDeletedEvent>
+ */
+class CalendarDeletionDefaultUpdaterListener implements IEventListener {
+
+ /** @var IConfig */
+ private $config;
+
+ /** @var LoggerInterface */
+ private $logger;
+
+ public function __construct(IConfig $config,
+ LoggerInterface $logger) {
+ $this->config = $config;
+ $this->logger = $logger;
+ }
+
+ /**
+ * In case the user has set their default calendar to the deleted one
+ */
+ public function handle(Event $event): void {
+ if (!($event instanceof CalendarDeletedEvent)) {
+ // Not what we subscribed to
+ return;
+ }
+
+ try {
+ $principalUri = $event->getCalendarData()['principaluri'];
+ if (strpos($principalUri, 'principals/users') !== 0) {
+ $this->logger->debug('Default calendar needs no update because the deleted calendar does not belong to a user principal');
+ return;
+ }
+
+ [, $uid] = \Sabre\Uri\split($principalUri);
+ $uri = $event->getCalendarData()['uri'];
+ if ($this->config->getUserValue($uid, 'dav', 'defaultCalendar') !== $uri) {
+ $this->logger->debug('Default calendar needs no update because the deleted calendar is no the user\'s default one');
+ return;
+ }
+
+ $this->config->deleteUserValue($uid, 'dav', 'defaultCalendar');
+
+ $this->logger->debug('Default user calendar reset');
+ } 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,
+ ]);
+ }
+ }
+}
diff --git a/apps/dav/lib/Listener/CalendarDeletionReminderUpdaterListener.php b/apps/dav/lib/Listener/CalendarDeletionReminderUpdaterListener.php
new file mode 100644
index 00000000000..3beecc8ff05
--- /dev/null
+++ b/apps/dav/lib/Listener/CalendarDeletionReminderUpdaterListener.php
@@ -0,0 +1,74 @@
+<?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,
+ ]);
+ }
+ }
+}