Notifier.php 8.7 KB

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