From 90578327d57856e26f3a81813c5fdcb3db99617b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 26 Sep 2016 13:50:39 +0200 Subject: [PATCH] Start working on calendar activities Signed-off-by: Joas Schilling --- apps/dav/lib/AppInfo/Application.php | 6 + apps/dav/lib/CalDAV/Activity.php | 250 ++++++++++++++++++++++++++ apps/dav/lib/CalDAV/CalDavBackend.php | 96 +++++++++- 3 files changed, 351 insertions(+), 1 deletion(-) create mode 100644 apps/dav/lib/CalDAV/Activity.php diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php index 2bdcff9cf8..d073131f00 100644 --- a/apps/dav/lib/AppInfo/Application.php +++ b/apps/dav/lib/AppInfo/Application.php @@ -24,6 +24,7 @@ */ namespace OCA\DAV\AppInfo; +use OCA\DAV\CalDAV\Activity; use OCA\DAV\CalDAV\BirthdayService; use OCA\DAV\Capabilities; use OCA\DAV\CardDAV\ContactsManager; @@ -88,6 +89,11 @@ class Application extends App { ); } }); + + $aM = $this->getContainer()->getServer()->getActivityManager(); + $aM->registerExtension(function() { + return $this->getContainer()->query(Activity::class); + }); } public function getSyncService() { diff --git a/apps/dav/lib/CalDAV/Activity.php b/apps/dav/lib/CalDAV/Activity.php new file mode 100644 index 0000000000..2f27a5bf07 --- /dev/null +++ b/apps/dav/lib/CalDAV/Activity.php @@ -0,0 +1,250 @@ + + * + * @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 . + * + */ + +namespace OCA\DAV\CalDAV; + +use OCP\Activity\IExtension; +use OCP\Activity\IManager; +use OCP\IL10N; +use OCP\IURLGenerator; +use OCP\L10N\IFactory; + +class Activity implements IExtension { + const APP = 'dav'; + /** + * Filter with all sharing related activities + */ + const CALENDAR = 'calendar'; + + /** + * Activity types known to this extension + */ + const SUBJECT_ADD = 'calendar_add'; + const SUBJECT_UPDATE = 'calendar_update'; + const SUBJECT_DELETE = 'calendar_delete'; + + /** + * Subject keys for translation of the subjections + */ + + /** @var IFactory */ + protected $languageFactory; + + /** @var IURLGenerator */ + protected $URLGenerator; + + /** + * @param IFactory $languageFactory + * @param IURLGenerator $URLGenerator + */ + public function __construct(IFactory $languageFactory, IURLGenerator $URLGenerator) { + $this->languageFactory = $languageFactory; + $this->URLGenerator = $URLGenerator; + } + + protected function getL10N($languageCode = null) { + return $this->languageFactory->get(self::APP, $languageCode); + } + + /** + * The extension can return an array of additional notification types. + * If no additional types are to be added false is to be returned + * + * @param string $languageCode + * @return array|false + */ + public function getNotificationTypes($languageCode) { + $l = $this->getL10N($languageCode); + + return array( + self::CALENDAR => (string) $l->t('A calendar was modified'), + ); + } + + /** + * For a given method additional types to be displayed in the settings can be returned. + * In case no additional types are to be added false is to be returned. + * + * @param string $method + * @return array|false + */ + public function getDefaultTypes($method) { + $defaultTypes = []; + if ($method === self::METHOD_STREAM) { + $defaultTypes[] = self::CALENDAR; + } + + return $defaultTypes; + } + + /** + * A string naming the css class for the icon to be used can be returned. + * If no icon is known for the given type false is to be returned. + * + * @param string $type + * @return string|false + */ + public function getTypeIcon($type) { + switch ($type) { + case self::CALENDAR: + return 'icon-calendar-dark'; + } + + return false; + } + + /** + * The extension can translate a given message to the requested languages. + * If no translation is available false is to be returned. + * + * @param string $app + * @param string $text + * @param array $params + * @param boolean $stripPath + * @param boolean $highlightParams + * @param string $languageCode + * @return string|false + */ + public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { + if ($app !== self::APP) { + return false; + } + + $l = $this->getL10N($languageCode); + + switch ($text) { + case self::SUBJECT_ADD: + return (string) $l->t('%1$s created calendar %2$s', $params); + case self::SUBJECT_ADD . '_self': + return (string) $l->t('You created calendar %2$s', $params); + case self::SUBJECT_DELETE: + return (string) $l->t('%1$s deleted calendar %2$s', $params); + case self::SUBJECT_DELETE . '_self': + return (string) $l->t('You deleted calendar %2$s', $params); + case self::SUBJECT_UPDATE: + return (string) $l->t('%1$s updated calendar %2$s', $params); + case self::SUBJECT_UPDATE . '_self': + return (string) $l->t('You updated calendar %2$s', $params); + } + + return false; + } + + /** + * The extension can define the type of parameters for translation + * + * Currently known types are: + * * file => will strip away the path of the file and add a tooltip with it + * * username => will add the avatar of the user + * + * @param string $app + * @param string $text + * @return array|false + */ + public function getSpecialParameterList($app, $text) { + if ($app === self::APP) { + switch ($text) { + case self::SUBJECT_ADD: + case self::SUBJECT_DELETE: + case self::SUBJECT_UPDATE: + return [ + 0 => 'username', + //1 => 'calendar', + ]; + } + } + + return false; + } + + /** + * The extension can define the parameter grouping by returning the index as integer. + * In case no grouping is required false is to be returned. + * + * @param array $activity + * @return integer|false + */ + public function getGroupParameter($activity) { + return false; + } + + /** + * The extension can define additional navigation entries. The array returned has to contain two keys 'top' + * and 'apps' which hold arrays with the relevant entries. + * If no further entries are to be added false is no be returned. + * + * @return array|false + */ + public function getNavigation() { + $l = $this->getL10N(); + return [ + 'apps' => [ + self::CALENDAR => [ + 'id' => self::CALENDAR, + 'icon' => 'icon-calendar-dark', + 'name' => (string) $l->t('Calendar'), + 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => self::CALENDAR]), + ], + ], + 'top' => [], + ]; + } + + /** + * The extension can check if a custom filter (given by a query string like filter=abc) is valid or not. + * + * @param string $filterValue + * @return boolean + */ + public function isFilterValid($filterValue) { + return $filterValue === self::CALENDAR; + } + + /** + * The extension can filter the types based on the filter if required. + * In case no filter is to be applied false is to be returned unchanged. + * + * @param array $types + * @param string $filter + * @return array|false + */ + public function filterNotificationTypes($types, $filter) { + switch ($filter) { + case self::CALENDAR: + return array_intersect([self::CALENDAR], $types); + } + return false; + } + + /** + * For a given filter the extension can specify the sql query conditions including parameters for that query. + * In case the extension does not know the filter false is to be returned. + * The query condition and the parameters are to be returned as array with two elements. + * E.g. return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%')); + * + * @param string $filter + * @return array|false + */ + public function getQueryForFilter($filter) { + return false; + } + +} diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index 07b61285ec..40488a2c75 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -31,6 +31,8 @@ use OCA\DAV\Connector\Sabre\Principal; use OCA\DAV\DAV\Sharing\Backend; use OCP\IConfig; use OCP\IDBConnection; +use OCP\IGroup; +use OCP\IGroupManager; use OCP\IUser; use OCP\IUserManager; use OCP\Security\ISecureRandom; @@ -613,7 +615,11 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription $query->setValue($column, $query->createNamedParameter($value)); } $query->execute(); - return $query->getLastInsertId(); + $calendarId = $query->getLastInsertId(); + + $this->triggerActivity(Activity::SUBJECT_ADD, $calendarId, $values); + + return $calendarId; } /** @@ -661,6 +667,8 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription $this->addChange($calendarId, "", 2); + $this->triggerActivity(Activity::SUBJECT_UPDATE, $calendarId, $mutations); + return true; }); } @@ -672,6 +680,8 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription * @return void */ function deleteCalendar($calendarId) { + $this->triggerActivity(Activity::SUBJECT_DELETE, $calendarId); + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?'); $stmt->execute([$calendarId]); @@ -1722,4 +1732,88 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription } return $principalUri; } + + protected function triggerActivity($action, $calendarId, array $changedProperties = []) { + $aM = \OC::$server->getActivityManager(); + $userSession = \OC::$server->getUserSession(); + + $properties = $this->getCalendarById($calendarId); + if (!isset($properties['principaluri'])) { + return; + } + + $principaluri = explode('/', $properties['principaluri']); + $owner = array_pop($principaluri); + + $currentUser = $userSession->getUser(); + if ($currentUser instanceof IUser) { + $currentUser = $currentUser->getUID(); + } else { + $currentUser = $owner; + } + + $event = $aM->generateEvent(); + $event->setApp('dav') + ->setObject(Activity::CALENDAR, $calendarId) + ->setType(Activity::CALENDAR) + ->setAuthor($currentUser); + + $changedVisibleInformation = array_intersect([ + '{DAV:}displayname', + '{http://apple.com/ns/ical/}calendar-color' + ], array_keys($changedProperties)); + + if ($action === Activity::SUBJECT_UPDATE && empty($changedVisibleInformation)) { + $users = [$owner]; + } else { + $users = $this->getUsersForCalendar($calendarId); + $users[] = $owner; + } + + foreach ($users as $user) { + $event->setAffectedUser($user) + ->setSubject( + $user === $currentUser ? $action . '_self' : $action, + [ + $currentUser, + $properties['{DAV:}displayname'], + ] + ); + $aM->publish($event); + } + } + + /** + * Get all users that have access to a given calendar + * + * @param int $calendarId + * @return string[] + */ + protected function getUsersForCalendar($calendarId) { + $gM = \OC::$server->getGroupManager(); + + $users = $groups = []; + $shares = $this->getShares($calendarId); + foreach ($shares as $share) { + $prinical = explode('/', $share['{http://owncloud.org/ns}principal']); + if ($prinical[1] === 'users') { + $users[] = $prinical[2]; + } else if ($prinical[1] === 'groups') { + $groups[] = $prinical[2]; + } + } + + if (!empty($groups)) { + foreach ($groups as $gid) { + $group = $gM->get($gid); + if ($group instanceof IGroup) { + foreach ($group->getUsers() as $user) { + $users[] = $user->getUID(); + } + } + } + } + + return $users; + } }