don't keep result types hard coded

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2017-09-06 16:09:29 +02:00
parent 579c1476a3
commit 4a96e22258
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
11 changed files with 122 additions and 58 deletions

View File

@ -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, [

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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];

View File

@ -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] = [];

View File

@ -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;
}

View File

@ -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

View File

@ -0,0 +1,57 @@
<?php
/**
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
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;
}
}