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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\IURLGenerator;
  33. use OCP\IUser;
  34. use OCP\L10N\IFactory as L10NFactory;
  35. use OCP\Notification\IManager;
  36. use OCP\Notification\INotification;
  37. use Psr\Log\LoggerInterface;
  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. public function __construct(IConfig $config,
  53. IManager $manager,
  54. LoggerInterface $logger,
  55. L10NFactory $l10nFactory,
  56. IURLGenerator $urlGenerator,
  57. ITimeFactory $timeFactory) {
  58. parent::__construct($logger, $l10nFactory, $urlGenerator, $config);
  59. $this->manager = $manager;
  60. $this->timeFactory = $timeFactory;
  61. }
  62. /**
  63. * Send push notification to all users.
  64. *
  65. * @param VEvent $vevent
  66. * @param string $calendarDisplayName
  67. * @param IUser[] $users
  68. * @throws \Exception
  69. */
  70. public function send(VEvent $vevent,
  71. string $calendarDisplayName = null,
  72. array $users = []):void {
  73. if ($this->config->getAppValue('dav', 'sendEventRemindersPush', 'no') !== 'yes') {
  74. return;
  75. }
  76. $eventDetails = $this->extractEventDetails($vevent);
  77. $eventDetails['calendar_displayname'] = $calendarDisplayName;
  78. $eventUUID = (string) $vevent->UID;
  79. if (!$eventUUID) {
  80. return;
  81. };
  82. $eventUUIDHash = hash('sha256', $eventUUID, false);
  83. foreach ($users as $user) {
  84. /** @var INotification $notification */
  85. $notification = $this->manager->createNotification();
  86. $notification->setApp(Application::APP_ID)
  87. ->setUser($user->getUID())
  88. ->setDateTime($this->timeFactory->getDateTime())
  89. ->setObject(Application::APP_ID, $eventUUIDHash)
  90. ->setSubject('calendar_reminder', [
  91. 'title' => $eventDetails['title'],
  92. 'start_atom' => $eventDetails['start_atom']
  93. ])
  94. ->setMessage('calendar_reminder', $eventDetails);
  95. $this->manager->notify($notification);
  96. }
  97. }
  98. /**
  99. * @var VEvent $vevent
  100. * @return array
  101. * @throws \Exception
  102. */
  103. protected function extractEventDetails(VEvent $vevent):array {
  104. /** @var Property\ICalendar\DateTime $start */
  105. $start = $vevent->DTSTART;
  106. $end = $this->getDTEndFromEvent($vevent);
  107. return [
  108. 'title' => isset($vevent->SUMMARY)
  109. ? ((string) $vevent->SUMMARY)
  110. : null,
  111. 'description' => isset($vevent->DESCRIPTION)
  112. ? ((string) $vevent->DESCRIPTION)
  113. : null,
  114. 'location' => isset($vevent->LOCATION)
  115. ? ((string) $vevent->LOCATION)
  116. : null,
  117. 'all_day' => $start instanceof Property\ICalendar\Date,
  118. 'start_atom' => $start->getDateTime()->format(\DateTimeInterface::ATOM),
  119. 'start_is_floating' => $start->isFloating(),
  120. 'start_timezone' => $start->getDateTime()->getTimezone()->getName(),
  121. 'end_atom' => $end->getDateTime()->format(\DateTimeInterface::ATOM),
  122. 'end_is_floating' => $end->isFloating(),
  123. 'end_timezone' => $end->getDateTime()->getTimezone()->getName(),
  124. ];
  125. }
  126. }