Completed
Push — master ( 5412c2...2d62f9 )
by Blizzz
45:23 queued 29:26
created

LookupPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Collaboration\Collaborators;
25
26
27
use OCP\Collaboration\Collaborators\ISearchPlugin;
28
use OCP\Collaboration\Collaborators\ISearchResult;
29
use OCP\Collaboration\Collaborators\SearchResultType;
30
use OCP\Http\Client\IClientService;
31
use OCP\IConfig;
32
use OCP\Share;
33
34
class LookupPlugin implements ISearchPlugin {
35
36
	/** @var IConfig */
37
	private $config;
38
	/** @var IClientService */
39
	private $clientService;
40
41
	public function __construct(IConfig $config, IClientService $clientService) {
42
		$this->config = $config;
43
		$this->clientService = $clientService;
44
	}
45
46
	public function search($search, $limit, $offset, ISearchResult $searchResult) {
47
		if ($this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no') !== 'yes') {
48
			return false;
49
		}
50
51
		$lookupServerUrl = $this->config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
52
		$lookupServerUrl = rtrim($lookupServerUrl, '/');
53
		$result = [];
54
55
		try {
56
			$client = $this->clientService->newClient();
57
			$response = $client->get(
58
				$lookupServerUrl . '/users?search=' . urlencode($search),
59
				[
60
					'timeout' => 10,
61
					'connect_timeout' => 3,
62
				]
63
			);
64
65
			$body = json_decode($response->getBody(), true);
66
67
			foreach ($body as $lookup) {
68
				$result[] = [
69
					'label' => $lookup['federationId'],
70
					'value' => [
71
						'shareType' => Share::SHARE_TYPE_REMOTE,
72
						'shareWith' => $lookup['federationId'],
73
					],
74
					'extra' => $lookup,
75
				];
76
			}
77
		} catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
78
		}
79
80
		$type = new SearchResultType('lookup');
81
		$searchResult->addResultSet($type, $result, []);
82
83
		return false;
84
	}
85
}
86