PushProvider.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /** @var IManager */
  28. private $manager;
  29. /** @var ITimeFactory */
  30. private $timeFactory;
  31. public function __construct(IConfig $config,
  32. IManager $manager,
  33. LoggerInterface $logger,
  34. L10NFactory $l10nFactory,
  35. IURLGenerator $urlGenerator,
  36. ITimeFactory $timeFactory) {
  37. parent::__construct($logger, $l10nFactory, $urlGenerator, $config);
  38. $this->manager = $manager;
  39. $this->timeFactory = $timeFactory;
  40. }
  41. /**
  42. * Send push notification to all users.
  43. *
  44. * @param VEvent $vevent
  45. * @param string|null $calendarDisplayName
  46. * @param string[] $principalEmailAddresses
  47. * @param IUser[] $users
  48. * @throws \Exception
  49. */
  50. public function send(VEvent $vevent,
  51. ?string $calendarDisplayName,
  52. array $principalEmailAddresses,
  53. array $users = []):void {
  54. if ($this->config->getAppValue('dav', 'sendEventRemindersPush', 'yes') !== 'yes') {
  55. return;
  56. }
  57. $eventDetails = $this->extractEventDetails($vevent);
  58. $eventUUID = (string) $vevent->UID;
  59. if (!$eventUUID) {
  60. return;
  61. };
  62. $eventUUIDHash = hash('sha256', $eventUUID, false);
  63. foreach ($users as $user) {
  64. $eventDetails['calendar_displayname'] = $calendarDisplayName ?? $this->getCalendarDisplayNameFallback($this->l10nFactory->getUserLanguage($user));
  65. /** @var INotification $notification */
  66. $notification = $this->manager->createNotification();
  67. $notification->setApp(Application::APP_ID)
  68. ->setUser($user->getUID())
  69. ->setDateTime($this->timeFactory->getDateTime())
  70. ->setObject(Application::APP_ID, $eventUUIDHash)
  71. ->setSubject('calendar_reminder', [
  72. 'title' => $eventDetails['title'],
  73. 'start_atom' => $eventDetails['start_atom']
  74. ])
  75. ->setMessage('calendar_reminder', $eventDetails);
  76. $this->manager->notify($notification);
  77. }
  78. }
  79. /**
  80. * @throws \Exception
  81. */
  82. protected function extractEventDetails(VEvent $vevent):array {
  83. /** @var Property\ICalendar\DateTime $start */
  84. $start = $vevent->DTSTART;
  85. $end = $this->getDTEndFromEvent($vevent);
  86. return [
  87. 'title' => isset($vevent->SUMMARY)
  88. ? ((string) $vevent->SUMMARY)
  89. : null,
  90. 'description' => isset($vevent->DESCRIPTION)
  91. ? ((string) $vevent->DESCRIPTION)
  92. : null,
  93. 'location' => isset($vevent->LOCATION)
  94. ? ((string) $vevent->LOCATION)
  95. : null,
  96. 'all_day' => $start instanceof Property\ICalendar\Date,
  97. 'start_atom' => $start->getDateTime()->format(\DateTimeInterface::ATOM),
  98. 'start_is_floating' => $start->isFloating(),
  99. 'start_timezone' => $start->getDateTime()->getTimezone()->getName(),
  100. 'end_atom' => $end->getDateTime()->format(\DateTimeInterface::ATOM),
  101. 'end_is_floating' => $end->isFloating(),
  102. 'end_timezone' => $end->getDateTime()->getTimezone()->getName(),
  103. ];
  104. }
  105. }