2016-11-29 18:21:36 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
|
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2020-01-21 12:32:48 +03:00
|
|
|
* @author Thomas Citharel <nextcloud@tcit.fr>
|
2017-11-06 17:56:42 +03:00
|
|
|
*
|
2016-11-29 18:21:36 +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/>.
|
2016-11-29 18:21:36 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\CalDAV\Activity\Provider;
|
|
|
|
|
2020-01-21 12:32:48 +03:00
|
|
|
use OC_App;
|
2016-11-29 18:21:36 +03:00
|
|
|
use OCP\Activity\IEvent;
|
|
|
|
use OCP\Activity\IEventMerger;
|
|
|
|
use OCP\Activity\IManager;
|
2020-01-21 12:32:48 +03:00
|
|
|
use OCP\App\IAppManager;
|
2018-01-24 15:54:49 +03:00
|
|
|
use OCP\IGroupManager;
|
2016-11-29 18:21:36 +03:00
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUserManager;
|
2016-12-01 13:35:23 +03:00
|
|
|
use OCP\L10N\IFactory;
|
2016-11-29 18:21:36 +03:00
|
|
|
|
2016-11-30 13:38:49 +03:00
|
|
|
class Event extends Base {
|
2020-04-10 17:54:27 +03:00
|
|
|
public const SUBJECT_OBJECT_ADD = 'object_add';
|
|
|
|
public const SUBJECT_OBJECT_UPDATE = 'object_update';
|
|
|
|
public const SUBJECT_OBJECT_DELETE = 'object_delete';
|
2016-11-29 18:21:36 +03:00
|
|
|
|
2016-12-01 13:35:23 +03:00
|
|
|
/** @var IFactory */
|
|
|
|
protected $languageFactory;
|
|
|
|
|
2016-11-29 18:21:36 +03:00
|
|
|
/** @var IL10N */
|
|
|
|
protected $l;
|
|
|
|
|
|
|
|
/** @var IManager */
|
|
|
|
protected $activityManager;
|
|
|
|
|
|
|
|
/** @var IEventMerger */
|
|
|
|
protected $eventMerger;
|
|
|
|
|
2020-01-21 12:32:48 +03:00
|
|
|
/** @var IAppManager */
|
|
|
|
protected $appManager;
|
|
|
|
|
2016-11-29 18:21:36 +03:00
|
|
|
/**
|
2016-12-01 13:35:23 +03:00
|
|
|
* @param IFactory $languageFactory
|
2016-11-29 18:21:36 +03:00
|
|
|
* @param IURLGenerator $url
|
|
|
|
* @param IManager $activityManager
|
|
|
|
* @param IUserManager $userManager
|
2018-01-24 15:54:49 +03:00
|
|
|
* @param IGroupManager $groupManager
|
2016-11-29 18:21:36 +03:00
|
|
|
* @param IEventMerger $eventMerger
|
2020-01-21 12:32:48 +03:00
|
|
|
* @param IAppManager $appManager
|
2016-11-29 18:21:36 +03:00
|
|
|
*/
|
2020-01-21 12:32:48 +03:00
|
|
|
public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IGroupManager $groupManager, IEventMerger $eventMerger, IAppManager $appManager) {
|
|
|
|
parent::__construct($userManager, $groupManager, $url);
|
2016-12-01 13:35:23 +03:00
|
|
|
$this->languageFactory = $languageFactory;
|
2016-11-29 18:21:36 +03:00
|
|
|
$this->activityManager = $activityManager;
|
|
|
|
$this->eventMerger = $eventMerger;
|
2020-01-21 12:32:48 +03:00
|
|
|
$this->appManager = $appManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $eventData
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function generateObjectParameter(array $eventData) {
|
|
|
|
if (!isset($eventData['id']) || !isset($eventData['name'])) {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'type' => 'calendar-event',
|
|
|
|
'id' => $eventData['id'],
|
|
|
|
'name' => $eventData['name'],
|
|
|
|
|
|
|
|
];
|
|
|
|
if (isset($eventData['link']) && is_array($eventData['link']) && $this->appManager->isEnabledForUser('calendar')) {
|
|
|
|
try {
|
|
|
|
// The calendar app needs to be manually loaded for the routes to be loaded
|
|
|
|
OC_App::loadApp('calendar');
|
|
|
|
$linkData = $eventData['link'];
|
|
|
|
$objectId = base64_encode('/remote.php/dav/calendars/' . $linkData['owner'] . '/' . $linkData['calendar_uri'] . '/' . $linkData['object_uri']);
|
|
|
|
$link = [
|
|
|
|
'view' => 'dayGridMonth',
|
|
|
|
'timeRange' => 'now',
|
|
|
|
'mode' => 'sidebar',
|
|
|
|
'objectId' => $objectId,
|
|
|
|
'recurrenceId' => 'next'
|
|
|
|
];
|
|
|
|
$params['link'] = $this->url->linkToRouteAbsolute('calendar.view.indexview.timerange.edit', $link);
|
|
|
|
} catch (\Exception $error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $params;
|
2016-11-29 18:21:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-01 13:35:23 +03:00
|
|
|
* @param string $language
|
2016-11-29 18:21:36 +03:00
|
|
|
* @param IEvent $event
|
|
|
|
* @param IEvent|null $previousEvent
|
|
|
|
* @return IEvent
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
* @since 11.0.0
|
|
|
|
*/
|
2016-12-01 13:35:23 +03:00
|
|
|
public function parse($language, IEvent $event, IEvent $previousEvent = null) {
|
2016-11-29 18:21:36 +03:00
|
|
|
if ($event->getApp() !== 'dav' || $event->getType() !== 'calendar_event') {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
|
2016-12-01 13:35:23 +03:00
|
|
|
$this->l = $this->languageFactory->get('dav', $language);
|
|
|
|
|
2017-06-20 14:48:51 +03:00
|
|
|
if ($this->activityManager->getRequirePNG()) {
|
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar-dark.png')));
|
|
|
|
} else {
|
2018-07-24 19:47:54 +03:00
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar.svg')));
|
2017-06-20 14:48:51 +03:00
|
|
|
}
|
2016-11-29 18:21:36 +03:00
|
|
|
|
|
|
|
if ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_event') {
|
|
|
|
$subject = $this->l->t('{actor} created event {event} in calendar {calendar}');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_event_self') {
|
2016-11-29 18:21:36 +03:00
|
|
|
$subject = $this->l->t('You created event {event} in calendar {calendar}');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_event') {
|
2016-11-29 18:21:36 +03:00
|
|
|
$subject = $this->l->t('{actor} deleted event {event} from calendar {calendar}');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_event_self') {
|
2016-11-29 18:21:36 +03:00
|
|
|
$subject = $this->l->t('You deleted event {event} from calendar {calendar}');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_event') {
|
2016-11-29 18:21:36 +03:00
|
|
|
$subject = $this->l->t('{actor} updated event {event} in calendar {calendar}');
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_event_self') {
|
2016-11-29 18:21:36 +03:00
|
|
|
$subject = $this->l->t('You updated event {event} in calendar {calendar}');
|
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsedParameters = $this->getParameters($event);
|
|
|
|
$this->setSubjects($event, $subject, $parsedParameters);
|
|
|
|
|
|
|
|
$event = $this->eventMerger->mergeEvents('event', $event, $previousEvent);
|
|
|
|
|
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IEvent $event
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getParameters(IEvent $event) {
|
|
|
|
$subject = $event->getSubject();
|
|
|
|
$parameters = $event->getSubjectParameters();
|
|
|
|
|
2017-10-16 19:31:43 +03:00
|
|
|
// Nextcloud 13+
|
|
|
|
if (isset($parameters['calendar'])) {
|
|
|
|
switch ($subject) {
|
|
|
|
case self::SUBJECT_OBJECT_ADD . '_event':
|
|
|
|
case self::SUBJECT_OBJECT_DELETE . '_event':
|
|
|
|
case self::SUBJECT_OBJECT_UPDATE . '_event':
|
|
|
|
return [
|
|
|
|
'actor' => $this->generateUserParameter($parameters['actor']),
|
|
|
|
'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
|
2019-01-08 19:46:07 +03:00
|
|
|
'event' => $this->generateClassifiedObjectParameter($parameters['object']),
|
2017-10-16 19:31:43 +03:00
|
|
|
];
|
|
|
|
case self::SUBJECT_OBJECT_ADD . '_event_self':
|
|
|
|
case self::SUBJECT_OBJECT_DELETE . '_event_self':
|
|
|
|
case self::SUBJECT_OBJECT_UPDATE . '_event_self':
|
|
|
|
return [
|
|
|
|
'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
|
2019-01-08 19:46:07 +03:00
|
|
|
'event' => $this->generateClassifiedObjectParameter($parameters['object']),
|
2017-10-16 19:31:43 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 13:35:55 +03:00
|
|
|
// Legacy - Do NOT Remove unless necessary
|
|
|
|
// Removing this will break parsing of activities that were created on
|
|
|
|
// Nextcloud 12, so we should keep this as long as it's acceptable.
|
|
|
|
// Otherwise if people upgrade over multiple releases in a short period,
|
|
|
|
// they will get the dead entries in their stream.
|
2016-11-29 18:21:36 +03:00
|
|
|
switch ($subject) {
|
|
|
|
case self::SUBJECT_OBJECT_ADD . '_event':
|
|
|
|
case self::SUBJECT_OBJECT_DELETE . '_event':
|
|
|
|
case self::SUBJECT_OBJECT_UPDATE . '_event':
|
|
|
|
return [
|
|
|
|
'actor' => $this->generateUserParameter($parameters[0]),
|
2021-01-11 14:57:03 +03:00
|
|
|
'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
|
2016-11-29 18:21:36 +03:00
|
|
|
'event' => $this->generateObjectParameter($parameters[2]),
|
|
|
|
];
|
|
|
|
case self::SUBJECT_OBJECT_ADD . '_event_self':
|
|
|
|
case self::SUBJECT_OBJECT_DELETE . '_event_self':
|
|
|
|
case self::SUBJECT_OBJECT_UPDATE . '_event_self':
|
|
|
|
return [
|
2021-01-11 14:57:03 +03:00
|
|
|
'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
|
2016-11-29 18:21:36 +03:00
|
|
|
'event' => $this->generateObjectParameter($parameters[2]),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
2019-01-08 19:46:07 +03:00
|
|
|
|
|
|
|
private function generateClassifiedObjectParameter(array $eventData) {
|
|
|
|
$parameter = $this->generateObjectParameter($eventData);
|
2019-01-09 16:43:51 +03:00
|
|
|
if (!empty($eventData['classified'])) {
|
2019-01-08 19:46:07 +03:00
|
|
|
$parameter['name'] = $this->l->t('Busy');
|
|
|
|
}
|
|
|
|
return $parameter;
|
|
|
|
}
|
2016-11-29 18:21:36 +03:00
|
|
|
}
|