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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * @author Richard Steinmetz <richard@steinmetz.cloud>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\DAV\CalDAV\Reminder\NotificationProvider;
  30. use OCA\DAV\CalDAV\Reminder\INotificationProvider;
  31. use OCP\IConfig;
  32. use OCP\IL10N;
  33. use OCP\IURLGenerator;
  34. use OCP\IUser;
  35. use OCP\L10N\IFactory as L10NFactory;
  36. use Psr\Log\LoggerInterface;
  37. use Sabre\VObject\Component\VEvent;
  38. use Sabre\VObject\DateTimeParser;
  39. use Sabre\VObject\Property;
  40. /**
  41. * Class AbstractProvider
  42. *
  43. * @package OCA\DAV\CalDAV\Reminder\NotificationProvider
  44. */
  45. abstract class AbstractProvider implements INotificationProvider {
  46. /** @var string */
  47. public const NOTIFICATION_TYPE = '';
  48. protected LoggerInterface $logger;
  49. /** @var L10NFactory */
  50. protected $l10nFactory;
  51. /** @var IL10N[] */
  52. private $l10ns;
  53. /** @var string */
  54. private $fallbackLanguage;
  55. /** @var IURLGenerator */
  56. protected $urlGenerator;
  57. /** @var IConfig */
  58. protected $config;
  59. public function __construct(LoggerInterface $logger,
  60. L10NFactory $l10nFactory,
  61. IURLGenerator $urlGenerator,
  62. IConfig $config) {
  63. $this->logger = $logger;
  64. $this->l10nFactory = $l10nFactory;
  65. $this->urlGenerator = $urlGenerator;
  66. $this->config = $config;
  67. }
  68. /**
  69. * Send notification
  70. *
  71. * @param VEvent $vevent
  72. * @param string|null $calendarDisplayName
  73. * @param string[] $principalEmailAddresses
  74. * @param IUser[] $users
  75. * @return void
  76. */
  77. abstract public function send(VEvent $vevent,
  78. ?string $calendarDisplayName,
  79. array $principalEmailAddresses,
  80. array $users = []): void;
  81. /**
  82. * @return string
  83. */
  84. protected function getFallbackLanguage():string {
  85. if ($this->fallbackLanguage) {
  86. return $this->fallbackLanguage;
  87. }
  88. $fallbackLanguage = $this->l10nFactory->findGenericLanguage();
  89. $this->fallbackLanguage = $fallbackLanguage;
  90. return $fallbackLanguage;
  91. }
  92. /**
  93. * @param string $lang
  94. * @return bool
  95. */
  96. protected function hasL10NForLang(string $lang):bool {
  97. return $this->l10nFactory->languageExists('dav', $lang);
  98. }
  99. /**
  100. * @param string $lang
  101. * @return IL10N
  102. */
  103. protected function getL10NForLang(string $lang):IL10N {
  104. if (isset($this->l10ns[$lang])) {
  105. return $this->l10ns[$lang];
  106. }
  107. $l10n = $this->l10nFactory->get('dav', $lang);
  108. $this->l10ns[$lang] = $l10n;
  109. return $l10n;
  110. }
  111. /**
  112. * @param VEvent $vevent
  113. * @return string
  114. */
  115. private function getStatusOfEvent(VEvent $vevent):string {
  116. if ($vevent->STATUS) {
  117. return (string) $vevent->STATUS;
  118. }
  119. // Doesn't say so in the standard,
  120. // but we consider events without a status
  121. // to be confirmed
  122. return 'CONFIRMED';
  123. }
  124. /**
  125. * @param VEvent $vevent
  126. * @return bool
  127. */
  128. protected function isEventTentative(VEvent $vevent):bool {
  129. return $this->getStatusOfEvent($vevent) === 'TENTATIVE';
  130. }
  131. /**
  132. * @param VEvent $vevent
  133. * @return Property\ICalendar\DateTime
  134. */
  135. protected function getDTEndFromEvent(VEvent $vevent):Property\ICalendar\DateTime {
  136. if (isset($vevent->DTEND)) {
  137. return $vevent->DTEND;
  138. }
  139. if (isset($vevent->DURATION)) {
  140. $isFloating = $vevent->DTSTART->isFloating();
  141. /** @var Property\ICalendar\DateTime $end */
  142. $end = clone $vevent->DTSTART;
  143. $endDateTime = $end->getDateTime();
  144. $endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
  145. $end->setDateTime($endDateTime, $isFloating);
  146. return $end;
  147. }
  148. if (!$vevent->DTSTART->hasTime()) {
  149. $isFloating = $vevent->DTSTART->isFloating();
  150. /** @var Property\ICalendar\DateTime $end */
  151. $end = clone $vevent->DTSTART;
  152. $endDateTime = $end->getDateTime();
  153. $endDateTime = $endDateTime->modify('+1 day');
  154. $end->setDateTime($endDateTime, $isFloating);
  155. return $end;
  156. }
  157. return clone $vevent->DTSTART;
  158. }
  159. protected function getCalendarDisplayNameFallback(string $lang): string {
  160. return $this->getL10NForLang($lang)->t('Untitled calendar');
  161. }
  162. }