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.4KB

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