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.

AbstractProvider.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 Joas Schilling <coding@schilljs.com>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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\CalDAV\Reminder\INotificationProvider;
  30. use OCP\IConfig;
  31. use OCP\IL10N;
  32. use OCP\IURLGenerator;
  33. use OCP\IUser;
  34. use OCP\L10N\IFactory as L10NFactory;
  35. use Psr\Log\LoggerInterface;
  36. use Sabre\VObject\Component\VEvent;
  37. use Sabre\VObject\DateTimeParser;
  38. use Sabre\VObject\Property;
  39. /**
  40. * Class AbstractProvider
  41. *
  42. * @package OCA\DAV\CalDAV\Reminder\NotificationProvider
  43. */
  44. abstract class AbstractProvider implements INotificationProvider {
  45. /** @var string */
  46. public const NOTIFICATION_TYPE = '';
  47. protected LoggerInterface $logger;
  48. /** @var L10NFactory */
  49. protected $l10nFactory;
  50. /** @var IL10N[] */
  51. private $l10ns;
  52. /** @var string */
  53. private $fallbackLanguage;
  54. /** @var IURLGenerator */
  55. protected $urlGenerator;
  56. /** @var IConfig */
  57. protected $config;
  58. public function __construct(LoggerInterface $logger,
  59. L10NFactory $l10nFactory,
  60. IURLGenerator $urlGenerator,
  61. IConfig $config) {
  62. $this->logger = $logger;
  63. $this->l10nFactory = $l10nFactory;
  64. $this->urlGenerator = $urlGenerator;
  65. $this->config = $config;
  66. }
  67. /**
  68. * Send notification
  69. *
  70. * @param VEvent $vevent
  71. * @param string $calendarDisplayName
  72. * @param IUser[] $users
  73. * @return void
  74. */
  75. abstract public function send(VEvent $vevent,
  76. string $calendarDisplayName,
  77. array $users = []): void;
  78. /**
  79. * @return string
  80. */
  81. protected function getFallbackLanguage():string {
  82. if ($this->fallbackLanguage) {
  83. return $this->fallbackLanguage;
  84. }
  85. $fallbackLanguage = $this->l10nFactory->findGenericLanguage();
  86. $this->fallbackLanguage = $fallbackLanguage;
  87. return $fallbackLanguage;
  88. }
  89. /**
  90. * @param string $lang
  91. * @return bool
  92. */
  93. protected function hasL10NForLang(string $lang):bool {
  94. return $this->l10nFactory->languageExists('dav', $lang);
  95. }
  96. /**
  97. * @param string $lang
  98. * @return IL10N
  99. */
  100. protected function getL10NForLang(string $lang):IL10N {
  101. if (isset($this->l10ns[$lang])) {
  102. return $this->l10ns[$lang];
  103. }
  104. $l10n = $this->l10nFactory->get('dav', $lang);
  105. $this->l10ns[$lang] = $l10n;
  106. return $l10n;
  107. }
  108. /**
  109. * @param VEvent $vevent
  110. * @return string
  111. */
  112. private function getStatusOfEvent(VEvent $vevent):string {
  113. if ($vevent->STATUS) {
  114. return (string) $vevent->STATUS;
  115. }
  116. // Doesn't say so in the standard,
  117. // but we consider events without a status
  118. // to be confirmed
  119. return 'CONFIRMED';
  120. }
  121. /**
  122. * @param VEvent $vevent
  123. * @return bool
  124. */
  125. protected function isEventTentative(VEvent $vevent):bool {
  126. return $this->getStatusOfEvent($vevent) === 'TENTATIVE';
  127. }
  128. /**
  129. * @param VEvent $vevent
  130. * @return Property\ICalendar\DateTime
  131. */
  132. protected function getDTEndFromEvent(VEvent $vevent):Property\ICalendar\DateTime {
  133. if (isset($vevent->DTEND)) {
  134. return $vevent->DTEND;
  135. }
  136. if (isset($vevent->DURATION)) {
  137. $isFloating = $vevent->DTSTART->isFloating();
  138. /** @var Property\ICalendar\DateTime $end */
  139. $end = clone $vevent->DTSTART;
  140. $endDateTime = $end->getDateTime();
  141. $endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
  142. $end->setDateTime($endDateTime, $isFloating);
  143. return $end;
  144. }
  145. if (!$vevent->DTSTART->hasTime()) {
  146. $isFloating = $vevent->DTSTART->isFloating();
  147. /** @var Property\ICalendar\DateTime $end */
  148. $end = clone $vevent->DTSTART;
  149. $endDateTime = $end->getDateTime();
  150. $endDateTime = $endDateTime->modify('+1 day');
  151. $end->setDateTime($endDateTime, $isFloating);
  152. return $end;
  153. }
  154. return clone $vevent->DTSTART;
  155. }
  156. }