2019-03-16 18:19:25 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2019-08-09 21:25:21 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2019-03-16 18:19:25 +03:00
|
|
|
/**
|
2019-08-09 21:25:21 +03:00
|
|
|
* @copyright Copyright (c) 2019, Thomas Citharel
|
|
|
|
* @copyright Copyright (c) 2019, Georg Ehrke
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
2019-08-09 21:25:21 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
* @author Thomas Citharel <tcit@tcit.fr>
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @license GNU AGPL version 3 or any later version
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2019-12-03 21:57:53 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-03-16 18:19:25 +03:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\CalDAV\Reminder;
|
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use DateTime;
|
2019-03-16 18:19:25 +03:00
|
|
|
use OCA\DAV\AppInfo\Application;
|
2019-08-09 21:25:21 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2019-03-16 18:19:25 +03:00
|
|
|
use OCP\IL10N;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IURLGenerator;
|
2019-03-16 18:19:25 +03:00
|
|
|
use OCP\L10N\IFactory;
|
|
|
|
use OCP\Notification\INotification;
|
|
|
|
use OCP\Notification\INotifier;
|
|
|
|
|
2019-08-09 21:25:21 +03:00
|
|
|
/**
|
|
|
|
* Class Notifier
|
|
|
|
*
|
|
|
|
* @package OCA\DAV\CalDAV\Reminder
|
|
|
|
*/
|
2019-03-16 18:19:25 +03:00
|
|
|
class Notifier implements INotifier {
|
|
|
|
|
|
|
|
/** @var IFactory */
|
2019-08-09 21:25:21 +03:00
|
|
|
private $l10nFactory;
|
2019-03-16 18:19:25 +03:00
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
2019-08-09 21:25:21 +03:00
|
|
|
private $urlGenerator;
|
2019-03-16 18:19:25 +03:00
|
|
|
|
|
|
|
/** @var IL10N */
|
2019-08-09 21:25:21 +03:00
|
|
|
private $l10n;
|
|
|
|
|
|
|
|
/** @var ITimeFactory */
|
|
|
|
private $timeFactory;
|
2019-03-16 18:19:25 +03:00
|
|
|
|
2019-08-09 21:25:21 +03:00
|
|
|
/**
|
|
|
|
* Notifier constructor.
|
|
|
|
*
|
|
|
|
* @param IFactory $factory
|
|
|
|
* @param IURLGenerator $urlGenerator
|
|
|
|
* @param ITimeFactory $timeFactory
|
|
|
|
*/
|
|
|
|
public function __construct(IFactory $factory,
|
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
ITimeFactory $timeFactory) {
|
|
|
|
$this->l10nFactory = $factory;
|
2019-03-16 18:19:25 +03:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
2019-08-09 21:25:21 +03:00
|
|
|
$this->timeFactory = $timeFactory;
|
2019-03-16 18:19:25 +03:00
|
|
|
}
|
|
|
|
|
2019-08-02 17:36:50 +03:00
|
|
|
/**
|
|
|
|
* Identifier of the notifier, only use [a-z0-9_]
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @since 17.0.0
|
|
|
|
*/
|
|
|
|
public function getID():string {
|
2019-08-09 21:25:21 +03:00
|
|
|
return Application::APP_ID;
|
2019-08-02 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Human readable name describing the notifier
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @since 17.0.0
|
|
|
|
*/
|
|
|
|
public function getName():string {
|
2019-08-09 21:25:21 +03:00
|
|
|
return $this->l10nFactory->get('dav')->t('Calendar');
|
2019-08-02 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
2019-03-16 18:19:25 +03:00
|
|
|
/**
|
2019-08-09 21:25:21 +03:00
|
|
|
* Prepare sending the notification
|
|
|
|
*
|
2019-03-16 18:19:25 +03:00
|
|
|
* @param INotification $notification
|
|
|
|
* @param string $languageCode The code of the language that should be used to prepare the notification
|
|
|
|
* @return INotification
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2019-08-09 21:25:21 +03:00
|
|
|
public function prepare(INotification $notification,
|
|
|
|
string $languageCode):INotification {
|
2019-03-16 18:19:25 +03:00
|
|
|
if ($notification->getApp() !== Application::APP_ID) {
|
|
|
|
throw new \InvalidArgumentException('Notification not from this app');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the language from the notification
|
2019-08-09 21:25:21 +03:00
|
|
|
$this->l10n = $this->l10nFactory->get('dav', $languageCode);
|
2019-03-16 18:19:25 +03:00
|
|
|
|
2019-08-09 21:25:21 +03:00
|
|
|
// Handle notifier subjects
|
|
|
|
switch($notification->getSubject()) {
|
|
|
|
case 'calendar_reminder':
|
|
|
|
return $this->prepareReminderNotification($notification);
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException('Unknown subject');
|
2019-03-16 18:19:25 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-09 21:25:21 +03:00
|
|
|
* @param INotification $notification
|
|
|
|
* @return INotification
|
2019-03-16 18:19:25 +03:00
|
|
|
*/
|
2019-08-09 21:25:21 +03:00
|
|
|
private function prepareReminderNotification(INotification $notification):INotification {
|
|
|
|
$imagePath = $this->urlGenerator->imagePath('core', 'places/calendar.svg');
|
|
|
|
$iconUrl = $this->urlGenerator->getAbsoluteURL($imagePath);
|
|
|
|
$notification->setIcon($iconUrl);
|
|
|
|
|
|
|
|
$this->prepareNotificationSubject($notification);
|
|
|
|
$this->prepareNotificationMessage($notification);
|
|
|
|
|
|
|
|
return $notification;
|
2019-03-16 18:19:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-09 21:25:21 +03:00
|
|
|
* Sets the notification subject based on the parameters set in PushProvider
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
2019-08-09 21:25:21 +03:00
|
|
|
* @param INotification $notification
|
2019-03-16 18:19:25 +03:00
|
|
|
*/
|
2019-08-09 21:25:21 +03:00
|
|
|
private function prepareNotificationSubject(INotification $notification): void {
|
|
|
|
$parameters = $notification->getSubjectParameters();
|
|
|
|
|
2019-08-12 14:20:03 +03:00
|
|
|
$startTime = \DateTime::createFromFormat(\DateTime::ATOM, $parameters['start_atom']);
|
2019-08-09 21:25:21 +03:00
|
|
|
$now = $this->timeFactory->getDateTime();
|
|
|
|
$title = $this->getTitleFromParameters($parameters);
|
|
|
|
|
|
|
|
$diff = $startTime->diff($now);
|
|
|
|
if ($diff === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$components = [];
|
|
|
|
if ($diff->y) {
|
|
|
|
$components[] = $this->l10n->n('%n year', '%n years', $diff->y);
|
|
|
|
}
|
|
|
|
if ($diff->m) {
|
|
|
|
$components[] = $this->l10n->n('%n month', '%n months', $diff->m);
|
|
|
|
}
|
|
|
|
if ($diff->d) {
|
|
|
|
$components[] = $this->l10n->n('%n day', '%n days', $diff->d);
|
|
|
|
}
|
|
|
|
if ($diff->h) {
|
|
|
|
$components[] = $this->l10n->n('%n hour', '%n hours', $diff->h);
|
|
|
|
}
|
|
|
|
if ($diff->i) {
|
|
|
|
$components[] = $this->l10n->n('%n minute', '%n minutes', $diff->i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limiting to the first three components to prevent
|
|
|
|
// the string from getting too long
|
|
|
|
$firstThreeComponents = array_slice($components, 0, 2);
|
|
|
|
$diffLabel = implode(', ', $firstThreeComponents);
|
|
|
|
|
|
|
|
if ($diff->invert) {
|
|
|
|
$title = $this->l10n->t('%s (in %s)', [$title, $diffLabel]);
|
|
|
|
} else {
|
|
|
|
$title = $this->l10n->t('%s (%s ago)', [$title, $diffLabel]);
|
2019-03-16 18:19:25 +03:00
|
|
|
}
|
2019-08-09 21:25:21 +03:00
|
|
|
|
|
|
|
$notification->setParsedSubject($title);
|
2019-03-16 18:19:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-09 21:25:21 +03:00
|
|
|
* Sets the notification message based on the parameters set in PushProvider
|
|
|
|
*
|
|
|
|
* @param INotification $notification
|
2019-03-16 18:19:25 +03:00
|
|
|
*/
|
2019-08-09 21:25:21 +03:00
|
|
|
private function prepareNotificationMessage(INotification $notification): void {
|
|
|
|
$parameters = $notification->getMessageParameters();
|
|
|
|
|
2019-03-16 18:19:25 +03:00
|
|
|
$description = [
|
2019-08-09 21:25:21 +03:00
|
|
|
$this->l10n->t('Calendar: %s', $parameters['calendar_displayname']),
|
|
|
|
$this->l10n->t('Date: %s', $this->generateDateString($parameters)),
|
2019-03-16 18:19:25 +03:00
|
|
|
];
|
2019-08-09 21:25:21 +03:00
|
|
|
if ($parameters['description']) {
|
|
|
|
$description[] = $this->l10n->t('Description: %s', $parameters['description']);
|
|
|
|
}
|
|
|
|
if ($parameters['location']) {
|
|
|
|
$description[] = $this->l10n->t('Where: %s', $parameters['location']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = implode("\r\n", $description);
|
|
|
|
$notification->setParsedMessage($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $parameters
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getTitleFromParameters(array $parameters):string {
|
|
|
|
return $parameters['title'] ?? $this->l10n->t('Untitled event');
|
|
|
|
}
|
2019-03-16 18:19:25 +03:00
|
|
|
|
2019-08-09 21:25:21 +03:00
|
|
|
/**
|
|
|
|
* @param array $parameters
|
|
|
|
* @return string
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
private function generateDateString(array $parameters):string {
|
2019-08-12 14:20:03 +03:00
|
|
|
$startDateTime = DateTime::createFromFormat(\DateTime::ATOM, $parameters['start_atom']);
|
|
|
|
$endDateTime = DateTime::createFromFormat(\DateTime::ATOM, $parameters['end_atom']);
|
2019-08-09 21:25:21 +03:00
|
|
|
$isAllDay = $parameters['all_day'];
|
|
|
|
$diff = $startDateTime->diff($endDateTime);
|
|
|
|
|
|
|
|
if ($isAllDay) {
|
|
|
|
// One day event
|
|
|
|
if ($diff->days === 1) {
|
|
|
|
return $this->getDateString($startDateTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(' - ', [
|
|
|
|
$this->getDateString($startDateTime),
|
|
|
|
$this->getDateString($endDateTime),
|
|
|
|
]);
|
2019-03-16 18:19:25 +03:00
|
|
|
}
|
2019-08-09 21:25:21 +03:00
|
|
|
|
|
|
|
$startTimezone = $endTimezone = null;
|
|
|
|
if (!$parameters['start_is_floating']) {
|
|
|
|
$startTimezone = $parameters['start_timezone'];
|
|
|
|
$endTimezone = $parameters['end_timezone'];
|
2019-03-16 18:19:25 +03:00
|
|
|
}
|
2019-08-09 21:25:21 +03:00
|
|
|
|
|
|
|
$localeStart = implode(', ', [
|
|
|
|
$this->getWeekDayName($startDateTime),
|
|
|
|
$this->getDateTimeString($startDateTime)
|
|
|
|
]);
|
|
|
|
|
|
|
|
// always show full date with timezone if timezones are different
|
|
|
|
if ($startTimezone !== $endTimezone) {
|
|
|
|
$localeEnd = implode(', ', [
|
|
|
|
$this->getWeekDayName($endDateTime),
|
|
|
|
$this->getDateTimeString($endDateTime)
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $localeStart
|
|
|
|
. ' (' . $startTimezone . ') '
|
|
|
|
. ' - '
|
|
|
|
. $localeEnd
|
|
|
|
. ' (' . $endTimezone . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show only the time if the day is the same
|
|
|
|
$localeEnd = $this->isDayEqual($startDateTime, $endDateTime)
|
|
|
|
? $this->getTimeString($endDateTime)
|
|
|
|
: implode(', ', [
|
|
|
|
$this->getWeekDayName($endDateTime),
|
|
|
|
$this->getDateTimeString($endDateTime)
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $localeStart
|
|
|
|
. ' - '
|
|
|
|
. $localeEnd
|
|
|
|
. ' (' . $startTimezone . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DateTime $dtStart
|
|
|
|
* @param DateTime $dtEnd
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isDayEqual(DateTime $dtStart,
|
|
|
|
DateTime $dtEnd):bool {
|
|
|
|
return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DateTime $dt
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getWeekDayName(DateTime $dt):string {
|
|
|
|
return $this->l10n->l('weekdayName', $dt, ['width' => 'abbreviated']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DateTime $dt
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getDateString(DateTime $dt):string {
|
|
|
|
return $this->l10n->l('date', $dt, ['width' => 'medium']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DateTime $dt
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getDateTimeString(DateTime $dt):string {
|
|
|
|
return $this->l10n->l('datetime', $dt, ['width' => 'medium|short']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DateTime $dt
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getTimeString(DateTime $dt):string {
|
|
|
|
return $this->l10n->l('time', $dt, ['width' => 'short']);
|
2019-03-16 18:19:25 +03:00
|
|
|
}
|
|
|
|
}
|