Notifier.php 8.3 KB

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