diff options
-rw-r--r-- | apps/files_sharing/api/server2server.php | 22 | ||||
-rw-r--r-- | lib/private/activity/event.php | 250 | ||||
-rw-r--r-- | lib/private/activitymanager.php | 132 | ||||
-rw-r--r-- | lib/public/activity/iconsumer.php | 16 | ||||
-rw-r--r-- | lib/public/activity/ievent.php | 200 | ||||
-rw-r--r-- | lib/public/activity/imanager.php | 78 | ||||
-rw-r--r-- | tests/lib/activitymanager.php | 197 |
7 files changed, 813 insertions, 82 deletions
diff --git a/apps/files_sharing/api/server2server.php b/apps/files_sharing/api/server2server.php index 211dc52c333..4328e3830ba 100644 --- a/apps/files_sharing/api/server2server.php +++ b/apps/files_sharing/api/server2server.php @@ -111,9 +111,14 @@ class Server2Server { if ($share) { list($file, $link) = self::getFile($share['uid_owner'], $share['file_source']); - \OC::$server->getActivityManager()->publishActivity( - Activity::FILES_SHARING_APP, Activity::SUBJECT_REMOTE_SHARE_ACCEPTED, array($share['share_with'], basename($file)), '', array(), - $file, $link, $share['uid_owner'], Activity::TYPE_REMOTE_SHARE, Activity::PRIORITY_LOW); + $event = \OC::$server->getActivityManager()->generateEvent(); + $event->setApp(Activity::FILES_SHARING_APP) + ->setType(Activity::TYPE_REMOTE_SHARE) + ->setAffectedUser($share['uid_owner']) + ->setSubject(Activity::SUBJECT_REMOTE_SHARE_ACCEPTED, [$share['share_with'], basename($file)]) + ->setObject('files', $share['file_source'], $file) + ->setLink($link); + \OC::$server->getActivityManager()->publish($event); } return new \OC_OCS_Result(); @@ -142,9 +147,14 @@ class Server2Server { list($file, $link) = $this->getFile($share['uid_owner'], $share['file_source']); - \OC::$server->getActivityManager()->publishActivity( - Activity::FILES_SHARING_APP, Activity::SUBJECT_REMOTE_SHARE_DECLINED, array($share['share_with'], basename($file)), '', array(), - $file, $link, $share['uid_owner'], Activity::TYPE_REMOTE_SHARE, Activity::PRIORITY_LOW); + $event = \OC::$server->getActivityManager()->generateEvent(); + $event->setApp(Activity::FILES_SHARING_APP) + ->setType(Activity::TYPE_REMOTE_SHARE) + ->setAffectedUser($share['uid_owner']) + ->setSubject(Activity::SUBJECT_REMOTE_SHARE_DECLINED, [$share['share_with'], basename($file)]) + ->setObject('files', $share['file_source'], $file) + ->setLink($link); + \OC::$server->getActivityManager()->publish($event); } return new \OC_OCS_Result(); diff --git a/lib/private/activity/event.php b/lib/private/activity/event.php new file mode 100644 index 00000000000..fe6fc485b7b --- /dev/null +++ b/lib/private/activity/event.php @@ -0,0 +1,250 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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 OC\Activity; + +use OCP\Activity\IEvent; + +class Event implements IEvent { + /** @var array */ + protected $data = [ + 'app' => null, + 'type' => null, + 'affected_user' => null, + 'author' => null, + 'timestamp' => null, + 'subject' => null, + 'subject_parameters' => null, + 'message' => '', + 'message_parameters' => [], + 'object_type' => '', + 'object_id' => 0, + 'object_name' => '', + 'link' => '', + ]; + + /** + * Set the app of the activity + * + * @param string $app + * @return IEvent + * @since 8.2.0 + */ + public function setApp($app) { + $this->data['app'] = (string) $app; + return $this; + } + + /** + * Set the type of the activity + * + * @param string $type + * @return IEvent + * @since 8.2.0 + */ + public function setType($type) { + $this->data['type'] = (string) $type; + return $this; + } + + /** + * Set the affected user of the activity + * + * @param string $affectedUser + * @return IEvent + * @since 8.2.0 + */ + public function setAffectedUser($affectedUser) { + $this->data['affected_user'] = (string) $affectedUser; + return $this; + } + + /** + * Set the author of the activity + * + * @param string $author + * @return IEvent + * @since 8.2.0 + */ + public function setAuthor($author) { + $this->data['author'] = (string) $author; + return $this; + } + + /** + * Set the author of the activity + * + * @param int $timestamp + * @return IEvent + * @since 8.2.0 + */ + public function setTimestamp($timestamp) { + $this->data['timestamp'] = (int) $timestamp; + return $this; + } + + /** + * Set the subject of the activity + * + * @param string $subject + * @param array $parameters + * @return IEvent + * @since 8.2.0 + */ + public function setSubject($subject, array $parameters = []) { + $this->data['subject'] = (string) $subject; + $this->data['subject_parameters'] = $parameters; + return $this; + } + + /** + * Set the message of the activity + * + * @param string $message + * @param array $parameters + * @return IEvent + * @since 8.2.0 + */ + public function setMessage($message, array $parameters = []) { + $this->data['message'] = (string) $message; + $this->data['message_parameters'] = $parameters; + return $this; + } + + /** + * Set the object of the activity + * + * @param string $objectType + * @param int $objectId + * @param string $objectName + * @return IEvent + * @since 8.2.0 + */ + public function setObject($objectType, $objectId, $objectName = '') { + $this->data['object_type'] = (string) $objectType; + $this->data['object_id'] = (int) $objectId; + $this->data['object_name'] = (string) $objectName; + return $this; + } + + /** + * Set the link of the activity + * + * @param string $link + * @return IEvent + * @since 8.2.0 + */ + public function setLink($link) { + $this->data['link'] = (string) $link; + return $this; + } + + /** + * @return string + */ + public function getApp() { + return $this->data['app']; + } + + /** + * @return string + */ + public function getType() { + return $this->data['type']; + } + + /** + * @return string + */ + public function getAffectedUser() { + return $this->data['affected_user']; + } + + /** + * @return string + */ + public function getAuthor() { + return $this->data['author']; + } + + /** + * @return int + */ + public function getTimestamp() { + return $this->data['timestamp']; + } + + /** + * @return string + */ + public function getSubject() { + return $this->data['subject']; + } + + /** + * @return array + */ + public function getSubjectParameters() { + return $this->data['subject_parameters']; + } + + /** + * @return string + */ + public function getMessage() { + return $this->data['message']; + } + + /** + * @return array + */ + public function getMessageParameters() { + return $this->data['message_parameters']; + } + + /** + * @return string + */ + public function getObjectType() { + return $this->data['object_type']; + } + + /** + * @return string + */ + public function getObjectId() { + return $this->data['object_id']; + } + + /** + * @return string + */ + public function getObjectName() { + return $this->data['object_name']; + } + + /** + * @return string + */ + public function getLink() { + return $this->data['link']; + } +} diff --git a/lib/private/activitymanager.php b/lib/private/activitymanager.php index 938335a87e1..a973db7206f 100644 --- a/lib/private/activitymanager.php +++ b/lib/private/activitymanager.php @@ -24,11 +24,14 @@ namespace OC; +use OC\Activity\Event; use OCP\Activity\IConsumer; +use OCP\Activity\IEvent; use OCP\Activity\IExtension; use OCP\Activity\IManager; use OCP\IConfig; use OCP\IRequest; +use OCP\IUser; use OCP\IUserSession; class ActivityManager implements IManager { @@ -124,36 +127,87 @@ class ActivityManager implements IManager { } /** - * @param $app - * @param $subject - * @param $subjectParams - * @param $message - * @param $messageParams - * @param $file - * @param $link - * @param $affectedUser - * @param $type - * @param $priority - * @return mixed + * Generates a new IEvent object + * + * Make sure to call at least the following methods before sending it to the + * app with via the publish() method: + * - setApp() + * - setType() + * - setAffectedUser() + * - setSubject() + * + * @return IEvent */ - function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) { - foreach($this->getConsumers() as $c) { - try { - $c->receive( - $app, - $subject, - $subjectParams, - $message, - $messageParams, - $file, - $link, - $affectedUser, - $type, - $priority); - } catch (\Exception $ex) { - // TODO: log the exception + public function generateEvent() { + return new Event(); + } + + /** + * Publish an event to the activity consumers + * + * Make sure to call at least the following methods before sending an Event: + * - setApp() + * - setType() + * - setAffectedUser() + * - setSubject() + * + * @param IEvent $event + * @return null + * @throws \BadMethodCallException if required values have not been set + */ + public function publish(IEvent $event) { + if (!$event->getApp()) { + throw new \BadMethodCallException('App not set', 10); + } + if (!$event->getType()) { + throw new \BadMethodCallException('Type not set', 11); + } + if ($event->getAffectedUser() === null) { + throw new \BadMethodCallException('Affected user not set', 12); + } + if ($event->getSubject() === null || $event->getSubjectParameters() === null) { + throw new \BadMethodCallException('Subject not set', 13); + } + + if ($event->getAuthor() === null) { + if ($this->session->getUser() instanceof IUser) { + $event->setAuthor($this->session->getUser()->getUID()); } } + + if (!$event->getTimestamp()) { + $event->setTimestamp(time()); + } + + foreach ($this->getConsumers() as $c) { + $c->receive($event); + } + } + + /** + * @param string $app The app where this event is associated with + * @param string $subject A short description of the event + * @param array $subjectParams Array with parameters that are filled in the subject + * @param string $message A longer description of the event + * @param array $messageParams Array with parameters that are filled in the message + * @param string $file The file including path where this event is associated with + * @param string $link A link where this event is associated with + * @param string $affectedUser Recipient of the activity + * @param string $type Type of the notification + * @param int $priority Priority of the notification + * @return null + */ + public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) { + $event = $this->generateEvent(); + $event->setApp($app) + ->setType($type) + ->setAffectedUser($affectedUser) + ->setSubject($subject, $subjectParams) + ->setMessage($message, $messageParams) + ->setObject('', 0, $file) + ->setLink($link); + + $this->publish($event); } /** @@ -164,7 +218,7 @@ class ActivityManager implements IManager { * * @param \Closure $callable */ - function registerConsumer(\Closure $callable) { + public function registerConsumer(\Closure $callable) { array_push($this->consumersClosures, $callable); $this->consumers = []; } @@ -178,7 +232,7 @@ class ActivityManager implements IManager { * @param \Closure $callable * @return void */ - function registerExtension(\Closure $callable) { + public function registerExtension(\Closure $callable) { array_push($this->extensionsClosures, $callable); $this->extensions = []; } @@ -189,7 +243,7 @@ class ActivityManager implements IManager { * @param string $languageCode * @return array */ - function getNotificationTypes($languageCode) { + public function getNotificationTypes($languageCode) { $notificationTypes = array(); foreach ($this->getExtensions() as $c) { $result = $c->getNotificationTypes($languageCode); @@ -205,7 +259,7 @@ class ActivityManager implements IManager { * @param string $method * @return array */ - function getDefaultTypes($method) { + public function getDefaultTypes($method) { $defaultTypes = array(); foreach ($this->getExtensions() as $c) { $types = $c->getDefaultTypes($method); @@ -220,7 +274,7 @@ class ActivityManager implements IManager { * @param string $type * @return string */ - function getTypeIcon($type) { + public function getTypeIcon($type) { if (isset($this->typeIcons[$type])) { return $this->typeIcons[$type]; } @@ -246,7 +300,7 @@ class ActivityManager implements IManager { * @param string $languageCode * @return string|false */ - function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { + public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { foreach ($this->getExtensions() as $c) { $translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode); if (is_string($translation)) { @@ -262,7 +316,7 @@ class ActivityManager implements IManager { * @param string $text * @return array|false */ - function getSpecialParameterList($app, $text) { + public function getSpecialParameterList($app, $text) { if (isset($this->specialParameters[$app][$text])) { return $this->specialParameters[$app][$text]; } @@ -287,7 +341,7 @@ class ActivityManager implements IManager { * @param array $activity * @return integer|false */ - function getGroupParameter($activity) { + public function getGroupParameter($activity) { foreach ($this->getExtensions() as $c) { $parameter = $c->getGroupParameter($activity); if ($parameter !== false) { @@ -301,7 +355,7 @@ class ActivityManager implements IManager { /** * @return array */ - function getNavigation() { + public function getNavigation() { $entries = array( 'apps' => array(), 'top' => array(), @@ -321,7 +375,7 @@ class ActivityManager implements IManager { * @param string $filterValue * @return boolean */ - function isFilterValid($filterValue) { + public function isFilterValid($filterValue) { if (isset($this->validFilters[$filterValue])) { return $this->validFilters[$filterValue]; } @@ -342,7 +396,7 @@ class ActivityManager implements IManager { * @param string $filter * @return array */ - function filterNotificationTypes($types, $filter) { + public function filterNotificationTypes($types, $filter) { if (!$this->isFilterValid($filter)) { return $types; } @@ -360,7 +414,7 @@ class ActivityManager implements IManager { * @param string $filter * @return array */ - function getQueryForFilter($filter) { + public function getQueryForFilter($filter) { if (!$this->isFilterValid($filter)) { return [null, null]; } diff --git a/lib/public/activity/iconsumer.php b/lib/public/activity/iconsumer.php index a55110ababc..e74884d76c5 100644 --- a/lib/public/activity/iconsumer.php +++ b/lib/public/activity/iconsumer.php @@ -37,19 +37,11 @@ namespace OCP\Activity; */ interface IConsumer { /** - * @param $app - * @param $subject - * @param $subjectParams - * @param $message - * @param $messageParams - * @param $file - * @param $link - * @param $affectedUser - * @param $type - * @param $priority - * @return mixed + * @param IEvent $event + * @return null * @since 6.0.0 + * @since 8.2.0 Replaced the parameters with an IEvent object */ - function receive($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority ); + public function receive(IEvent $event); } diff --git a/lib/public/activity/ievent.php b/lib/public/activity/ievent.php new file mode 100644 index 00000000000..184c7ae503f --- /dev/null +++ b/lib/public/activity/ievent.php @@ -0,0 +1,200 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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/> + * + */ + +/** + * Public interface of ownCloud for apps to use. + * Activity/IEvent 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 IEvent + * + * @package OCP\Activity + * @since 8.2.0 + */ +interface IEvent { + /** + * Set the app of the activity + * + * @param string $app + * @return IEvent + * @since 8.2.0 + */ + public function setApp($app); + + /** + * Set the type of the activity + * + * @param string $type + * @return IEvent + * @since 8.2.0 + */ + public function setType($type); + + /** + * Set the affected user of the activity + * + * @param string $user + * @return IEvent + * @since 8.2.0 + */ + public function setAffectedUser($user); + + /** + * Set the author of the activity + * + * @param string $author + * @return IEvent + * @since 8.2.0 + */ + public function setAuthor($author); + + /** + * Set the author of the activity + * + * @param int $timestamp + * @return IEvent + * @since 8.2.0 + */ + public function setTimestamp($timestamp); + + /** + * Set the subject of the activity + * + * @param string $subject + * @param array $parameters + * @return IEvent + * @since 8.2.0 + */ + public function setSubject($subject, array $parameters = []); + + /** + * Set the message of the activity + * + * @param string $message + * @param array $parameters + * @return IEvent + * @since 8.2.0 + */ + public function setMessage($message, array $parameters = []); + + /** + * Set the object of the activity + * + * @param string $objectType + * @param int $objectId + * @param string $objectName + * @return IEvent + * @since 8.2.0 + */ + public function setObject($objectType, $objectId, $objectName = ''); + + /** + * Set the link of the activity + * + * @param string $link + * @return IEvent + * @since 8.2.0 + */ + public function setLink($link); + + /** + * @return string + * @since 8.2.0 + */ + public function getApp(); + + /** + * @return string + * @since 8.2.0 + */ + public function getType(); + + /** + * @return string + * @since 8.2.0 + */ + public function getAffectedUser(); + + /** + * @return string + * @since 8.2.0 + */ + public function getAuthor(); + + /** + * @return int + * @since 8.2.0 + */ + public function getTimestamp(); + + /** + * @return string + * @since 8.2.0 + */ + public function getSubject(); + + /** + * @return array + * @since 8.2.0 + */ + public function getSubjectParameters(); + + /** + * @return string + * @since 8.2.0 + */ + public function getMessage(); + + /** + * @return array + * @since 8.2.0 + */ + public function getMessageParameters(); + + /** + * @return string + * @since 8.2.0 + */ + public function getObjectType(); + + /** + * @return string + * @since 8.2.0 + */ + public function getObjectId(); + + /** + * @return string + * @since 8.2.0 + */ + public function getObjectName(); + + /** + * @return string + * @since 8.2.0 + */ + public function getLink(); +} diff --git a/lib/public/activity/imanager.php b/lib/public/activity/imanager.php index 0f5dccd8ba1..b3a4969fb06 100644 --- a/lib/public/activity/imanager.php +++ b/lib/public/activity/imanager.php @@ -38,22 +38,52 @@ namespace OCP\Activity; * @since 6.0.0 */ interface IManager { + /** + * Generates a new IEvent object + * + * Make sure to call at least the following methods before sending it to the + * app with via the publish() method: + * - setApp() + * - setType() + * - setAffectedUser() + * - setSubject() + * + * @return IEvent + * @since 8.2.0 + */ + public function generateEvent(); + + /** + * Publish an event to the activity consumers + * + * Make sure to call at least the following methods before sending an Event: + * - setApp() + * - setType() + * - setAffectedUser() + * - setSubject() + * + * @param IEvent $event + * @return null + * @since 8.2.0 + */ + public function publish(IEvent $event); /** - * @param $app - * @param $subject - * @param $subjectParams - * @param $message - * @param $messageParams - * @param $file - * @param $link - * @param $affectedUser - * @param $type - * @param $priority - * @return mixed + * @param string $app The app where this event is associated with + * @param string $subject A short description of the event + * @param array $subjectParams Array with parameters that are filled in the subject + * @param string $message A longer description of the event + * @param array $messageParams Array with parameters that are filled in the message + * @param string $file The file including path where this event is associated with + * @param string $link A link where this event is associated with + * @param string $affectedUser Recipient of the activity + * @param string $type Type of the notification + * @param int $priority Priority of the notification + * @return null * @since 6.0.0 + * @deprecated 8.2.0 Grab an IEvent from generateEvent() instead and use the publish() method */ - function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority); + public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority); /** * In order to improve lazy loading a closure can be registered which will be called in case @@ -65,7 +95,7 @@ interface IManager { * @return void * @since 6.0.0 */ - function registerConsumer(\Closure $callable); + public function registerConsumer(\Closure $callable); /** * In order to improve lazy loading a closure can be registered which will be called in case @@ -77,7 +107,7 @@ interface IManager { * @return void * @since 8.0.0 */ - function registerExtension(\Closure $callable); + public function registerExtension(\Closure $callable); /** * Will return additional notification types as specified by other apps @@ -91,21 +121,21 @@ interface IManager { * @since 8.0.0 * @changed 8.2.0 - Added support to allow limiting notifications to certain methods */ - function getNotificationTypes($languageCode); + public function getNotificationTypes($languageCode); /** * @param string $method * @return array * @since 8.0.0 */ - function getDefaultTypes($method); + public function getDefaultTypes($method); /** * @param string $type * @return string * @since 8.0.0 */ - function getTypeIcon($type); + public function getTypeIcon($type); /** * @param string $app @@ -117,7 +147,7 @@ interface IManager { * @return string|false * @since 8.0.0 */ - function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode); + public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode); /** * @param string $app @@ -125,27 +155,27 @@ interface IManager { * @return array|false * @since 8.0.0 */ - function getSpecialParameterList($app, $text); + public function getSpecialParameterList($app, $text); /** * @param array $activity * @return integer|false * @since 8.0.0 */ - function getGroupParameter($activity); + public function getGroupParameter($activity); /** * @return array * @since 8.0.0 */ - function getNavigation(); + public function getNavigation(); /** * @param string $filterValue * @return boolean * @since 8.0.0 */ - function isFilterValid($filterValue); + public function isFilterValid($filterValue); /** * @param array $types @@ -153,14 +183,14 @@ interface IManager { * @return array * @since 8.0.0 */ - function filterNotificationTypes($types, $filter); + public function filterNotificationTypes($types, $filter); /** * @param string $filter * @return array * @since 8.0.0 */ - function getQueryForFilter($filter); + public function getQueryForFilter($filter); /** * Get the user we need to use diff --git a/tests/lib/activitymanager.php b/tests/lib/activitymanager.php index 28caf575948..26759d46270 100644 --- a/tests/lib/activitymanager.php +++ b/tests/lib/activitymanager.php @@ -41,6 +41,9 @@ class Test_ActivityManager extends \Test\TestCase { $this->config ); + $this->assertSame([], $this->invokePrivate($this->activityManager, 'getConsumers')); + $this->assertSame([], $this->invokePrivate($this->activityManager, 'getExtensions')); + $this->activityManager->registerConsumer(function() { return new NoOpConsumer(); }); @@ -50,6 +53,11 @@ class Test_ActivityManager extends \Test\TestCase { $this->activityManager->registerExtension(function() { return new SimpleExtension(); }); + + $this->assertNotEmpty($this->invokePrivate($this->activityManager, 'getConsumers')); + $this->assertNotEmpty($this->invokePrivate($this->activityManager, 'getConsumers')); + $this->assertNotEmpty($this->invokePrivate($this->activityManager, 'getExtensions')); + $this->assertNotEmpty($this->invokePrivate($this->activityManager, 'getExtensions')); } public function testGetConsumers() { @@ -249,6 +257,192 @@ class Test_ActivityManager extends \Test\TestCase { ->method('getUser') ->willReturn($mockUser); } + + /** + * @expectedException BadMethodCallException + * @expectedExceptionMessage App not set + * @expectedExceptionCode 10 + */ + public function testPublishExceptionNoApp() { + $event = new \OC\Activity\Event(); + $this->activityManager->publish($event); + } + + /** + * @expectedException BadMethodCallException + * @expectedExceptionMessage Type not set + * @expectedExceptionCode 11 + */ + public function testPublishExceptionNoType() { + $event = new \OC\Activity\Event(); + $event->setApp('test'); + $this->activityManager->publish($event); + } + + /** + * @expectedException BadMethodCallException + * @expectedExceptionMessage Affected user not set + * @expectedExceptionCode 12 + */ + public function testPublishExceptionNoAffectedUser() { + $event = new \OC\Activity\Event(); + $event->setApp('test') + ->setType('test_type'); + $this->activityManager->publish($event); + } + + /** + * @expectedException BadMethodCallException + * @expectedExceptionMessage Subject not set + * @expectedExceptionCode 13 + */ + public function testPublishExceptionNoSubject() { + $event = new \OC\Activity\Event(); + $event->setApp('test') + ->setType('test_type') + ->setAffectedUser('test_affected'); + $this->activityManager->publish($event); + } + + public function dataPublish() { + return [ + [null], + ['test_author'], + ]; + } + + /** + * @dataProvider dataPublish + * @param string $author + */ + public function testPublish($author) { + if ($author !== null) { + $authorObject = $this->getMockBuilder('OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $authorObject->expects($this->once()) + ->method('getUID') + ->willReturn($author); + $this->session->expects($this->atLeastOnce()) + ->method('getUser') + ->willReturn($authorObject); + } + + $event = new \OC\Activity\Event(); + $event->setApp('test') + ->setType('test_type') + ->setSubject('test_subject', []) + ->setAffectedUser('test_affected'); + + $consumer = $this->getMockBuilder('OCP\Activity\IConsumer') + ->disableOriginalConstructor() + ->getMock(); + $consumer->expects($this->once()) + ->method('receive') + ->with($event) + ->willReturnCallback(function(\OCP\Activity\IEvent $event) use ($author) { + $this->assertLessThanOrEqual(time() + 2, $event->getTimestamp(), 'Timestamp not set correctly'); + $this->assertGreaterThanOrEqual(time() - 2, $event->getTimestamp(), 'Timestamp not set correctly'); + $this->assertSame($author, $event->getAuthor(), 'Author name not set correctly'); + }); + $this->activityManager->registerConsumer(function () use ($consumer) { + return $consumer; + }); + + $this->activityManager->publish($event); + } + + public function testPublishAllManually() { + $event = new \OC\Activity\Event(); + $event->setApp('test_app') + ->setType('test_type') + ->setAffectedUser('test_affected') + ->setAuthor('test_author') + ->setTimestamp(1337) + ->setSubject('test_subject', ['test_subject_param']) + ->setMessage('test_message', ['test_message_param']) + ->setObject('test_object_type', 42, 'test_object_name') + ->setLink('test_link') + ; + + $consumer = $this->getMockBuilder('OCP\Activity\IConsumer') + ->disableOriginalConstructor() + ->getMock(); + $consumer->expects($this->once()) + ->method('receive') + ->willReturnCallback(function(\OCP\Activity\IEvent $event) { + $this->assertSame('test_app', $event->getApp(), 'App not set correctly'); + $this->assertSame('test_type', $event->getType(), 'Type not set correctly'); + $this->assertSame('test_affected', $event->getAffectedUser(), 'Affected user not set correctly'); + $this->assertSame('test_author', $event->getAuthor(), 'Author not set correctly'); + $this->assertSame(1337, $event->getTimestamp(), 'Timestamp not set correctly'); + $this->assertSame('test_subject', $event->getSubject(), 'Subject not set correctly'); + $this->assertSame(['test_subject_param'], $event->getSubjectParameters(), 'Subject parameter not set correctly'); + $this->assertSame('test_message', $event->getMessage(), 'Message not set correctly'); + $this->assertSame(['test_message_param'], $event->getMessageParameters(), 'Message parameter not set correctly'); + $this->assertSame('test_object_type', $event->getObjectType(), 'Object type not set correctly'); + $this->assertSame(42, $event->getObjectId(), 'Object ID not set correctly'); + $this->assertSame('test_object_name', $event->getObjectName(), 'Object name not set correctly'); + $this->assertSame('test_link', $event->getLink(), 'Link not set correctly'); + }); + $this->activityManager->registerConsumer(function () use ($consumer) { + return $consumer; + }); + + $this->activityManager->publish($event); + } + + public function testDeprecatedPublishActivity() { + $event = new \OC\Activity\Event(); + $event->setApp('test_app') + ->setType('test_type') + ->setAffectedUser('test_affected') + ->setAuthor('test_author') + ->setTimestamp(1337) + ->setSubject('test_subject', ['test_subject_param']) + ->setMessage('test_message', ['test_message_param']) + ->setObject('test_object_type', 42, 'test_object_name') + ->setLink('test_link') + ; + + $consumer = $this->getMockBuilder('OCP\Activity\IConsumer') + ->disableOriginalConstructor() + ->getMock(); + $consumer->expects($this->once()) + ->method('receive') + ->willReturnCallback(function(\OCP\Activity\IEvent $event) { + $this->assertSame('test_app', $event->getApp(), 'App not set correctly'); + $this->assertSame('test_type', $event->getType(), 'Type not set correctly'); + $this->assertSame('test_affected', $event->getAffectedUser(), 'Affected user not set correctly'); + $this->assertSame('test_subject', $event->getSubject(), 'Subject not set correctly'); + $this->assertSame(['test_subject_param'], $event->getSubjectParameters(), 'Subject parameter not set correctly'); + $this->assertSame('test_message', $event->getMessage(), 'Message not set correctly'); + $this->assertSame(['test_message_param'], $event->getMessageParameters(), 'Message parameter not set correctly'); + $this->assertSame('test_object_name', $event->getObjectName(), 'Object name not set correctly'); + $this->assertSame('test_link', $event->getLink(), 'Link not set correctly'); + + // The following values can not be used via publishActivity() + $this->assertLessThanOrEqual(time() + 2, $event->getTimestamp(), 'Timestamp not set correctly'); + $this->assertGreaterThanOrEqual(time() - 2, $event->getTimestamp(), 'Timestamp not set correctly'); + $this->assertSame(null, $event->getAuthor(), 'Author not set correctly'); + $this->assertSame('', $event->getObjectType(), 'Object type should not be set'); + $this->assertSame(0, $event->getObjectId(), 'Object ID should not be set'); + }); + $this->activityManager->registerConsumer(function () use ($consumer) { + return $consumer; + }); + + $this->activityManager->publishActivity( + $event->getApp(), + $event->getSubject(), $event->getSubjectParameters(), + $event->getMessage(), $event->getMessageParameters(), + $event->getObjectName(), + $event->getLink(), + $event->getAffectedUser(), + $event->getType(), + \OCP\Activity\IExtension::PRIORITY_MEDIUM + ); + } } class SimpleExtension implements \OCP\Activity\IExtension { @@ -368,6 +562,7 @@ class NoOpExtension implements \OCP\Activity\IExtension { class NoOpConsumer implements \OCP\Activity\IConsumer { - public function receive($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) { + public function receive(\OCP\Activity\IEvent $event) { + } } |