From cbbb7a752c62523b465834d0c486ccf7fc90d0b1 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 2 Jul 2015 13:45:48 +0200 Subject: [PATCH] Find the federated cloud id in contacts for better readability --- appinfo/application.php | 1 + lib/parameterhelper.php | 56 ++++++++++++++++++++++++++- tests/datahelpertest.php | 2 + tests/grouphelpertest.php | 1 + tests/parameterhelpertest.php | 72 ++++++++++++++++++++++++++++++++++- 5 files changed, 129 insertions(+), 3 deletions(-) diff --git a/appinfo/application.php b/appinfo/application.php index 1d7d829e..683c5e19 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -74,6 +74,7 @@ class Application extends App { $server->getActivityManager(), $server->getUserManager(), $server->getURLGenerator(), + $server->getContactsManager(), new View(''), $server->getConfig(), $c->query('ActivityL10N'), diff --git a/lib/parameterhelper.php b/lib/parameterhelper.php index b3c14c52..40b32218 100644 --- a/lib/parameterhelper.php +++ b/lib/parameterhelper.php @@ -23,7 +23,9 @@ namespace OCA\Activity; +use OC\Share\Helper; use OCP\Activity\IManager; +use OCP\Contacts\IManager as IContactsManager; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; @@ -38,6 +40,9 @@ class ParameterHelper { /** @var \OCP\IUserManager */ protected $userManager; + /** @var \OCP\Contacts\IManager */ + protected $contactsManager; + /** @var \OC\Files\View */ protected $rootView; @@ -57,6 +62,7 @@ class ParameterHelper { * @param IManager $activityManager * @param IUserManager $userManager * @param IURLGenerator $urlGenerator + * @param IContactsManager $contactsManager * @param View $rootView * @param IConfig $config * @param IL10N $l @@ -65,6 +71,7 @@ class ParameterHelper { public function __construct(IManager $activityManager, IUserManager $userManager, IURLGenerator $urlGenerator, + IContactsManager $contactsManager, View $rootView, IConfig $config, IL10N $l, @@ -72,6 +79,7 @@ class ParameterHelper { $this->activityManager = $activityManager; $this->userManager = $userManager; $this->urlGenerator = $urlGenerator; + $this->contactsManager = $contactsManager; $this->rootView = $rootView; $this->config = $config; $this->l = $l; @@ -126,6 +134,8 @@ class ParameterHelper { return $this->prepareFileParam($param, $stripPath, $highlightParams); } else if ($paramType === 'username') { return $this->prepareUserParam($param, $highlightParams); + } else if ($paramType === 'federated_cloud_id') { + return $this->prepareFederatedCloudIDParam($param, $stripPath, $highlightParams); } return $this->prepareParam($param, $highlightParams); } @@ -186,14 +196,56 @@ class ParameterHelper { $user = $this->userManager->get($param); $displayName = ($user) ? $user->getDisplayName() : $param; $param = Util::sanitizeHTML($param); - $displayName = Util::sanitizeHTML($displayName); if ($highlightParams) { $avatarPlaceholder = ''; if ($this->config->getSystemValue('enable_avatars', true)) { $avatarPlaceholder = '
'; } - return $avatarPlaceholder . '' . $displayName . ''; + return $avatarPlaceholder . '' . Util::sanitizeHTML($displayName) . ''; + } else { + return $displayName; + } + } + + /** + * Prepares a federated cloud id parameter for usage + * + * Search in contacts and do not output the remote in html + * + * @param string $federatedCloudId + * @param bool $stripRemote Shall we remove the remote + * @param bool $highlightParams + * @return string + */ + protected function prepareFederatedCloudIDParam($federatedCloudId, $stripRemote, $highlightParams) { + $displayName = $federatedCloudId; + if ($stripRemote) { + try { + list($user,) = Helper::splitUserRemote($federatedCloudId); + $displayName = $user . '@…'; + } catch (\OC\HintException $e) {} + } + + /** + * Try to find the user in the contacts + */ + $addressBookEntries = $this->contactsManager->search(strtolower($federatedCloudId), ['CLOUD']); + foreach ($addressBookEntries as $entry) { + if (isset($entry['CLOUD'])) { + foreach ($entry['CLOUD'] as $cloudID) { + if ($cloudID === strtolower($federatedCloudId)) { + $displayName = $entry['FN']; + // We got a name, exit the loops + break 2; + } + } + } + } + + if ($highlightParams) { + $title = ' title="' . Util::sanitizeHTML($federatedCloudId) . '"'; + return '' . Util::sanitizeHTML($displayName) . ''; } else { return $displayName; } diff --git a/tests/datahelpertest.php b/tests/datahelpertest.php index 657c6353..4a4cc517 100644 --- a/tests/datahelpertest.php +++ b/tests/datahelpertest.php @@ -167,6 +167,7 @@ class DataHelperTest extends TestCase { $activityManager, $this->getMockBuilder('OCP\IUserManager')->disableOriginalConstructor()->getMock(), $urlGenerator, + $this->getMockBuilder('OCP\Contacts\IManager')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('OC\Files\View')->disableOriginalConstructor()->getMock(), $config, $activityLanguage, @@ -220,6 +221,7 @@ class DataHelperTest extends TestCase { $activityManager, $this->getMockBuilder('OCP\IUserManager')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('\OCP\IURLGenerator')->disableOriginalConstructor()->getMock(), + $this->getMockBuilder('OCP\Contacts\IManager')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('OC\Files\View')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('OCP\IConfig')->disableOriginalConstructor()->getMock(), $activityLanguage, diff --git a/tests/grouphelpertest.php b/tests/grouphelpertest.php index d484f437..fc1b75d1 100644 --- a/tests/grouphelpertest.php +++ b/tests/grouphelpertest.php @@ -584,6 +584,7 @@ class GroupHelperTest extends TestCase { $activityManager, $this->getMockBuilder('OCP\IUserManager')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('\OCP\IURLGenerator')->disableOriginalConstructor()->getMock(), + $this->getMockBuilder('OCP\Contacts\IManager')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('OC\Files\View')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('OCP\IConfig')->disableOriginalConstructor()->getMock(), $activityLanguage, diff --git a/tests/parameterhelpertest.php b/tests/parameterhelpertest.php index d11e6b23..3d446d9f 100644 --- a/tests/parameterhelpertest.php +++ b/tests/parameterhelpertest.php @@ -33,6 +33,7 @@ class ParameterHelperTest extends TestCase { /** @var \PHPUnit_Framework_MockObject_MockObject */ protected $view; /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $contactsManager; /** @var \PHPUnit_Framework_MockObject_MockObject */ protected $config; @@ -82,12 +83,16 @@ class ParameterHelperTest extends TestCase { ['user2', $this->getUserMockDisplayName('user1', 'User Two')], ['user', $this->getUserMockDisplayName('user', 'User ')], ]); + $this->contactsManager = $this->getMockBuilder('OCP\Contacts\IManager') + ->disableOriginalConstructor() + ->getMock(); /** @var \OC\Files\View $view */ $this->parameterHelper = new \OCA\Activity\ParameterHelper( $activityManager, $this->userManager, $urlGenerator, + $this->contactsManager, $view, $this->config, $activityLanguage, @@ -115,12 +120,18 @@ class ParameterHelperTest extends TestCase { return array( array(array(), false, false, false, array()), + /** + * No type + */ // No file position: no path strip array(array('/foo/bar.file'), array(), false, false, array('/foo/bar.file')), array(array('/foo/bar.file'), array(), true, false, array('/foo/bar.file')), array(array('/foo/bar.file'), array(), false, true, array('/foo/bar.file')), array(array('/foo/bar.file'), array(), true, true, array('/foo/bar.file')), + /** + * File + */ // Valid file position array(array('/foo/bar.file'), array(0 => 'file'), true, false, array('bar.file')), array(array('/folder/trailingslash/fromsharing/'), array(0 => 'file'), true, false, array('fromsharing')), @@ -156,6 +167,10 @@ class ParameterHelperTest extends TestCase { 'UserA', '/foo/bar.file', )), + + /** + * User + */ array(array('user1', '/foo/bar.file'), array(0 => 'username'), true, true, array( '
User One', '/foo/bar.file', @@ -187,13 +202,41 @@ class ParameterHelperTest extends TestCase { array(array('NoAvatar'), array(0 => 'username'), true, true, array( 'NoAvatar', ), '', false), + + /** + * Federated Cloud ID + */ + array(array('username@localhost'), array(0 => 'federated_cloud_id'), false, true, array( + 'username@localhost', + )), + array(array('username@localhost'), array(0 => 'federated_cloud_id'), false, false, array( + 'username@localhost', + )), + array(array('username@localhost'), array(0 => 'federated_cloud_id'), true, true, array( + 'username@…', + )), + array(array('username@localhost'), array(0 => 'federated_cloud_id'), true, false, array( + 'username@…', + )), + array(array('username@localhost'), array(0 => 'federated_cloud_id'), false, true, array( + 'User @ Localhost', + ), '', true, true), + array(array('username@localhost'), array(0 => 'federated_cloud_id'), false, false, array( + 'User @ Localhost', + ), '', true, true), + array(array('username@localhost'), array(0 => 'federated_cloud_id'), true, true, array( + 'User @ Localhost', + ), '', true, true), + array(array('username@localhost'), array(0 => 'federated_cloud_id'), true, false, array( + 'User @ Localhost', + ), '', true, true), ); } /** * @dataProvider prepareParametersData */ - public function testPrepareParameters($params, $filePosition, $stripPath, $highlightParams, $expected, $createFolder = '', $enableAvatars = true) { + public function testPrepareParameters($params, $filePosition, $stripPath, $highlightParams, $expected, $createFolder = '', $enableAvatars = true, $contactsResult = false) { if ($createFolder !== '') { $this->view->expects($this->any()) ->method('is_dir') @@ -201,6 +244,33 @@ class ParameterHelperTest extends TestCase { ->willReturn(true); } + if ($contactsResult) { + $this->contactsManager->expects($this->any()) + ->method('search') + ->with($params[0], ['CLOUD']) + ->willReturn([ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'CLOUD' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'CLOUD' => [ + 'username@localhost', + ], + ], + ]); + } else { + $this->contactsManager->expects($this->any()) + ->method('search') + ->with($this->anything(), ['CLOUD']) + ->willReturn([]); + } + $this->config->expects($this->any()) ->method('getSystemValue') ->with('enable_avatars', true)