Completed
Pull Request — master (#6982)
by Blizzz
14:42
created

AutoCompleteController::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 6
dl 0
loc 20
rs 9.4285
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\Core\Controller;
25
26
use OCP\AppFramework\Controller;
27
use OCP\AppFramework\Http\DataResponse;
28
use OCP\Collaboration\AutoComplete\IManager;
29
use OCP\Collaboration\Collaborators\ISearch;
30
use OCP\IConfig;
31
use OCP\IRequest;
32
use OCP\Share;
33
34
class AutoCompleteController extends Controller {
35
	/** @var ISearch */
36
	private $collaboratorSearch;
37
	/** @var IManager */
38
	private $autoCompleteManager;
39
	/** @var IConfig */
40
	private $config;
41
42
	public function __construct(
43
		$appName,
44
		IRequest $request,
45
		ISearch $collaboratorSearch,
46
		IManager $autoCompleteManager,
47
		IConfig $config
48
	) {
49
		parent::__construct($appName, $request);
50
51
		$this->collaboratorSearch = $collaboratorSearch;
52
		$this->autoCompleteManager = $autoCompleteManager;
53
		$this->config = $config;
54
	}
55
56
	/**
57
	 * @NoAdminRequired
58
	 *
59
	 * @param string $search
60
	 * @param string $itemType
61
	 * @param string $itemId
62
	 * @param string|null $sorter can be piped, top prio first, e.g.: "commenters|share-recipients"
63
	 * @param array $shareTypes
64
	 * @param int $limit
65
	 * @return DataResponse
66
	 */
67
	public function get($search, $itemType, $itemId, $sorter = null, $shareTypes = [Share::SHARE_TYPE_USER], $limit = 10) {
68
		// if enumeration/user listings are disabled, we'll receive an empty
69
		// result from search() – thus nothing else to do here.
70
		list($results,) = $this->collaboratorSearch->search($search, $shareTypes, false, $limit, 0);
71
72
		$exactMatches = $results['exact'];
73
		unset($results['exact']);
74
		$results = array_merge_recursive($exactMatches, $results);
75
76
		$sorters = array_reverse(explode('|', $sorter));
77
		$this->autoCompleteManager->runSorters($sorters, $results, [
78
			'itemType' => $itemType,
79
			'itemId' => $itemId,
80
		]);
81
82
		// transform to expected format
83
		$results = $this->prepareResultArray($results);
84
85
		return new DataResponse($results);
86
	}
87
88
89
	protected function prepareResultArray(array $results) {
90
		$output = [];
91
		foreach ($results as $type => $subResult) {
92
			foreach ($subResult as $result) {
93
				$output[] = [
94
					'id' => $result['value']['shareWith'],
95
					'label' => $result['label'],
96
					'source' => $type,
97
				];
98
			}
99
		}
100
		return $output;
101
	}
102
}
103