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.

Notifier.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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;
  28. use DateTime;
  29. use OCA\DAV\AppInfo\Application;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\IL10N;
  32. use OCP\IURLGenerator;
  33. use OCP\L10N\IFactory;
  34. use OCP\Notification\INotification;
  35. use OCP\Notification\INotifier;
  36. /**
  37. * Class Notifier
  38. *
  39. * @package OCA\DAV\CalDAV\Reminder
  40. */
  41. class Notifier implements INotifier {
  42. /** @var IFactory */
  43. private $l10nFactory;
  44. /** @var IURLGenerator */
  45. private $urlGenerator;
  46. /** @var IL10N */
  47. private $l10n;
  48. /** @var ITimeFactory */
  49. private $timeFactory;
  50. /**
  51. * Notifier constructor.
  52. *
  53. * @param IFactory $factory
  54. * @param IURLGenerator $urlGenerator
  55. * @param ITimeFactory $timeFactory
  56. */
  57. public function __construct(IFactory $factory,
  58. IURLGenerator $urlGenerator,
  59. ITimeFactory $timeFactory) {
  60. $this->l10nFactory = $factory;
  61. $this->urlGenerator = $urlGenerator;
  62. $this->timeFactory = $timeFactory;
  63. }
  64. /**
  65. * Identifier of the notifier, only use [a-z0-9_]
  66. *
  67. * @return string
  68. * @since 17.0.0
  69. */
  70. public function getID():string {
  71. return Application::APP_ID;
  72. }
  73. /**
  74. * Human readable name describing the notifier
  75. *
  76. * @return string
  77. * @since 17.0.0
  78. */
  79. public function getName():string {
  80. return $this->l10nFactory->get('dav')->t('Calendar');
  81. }
  82. /**
  83. * Prepare sending the notification
  84. *
  85. * @param INotification $notification
  86. * @param string $languageCode The code of the language that should be used to prepare the notification
  87. * @return INotification
  88. * @throws \Exception
  89. */
  90. public function prepare(INotification $notification,
  91. string $languageCode):INotification {
  92. if ($notification->getApp() !== Application::APP_ID) {
  93. throw new \InvalidArgumentException('Notification not from this app');
  94. }
  95. // Read the language from the notification
  96. $this->l10n = $this->l10nFactory->get('dav', $languageCode);
  97. // Handle notifier subjects
  98. switch($notification->getSubject()) {
  99. case 'calendar_reminder':
  100. return $this->prepareReminderNotification($notification);
  101. default:
  102. throw new \InvalidArgumentException('Unknown subject');
  103. }
  104. }
  105. /**
  106. * @param INotification $notification
  107. * @return INotification
  108. */
  109. private function prepareReminderNotification(INotification $notification):INotification {
  110. $imagePath = $this->urlGenerator->imagePath('core', 'places/calendar.svg');
  111. $iconUrl = $this->urlGenerator->getAbsoluteURL($imagePath);
  112. $notification->setIcon($iconUrl);
  113. $this->prepareNotificationSubject($notification);
  114. $this->prepareNotificationMessage($notification);
  115. return $notification;
  116. }
  117. /**
  118. * Sets the notification subject based on the parameters set in PushProvider
  119. *
  120. * @param INotification $notification
  121. */
  122. private function prepareNotificationSubject(INotification $notification): void {
  123. $parameters = $notification->getSubjectParameters();
  124. $startTime = \DateTime::createFromFormat(\DateTime::ATOM, $parameters['start_atom']);
  125. $now = $this->timeFactory->getDateTime();
  126. $title = $this->getTitleFromParameters($parameters);
  127. $diff = $startTime->diff($now);
  128. if ($diff === false) {
  129. return;
  130. }
  131. $components = [];
  132. if ($diff->y) {
  133. $components[] = $this->l10n->n('%n year', '%n years', $diff->y);
  134. }
  135. if ($diff->m) {
  136. $components[] = $this->l10n->n('%n month', '%n months', $diff->m);
  137. }
  138. if ($diff->d) {
  139. $components[] = $this->l10n->n('%n day', '%n days', $diff->d);
  140. }
  141. if ($diff->h) {
  142. $components[] = $this->l10n->n('%n hour', '%n hours', $diff->h);
  143. }
  144. if ($diff->i) {
  145. $components[] = $this->l10n->n('%n minute', '%n minutes', $diff->i);
  146. }
  147. // Limiting to the first three components to prevent
  148. // the string from getting too long
  149. $firstThreeComponents = array_slice($components, 0, 2);
  150. $diffLabel = implode(', ', $firstThreeComponents);
  151. if ($diff->invert) {
  152. $title = $this->l10n->t('%s (in %s)', [$title, $diffLabel]);
  153. } else {
  154. $title = $this->l10n->t('%s (%s ago)', [$title, $diffLabel]);
  155. }
  156. $notification->setParsedSubject($title);
  157. }
  158. /**
  159. * Sets the notification message based on the parameters set in PushProvider
  160. *
  161. * @param INotification $notification
  162. */
  163. private function prepareNotificationMessage(INotification $notification): void {
  164. $parameters = $notification->getMessageParameters();
  165. $description = [
  166. $this->l10n->t('Calendar: %s', $parameters['calendar_displayname']),
  167. $this->l10n->t('Date: %s', $this->generateDateString($parameters)),
  168. ];
  169. if ($parameters['description']) {
  170. $description[] = $this->l10n->t('Description: %s', $parameters['description']);
  171. }
  172. if ($parameters['location']) {
  173. $description[] = $this->l10n->t('Where: %s', $parameters['location']);
  174. }
  175. $message = implode("\r\n", $description);
  176. $notification->setParsedMessage($message);
  177. }
  178. /**
  179. * @param array $parameters
  180. * @return string
  181. */
  182. private function getTitleFromParameters(array $parameters):string {
  183. return $parameters['title'] ?? $this->l10n->t('Untitled event');
  184. }
  185. /**
  186. * @param array $parameters
  187. * @return string
  188. * @throws \Exception
  189. */
  190. private function generateDateString(array $parameters):string {
  191. $startDateTime = DateTime::createFromFormat(\DateTime::ATOM, $parameters['start_atom']);
  192. $endDateTime = DateTime::createFromFormat(\DateTime::ATOM, $parameters['end_atom']);
  193. $isAllDay = $parameters['all_day'];
  194. $diff = $startDateTime->diff($endDateTime);
  195. if ($isAllDay) {
  196. // One day event
  197. if ($diff->days === 1) {
  198. return $this->getDateString($startDateTime);
  199. }
  200. return implode(' - ', [
  201. $this->getDateString($startDateTime),
  202. $this->getDateString($endDateTime),
  203. ]);
  204. }
  205. $startTimezone = $endTimezone = null;
  206. if (!$parameters['start_is_floating']) {
  207. $startTimezone = $parameters['start_timezone'];
  208. $endTimezone = $parameters['end_timezone'];
  209. }
  210. $localeStart = implode(', ', [
  211. $this->getWeekDayName($startDateTime),
  212. $this->getDateTimeString($startDateTime)
  213. ]);
  214. // always show full date with timezone if timezones are different
  215. if ($startTimezone !== $endTimezone) {
  216. $localeEnd = implode(', ', [
  217. $this->getWeekDayName($endDateTime),
  218. $this->getDateTimeString($endDateTime)
  219. ]);
  220. return $localeStart
  221. . ' (' . $startTimezone . ') '
  222. . ' - '
  223. . $localeEnd
  224. . ' (' . $endTimezone . ')';
  225. }
  226. // Show only the time if the day is the same
  227. $localeEnd = $this->isDayEqual($startDateTime, $endDateTime)
  228. ? $this->getTimeString($endDateTime)
  229. : implode(', ', [
  230. $this->getWeekDayName($endDateTime),
  231. $this->getDateTimeString($endDateTime)
  232. ]);
  233. return $localeStart
  234. . ' - '
  235. . $localeEnd
  236. . ' (' . $startTimezone . ')';
  237. }
  238. /**
  239. * @param DateTime $dtStart
  240. * @param DateTime $dtEnd
  241. * @return bool
  242. */
  243. private function isDayEqual(DateTime $dtStart,
  244. DateTime $dtEnd):bool {
  245. return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d');
  246. }
  247. /**
  248. * @param DateTime $dt
  249. * @return string
  250. */
  251. private function getWeekDayName(DateTime $dt):string {
  252. return $this->l10n->l('weekdayName', $dt, ['width' => 'abbreviated']);
  253. }
  254. /**
  255. * @param DateTime $dt
  256. * @return string
  257. */
  258. private function getDateString(DateTime $dt):string {
  259. return $this->l10n->l('date', $dt, ['width' => 'medium']);
  260. }
  261. /**
  262. * @param DateTime $dt
  263. * @return string
  264. */
  265. private function getDateTimeString(DateTime $dt):string {
  266. return $this->l10n->l('datetime', $dt, ['width' => 'medium|short']);
  267. }
  268. /**
  269. * @param DateTime $dt
  270. * @return string
  271. */
  272. private function getTimeString(DateTime $dt):string {
  273. return $this->l10n->l('time', $dt, ['width' => 'short']);
  274. }
  275. }