PushProvider.php 4.3 KB

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