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

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