Notifier.php 8.5 KB

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