PushProvider.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV\Reminder\NotificationProvider;
  8. use OCA\DAV\AppInfo\Application;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\IConfig;
  11. use OCP\IURLGenerator;
  12. use OCP\IUser;
  13. use OCP\L10N\IFactory as L10NFactory;
  14. use OCP\Notification\IManager;
  15. use OCP\Notification\INotification;
  16. use Psr\Log\LoggerInterface;
  17. use Sabre\VObject\Component\VEvent;
  18. use Sabre\VObject\Property;
  19. /**
  20. * Class PushProvider
  21. *
  22. * @package OCA\DAV\CalDAV\Reminder\NotificationProvider
  23. */
  24. class PushProvider extends AbstractProvider {
  25. /** @var string */
  26. public const NOTIFICATION_TYPE = 'DISPLAY';
  27. public function __construct(
  28. IConfig $config,
  29. private IManager $manager,
  30. LoggerInterface $logger,
  31. L10NFactory $l10nFactory,
  32. IURLGenerator $urlGenerator,
  33. private ITimeFactory $timeFactory,
  34. ) {
  35. parent::__construct($logger, $l10nFactory, $urlGenerator, $config);
  36. }
  37. /**
  38. * Send push notification to all users.
  39. *
  40. * @param VEvent $vevent
  41. * @param string|null $calendarDisplayName
  42. * @param string[] $principalEmailAddresses
  43. * @param IUser[] $users
  44. * @throws \Exception
  45. */
  46. public function send(VEvent $vevent,
  47. ?string $calendarDisplayName,
  48. array $principalEmailAddresses,
  49. array $users = []):void {
  50. if ($this->config->getAppValue('dav', 'sendEventRemindersPush', 'yes') !== 'yes') {
  51. return;
  52. }
  53. $eventDetails = $this->extractEventDetails($vevent);
  54. $eventUUID = (string)$vevent->UID;
  55. if (!$eventUUID) {
  56. return;
  57. };
  58. $eventUUIDHash = hash('sha256', $eventUUID, false);
  59. foreach ($users as $user) {
  60. $eventDetails['calendar_displayname'] = $calendarDisplayName ?? $this->getCalendarDisplayNameFallback($this->l10nFactory->getUserLanguage($user));
  61. /** @var INotification $notification */
  62. $notification = $this->manager->createNotification();
  63. $notification->setApp(Application::APP_ID)
  64. ->setUser($user->getUID())
  65. ->setDateTime($this->timeFactory->getDateTime())
  66. ->setObject(Application::APP_ID, $eventUUIDHash)
  67. ->setSubject('calendar_reminder', [
  68. 'title' => $eventDetails['title'],
  69. 'start_atom' => $eventDetails['start_atom']
  70. ])
  71. ->setMessage('calendar_reminder', $eventDetails);
  72. $this->manager->notify($notification);
  73. }
  74. }
  75. /**
  76. * @throws \Exception
  77. */
  78. protected function extractEventDetails(VEvent $vevent):array {
  79. /** @var Property\ICalendar\DateTime $start */
  80. $start = $vevent->DTSTART;
  81. $end = $this->getDTEndFromEvent($vevent);
  82. return [
  83. 'title' => isset($vevent->SUMMARY)
  84. ? ((string)$vevent->SUMMARY)
  85. : null,
  86. 'description' => isset($vevent->DESCRIPTION)
  87. ? ((string)$vevent->DESCRIPTION)
  88. : null,
  89. 'location' => isset($vevent->LOCATION)
  90. ? ((string)$vevent->LOCATION)
  91. : null,
  92. 'all_day' => $start instanceof Property\ICalendar\Date,
  93. 'start_atom' => $start->getDateTime()->format(\DateTimeInterface::ATOM),
  94. 'start_is_floating' => $start->isFloating(),
  95. 'start_timezone' => $start->getDateTime()->getTimezone()->getName(),
  96. 'end_atom' => $end->getDateTime()->format(\DateTimeInterface::ATOM),
  97. 'end_is_floating' => $end->isFloating(),
  98. 'end_timezone' => $end->getDateTime()->getTimezone()->getName(),
  99. ];
  100. }
  101. }