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>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Thomas Citharel <nextcloud@tcit.fr>
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
|
|
|
* @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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2019-03-16 18:19:25 +03:00
|
|
|
namespace OCA\DAV\CalDAV\Reminder\NotificationProvider;
|
|
|
|
|
|
|
|
use OCA\DAV\AppInfo\Application;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2019-03-16 18:19:25 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IURLGenerator;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IUser;
|
2019-03-16 18:19:25 +03:00
|
|
|
use OCP\L10N\IFactory as L10NFactory;
|
|
|
|
use OCP\Notification\IManager;
|
|
|
|
use OCP\Notification\INotification;
|
2019-08-09 21:25:21 +03:00
|
|
|
use Sabre\VObject\Component\VEvent;
|
|
|
|
use Sabre\VObject\Property;
|
2019-03-16 18:19:25 +03:00
|
|
|
|
2019-08-09 21:25:21 +03:00
|
|
|
/**
|
|
|
|
* Class PushProvider
|
|
|
|
*
|
|
|
|
* @package OCA\DAV\CalDAV\Reminder\NotificationProvider
|
|
|
|
*/
|
|
|
|
class PushProvider extends AbstractProvider {
|
2019-03-16 18:19:25 +03:00
|
|
|
|
2019-08-02 17:34:39 +03:00
|
|
|
/** @var string */
|
2019-03-16 18:19:25 +03:00
|
|
|
public const NOTIFICATION_TYPE = 'DISPLAY';
|
|
|
|
|
2019-08-12 14:20:03 +03:00
|
|
|
/** @var IManager */
|
2019-03-16 18:19:25 +03:00
|
|
|
private $manager;
|
|
|
|
|
2019-08-09 21:25:21 +03:00
|
|
|
/** @var ITimeFactory */
|
2019-03-16 18:19:25 +03:00
|
|
|
private $timeFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param IManager $manager
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param L10NFactory $l10nFactory
|
|
|
|
* @param IUrlGenerator $urlGenerator
|
|
|
|
* @param ITimeFactory $timeFactory
|
|
|
|
*/
|
2019-08-09 21:25:21 +03:00
|
|
|
public function __construct(IConfig $config,
|
|
|
|
IManager $manager,
|
|
|
|
ILogger $logger,
|
2019-03-16 18:19:25 +03:00
|
|
|
L10NFactory $l10nFactory,
|
2019-08-09 21:25:21 +03:00
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
ITimeFactory $timeFactory) {
|
2019-03-16 18:19:25 +03:00
|
|
|
parent::__construct($logger, $l10nFactory, $urlGenerator, $config);
|
|
|
|
$this->manager = $manager;
|
|
|
|
$this->timeFactory = $timeFactory;
|
2019-08-12 14:20:03 +03:00
|
|
|
}
|
2019-03-16 18:19:25 +03:00
|
|
|
|
|
|
|
/**
|
2019-08-09 21:25:21 +03:00
|
|
|
* Send push notification to all users.
|
2019-03-16 18:19:25 +03:00
|
|
|
*
|
2019-08-09 21:25:21 +03:00
|
|
|
* @param VEvent $vevent
|
2019-03-16 18:19:25 +03:00
|
|
|
* @param string $calendarDisplayName
|
2019-08-09 21:25:21 +03:00
|
|
|
* @param IUser[] $users
|
2019-03-16 18:19:25 +03:00
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2019-08-12 14:20:03 +03:00
|
|
|
public function send(VEvent $vevent,
|
2019-08-09 21:25:21 +03:00
|
|
|
string $calendarDisplayName=null,
|
|
|
|
array $users=[]):void {
|
2019-09-02 16:18:43 +03:00
|
|
|
if ($this->config->getAppValue('dav', 'sendEventRemindersPush', 'no') !== 'yes') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-09 21:25:21 +03:00
|
|
|
$eventDetails = $this->extractEventDetails($vevent);
|
|
|
|
$eventDetails['calendar_displayname'] = $calendarDisplayName;
|
2020-02-01 17:29:02 +03:00
|
|
|
$eventUUID = (string) $vevent->UID;
|
|
|
|
// Empty Notification ObjectId will be catched by OC\Notification\Notification
|
|
|
|
$eventUUIDHash = $eventUUID ? hash('sha256', $eventUUID, false) : '';
|
2019-03-16 18:19:25 +03:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($users as $user) {
|
2019-08-09 21:25:21 +03:00
|
|
|
/** @var INotification $notification */
|
|
|
|
$notification = $this->manager->createNotification();
|
|
|
|
$notification->setApp(Application::APP_ID)
|
|
|
|
->setUser($user->getUID())
|
|
|
|
->setDateTime($this->timeFactory->getDateTime())
|
2020-02-01 17:29:02 +03:00
|
|
|
->setObject(Application::APP_ID, $eventUUIDHash)
|
2019-08-09 21:25:21 +03:00
|
|
|
->setSubject('calendar_reminder', [
|
|
|
|
'title' => $eventDetails['title'],
|
|
|
|
'start_atom' => $eventDetails['start_atom']
|
|
|
|
])
|
|
|
|
->setMessage('calendar_reminder', $eventDetails);
|
|
|
|
|
|
|
|
$this->manager->notify($notification);
|
|
|
|
}
|
2019-08-12 14:20:03 +03:00
|
|
|
}
|
2019-08-09 21:25:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var VEvent $vevent
|
|
|
|
* @return array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
protected function extractEventDetails(VEvent $vevent):array {
|
|
|
|
/** @var Property\ICalendar\DateTime $start */
|
|
|
|
$start = $vevent->DTSTART;
|
|
|
|
$end = $this->getDTEndFromEvent($vevent);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'title' => isset($vevent->SUMMARY)
|
|
|
|
? ((string) $vevent->SUMMARY)
|
|
|
|
: null,
|
|
|
|
'description' => isset($vevent->DESCRIPTION)
|
|
|
|
? ((string) $vevent->DESCRIPTION)
|
|
|
|
: null,
|
|
|
|
'location' => isset($vevent->LOCATION)
|
|
|
|
? ((string) $vevent->LOCATION)
|
|
|
|
: null,
|
|
|
|
'all_day' => $start instanceof Property\ICalendar\Date,
|
2019-08-12 14:20:03 +03:00
|
|
|
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
|
2019-08-09 21:25:21 +03:00
|
|
|
'start_atom' => $start->getDateTime()->format(\DateTime::ATOM),
|
|
|
|
'start_is_floating' => $start->isFloating(),
|
2019-08-12 14:20:03 +03:00
|
|
|
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
|
2019-08-09 21:25:21 +03:00
|
|
|
'start_timezone' => $start->getDateTime()->getTimezone()->getName(),
|
2019-08-12 14:20:03 +03:00
|
|
|
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
|
2019-08-09 21:25:21 +03:00
|
|
|
'end_atom' => $end->getDateTime()->format(\DateTime::ATOM),
|
|
|
|
'end_is_floating' => $end->isFloating(),
|
2019-08-12 14:20:03 +03:00
|
|
|
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
|
2019-08-09 21:25:21 +03:00
|
|
|
'end_timezone' => $end->getDateTime()->getTimezone()->getName(),
|
|
|
|
];
|
|
|
|
}
|
2019-03-16 18:19:25 +03:00
|
|
|
}
|