diff options
author | Thomas Citharel <tcit@tcit.fr> | 2019-03-16 16:19:25 +0100 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2019-08-15 20:02:56 +0200 |
commit | 7bddcc091d5fe0f5e01325e16524d44fe8c1fb74 (patch) | |
tree | 6af37e4e745f5816292a2c496d34b12afe95440e /apps/dav/lib | |
parent | f452e23a7db1742afa50eaa80b746afe769bdf7b (diff) | |
download | nextcloud-server-7bddcc091d5fe0f5e01325e16524d44fe8c1fb74.tar.gz nextcloud-server-7bddcc091d5fe0f5e01325e16524d44fe8c1fb74.zip |
Support event reminders (email and notifications)
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'apps/dav/lib')
14 files changed, 1265 insertions, 6 deletions
diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php index 41570ee7442..2e3b95d8bfa 100644 --- a/apps/dav/lib/AppInfo/Application.php +++ b/apps/dav/lib/AppInfo/Application.php @@ -30,6 +30,12 @@ use OCA\DAV\CalDAV\Activity\Backend; use OCA\DAV\CalDAV\Activity\Provider\Event; use OCA\DAV\CalDAV\BirthdayService; use OCA\DAV\CalDAV\CalendarManager; +use OCA\DAV\CalDAV\Reminder\Backend as ReminderBackend; +use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider; +use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider; +use OCA\DAV\CalDAV\Reminder\NotificationProviderManager; +use OCA\DAV\CalDAV\Reminder\Notifier; +use OCA\DAV\CalDAV\Reminder\ReminderService; use OCA\DAV\Capabilities; use OCA\DAV\CardDAV\ContactsManager; use OCA\DAV\CardDAV\PhotoCache; @@ -43,6 +49,8 @@ use Symfony\Component\EventDispatcher\GenericEvent; class Application extends App { + const APP_ID = 'dav'; + /** * Application constructor. */ @@ -109,8 +117,7 @@ class Application extends App { } }); - // carddav/caldav sync event setup - $listener = function($event) { + $birthdayListener = function ($event) { if ($event instanceof GenericEvent) { /** @var BirthdayService $b */ $b = $this->getContainer()->query(BirthdayService::class); @@ -122,9 +129,9 @@ class Application extends App { } }; - $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::createCard', $listener); - $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $listener); - $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', function($event) { + $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::createCard', $birthdayListener); + $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $birthdayListener); + $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', function ($event) { if ($event instanceof GenericEvent) { /** @var BirthdayService $b */ $b = $this->getContainer()->query(BirthdayService::class); @@ -177,6 +184,11 @@ class Application extends App { $event->getArgument('calendarData'), $event->getArgument('shares') ); + + $reminderBackend = $this->getContainer()->query(ReminderBackend::class); + $reminderBackend->cleanRemindersForCalendar( + $event->getArgument('calendarId') + ); }); $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateShares', function(GenericEvent $event) { /** @var Backend $backend */ @@ -187,6 +199,8 @@ class Application extends App { $event->getArgument('add'), $event->getArgument('remove') ); + + // Here we should recalculate if reminders should be sent to new or old sharees }); $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::publishCalendar', function(GenericEvent $event) { @@ -214,6 +228,16 @@ class Application extends App { $event->getArgument('shares'), $event->getArgument('objectData') ); + + /** @var ReminderService $reminderBackend */ + $reminderService= $this->getContainer()->query(ReminderService::class); + + $reminderService->onTouchCalendarObject( + $eventName, + $event->getArgument('calendarData'), + $event->getArgument('shares'), + $event->getArgument('objectData') + ); }; $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', $listener); $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', $listener); @@ -224,4 +248,28 @@ class Application extends App { return $this->getContainer()->query(SyncService::class); } + public function registerNotifier() { + $this->getContainer()->getServer()->getNotificationManager()->registerNotifier(function() { + return $this->getContainer()->query(Notifier::class); + }, function() { + $l = $this->getContainer()->getServer()->getL10NFactory()->get(self::APP_ID); + return [ + 'id' => self::APP_ID, + 'name' => $l->t('Calendars and Contacts'), + ]; + }); + } + + public function registerCalendarReminders(): void + { + try { + /** @var NotificationProviderManager $notificationProviderManager */ + $notificationProviderManager = $this->getContainer()->query(NotificationProviderManager::class); + $notificationProviderManager->registerProvider(EmailProvider::class); + $notificationProviderManager->registerProvider(PushProvider::class); + } catch(\Exception $ex) { + $this->getContainer()->getServer()->getLogger()->logException($ex); + } + } + } diff --git a/apps/dav/lib/BackgroundJob/EventReminderJob.php b/apps/dav/lib/BackgroundJob/EventReminderJob.php new file mode 100644 index 00000000000..e0a147e4203 --- /dev/null +++ b/apps/dav/lib/BackgroundJob/EventReminderJob.php @@ -0,0 +1,59 @@ +<?php +/** + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OCA\DAV\BackgroundJob; + +use OC\BackgroundJob\TimedJob; +use OCA\DAV\CalDAV\Reminder\ReminderService; +use OCP\IConfig; + +class EventReminderJob extends TimedJob { + + /** @var ReminderService */ + private $reminderService; + + /** @var IConfig */ + private $config; + + /** + * EventReminderJob constructor. + * + * @param ReminderService $reminderService + * @param IConfig $config + */ + public function __construct(ReminderService $reminderService, IConfig $config) { + $this->reminderService = $reminderService; + $this->config = $config; + /** Run every 5 minutes */ + $this->setInterval(5); + } + + /** + * @param $arg + * @throws \OCA\DAV\CalDAV\Reminder\NotificationProvider\ProviderNotAvailableException + * @throws \OCA\DAV\CalDAV\Reminder\NotificationTypeDoesNotExistException + * @throws \OC\User\NoUserException + */ + public function run($arg): void + { + if ($this->config->getAppValue('dav', 'sendEventReminders', 'yes') === 'yes') { + $this->reminderService->processReminders(); + } + } +} diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index 91281dc0cb3..62d3909ce39 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -1135,7 +1135,6 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription */ function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType=self::CALENDAR_TYPE_CALENDAR) { $extraData = $this->getDenormalizedData($calendarData); - $query = $this->db->getQueryBuilder(); $query->update('calendarobjects') ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) diff --git a/apps/dav/lib/CalDAV/Reminder/AbstractNotificationProvider.php b/apps/dav/lib/CalDAV/Reminder/AbstractNotificationProvider.php new file mode 100644 index 00000000000..ba928fac80f --- /dev/null +++ b/apps/dav/lib/CalDAV/Reminder/AbstractNotificationProvider.php @@ -0,0 +1,208 @@ +<?php +/** + * @copyright Copyright (c) 2018 Thomas Citharel <tcit@tcit.fr> + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license GNU AGPL version 3 or any later version + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * 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/>. + * + */ + +namespace OCA\DAV\CalDAV\Reminder; + +use \DateTime; +use \DateTimeImmutable; +use OCP\IConfig; +use OCP\IL10N; +use OCP\ILogger; +use OCP\IURLGenerator; +use OCP\L10N\IFactory as L10NFactory; +use OCP\IUser; +use Sabre\VObject\Component\VCalendar; +use Sabre\VObject\Component\VEvent; +use Sabre\VObject\DateTimeParser; +use Sabre\VObject\Parameter; +use Sabre\VObject\Property; + +abstract class AbstractNotificationProvider +{ + + public const NOTIFICATION_TYPE = ''; + + /** @var ILogger */ + protected $logger; + + /** @var L10NFactory */ + protected $l10nFactory; + + /** @var IL10N */ + protected $l10n; + + /** @var IURLGenerator */ + protected $urlGenerator; + + /** @var IConfig */ + protected $config; + + /** + * @param ILogger $logger + * @param L10NFactory $l10nFactory + * @param IConfig $config + * @param IUrlGenerator $urlGenerator + */ + public function __construct(ILogger $logger, L10NFactory $l10nFactory, IURLGenerator $urlGenerator, IConfig $config) { + $this->logger = $logger; + $this->l10nFactory = $l10nFactory; + $this->urlGenerator = $urlGenerator; + $this->config = $config; + } + + /** + * Send notification + * + * @param VCalendar $vcalendar + * @param string $calendarDisplayName + * @param IUser $user + * @return void + */ + public function send(VCalendar $vcalendar, string $calendarDisplayName, IUser $user): void {} + + /** + * @var VCalendar $vcalendar + * @var string $defaultValue + * @return array + * @throws \Exception + */ + protected function extractEventDetails(VCalendar $vcalendar, $defaultValue = ''): array + { + /** @var VEvent $vevent */ + $vevent = $vcalendar->VEVENT; + + /** @var Property $start */ + $start = $vevent->DTSTART; + if (isset($vevent->DTEND)) { + $end = $vevent->DTEND; + } elseif (isset($vevent->DURATION)) { + $isFloating = $vevent->DTSTART->isFloating(); + $end = clone $vevent->DTSTART; + $endDateTime = $end->getDateTime(); + $endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue())); + $end->setDateTime($endDateTime, $isFloating); + } elseif (!$vevent->DTSTART->hasTime()) { + $isFloating = $vevent->DTSTART->isFloating(); + $end = clone $vevent->DTSTART; + $endDateTime = $end->getDateTime(); + $endDateTime = $endDateTime->modify('+1 day'); + $end->setDateTime($endDateTime, $isFloating); + } else { + $end = clone $vevent->DTSTART; + } + + return [ + 'title' => (string) $vevent->SUMMARY ?: $defaultValue, + 'description' => (string) $vevent->DESCRIPTION ?: $defaultValue, + 'start'=> $start->getDateTime(), + 'end' => $end->getDateTime(), + 'when' => $this->generateWhenString($start, $end), + 'url' => (string) $vevent->URL ?: $defaultValue, + 'location' => (string) $vevent->LOCATION ?: $defaultValue, + 'uid' => (string) $vevent->UID, + ]; + } + + /** + * @param Property $dtstart + * @param Property $dtend + * @return string + * @throws \Exception + */ + private function generateWhenString(Property $dtstart, Property $dtend): string + { + $isAllDay = $dtstart instanceof Property\ICalendar\Date; + + /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtstart */ + /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtend */ + /** @var DateTimeImmutable $dtstartDt */ + $dtstartDt = $dtstart->getDateTime(); + /** @var DateTimeImmutable $dtendDt */ + $dtendDt = $dtend->getDateTime(); + + $diff = $dtstartDt->diff($dtendDt); + + $dtstartDt = new DateTime($dtstartDt->format(DateTime::ATOM)); + $dtendDt = new DateTime($dtendDt->format(DateTime::ATOM)); + + if ($isAllDay) { + // One day event + if ($diff->days === 1) { + return $this->l10n->l('date', $dtstartDt, ['width' => 'medium']); + } + + //event that spans over multiple days + $localeStart = $this->l10n->l('date', $dtstartDt, ['width' => 'medium']); + $localeEnd = $this->l10n->l('date', $dtendDt, ['width' => 'medium']); + + return $localeStart . ' - ' . $localeEnd; + } + + /** @var Property\ICalendar\DateTime $dtstart */ + /** @var Property\ICalendar\DateTime $dtend */ + $isFloating = $dtstart->isFloating(); + $startTimezone = $endTimezone = null; + if (!$isFloating) { + $prop = $dtstart->offsetGet('TZID'); + if ($prop instanceof Parameter) { + $startTimezone = $prop->getValue(); + } + + $prop = $dtend->offsetGet('TZID'); + if ($prop instanceof Parameter) { + $endTimezone = $prop->getValue(); + } + } + + $localeStart = $this->l10n->l('weekdayName', $dtstartDt, ['width' => 'abbreviated']) . ', ' . + $this->l10n->l('datetime', $dtstartDt, ['width' => 'medium|short']); + + // always show full date with timezone if timezones are different + if ($startTimezone !== $endTimezone) { + $localeEnd = $this->l10n->l('datetime', $dtendDt, ['width' => 'medium|short']); + + return $localeStart . ' (' . $startTimezone . ') - ' . + $localeEnd . ' (' . $endTimezone . ')'; + } + + // show only end time if date is the same + if ($this->isDayEqual($dtstartDt, $dtendDt)) { + $localeEnd = $this->l10n->l('time', $dtendDt, ['width' => 'short']); + } else { + $localeEnd = $this->l10n->l('weekdayName', $dtendDt, ['width' => 'abbreviated']) . ', ' . + $this->l10n->l('datetime', $dtendDt, ['width' => 'medium|short']); + } + + 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'); + } +} diff --git a/apps/dav/lib/CalDAV/Reminder/Backend.php b/apps/dav/lib/CalDAV/Reminder/Backend.php new file mode 100644 index 00000000000..c85e7c365dc --- /dev/null +++ b/apps/dav/lib/CalDAV/Reminder/Backend.php @@ -0,0 +1,139 @@ +<?php +/** + * @copyright Copyright (c) 2019 Thomas Citharel <tcit@tcit.fr> + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license GNU AGPL version 3 or any later version + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * 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/>. + * + */ + +namespace OCA\DAV\CalDAV\Reminder; + +use OCP\IDBConnection; +use OCP\AppFramework\Utility\ITimeFactory; + +/** + * Class Backend + * + * @package OCA\DAV\CalDAV\Reminder + */ +class Backend { + + /** @var IDBConnection */ + protected $db; + + /** @var ITimeFactory */ + private $timeFactory; + + /** + * @param IDBConnection $db + * @param ITimeFactory $timeFactory + */ + public function __construct(IDBConnection $db, ITimeFactory $timeFactory) { + $this->db = $db; + $this->timeFactory = $timeFactory; + } + + /** + * @param string $uid + * @param string $calendarId + * @param string $uri + * @param string $type + * @param int $notificationDate + * @param int $eventStartDate + */ + public function insertReminder(string $uid, string $calendarId, string $uri, string $type, int $notificationDate, int $eventStartDate): void + { + $query = $this->db->getQueryBuilder(); + $query->insert('calendar_reminders') + ->values([ + 'uid' => $query->createNamedParameter($uid), + 'calendarid' => $query->createNamedParameter($calendarId), + 'objecturi' => $query->createNamedParameter($uri), + 'type' => $query->createNamedParameter($type), + 'notificationdate' => $query->createNamedParameter($notificationDate), + 'eventstartdate' => $query->createNamedParameter($eventStartDate), + ])->execute(); + } + + /** + * Cleans reminders in database + * + * @param int $calendarId + * @param string $objectUri + */ + public function cleanRemindersForEvent(int $calendarId, string $objectUri): void + { + $query = $this->db->getQueryBuilder(); + + $query->delete('calendar_reminders') + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) + ->andWhere($query->expr()->eq('objecturi', $query->createNamedParameter($objectUri))) + ->execute(); + } + + /** + * Remove all reminders for a calendar + * + * @param integer $calendarId + * @return void + */ + public function cleanRemindersForCalendar(int $calendarId): void + { + $query = $this->db->getQueryBuilder(); + + $query->delete('calendar_reminders') + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) + ->execute(); + } + + /** + * Remove a reminder by it's id + * + * @param integer $reminderId + * @return void + */ + public function removeReminder(int $reminderId): void + { + $query = $this->db->getQueryBuilder(); + + $query->delete('calendar_reminders') + ->where($query->expr()->eq('id', $query->createNamedParameter($reminderId))) + ->execute(); + } + + /** + * Get all reminders with a notification date before now + * + * @return array + * @throws \Exception + */ + public function getRemindersToProcess(): array + { + $query = $this->db->getQueryBuilder(); + $fields = ['cr.id', 'cr.calendarid', 'cr.objecturi', 'cr.type', 'cr.notificationdate', 'cr.uid', 'co.calendardata', 'c.displayname']; + $stmt = $query->select($fields) + ->from('calendar_reminders', 'cr') + ->where($query->expr()->lte('cr.notificationdate', $query->createNamedParameter($this->timeFactory->getTime()))) + ->andWhere($query->expr()->gte('cr.eventstartdate', $query->createNamedParameter($this->timeFactory->getTime()))) # We check that DTSTART isn't before + ->leftJoin('cr', 'calendars', 'c', $query->expr()->eq('cr.calendarid', 'c.id')) + ->leftJoin('cr', 'calendarobjects', 'co', $query->expr()->andX($query->expr()->eq('cr.calendarid', 'c.id'), $query->expr()->eq('co.uri', 'cr.objecturi'))) + ->execute(); + + return $stmt->fetchAll(); + } +} diff --git a/apps/dav/lib/CalDAV/Reminder/NotificationProvider/EmailProvider.php b/apps/dav/lib/CalDAV/Reminder/NotificationProvider/EmailProvider.php new file mode 100644 index 00000000000..81d4474011c --- /dev/null +++ b/apps/dav/lib/CalDAV/Reminder/NotificationProvider/EmailProvider.php @@ -0,0 +1,157 @@ +<?php +/** + * @copyright Copyright (c) 2018 Thomas Citharel <tcit@tcit.fr> + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license GNU AGPL version 3 or any later version + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * 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/>. + * + */ + +namespace OCA\DAV\CalDAV\Reminder\NotificationProvider; + +use OCA\DAV\CalDAV\Reminder\AbstractNotificationProvider; +use OCP\IConfig; +use OCP\ILogger; +use OCP\IURLGenerator; +use OCP\L10N\IFactory as L10NFactory; +use OCP\Mail\IEMailTemplate; +use OCP\Mail\IMailer; +use OCP\IUser; +use Sabre\VObject\Component\VCalendar; + +class EmailProvider extends AbstractNotificationProvider +{ + /** @var IMailer */ + private $mailer; + + public const NOTIFICATION_TYPE = 'EMAIL'; + + /** + * @param IConfig $config + * @param IMailer $mailer + * @param ILogger $logger + * @param L10NFactory $l10nFactory + * @param IUrlGenerator $urlGenerator + */ + public function __construct(IConfig $config, IMailer $mailer, ILogger $logger, + L10NFactory $l10nFactory, + IURLGenerator $urlGenerator) { + parent::__construct($logger, $l10nFactory, $urlGenerator, $config); + $this->mailer = $mailer; + } + + /** + * Send notification + * + * @param VCalendar $vcalendar + * @param string $calendarDisplayName + * @param IUser $user + * @return void + * @throws \Exception + */ + public function send(VCalendar $vcalendar, string $calendarDisplayName, IUser $user): void + { + if ($user->getEMailAddress() === null) { + return; + } + + $lang = $this->config->getUserValue($user->getUID(), 'core', 'lang', $this->l10nFactory->findLanguage()); + $this->l10n = $this->l10nFactory->get('dav', $lang); + + $event = $this->extractEventDetails($vcalendar); + $fromEMail = \OCP\Util::getDefaultEmailAddress('invitations-noreply'); + + $message = $this->mailer->createMessage() + ->setFrom([$fromEMail => 'Nextcloud']) + // TODO: Set reply to from event creator + // ->setReplyTo([$sender => $senderName]) + ->setTo([$user->getEMailAddress() => $user->getDisplayName()]); + + $template = $this->mailer->createEMailTemplate('dav.calendarReminder', $event); + $template->addHeader(); + + $this->addSubjectAndHeading($template, $event['title']); + $this->addBulletList($template, $event, $calendarDisplayName); + + $template->addFooter(); + $message->useTemplate($template); + + $attachment = $this->mailer->createAttachment( + $vcalendar->serialize(), + $event['uid'].'.ics',// TODO(leon): Make file name unique, e.g. add event id + 'text/calendar' + ); + $message->attach($attachment); + + try { + $failed = $this->mailer->send($message); + if ($failed) { + $this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => implode(', ', $failed)]); + } + } catch(\Exception $ex) { + $this->logger->logException($ex, ['app' => 'dav']); + } + } + + /** + * @param IEMailTemplate $template + * @param string $summary + */ + private function addSubjectAndHeading(IEMailTemplate $template, string $summary): void + { + $template->setSubject('Notification: ' . $summary); + $template->addHeading($summary); + } + + /** + * @param IEMailTemplate $template + * @param array $eventData + * @param string $calendarDisplayName + */ + private function addBulletList(IEMailTemplate $template, array $eventData, string $calendarDisplayName): void + { + $template->addBodyListItem($calendarDisplayName, $this->l10n->t('Calendar:'), + $this->getAbsoluteImagePath('actions/info.svg')); + + $template->addBodyListItem($eventData['when'], $this->l10n->t('Date:'), + $this->getAbsoluteImagePath('places/calendar.svg')); + + if ($eventData['location']) { + $template->addBodyListItem((string) $eventData['location'], $this->l10n->t('Where:'), + $this->getAbsoluteImagePath('actions/address.svg')); + } + if ($eventData['description']) { + $template->addBodyListItem((string) $eventData['description'], $this->l10n->t('Description:'), + $this->getAbsoluteImagePath('actions/more.svg')); + } + if ($eventData['url']) { + $template->addBodyListItem((string) $eventData['url'], $this->l10n->t('Link:'), + $this->getAbsoluteImagePath('places/link.svg')); + } + } + + /** + * @param string $path + * @return string + */ + private function getAbsoluteImagePath($path): string + { + return $this->urlGenerator->getAbsoluteURL( + $this->urlGenerator->imagePath('core', $path) + ); + } +} diff --git a/apps/dav/lib/CalDAV/Reminder/NotificationProvider/ProviderNotAvailableException.php b/apps/dav/lib/CalDAV/Reminder/NotificationProvider/ProviderNotAvailableException.php new file mode 100644 index 00000000000..bf736db8a34 --- /dev/null +++ b/apps/dav/lib/CalDAV/Reminder/NotificationProvider/ProviderNotAvailableException.php @@ -0,0 +1,39 @@ +<?php +/** + * @copyright Copyright (c) 2018 Thomas Citharel <tcit@tcit.fr> + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license GNU AGPL version 3 or any later version + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * 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/>. + * + */ + +namespace OCA\DAV\CalDAV\Reminder\NotificationProvider; + +class ProviderNotAvailableException extends \Exception { + + /** + * ProviderNotAvailableException constructor. + * + * @since 16.0.0 + * + * @param string $type ReminderType + */ + public function __construct(string $type) { + parent::__construct("No notification provider for type $type available"); + } + +} diff --git a/apps/dav/lib/CalDAV/Reminder/NotificationProvider/PushProvider.php b/apps/dav/lib/CalDAV/Reminder/NotificationProvider/PushProvider.php new file mode 100644 index 00000000000..1bb0e5c68b1 --- /dev/null +++ b/apps/dav/lib/CalDAV/Reminder/NotificationProvider/PushProvider.php @@ -0,0 +1,101 @@ +<?php +/** + * @copyright Copyright (c) 2018 Thomas Citharel <tcit@tcit.fr> + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license GNU AGPL version 3 or any later version + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * 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/>. + * + */ + +namespace OCA\DAV\CalDAV\Reminder\NotificationProvider; + +use OCA\DAV\AppInfo\Application; +use OCA\DAV\CalDAV\Reminder\AbstractNotificationProvider; +use OCP\IConfig; +use OCP\ILogger; +use OCP\IURLGenerator; +use OCP\L10N\IFactory as L10NFactory; +use OCP\Notification\IManager; +use OCP\IUser; +use OCP\Notification\INotification; +use Sabre\VObject\Component\VCalendar; +use OCP\AppFramework\Utility\ITimeFactory; + +class PushProvider extends AbstractNotificationProvider +{ + + public const NOTIFICATION_TYPE = 'DISPLAY'; + + /** + * @var IManager + */ + private $manager; + + /** + * @var ITimeFactory + */ + private $timeFactory; + + /** + * @param IConfig $config + * @param IManager $manager + * @param ILogger $logger + * @param L10NFactory $l10nFactory + * @param IUrlGenerator $urlGenerator + * @param ITimeFactory $timeFactory + */ + public function __construct(IConfig $config, IManager $manager, ILogger $logger, + L10NFactory $l10nFactory, + IURLGenerator $urlGenerator, ITimeFactory $timeFactory) { + parent::__construct($logger, $l10nFactory, $urlGenerator, $config); + $this->manager = $manager; + $this->timeFactory = $timeFactory; + } + + /** + * Send notification + * + * @param VCalendar $vcalendar + * @param string $calendarDisplayName + * @param IUser $user + * @return void + * @throws \Exception + */ + public function send(VCalendar $vcalendar, string $calendarDisplayName, IUser $user): void + { + + $lang = $this->config->getUserValue($user->getUID(), 'core', 'lang', $this->l10nFactory->findLanguage()); + $this->l10n = $this->l10nFactory->get('dav', $lang); + + $event = $this->extractEventDetails($vcalendar); + /** @var INotification $notification */ + $notification = $this->manager->createNotification(); + $notification->setApp(Application::APP_ID) + ->setUser($user->getUID()) + ->setDateTime($this->timeFactory->getDateTime()) + ->setObject(Application::APP_ID, $event['uid']) // $type and $id + ->setSubject('calendar_reminder', ['title' => $event['title'], 'start' => $event['start']->getTimestamp()]) // $subject and $parameters + ->setMessage('calendar_reminder', [ + 'when' => $event['when'], + 'description' => $event['description'], + 'location' => $event['location'], + 'calendar' => $calendarDisplayName + ]) + ; + $this->manager->notify($notification); + } +} diff --git a/apps/dav/lib/CalDAV/Reminder/NotificationProviderManager.php b/apps/dav/lib/CalDAV/Reminder/NotificationProviderManager.php new file mode 100644 index 00000000000..389cbbd2bfa --- /dev/null +++ b/apps/dav/lib/CalDAV/Reminder/NotificationProviderManager.php @@ -0,0 +1,59 @@ +<?php +/** + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OCA\DAV\CalDAV\Reminder; + +use OCA\DAV\CalDAV\Reminder\NotificationProvider\ProviderNotAvailableException; + +class NotificationProviderManager { + + /** @var array */ + private $providers = []; + /** + * @var string $type + * @return AbstractNotificationProvider + * @throws ProviderNotAvailableException + * @throws NotificationTypeDoesNotExistException + */ + public function getProvider(string $type): AbstractNotificationProvider + { + if (in_array($type, ReminderService::REMINDER_TYPES, true)) { + if (isset($this->providers[$type])) { + return $this->providers[$type]; + } + throw new ProviderNotAvailableException($type); + } + throw new NotificationTypeDoesNotExistException($type); + } + + /** + * @param string $providerClassName + * @throws \OCP\AppFramework\QueryException + */ + public function registerProvider(string $providerClassName): void + { + $provider = \OC::$server->query($providerClassName); + + if (!$provider instanceof AbstractNotificationProvider) { + throw new \InvalidArgumentException('Invalid notification provider registered'); + } + + $this->providers[$provider::NOTIFICATION_TYPE] = $provider; + } +} diff --git a/apps/dav/lib/CalDAV/Reminder/NotificationTypeDoesNotExistException.php b/apps/dav/lib/CalDAV/Reminder/NotificationTypeDoesNotExistException.php new file mode 100644 index 00000000000..ae4ec3bd3b7 --- /dev/null +++ b/apps/dav/lib/CalDAV/Reminder/NotificationTypeDoesNotExistException.php @@ -0,0 +1,39 @@ +<?php +/** + * @copyright Copyright (c) 2018 Thomas Citharel <tcit@tcit.fr> + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license GNU AGPL version 3 or any later version + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * 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/>. + * + */ + +namespace OCA\DAV\CalDAV\Reminder; + +class NotificationTypeDoesNotExistException extends \Exception { + + /** + * NotificationTypeDoesNotExistException constructor. + * + * @since 16.0.0 + * + * @param string $type ReminderType + */ + public function __construct(string $type) { + parent::__construct("Type $type is not an accepted type of notification"); + } + +} diff --git a/apps/dav/lib/CalDAV/Reminder/Notifier.php b/apps/dav/lib/CalDAV/Reminder/Notifier.php new file mode 100644 index 00000000000..d95774e019e --- /dev/null +++ b/apps/dav/lib/CalDAV/Reminder/Notifier.php @@ -0,0 +1,143 @@ +<?php +/** + * @copyright Copyright (c) 2019 Thomas Citharel <tcit@tcit.fr> + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\DAV\CalDAV\Reminder; + +use OCA\DAV\AppInfo\Application; +use OCP\IL10N; +use OCP\L10N\IFactory; +use OCP\Notification\INotification; +use OCP\Notification\INotifier; +use OCP\IURLGenerator; + +class Notifier implements INotifier { + + public static $units = array( + 'y' => 'year', + 'm' => 'month', + 'd' => 'day', + 'h' => 'hour', + 'i' => 'minute', + 's' => 'second', + ); + + /** @var IFactory */ + protected $factory; + + /** @var IURLGenerator */ + protected $urlGenerator; + + /** @var IL10N */ + protected $l; + + public function __construct(IFactory $factory, IURLGenerator $urlGenerator) { + $this->factory = $factory; + $this->urlGenerator = $urlGenerator; + } + + /** + * @param INotification $notification + * @param string $languageCode The code of the language that should be used to prepare the notification + * @return INotification + * @throws \Exception + */ + public function prepare(INotification $notification, $languageCode): INotification + { + if ($notification->getApp() !== Application::APP_ID) { + throw new \InvalidArgumentException('Notification not from this app'); + } + + // Read the language from the notification + $this->l = $this->factory->get('dav', $languageCode); + + if ($notification->getSubject() === 'calendar_reminder') { + $subjectParameters = $notification->getSubjectParameters(); + $notification->setParsedSubject($this->processEventTitle($subjectParameters)); + + $messageParameters = $notification->getMessageParameters(); + $notification->setParsedMessage($this->processEventDescription($messageParameters)); + $notification->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'places/calendar.svg'))); + return $notification; + } + // Unknown subject => Unknown notification => throw + throw new \InvalidArgumentException('Unknown subject'); + } + + /** + * @param array $event + * @return string + * @throws \Exception + */ + private function processEventTitle(array $event): string + { + $event_datetime = new \DateTime(); + $event_datetime->setTimestamp($event['start']); + $now = new \DateTime(); + + $diff = $event_datetime->diff($now); + + foreach (self::$units as $attribute => $unit) { + $count = $diff->$attribute; + if (0 !== $count) { + return $this->getPluralizedTitle($count, $diff->invert, $unit, $event['title']); + } + } + return ''; + } + + /** + * + * @param int $count + * @param int $invert + * @param string $unit + * @param string $title + * @return string + */ + private function getPluralizedTitle(int $count, int $invert, string $unit, string $title): string + { + if ($invert) { + return $this->l->n('%s (in one %s)', '%s (in %n %ss)', $count, [$title, $unit]); + } + // This should probably not show up + return $this->l->n('%s (one %s ago)', '%s (%n %ss ago)', $count, [$title, $unit]); + } + + /** + * @param array $event + * @return string + */ + private function processEventDescription(array $event): string + { + $description = [ + $this->l->t('Calendar: %s', $event['calendar']), + $this->l->t('Date: %s', $event['when']), + ]; + + if ($event['description']) { + $description[] = $this->l->t('Description: %s', $event['description']); + } + if ($event['location']) { + $description[] = $this->l->t('Where: %s', $event['location']); + } + return implode('<br>', $description); + } +} diff --git a/apps/dav/lib/CalDAV/Reminder/ReminderService.php b/apps/dav/lib/CalDAV/Reminder/ReminderService.php new file mode 100644 index 00000000000..87c2ce10673 --- /dev/null +++ b/apps/dav/lib/CalDAV/Reminder/ReminderService.php @@ -0,0 +1,185 @@ +<?php +/** + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OCA\DAV\CalDAV\Reminder; + +use OC\User\NoUserException; +use OCP\IGroup; +use OCP\IGroupManager; +use OCP\IUserManager; +use OCP\IUserSession; +use Sabre\VObject; +use Sabre\VObject\Component\VAlarm; +use Sabre\VObject\Reader; + +class ReminderService { + + /** @var Backend */ + private $backend; + + /** @var NotificationProviderManager */ + private $notificationProviderManager; + + /** @var IUserManager */ + private $userManager; + + /** @var IGroupManager */ + private $groupManager; + + /** @var IUserSession */ + private $userSession; + + public const REMINDER_TYPE_EMAIL = 'EMAIL'; + public const REMINDER_TYPE_DISPLAY = 'DISPLAY'; + public const REMINDER_TYPE_AUDIO = 'AUDIO'; + + public const REMINDER_TYPES = [self::REMINDER_TYPE_EMAIL, self::REMINDER_TYPE_DISPLAY, self::REMINDER_TYPE_AUDIO]; + + public function __construct(Backend $backend, + NotificationProviderManager $notificationProviderManager, + IUserManager $userManager, + IGroupManager $groupManager, + IUserSession $userSession) { + $this->backend = $backend; + $this->notificationProviderManager = $notificationProviderManager; + $this->userManager = $userManager; + $this->groupManager = $groupManager; + $this->userSession = $userSession; + } + + /** + * Process reminders to activate + * + * @throws NoUserException + * @throws NotificationProvider\ProviderNotAvailableException + * @throws NotificationTypeDoesNotExistException + */ + public function processReminders(): void + { + + $reminders = $this->backend->getRemindersToProcess(); + + foreach ($reminders as $reminder) { + $calendarData = Reader::read($reminder['calendardata']); + + $user = $this->userManager->get($reminder['uid']); + + if ($user === null) { + throw new NoUserException('User not found for calendar'); + } + + $notificationProvider = $this->notificationProviderManager->getProvider($reminder['type']); + $notificationProvider->send($calendarData, $reminder['displayname'], $user); + $this->backend->removeReminder($reminder['id']); + } + } + + /** + * Saves reminders when a calendar object with some alarms was created/updated/deleted + * + * @param string $action + * @param array $calendarData + * @param array $shares + * @param array $objectData + * @return void + * @throws VObject\InvalidDataException + * @throws NoUserException + */ + public function onTouchCalendarObject(string $action, array $calendarData, array $shares, array $objectData): void + { + if (!isset($calendarData['principaluri'])) { + return; + } + + // Always remove existing reminders for this event + $this->backend->cleanRemindersForEvent($objectData['calendarid'], $objectData['uri']); + + /** + * If we are deleting the event, no need to go further + */ + if ($action === '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject') { + return; + } + + $user = $this->userSession->getUser(); + + if ($user === null) { + throw new NoUserException('No user in session'); + } + + $users = $this->getUsersForShares($shares); + + $users[] = $user->getUID(); + + $vobject = VObject\Reader::read($objectData['calendardata']); + + foreach ($vobject->VEVENT->VALARM as $alarm) { + if ($alarm instanceof VAlarm) { + $type = strtoupper($alarm->ACTION->getValue()); + if (in_array($type, self::REMINDER_TYPES, true)) { + $time = $alarm->getEffectiveTriggerTime(); + + foreach ($users as $uid) { + $this->backend->insertReminder( + $uid, + $objectData['calendarid'], + $objectData['uri'], + $type, + $time->getTimestamp(), + $vobject->VEVENT->DTSTART->getDateTime()->getTimestamp()); + + } + } + } + } + } + + + /** + * Get all users that have access to a given calendar + * + * @param array $shares + * @return string[] + */ + private function getUsersForShares(array $shares): array + { + $users = $groups = []; + foreach ($shares as $share) { + $principal = explode('/', $share['{http://owncloud.org/ns}principal']); + if ($principal[1] === 'users') { + $users[] = $principal[2]; + } else if ($principal[1] === 'groups') { + $groups[] = $principal[2]; + } + } + + if (!empty($groups)) { + foreach ($groups as $gid) { + $group = $this->groupManager->get($gid); + if ($group instanceof IGroup) { + foreach ($group->getUsers() as $user) { + $users[] = $user->getUID(); + } + } + } + } + + return array_unique($users); + } +} diff --git a/apps/dav/lib/Migration/Version1007Date20181005133326.php b/apps/dav/lib/Migration/Version1007Date20181005133326.php new file mode 100644 index 00000000000..1e4cce950ac --- /dev/null +++ b/apps/dav/lib/Migration/Version1007Date20181005133326.php @@ -0,0 +1,82 @@ +<?php + +declare(strict_types=1); + +namespace OCA\DAV\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\Migration\SimpleMigrationStep; +use OCP\Migration\IOutput; +use Doctrine\DBAL\Types\Type; + +/** + * Auto-generated migration step: Please modify to your needs! + */ +class Version1007Date20181005133326 extends SimpleMigrationStep { + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if (!$schema->hasTable('calendar_reminders')) { + $table = $schema->createTable('calendar_reminders'); + + $table->addColumn('id', Type::BIGINT, [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 11, + 'unsigned' => true, + ]); + $table->addColumn('uid', Type::STRING, [ + 'notnull' => true, + 'length' => 255, + ]); + $table->addColumn('calendarid', Type::BIGINT, [ + 'notnull' => false, + 'length' => 11, + ]); + $table->addColumn('objecturi', Type::STRING, [ + 'notnull' => true, + 'length' => 255, + ]); + $table->addColumn('type', Type::STRING, [ + 'notnull' => true, + 'length' => 255, + ]); + $table->addColumn('notificationdate', Type::DATETIME, [ + 'notnull' => false, + ]); + $table->addColumn('eventstartdate', Type::DATETIME, [ + 'notnull' => false, + ]); + + $table->setPrimaryKey(['id']); + $table->addIndex(['calendarid'], 'calendar_reminder_calendars'); + + return $schema; + } + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { + } +} diff --git a/apps/dav/lib/Settings/CalDAVSettings.php b/apps/dav/lib/Settings/CalDAVSettings.php index f38143b5b4e..958c463b1d3 100644 --- a/apps/dav/lib/Settings/CalDAVSettings.php +++ b/apps/dav/lib/Settings/CalDAVSettings.php @@ -48,6 +48,7 @@ class CalDAVSettings implements ISettings { $parameters = [ 'send_invitations' => $this->config->getAppValue('dav', 'sendInvitations', 'yes'), 'generate_birthday_calendar' => $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes'), + 'send_reminders_notifications' => $this->config->getAppValue('dav', 'sendEventReminders', 'yes'), ]; return new TemplateResponse('dav', 'settings-admin-caldav', $parameters); |