You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SubscriptionListener.php 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2022 Thomas Citharel <nextcloud@tcit.fr>
  5. *
  6. * @author Thomas Citharel <nextcloud@tcit.fr>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Listener;
  25. use OCA\DAV\BackgroundJob\RefreshWebcalJob;
  26. use OCA\DAV\CalDAV\Reminder\Backend as ReminderBackend;
  27. use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
  28. use OCA\DAV\Events\SubscriptionCreatedEvent;
  29. use OCA\DAV\Events\SubscriptionDeletedEvent;
  30. use OCP\BackgroundJob\IJobList;
  31. use OCP\EventDispatcher\Event;
  32. use OCP\EventDispatcher\IEventListener;
  33. use Psr\Log\LoggerInterface;
  34. /** @template-implements IEventListener<SubscriptionCreatedEvent|SubscriptionDeletedEvent> */
  35. class SubscriptionListener implements IEventListener {
  36. private IJobList $jobList;
  37. private RefreshWebcalService $refreshWebcalService;
  38. private ReminderBackend $reminderBackend;
  39. private LoggerInterface $logger;
  40. public function __construct(IJobList $jobList, RefreshWebcalService $refreshWebcalService, ReminderBackend $reminderBackend,
  41. LoggerInterface $logger) {
  42. $this->jobList = $jobList;
  43. $this->refreshWebcalService = $refreshWebcalService;
  44. $this->reminderBackend = $reminderBackend;
  45. $this->logger = $logger;
  46. }
  47. /**
  48. * In case the user has set their default calendar to the deleted one
  49. */
  50. public function handle(Event $event): void {
  51. if ($event instanceof SubscriptionCreatedEvent) {
  52. $subscriptionId = $event->getSubscriptionId();
  53. $subscriptionData = $event->getSubscriptionData();
  54. $this->logger->debug('Refreshing webcal data for subscription ' . $subscriptionId);
  55. $this->refreshWebcalService->refreshSubscription(
  56. (string)$subscriptionData['principaluri'],
  57. (string)$subscriptionData['uri']
  58. );
  59. $this->logger->debug('Scheduling webcal data refreshment for subscription ' . $subscriptionId);
  60. $this->jobList->add(RefreshWebcalJob::class, [
  61. 'principaluri' => $subscriptionData['principaluri'],
  62. 'uri' => $subscriptionData['uri']
  63. ]);
  64. } elseif ($event instanceof SubscriptionDeletedEvent) {
  65. $subscriptionId = $event->getSubscriptionId();
  66. $subscriptionData = $event->getSubscriptionData();
  67. $this->logger->debug('Removing refresh webcal job for subscription ' . $subscriptionId);
  68. $this->jobList->remove(RefreshWebcalJob::class, [
  69. 'principaluri' => $subscriptionData['principaluri'],
  70. 'uri' => $subscriptionData['uri']
  71. ]);
  72. $this->logger->debug('Cleaning all reminders for subscription ' . $subscriptionId);
  73. $this->reminderBackend->cleanRemindersForCalendar($subscriptionId);
  74. }
  75. }
  76. }