some small fixes and improvements

Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
This commit is contained in:
Bjoern Schiessle 2018-10-10 16:07:01 +02:00
parent 9e76d4049a
commit fd5fc24da2
No known key found for this signature in database
GPG Key ID: 2378A753E2BF04F6
2 changed files with 29 additions and 6 deletions

View File

@ -30,6 +30,7 @@ use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\Federation\ICloudIdManager; use OCP\Federation\ICloudIdManager;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Share; use OCP\Share;
@ -43,23 +44,27 @@ class LookupPlugin implements ISearchPlugin {
private $currentUserRemote; private $currentUserRemote;
/** @var ICloudIdManager */ /** @var ICloudIdManager */
private $cloudIdManager; private $cloudIdManager;
/** @var ILogger */
private $logger;
public function __construct(IConfig $config, public function __construct(IConfig $config,
IClientService $clientService, IClientService $clientService,
IUserSession $userSession, IUserSession $userSession,
ICloudIdManager $cloudIdManager) { ICloudIdManager $cloudIdManager,
ILogger $logger) {
$this->config = $config; $this->config = $config;
$this->clientService = $clientService; $this->clientService = $clientService;
$this->cloudIdManager = $cloudIdManager; $this->cloudIdManager = $cloudIdManager;
$currentUserCloudId = $userSession->getUser()->getCloudId(); $currentUserCloudId = $userSession->getUser()->getCloudId();
$this->currentUserRemote = $cloudIdManager->resolveCloudId($currentUserCloudId)->getRemote(); $this->currentUserRemote = $cloudIdManager->resolveCloudId($currentUserCloudId)->getRemote();
$this->logger = $logger;
} }
public function search($search, $limit, $offset, ISearchResult $searchResult) { public function search($search, $limit, $offset, ISearchResult $searchResult) {
$isGlobalScaleEnabled = $this->config->getSystemValue('gs.enabled', false); $isGlobalScaleEnabled = $this->config->getSystemValue('gs.enabled', false);
$isLookupServerEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no'); $isLookupServerEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no') === 'yes';
// if case of Global Scale we always search the lookup server // if case of Global Scale we always search the lookup server
if ($isLookupServerEnabled !== 'yes' && !$isGlobalScaleEnabled) { if (!$isLookupServerEnabled && !$isGlobalScaleEnabled) {
return false; return false;
} }
@ -80,8 +85,16 @@ class LookupPlugin implements ISearchPlugin {
$body = json_decode($response->getBody(), true); $body = json_decode($response->getBody(), true);
foreach ($body as $lookup) { foreach ($body as $lookup) {
try {
$remote = $this->cloudIdManager->resolveCloudId($lookup['federationId'])->getRemote(); $remote = $this->cloudIdManager->resolveCloudId($lookup['federationId'])->getRemote();
if ($this->currentUserRemote === $remote) continue; } catch (\Exception $e) {
$this->logger->error('Can not parse federated cloud ID "' . $lookup['federationId'] . '"');
$this->logger->error($e->getMessage());
continue;
}
if ($this->currentUserRemote === $remote) {
continue;
}
$name = isset($lookup['name']['value']) ? $lookup['name']['value'] : ''; $name = isset($lookup['name']['value']) ? $lookup['name']['value'] : '';
$label = empty($name) ? $lookup['federationId'] : $name . ' (' . $lookup['federationId'] . ')'; $label = empty($name) ? $lookup['federationId'] : $name . ' (' . $lookup['federationId'] . ')';
$result[] = [ $result[] = [

View File

@ -34,6 +34,7 @@ use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse; use OCP\Http\Client\IResponse;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger;
use OCP\IUser; use OCP\IUser;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Share; use OCP\Share;
@ -51,6 +52,8 @@ class LookupPluginTest extends TestCase {
protected $cloudIdManager; protected $cloudIdManager;
/** @var LookupPlugin */ /** @var LookupPlugin */
protected $plugin; protected $plugin;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
protected $logger;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
@ -58,6 +61,7 @@ class LookupPluginTest extends TestCase {
$this->userSession = $this->createMock(IUserSession::class); $this->userSession = $this->createMock(IUserSession::class);
$this->cloudIdManager = $this->createMock(ICloudIdManager::class); $this->cloudIdManager = $this->createMock(ICloudIdManager::class);
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->logger = $this->createMock(ILogger::class);
$this->clientService = $this->createMock(IClientService::class); $this->clientService = $this->createMock(IClientService::class);
$cloudId = $this->createMock(ICloudId::class); $cloudId = $this->createMock(ICloudId::class);
$cloudId->expects($this->any())->method('getRemote')->willReturn('myNextcloud.net'); $cloudId->expects($this->any())->method('getRemote')->willReturn('myNextcloud.net');
@ -74,7 +78,13 @@ class LookupPluginTest extends TestCase {
}); });
$this->plugin = new LookupPlugin($this->config, $this->clientService, $this->userSession, $this->cloudIdManager); $this->plugin = new LookupPlugin(
$this->config,
$this->clientService,
$this->userSession,
$this->cloudIdManager,
$this->logger
);
} }
/** /**