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.

PushProvider.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Thomas Citharel
  5. * @copyright Copyright (c) 2019, Georg Ehrke
  6. *
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Citharel <nextcloud@tcit.fr>
  11. * @author Richard Steinmetz <richard@steinmetz.cloud>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\DAV\CalDAV\Reminder\NotificationProvider;
  30. use OCA\DAV\AppInfo\Application;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\IConfig;
  33. use OCP\IURLGenerator;
  34. use OCP\IUser;
  35. use OCP\L10N\IFactory as L10NFactory;
  36. use OCP\Notification\IManager;
  37. use OCP\Notification\INotification;
  38. use Psr\Log\LoggerInterface;
  39. use Sabre\VObject\Component\VEvent;
  40. use Sabre\VObject\Property;
  41. /**
  42. * Class PushProvider
  43. *
  44. * @package OCA\DAV\CalDAV\Reminder\NotificationProvider
  45. */
  46. class PushProvider extends AbstractProvider {
  47. /** @var string */
  48. public const NOTIFICATION_TYPE = 'DISPLAY';
  49. /** @var IManager */
  50. private $manager;
  51. /** @var ITimeFactory */
  52. private $timeFactory;
  53. public function __construct(IConfig $config,
  54. IManager $manager,
  55. LoggerInterface $logger,
  56. L10NFactory $l10nFactory,
  57. IURLGenerator $urlGenerator,
  58. ITimeFactory $timeFactory) {
  59. parent::__construct($logger, $l10nFactory, $urlGenerator, $config);
  60. $this->manager = $manager;
  61. $this->timeFactory = $timeFactory;
  62. }
  63. /**
  64. * Send push notification to all users.
  65. *
  66. * @param VEvent $vevent
  67. * @param string|null $calendarDisplayName
  68. * @param string[] $principalEmailAddresses
  69. * @param IUser[] $users
  70. * @throws \Exception
  71. */
  72. public function send(VEvent $vevent,
  73. ?string $calendarDisplayName,
  74. array $principalEmailAddresses,
  75. array $users = []):void {
  76. if ($this->config->getAppValue('dav', 'sendEventRemindersPush', 'yes') !== 'yes') {
  77. return;
  78. }
  79. $eventDetails = $this->extractEventDetails($vevent);
  80. $eventUUID = (string) $vevent->UID;
  81. if (!$eventUUID) {
  82. return;
  83. };
  84. $eventUUIDHash = hash('sha256', $eventUUID, false);
  85. foreach ($users as $user) {
  86. $eventDetails['calendar_displayname'] = $calendarDisplayName ?? $this->getCalendarDisplayNameFallback($this->l10nFactory->getUserLanguage($user));
  87. /** @var INotification $notification */
  88. $notification = $this->manager->createNotification();
  89. $notification->setApp(Application::APP_ID)
  90. ->setUser($user->getUID())
  91. ->setDateTime($this->timeFactory->getDateTime())
  92. ->setObject(Application::APP_ID, $eventUUIDHash)
  93. ->setSubject('calendar_reminder', [
  94. 'title' => $eventDetails['title'],
  95. 'start_atom' => $eventDetails['start_atom']
  96. ])
  97. ->setMessage('calendar_reminder', $eventDetails);
  98. $this->manager->notify($notification);
  99. }
  100. }
  101. /**
  102. * @throws \Exception
  103. */
  104. protected function extractEventDetails(VEvent $vevent):array {
  105. /** @var Property\ICalendar\DateTime $start */
  106. $start = $vevent->DTSTART;
  107. $end = $this->getDTEndFromEvent($vevent);
  108. return [
  109. 'title' => isset($vevent->SUMMARY)
  110. ? ((string) $vevent->SUMMARY)
  111. : null,
  112. 'description' => isset($vevent->DESCRIPTION)
  113. ? ((string) $vevent->DESCRIPTION)
  114. : null,
  115. 'location' => isset($vevent->LOCATION)
  116. ? ((string) $vevent->LOCATION)
  117. : null,
  118. 'all_day' => $start instanceof Property\ICalendar\Date,
  119. 'start_atom' => $start->getDateTime()->format(\DateTimeInterface::ATOM),
  120. 'start_is_floating' => $start->isFloating(),
  121. 'start_timezone' => $start->getDateTime()->getTimezone()->getName(),
  122. 'end_atom' => $end->getDateTime()->format(\DateTimeInterface::ATOM),
  123. 'end_is_floating' => $end->isFloating(),
  124. 'end_timezone' => $end->getDateTime()->getTimezone()->getName(),
  125. ];
  126. }
  127. }