From 8363f3c6355e5bd2bb54875d11e326747df265c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 3 Jul 2014 13:48:15 +0200 Subject: [PATCH] Adding new interface \OCP\Activity\IExtentsion Adding method getNotificationTypes() Adding method filterNotificationTypes() Adding method getDefaultTypes() Adding method translate() and getTypeIcon() Adding method getGroupParameter() Adding method getNavigation() Adding method getNavigation() Adding method isFilterValid() and getQueryForFilter() Adding unit tests for \OC\ActivityManager --- lib/private/activitymanager.php | 194 +++++++++++++++++++++++++++ lib/private/l10n.php | 3 +- lib/public/activity/iextension.php | 120 +++++++++++++++++ lib/public/activity/imanager.php | 72 +++++++++- tests/lib/activitymanager.php | 202 +++++++++++++++++++++++++++++ 5 files changed, 589 insertions(+), 2 deletions(-) create mode 100644 lib/public/activity/iextension.php create mode 100644 tests/lib/activitymanager.php diff --git a/lib/private/activitymanager.php b/lib/private/activitymanager.php index 66aa039eb1..f31b121c8e 100755 --- a/lib/private/activitymanager.php +++ b/lib/private/activitymanager.php @@ -10,12 +10,21 @@ namespace OC; use OCP\Activity\IConsumer; +use OCP\Activity\IExtension; use OCP\Activity\IManager; class ActivityManager implements IManager { + /** + * @var \Closure[] + */ private $consumers = array(); + /** + * @var \Closure[] + */ + private $extensions = array(); + /** * @param $app * @param $subject @@ -65,4 +74,189 @@ class ActivityManager implements IManager { array_push($this->consumers, $callable); } + /** + * In order to improve lazy loading a closure can be registered which will be called in case + * activity consumers are actually requested + * + * $callable has to return an instance of OCA\Activity\IConsumer + * + * @param \Closure $callable + * @return void + */ + function registerExtension(\Closure $callable) { + array_push($this->extensions, $callable); + } + + /** + * Will return additional notification types as specified by other apps + * + * @param string $languageCode + * @return array + */ + function getNotificationTypes($languageCode) { + $notificationTypes = array(); + foreach($this->extensions as $extension) { + $c = $extension(); + if ($c instanceof IExtension) { + $result = $c->getNotificationTypes($languageCode); + if (is_array($result)) { + $notificationTypes = array_merge($notificationTypes, $result); + } + } + } + + return $notificationTypes; + } + + /** + * @param array $types + * @param string $filter + * @return array + */ + function filterNotificationTypes($types, $filter) { + foreach($this->extensions as $extension) { + $c = $extension(); + if ($c instanceof IExtension) { + $result = $c->filterNotificationTypes($types, $filter); + if (is_array($result)) { + $types = $result; + } + } + } + return $types; + } + + /** + * @param string $method + * @return array + */ + function getDefaultTypes($method) { + $defaultTypes = array(); + foreach($this->extensions as $extension) { + $c = $extension(); + if ($c instanceof IExtension) { + $types = $c->getDefaultTypes($method); + if (is_array($types)) { + $defaultTypes = array_merge($types, $defaultTypes); + } + } + } + return $defaultTypes; + } + + /** + * @param string $app + * @param string $text + * @param array $params + * @param boolean $stripPath + * @param boolean $highlightParams + * @param string $languageCode + * @return string|false + */ + function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { + foreach($this->extensions as $extension) { + $c = $extension(); + if ($c instanceof IExtension) { + $translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode); + if (is_string($translation)) { + return $translation; + } + } + } + + return false; + } + + /** + * @param string $type + * @return string + */ + function getTypeIcon($type) { + foreach($this->extensions as $extension) { + $c = $extension(); + if ($c instanceof IExtension) { + $icon = $c->getTypeIcon($type); + if (is_string($icon)) { + return $icon; + } + } + } + + return ''; + } + + /** + * @param array $activity + * @return integer|false + */ + function getGroupParameter($activity) { + foreach($this->extensions as $extension) { + $c = $extension(); + if ($c instanceof IExtension) { + $parameter = $c->getGroupParameter($activity); + if ($parameter !== false) { + return $parameter; + } + } + } + + return false; + } + + /** + * @return array + */ + function getNavigation() { + $entries = array( + 'apps' => array(), + 'top' => array(), + ); + foreach($this->extensions as $extension) { + $c = $extension(); + if ($c instanceof IExtension) { + $additionalEntries = $c->getNavigation(); + if (is_array($additionalEntries)) { + $entries['apps'] = array_merge($entries['apps'], $additionalEntries['apps']); + $entries['top'] = array_merge($entries['top'], $additionalEntries['top']); + } + } + } + + return $entries; + } + + /** + * @param string $filterValue + * @return boolean + */ + function isFilterValid($filterValue) { + foreach($this->extensions as $extension) { + $c = $extension(); + if ($c instanceof IExtension) { + if ($c->isFilterValid($filterValue) === true) { + return true; + } + } + } + + return false; + } + + /** + * @param string $filter + * @return array + */ + function getQueryForFilter($filter) { + foreach($this->extensions as $extension) { + $c = $extension(); + if ($c instanceof IExtension) { + $result = $c->getQueryForFilter($filter); + if (is_array($result)) { + return $result; + } + } + } + + return array(null, null); + } } diff --git a/lib/private/l10n.php b/lib/private/l10n.php index 3e44be8815..28b35e92a2 100644 --- a/lib/private/l10n.php +++ b/lib/private/l10n.php @@ -404,14 +404,15 @@ class OC_L10N implements \OCP\IL10N { /** * find the best language + * * @param array|string $app details below - * string language * * If $app is an array, ownCloud assumes that these are the available * languages. Otherwise ownCloud tries to find the files in the l10n * folder. * * If nothing works it returns 'en' + * @return string language */ public function getLanguageCode($app=null) { return self::findLanguage($app); diff --git a/lib/public/activity/iextension.php b/lib/public/activity/iextension.php new file mode 100644 index 0000000000..6bb403a889 --- /dev/null +++ b/lib/public/activity/iextension.php @@ -0,0 +1,120 @@ +. + * + */ + +/** + * Public interface of ownCloud for apps to use. + * Activity/IExtension interface + */ + +// use OCP namespace for all classes that are considered public. +// This means that they should be used by apps instead of the internal ownCloud classes +namespace OCP\Activity; + +interface IExtension { + /** + * 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); + + /** + * 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); + + /** + * 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); + + /** + * 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); + + /** + * 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); + + /** + * 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); + + /** + * 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(); + + /** + * The extension can check if a customer filter (given by a query string like filter=abc) is valid or not. + * + * @param string $filterValue + * @return boolean + */ + public function isFilterValid($filterValue); + + /** + * 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); +} diff --git a/lib/public/activity/imanager.php b/lib/public/activity/imanager.php index 449fdcc934..0a49fdf499 100644 --- a/lib/public/activity/imanager.php +++ b/lib/public/activity/imanager.php @@ -50,11 +50,81 @@ interface IManager { * In order to improve lazy loading a closure can be registered which will be called in case * activity consumers are actually requested * - * $callable has to return an instance of OCA\Activity\IConsumer + * $callable has to return an instance of \OCP\Activity\IConsumer * * @param \Closure $callable * @return void */ function registerConsumer(\Closure $callable); + /** + * In order to improve lazy loading a closure can be registered which will be called in case + * activity consumers are actually requested + * + * $callable has to return an instance of \OCP\Activity\IExtension + * + * @param \Closure $callable + * @return void + */ + function registerExtension(\Closure $callable); + + /** + * Will return additional notification types as specified by other apps + * @param string $languageCode + * @return array + */ + function getNotificationTypes($languageCode); + + /** + * @param array $types + * @param string $filter + * @return array + */ + function filterNotificationTypes($types, $filter); + + /** + * @param string $method + * @return array + */ + function getDefaultTypes($method); + + /** + * @param string $app + * @param string $text + * @param array $params + * @param boolean $stripPath + * @param boolean $highlightParams + * @param string $languageCode + * @return string|false + */ + function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode); + + /** + * @param string $type + * @return string + */ + function getTypeIcon($type); + + /** + * @param array $activity + * @return integer|false + */ + function getGroupParameter($activity); + + /** + * @return array + */ + function getNavigation(); + + /** + * @param string $filterValue + * @return boolean + */ + function isFilterValid($filterValue); + + /** + * @param string $filter + * @return array + */ + function getQueryForFilter($filter); } diff --git a/tests/lib/activitymanager.php b/tests/lib/activitymanager.php new file mode 100644 index 0000000000..f21b82c52c --- /dev/null +++ b/tests/lib/activitymanager.php @@ -0,0 +1,202 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + * +*/ + +class Test_ActivityManager extends PHPUnit_Framework_TestCase { + + /** @var \OC\ActivityManager */ + private $activityManager; + + public function setUp() { + $this->activityManager = new \OC\ActivityManager(); + $this->activityManager->registerExtension(function() { + return new NoOpExtension(); + }); + $this->activityManager->registerExtension(function() { + return new SimpleExtension(); + }); + } + + public function testNotificationTypes() { + $result = $this->activityManager->getNotificationTypes('en'); + $this->assertTrue(is_array($result)); + $this->assertEquals(2, sizeof($result)); + } + + public function testFilterNotificationTypes() { + $result = $this->activityManager->filterNotificationTypes(array('NT0', 'NT1', 'NT2', 'NT3'), 'FILTER1'); + $this->assertTrue(is_array($result)); + $this->assertEquals(3, sizeof($result)); + + $result = $this->activityManager->filterNotificationTypes(array('NT0', 'NT1', 'NT2', 'NT3'), 'FILTER2'); + $this->assertTrue(is_array($result)); + $this->assertEquals(4, sizeof($result)); + } + + public function testDefaultTypes() { + $result = $this->activityManager->getDefaultTypes('stream'); + $this->assertTrue(is_array($result)); + $this->assertEquals(1, sizeof($result)); + + $result = $this->activityManager->getDefaultTypes('email'); + $this->assertTrue(is_array($result)); + $this->assertEquals(0, sizeof($result)); + } + + public function testTranslate() { + $result = $this->activityManager->translate('APP0', '', '', array(), false, false, 'en'); + $this->assertEquals('Stupid translation', $result); + + $result = $this->activityManager->translate('APP1', '', '', array(), false, false, 'en'); + $this->assertFalse($result); + } + + public function testTypeIcon() { + $result = $this->activityManager->getTypeIcon('NT1'); + $this->assertEquals('icon-nt-one', $result); + + $result = $this->activityManager->getTypeIcon('NT2'); + $this->assertEquals('', $result); + } + + public function testGroupParameter() { + $result = $this->activityManager->getGroupParameter(array()); + $this->assertEquals(5, $result); + } + + public function testNavigation() { + $result = $this->activityManager->getNavigation(); + $this->assertEquals(4, sizeof($result['apps'])); + $this->assertEquals(2, sizeof($result['top'])); + } + + public function testIsFilterValid() { + $result = $this->activityManager->isFilterValid('fv01'); + $this->assertTrue($result); + + $result = $this->activityManager->isFilterValid('FV2'); + $this->assertFalse($result); + } + + public function testQueryForFilter() { + $result = $this->activityManager->getQueryForFilter('filter1'); + $this->assertEquals( + array( + '`app` = ? and `message` like ?', + array('mail', 'ownCloud%') + ), $result + ); + + $result = $this->activityManager->isFilterValid('filter2'); + $this->assertFalse($result); + } +} + +class SimpleExtension implements \OCP\Activity\IExtension { + + public function getNotificationTypes($languageCode) { + return array('NT1', 'NT2'); + } + + public function filterNotificationTypes($types, $filter) { + if ($filter === 'FILTER1') { + unset($types[0]); + } + return $types; + } + + public function getDefaultTypes($method) { + if ($method === 'stream') { + return array('DT0'); + } + + return array(); + } + + public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { + if ($app === 'APP0') { + return "Stupid translation"; + } + + return false; + } + + public function getTypeIcon($type) { + if ($type === 'NT1') { + return 'icon-nt-one'; + } + return ''; + } + + public function getGroupParameter($activity) { + return 5; + } + + public function getNavigation() { + return array( + 'apps' => array('nav1', 'nav2', 'nav3', 'nav4'), + 'top' => array('top1', 'top2') + ); + } + + public function isFilterValid($filterValue) { + if ($filterValue === 'fv01') { + return true; + } + + return false; + } + + public function getQueryForFilter($filter) { + if ($filter === 'filter1') { + return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%')); + } + + return false; + } +} + +class NoOpExtension implements \OCP\Activity\IExtension { + + public function getNotificationTypes($languageCode) { + return false; + } + + public function filterNotificationTypes($types, $filter) { + return false; + } + + public function getDefaultTypes($method) { + return false; + } + + public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { + return false; + } + + public function getTypeIcon($type) { + return false; + } + + public function getGroupParameter($activity) { + return false; + } + + public function getNavigation() { + return false; + } + + public function isFilterValid($filterValue) { + return false; + } + + public function getQueryForFilter($filter) { + return false; + } +}