From 4a96e222588104f647f911857e370b3ab692ea22 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 6 Sep 2017 16:09:29 +0200 Subject: [PATCH] don't keep result types hard coded Signed-off-by: Arthur Schiwon --- .../lib/Controller/ShareesAPIController.php | 3 +- .../Collaborators/CirclePlugin.php | 5 +- .../Collaborators/GroupPlugin.php | 6 +- .../Collaborators/LookupPlugin.php | 5 +- .../Collaborators/MailPlugin.php | 20 ++++--- .../Collaborators/RemotePlugin.php | 8 ++- .../Collaboration/Collaborators/Search.php | 11 ++-- .../Collaborators/SearchResult.php | 41 +++++-------- .../Collaborators/UserPlugin.php | 4 +- .../Collaborators/ISearchResult.php | 20 +++---- .../Collaborators/SearchResultType.php | 57 +++++++++++++++++++ 11 files changed, 122 insertions(+), 58 deletions(-) create mode 100644 lib/public/Collaboration/Collaborators/SearchResultType.php diff --git a/apps/files_sharing/lib/Controller/ShareesAPIController.php b/apps/files_sharing/lib/Controller/ShareesAPIController.php index 0acec15047..9e714226d2 100644 --- a/apps/files_sharing/lib/Controller/ShareesAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareesAPIController.php @@ -198,7 +198,8 @@ class ShareesAPIController extends OCSController { list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset); - $response = new DataResponse($result); + $this->result = array_merge($this->result, $result); + $response = new DataResponse($this->result); if ($hasMoreResults) { $response->addHeader('Link', $this->getPaginationLink($page, [ diff --git a/lib/private/Collaboration/Collaborators/CirclePlugin.php b/lib/private/Collaboration/Collaborators/CirclePlugin.php index 3d476644ec..951f70e968 100644 --- a/lib/private/Collaboration/Collaborators/CirclePlugin.php +++ b/lib/private/Collaboration/Collaborators/CirclePlugin.php @@ -27,8 +27,10 @@ namespace OC\Collaboration\Collaborators; use OCA\Circles\Api\Sharees; use OCP\Collaboration\Collaborators\ISearchPlugin; use OCP\Collaboration\Collaborators\ISearchResult; +use OCP\Collaboration\Collaborators\SearchResultType; class CirclePlugin implements ISearchPlugin { + public function search($search, $limit, $offset, ISearchResult $searchResult) { $result = ['wide' => [], 'exact' => []]; @@ -41,7 +43,8 @@ class CirclePlugin implements ISearchPlugin { $result['wide'] = $circles['circles']; } - $searchResult->addResultSet('circles', $result['wide'], $result['exact']); + $type = new SearchResultType('circles'); + $searchResult->addResultSet($type, $result['wide'], $result['exact']); } return false; diff --git a/lib/private/Collaboration/Collaborators/GroupPlugin.php b/lib/private/Collaboration/Collaborators/GroupPlugin.php index d156190eed..0b2b3d7102 100644 --- a/lib/private/Collaboration/Collaborators/GroupPlugin.php +++ b/lib/private/Collaboration/Collaborators/GroupPlugin.php @@ -25,6 +25,7 @@ namespace OC\Collaboration\Collaborators; use OCP\Collaboration\Collaborators\ISearchPlugin; use OCP\Collaboration\Collaborators\ISearchResult; +use OCP\Collaboration\Collaborators\SearchResultType; use OCP\IConfig; use OCP\IGroup; use OCP\IGroupManager; @@ -115,8 +116,9 @@ class GroupPlugin implements ISearchPlugin { $result['wide'] = []; } - $searchResult->addResultSet('groups', $result['wide'], $result['exact']); + $type = new SearchResultType('groups'); + $searchResult->addResultSet($type, $result['wide'], $result['exact']); - return [$result, $hasMoreResults]; + return $hasMoreResults; } } diff --git a/lib/private/Collaboration/Collaborators/LookupPlugin.php b/lib/private/Collaboration/Collaborators/LookupPlugin.php index 567b41ca76..e3a04fb7dd 100644 --- a/lib/private/Collaboration/Collaborators/LookupPlugin.php +++ b/lib/private/Collaboration/Collaborators/LookupPlugin.php @@ -26,6 +26,7 @@ namespace OC\Collaboration\Collaborators; use OCP\Collaboration\Collaborators\ISearchPlugin; use OCP\Collaboration\Collaborators\ISearchResult; +use OCP\Collaboration\Collaborators\SearchResultType; use OCP\Http\Client\IClientService; use OCP\IConfig; use OCP\Share; @@ -77,7 +78,9 @@ class LookupPlugin implements ISearchPlugin { } catch (\Exception $e) { } - $searchResult->addResultSet('lookup', $result, []); + $type = new SearchResultType('lookup'); + $searchResult->addResultSet($type, $result, []); + return false; } } diff --git a/lib/private/Collaboration/Collaborators/MailPlugin.php b/lib/private/Collaboration/Collaborators/MailPlugin.php index bd4d70e41d..e6bda17656 100644 --- a/lib/private/Collaboration/Collaborators/MailPlugin.php +++ b/lib/private/Collaboration/Collaborators/MailPlugin.php @@ -26,6 +26,7 @@ namespace OC\Collaboration\Collaborators; use OCP\Collaboration\Collaborators\ISearchPlugin; use OCP\Collaboration\Collaborators\ISearchResult; +use OCP\Collaboration\Collaborators\SearchResultType; use OCP\Contacts\IManager; use OCP\Federation\ICloudIdManager; use OCP\IConfig; @@ -59,6 +60,8 @@ class MailPlugin implements ISearchPlugin { */ public function search($search, $limit, $offset, ISearchResult $searchResult) { $result = ['wide' => [], 'exact' => []]; + $userType = new SearchResultType('users'); + $emailType = new SearchResultType('emails'); // Search in contacts //@todo Pagination missing @@ -81,7 +84,7 @@ class MailPlugin implements ISearchPlugin { continue; } - if (!$searchResult->hasResult('users', $cloud->getUser())) { + if (!$searchResult->hasResult($userType, $cloud->getUser())) { $singleResult = [[ 'label' => $contact['FN'] . " ($emailAddress)", 'value' => [ @@ -89,8 +92,8 @@ class MailPlugin implements ISearchPlugin { 'shareWith' => $cloud->getUser(), ], ]]; - $searchResult->addResultSet('users', [], $singleResult); - $searchResult->markExactIdMatch('emails'); + $searchResult->addResultSet($userType, [], $singleResult); + $searchResult->markExactIdMatch($emailType); } return false; } @@ -102,7 +105,7 @@ class MailPlugin implements ISearchPlugin { continue; } - if (!$searchResult->hasResult('users', $cloud->getUser())) { + if (!$searchResult->hasResult($userType, $cloud->getUser())) { $singleResult = [[ 'label' => $contact['FN'] . " ($emailAddress)", 'value' => [ @@ -110,8 +113,7 @@ class MailPlugin implements ISearchPlugin { 'shareWith' => $cloud->getUser(), ]], ]; - $searchResult->addResultSet('users', $singleResult, []); - $result = []; + $searchResult->addResultSet($userType, $singleResult, []); } } continue; @@ -119,7 +121,7 @@ class MailPlugin implements ISearchPlugin { if ($exactEmailMatch || strtolower($contact['FN']) === $lowerSearch) { if ($exactEmailMatch) { - $searchResult->markExactIdMatch('emails'); + $searchResult->markExactIdMatch($emailType); } $result['exact'][] = [ 'label' => $contact['FN'] . " ($emailAddress)", @@ -145,7 +147,7 @@ class MailPlugin implements ISearchPlugin { $result['wide'] = []; } - if (!$searchResult->hasExactIdMatch('emails') && filter_var($search, FILTER_VALIDATE_EMAIL)) { + if (!$searchResult->hasExactIdMatch($emailType) && filter_var($search, FILTER_VALIDATE_EMAIL)) { $result['exact'][] = [ 'label' => $search, 'value' => [ @@ -155,7 +157,7 @@ class MailPlugin implements ISearchPlugin { ]; } - $searchResult->addResultSet('emails', $result['wide'], $result['exact']); + $searchResult->addResultSet($emailType, $result['wide'], $result['exact']); return false; } diff --git a/lib/private/Collaboration/Collaborators/RemotePlugin.php b/lib/private/Collaboration/Collaborators/RemotePlugin.php index ff075b8e42..c97a951be7 100644 --- a/lib/private/Collaboration/Collaborators/RemotePlugin.php +++ b/lib/private/Collaboration/Collaborators/RemotePlugin.php @@ -26,6 +26,7 @@ namespace OC\Collaboration\Collaborators; use OCP\Collaboration\Collaborators\ISearchPlugin; use OCP\Collaboration\Collaborators\ISearchResult; +use OCP\Collaboration\Collaborators\SearchResultType; use OCP\Contacts\IManager; use OCP\Federation\ICloudIdManager; use OCP\IConfig; @@ -51,6 +52,7 @@ class RemotePlugin implements ISearchPlugin { public function search($search, $limit, $offset, ISearchResult $searchResult) { $result = ['wide' => [], 'exact' => []]; + $resultType = new SearchResultType('remotes'); // Search in contacts //@todo Pagination missing @@ -74,7 +76,7 @@ class RemotePlugin implements ISearchPlugin { if (strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) { if (strtolower($cloudId) === $lowerSearch) { - $searchResult->hasExactIdMatch('remotes'); + $searchResult->hasExactIdMatch($resultType); } $result['exact'][] = [ 'label' => $contact['FN'] . " ($cloudId)", @@ -102,7 +104,7 @@ class RemotePlugin implements ISearchPlugin { $result['wide'] = []; } - if (!$searchResult->hasExactIdMatch('remotes') && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) { + if (!$searchResult->hasExactIdMatch($resultType) && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) { $result['exact'][] = [ 'label' => $search, 'value' => [ @@ -112,7 +114,7 @@ class RemotePlugin implements ISearchPlugin { ]; } - $searchResult->addResultSet('remotes', $result['wide'], $result['exact']); + $searchResult->addResultSet($resultType, $result['wide'], $result['exact']); return false; } diff --git a/lib/private/Collaboration/Collaborators/Search.php b/lib/private/Collaboration/Collaborators/Search.php index abca13bcfe..815b9eea3e 100644 --- a/lib/private/Collaboration/Collaborators/Search.php +++ b/lib/private/Collaboration/Collaborators/Search.php @@ -26,6 +26,7 @@ namespace OC\Collaboration\Collaborators; use OCP\Collaboration\Collaborators\ISearch; use OCP\Collaboration\Collaborators\ISearchPlugin; use OCP\Collaboration\Collaborators\ISearchResult; +use OCP\Collaboration\Collaborators\SearchResultType; use OCP\IContainer; use OCP\Share; @@ -71,10 +72,12 @@ class Search implements ISearch { // if we have a exact match, either for the federated cloud id or for the // email address we only return the exact match. It is highly unlikely // that the exact same email address and federated cloud id exists - if($searchResult->hasExactIdMatch('emails') && !$searchResult->hasExactIdMatch('remotes')) { - $searchResult->unsetResult('remotes'); - } elseif (!$searchResult->hasExactIdMatch('emails') && $searchResult->hasExactIdMatch('remotes')) { - $searchResult->unsetResult('emails'); + $emailType = new SearchResultType('emails'); + $remoteType = new SearchResultType('remotes'); + if($searchResult->hasExactIdMatch($emailType) && !$searchResult->hasExactIdMatch($remoteType)) { + $searchResult->unsetResult($remoteType); + } elseif (!$searchResult->hasExactIdMatch($emailType) && $searchResult->hasExactIdMatch($remoteType)) { + $searchResult->unsetResult($emailType); } return [$searchResult->asArray(), $hasMoreResults]; diff --git a/lib/private/Collaboration/Collaborators/SearchResult.php b/lib/private/Collaboration/Collaborators/SearchResult.php index f3559c9327..9c9503ea94 100644 --- a/lib/private/Collaboration/Collaborators/SearchResult.php +++ b/lib/private/Collaboration/Collaborators/SearchResult.php @@ -25,30 +25,21 @@ namespace OC\Collaboration\Collaborators; use OCP\Collaboration\Collaborators\ISearchResult; +use OCP\Collaboration\Collaborators\SearchResultType; class SearchResult implements ISearchResult { protected $result = [ - 'exact' => [ - 'users' => [], - 'groups' => [], - 'remotes' => [], - 'emails' => [], - 'circles' => [], - ], - 'users' => [], - 'groups' => [], - 'remotes' => [], - 'emails' => [], - 'lookup' => [], - 'circles' => [], + 'exact' => [], ]; protected $exactIdMatches = []; - public function addResultSet($type, array $matches, array $exactMatches = null) { + public function addResultSet(SearchResultType $type, array $matches, array $exactMatches = null) { + $type = $type->getLabel(); if(!isset($this->result[$type])) { - throw new \InvalidArgumentException('Invalid type provided'); + $this->result[$type] = []; + $this->result['exact'][$type] = []; } $this->result[$type] = array_merge($this->result[$type], $matches); @@ -57,17 +48,18 @@ class SearchResult implements ISearchResult { } } - public function markExactIdMatch($type) { - $this->exactIdMatches[$type] = 1; + public function markExactIdMatch(SearchResultType $type) { + $this->exactIdMatches[$type->getLabel()] = 1; } - public function hasExactIdMatch($type) { - return isset($this->exactIdMatches[$type]); + public function hasExactIdMatch(SearchResultType$type) { + return isset($this->exactIdMatches[$type->getLabel()]); } - public function hasResult($type, $collaboratorId) { + public function hasResult(SearchResultType $type, $collaboratorId) { + $type = $type->getLabel(); if(!isset($this->result[$type])) { - throw new \InvalidArgumentException('Invalid type provided'); + return false; } $resultArrays = [$this->result['exact'][$type], $this->result[$type]]; @@ -84,11 +76,8 @@ class SearchResult implements ISearchResult { return $this->result; } - public function unsetResult($type) { - if(!isset($this->result[$type])) { - throw new \InvalidArgumentException('Invalid type provided'); - } - + public function unsetResult(SearchResultType $type) { + $type = $type->getLabel(); $this->result[$type] = []; if(isset($this->$result['exact'][$type])) { $this->result['exact'][$type] = []; diff --git a/lib/private/Collaboration/Collaborators/UserPlugin.php b/lib/private/Collaboration/Collaborators/UserPlugin.php index 815862b872..6d2709f8bd 100644 --- a/lib/private/Collaboration/Collaborators/UserPlugin.php +++ b/lib/private/Collaboration/Collaborators/UserPlugin.php @@ -26,6 +26,7 @@ namespace OC\Collaboration\Collaborators; use OCP\Collaboration\Collaborators\ISearchPlugin; use OCP\Collaboration\Collaborators\ISearchResult; +use OCP\Collaboration\Collaborators\SearchResultType; use OCP\IConfig; use OCP\IGroupManager; use OCP\IUser; @@ -140,7 +141,8 @@ class UserPlugin implements ISearchPlugin { $result['wide'] = []; } - $searchResult->addResultSet('users', $result['wide'], $result['exact']); + $type = new SearchResultType('users'); + $searchResult->addResultSet($type, $result['wide'], $result['exact']); return $hasMoreResults; } diff --git a/lib/public/Collaboration/Collaborators/ISearchResult.php b/lib/public/Collaboration/Collaborators/ISearchResult.php index 87c787c040..abea5df859 100644 --- a/lib/public/Collaboration/Collaborators/ISearchResult.php +++ b/lib/public/Collaboration/Collaborators/ISearchResult.php @@ -31,39 +31,39 @@ namespace OCP\Collaboration\Collaborators; */ interface ISearchResult { /** - * @param string $type one of: users, groups, remotes, email, circles, lookup + * @param SearchResultType $type * @param array $matches * @param array|null $exactMatches * @since 13.0.0 */ - public function addResultSet($type, array $matches, array $exactMatches = null); + public function addResultSet(SearchResultType $type, array $matches, array $exactMatches = null); /** - * @param string $type one of: users, groups, remotes, email, circles, lookup + * @param SearchResultType $type * @param string $collaboratorId * @return bool * @since 13.0.0 */ - public function hasResult($type, $collaboratorId); + public function hasResult(SearchResultType $type, $collaboratorId); /** - * @param string $type one of: users, groups, remotes, email, circles, lookup + * @param SearchResultType $type * @since 13.0.0 */ - public function unsetResult($type); + public function unsetResult(SearchResultType $type); /** - * @param string $type one of: users, groups, remotes, email, circles, lookup + * @param SearchResultType $type * @since 13.0.0 */ - public function markExactIdMatch($type); + public function markExactIdMatch(SearchResultType $type); /** - * @param string $type one of: users, groups, remotes, email, circles, lookup + * @param SearchResultType $type * @return bool * @since 13.0.0 */ - public function hasExactIdMatch($type); + public function hasExactIdMatch(SearchResultType $type); /** * @return array diff --git a/lib/public/Collaboration/Collaborators/SearchResultType.php b/lib/public/Collaboration/Collaborators/SearchResultType.php new file mode 100644 index 0000000000..569adb864e --- /dev/null +++ b/lib/public/Collaboration/Collaborators/SearchResultType.php @@ -0,0 +1,57 @@ + + * + * @author Arthur Schiwon + * + * @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 OCP\Collaboration\Collaborators; + +/** + * Class SearchResultType + * + * @package OCP\Collaboration\Collaborators + * @since 13.0.0 + */ +class SearchResultType { + /** @var string */ + protected $label; + + public function __construct($label) { + $this->label = $this->getValidatedType($label); + } + + public function getLabel() { + return $this->label; + } + + protected function getValidatedType($type) { + $type = trim(strval($type)); + + if($type === '') { + throw new \InvalidArgumentException('Type must not be empty'); + } + + if(trim($type) === 'exact') { + throw new \InvalidArgumentException('Provided type is a reserved word'); + } + + return $type; + } +}