aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/Listener/SubscriptionListener.php
blob: fc9dfcf122db029e010b6d67be54ab044afd0aa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\DAV\Listener;

use OCA\DAV\BackgroundJob\RefreshWebcalJob;
use OCA\DAV\CalDAV\Reminder\Backend as ReminderBackend;
use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
use OCA\DAV\Events\SubscriptionCreatedEvent;
use OCA\DAV\Events\SubscriptionDeletedEvent;
use OCP\BackgroundJob\IJobList;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use Psr\Log\LoggerInterface;

/** @template-implements IEventListener<SubscriptionCreatedEvent|SubscriptionDeletedEvent> */
class SubscriptionListener implements IEventListener {
	public function __construct(
		private IJobList $jobList,
		private RefreshWebcalService $refreshWebcalService,
		private ReminderBackend $reminderBackend,
		private LoggerInterface $logger,
	) {
	}

	/**
	 * In case the user has set their default calendar to the deleted one
	 */
	public function handle(Event $event): void {
		if ($event instanceof SubscriptionCreatedEvent) {
			$subscriptionId = $event->getSubscriptionId();
			$subscriptionData = $event->getSubscriptionData();

			$this->logger->debug('Refreshing webcal data for subscription ' . $subscriptionId);
			$this->refreshWebcalService->refreshSubscription(
				(string)$subscriptionData['principaluri'],
				(string)$subscriptionData['uri']
			);

			$this->logger->debug('Scheduling webcal data refreshment for subscription ' . $subscriptionId);
			$this->jobList->add(RefreshWebcalJob::class, [
				'principaluri' => $subscriptionData['principaluri'],
				'uri' => $subscriptionData['uri']
			]);
		} elseif ($event instanceof SubscriptionDeletedEvent) {
			$subscriptionId = $event->getSubscriptionId();
			$subscriptionData = $event->getSubscriptionData();

			$this->logger->debug('Removing refresh webcal job for subscription ' . $subscriptionId);
			$this->jobList->remove(RefreshWebcalJob::class, [
				'principaluri' => $subscriptionData['principaluri'],
				'uri' => $subscriptionData['uri']
			]);

			$this->logger->debug('Cleaning all reminders for subscription ' . $subscriptionId);
			$this->reminderBackend->cleanRemindersForCalendar($subscriptionId);
		}
	}
}