Skip to content

Commit a2e0dc0

Browse files
committed
Move createSubscription and deleteSubscription event handlers to
proper listeners Signed-off-by: Thomas Citharel <tcit@tcit.fr>
1 parent fe58a0b commit a2e0dc0

File tree

3 files changed

+168
-45
lines changed

3 files changed

+168
-45
lines changed

apps/dav/lib/AppInfo/Application.php

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,14 @@
3636
use OCA\DAV\BackgroundJob\UpdateCalendarResourcesRoomsBackgroundJob;
3737
use OCA\DAV\CalDAV\Activity\Backend;
3838
use OCA\DAV\CalDAV\BirthdayService;
39-
use OCA\DAV\CalDAV\CalDavBackend;
4039
use OCA\DAV\CalDAV\CalendarManager;
4140
use OCA\DAV\CalDAV\CalendarProvider;
42-
use OCA\DAV\CalDAV\Reminder\Backend as ReminderBackend;
4341
use OCA\DAV\CalDAV\Reminder\NotificationProvider\AudioProvider;
4442
use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider;
4543
use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider;
4644
use OCA\DAV\CalDAV\Reminder\NotificationProviderManager;
4745
use OCA\DAV\CalDAV\Reminder\Notifier;
4846

49-
use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
5047
use OCA\DAV\Capabilities;
5148
use OCA\DAV\CardDAV\CardDavBackend;
5249
use OCA\DAV\CardDAV\ContactsManager;
@@ -70,13 +67,17 @@
7067
use OCA\DAV\Events\CardCreatedEvent;
7168
use OCA\DAV\Events\CardDeletedEvent;
7269
use OCA\DAV\Events\CardUpdatedEvent;
70+
use OCA\DAV\Events\SubscriptionCreatedEvent;
71+
use OCA\DAV\Events\SubscriptionDeletedEvent;
7372
use OCA\DAV\HookManager;
7473
use OCA\DAV\Listener\ActivityUpdaterListener;
7574
use OCA\DAV\Listener\AddressbookListener;
7675
use OCA\DAV\Listener\CalendarContactInteractionListener;
7776
use OCA\DAV\Listener\CalendarDeletionDefaultUpdaterListener;
7877
use OCA\DAV\Listener\CalendarObjectReminderUpdaterListener;
7978
use OCA\DAV\Listener\CardListener;
79+
use OCA\DAV\Listener\SubscriptionCreationListener;
80+
use OCA\DAV\Listener\SubscriptionDeletionListener;
8081
use OCA\DAV\Search\ContactsSearchProvider;
8182
use OCA\DAV\Search\EventsSearchProvider;
8283
use OCA\DAV\Search\TasksSearchProvider;
@@ -153,6 +154,9 @@ public function register(IRegistrationContext $context): void {
153154
$context->registerEventListener(CalendarObjectRestoredEvent::class, CalendarObjectReminderUpdaterListener::class);
154155
$context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class);
155156

157+
$context->registerEventListener(SubscriptionCreatedEvent::class, SubscriptionCreationListener::class);
158+
$context->registerEventListener(SubscriptionDeletedEvent::class, SubscriptionDeletionListener::class);
159+
156160

157161
$context->registerEventListener(AddressBookCreatedEvent::class, AddressbookListener::class);
158162
$context->registerEventListener(AddressBookDeletedEvent::class, AddressbookListener::class);
@@ -271,48 +275,6 @@ function (GenericEvent $event) {
271275
}
272276
);
273277

274-
$dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createSubscription',
275-
function (GenericEvent $event) use ($container, $serverContainer) {
276-
$jobList = $serverContainer->getJobList();
277-
$subscriptionData = $event->getArgument('subscriptionData');
278-
279-
/**
280-
* Initial subscription refetch
281-
*
282-
* @var RefreshWebcalService $refreshWebcalService
283-
*/
284-
$refreshWebcalService = $container->query(RefreshWebcalService::class);
285-
$refreshWebcalService->refreshSubscription(
286-
(string) $subscriptionData['principaluri'],
287-
(string) $subscriptionData['uri']
288-
);
289-
290-
$jobList->add(\OCA\DAV\BackgroundJob\RefreshWebcalJob::class, [
291-
'principaluri' => $subscriptionData['principaluri'],
292-
'uri' => $subscriptionData['uri']
293-
]);
294-
}
295-
);
296-
297-
$dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription',
298-
function (GenericEvent $event) use ($container, $serverContainer) {
299-
$jobList = $serverContainer->getJobList();
300-
$subscriptionData = $event->getArgument('subscriptionData');
301-
302-
$jobList->remove(\OCA\DAV\BackgroundJob\RefreshWebcalJob::class, [
303-
'principaluri' => $subscriptionData['principaluri'],
304-
'uri' => $subscriptionData['uri']
305-
]);
306-
307-
/** @var CalDavBackend $calDavBackend */
308-
$calDavBackend = $container->query(CalDavBackend::class);
309-
$calDavBackend->purgeAllCachedEventsForSubscription($subscriptionData['id']);
310-
/** @var ReminderBackend $calDavBackend */
311-
$reminderBackend = $container->query(ReminderBackend::class);
312-
$reminderBackend->cleanRemindersForCalendar($subscriptionData['id']);
313-
}
314-
);
315-
316278
$eventHandler = function () use ($container, $serverContainer): void {
317279
try {
318280
/** @var UpdateCalendarResourcesRoomsBackgroundJob $job */
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2021 Thomas Citharel <nextcloud@tcit.fr>
7+
*
8+
* @author Thomas Citharel <nextcloud@tcit.fr>
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\BackgroundJob\RefreshWebcalJob;
29+
use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
30+
use OCA\DAV\Events\SubscriptionCreatedEvent;
31+
use OCP\BackgroundJob\IJobList;
32+
use OCP\EventDispatcher\Event;
33+
use OCP\EventDispatcher\IEventListener;
34+
use Psr\Log\LoggerInterface;
35+
36+
/**
37+
* @template-implements IEventListener<\OCA\DAV\Events\SubscriptionCreatedEvent>
38+
*/
39+
class SubscriptionCreationListener implements IEventListener {
40+
41+
/** @var IJobList */
42+
private $jobList;
43+
44+
/** @var RefreshWebcalService */
45+
private $refreshWebcalService;
46+
47+
/** @var LoggerInterface */
48+
private $logger;
49+
50+
public function __construct(IJobList $jobList, RefreshWebcalService $refreshWebcalService,
51+
LoggerInterface $logger) {
52+
$this->jobList = $jobList;
53+
$this->refreshWebcalService = $refreshWebcalService;
54+
$this->logger = $logger;
55+
}
56+
57+
/**
58+
* In case the user has set their default calendar to the deleted one
59+
*/
60+
public function handle(Event $event): void {
61+
if (!($event instanceof SubscriptionCreatedEvent)) {
62+
// Not what we subscribed to
63+
return;
64+
}
65+
66+
$subscriptionId = $event->getSubscriptionId();
67+
$subscriptionData = $event->getSubscriptionData();
68+
69+
$this->logger->debug('Refreshing webcal data for subscription ' . $subscriptionId);
70+
$this->refreshWebcalService->refreshSubscription(
71+
(string) $subscriptionData['principaluri'],
72+
(string) $subscriptionData['uri']
73+
);
74+
75+
$this->logger->debug('Scheduling webcal data refreshment for subscription ' . $subscriptionId);
76+
$this->jobList->add(RefreshWebcalJob::class, [
77+
'principaluri' => $subscriptionData['principaluri'],
78+
'uri' => $subscriptionData['uri']
79+
]);
80+
81+
}
82+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2021 Thomas Citharel <nextcloud@tcit.fr>
7+
*
8+
* @author Thomas Citharel <nextcloud@tcit.fr>
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\BackgroundJob\RefreshWebcalJob;
29+
use OCA\DAV\CalDAV\Reminder\Backend as ReminderBackend;
30+
use OCA\DAV\Events\SubscriptionDeletedEvent;
31+
use OCP\BackgroundJob\IJobList;
32+
use OCP\EventDispatcher\Event;
33+
use OCP\EventDispatcher\IEventListener;
34+
use Psr\Log\LoggerInterface;
35+
36+
/**
37+
* @template-implements IEventListener<\OCA\DAV\Events\SubscriptionDeletedEvent>
38+
*/
39+
class SubscriptionDeletionListener implements IEventListener {
40+
41+
/** @var IJobList */
42+
private $jobList;
43+
44+
/** @var ReminderBackend */
45+
private $reminderBackend;
46+
47+
/** @var LoggerInterface */
48+
private $logger;
49+
50+
public function __construct(IJobList $jobList, ReminderBackend $reminderBackend,
51+
LoggerInterface $logger) {
52+
$this->jobList = $jobList;
53+
$this->reminderBackend = $reminderBackend;
54+
$this->logger = $logger;
55+
}
56+
57+
/**
58+
* In case the user has set their default calendar to the deleted one
59+
*/
60+
public function handle(Event $event): void {
61+
if (!($event instanceof SubscriptionDeletedEvent)) {
62+
// Not what we subscribed to
63+
return;
64+
}
65+
66+
$subscriptionId = $event->getSubscriptionId();
67+
$subscriptionData = $event->getSubscriptionData();
68+
69+
$this->logger->debug('Removing refresh webcal job for subscription ' . $subscriptionId);
70+
$this->jobList->remove(RefreshWebcalJob::class, [
71+
'principaluri' => $subscriptionData['principaluri'],
72+
'uri' => $subscriptionData['uri']
73+
]);
74+
75+
$this->logger->debug('Cleaning all reminders for subscription ' . $subscriptionId);
76+
$this->reminderBackend->cleanRemindersForCalendar($subscriptionId);
77+
78+
}
79+
}

0 commit comments

Comments
 (0)