Completed
Push — master ( 8255fa...61397e )
by Morris
16:01
created

Provider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
B search() 0 52 8
A getFileForComment() 0 8 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Comments\Search;
23
24
use OCP\Comments\IComment;
25
use OCP\Files\Folder;
26
use OCP\Files\Node;
27
use OCP\Files\NotFoundException;
28
use OCP\IUser;
29
30
class Provider extends \OCP\Search\Provider {
31
32
	/**
33
	 * Search for $query
34
	 *
35
	 * @param string $query
36
	 * @return array An array of OCP\Search\Result's
37
	 * @since 7.0.0
38
	 */
39
	public function search($query): array {
40
		$cm = \OC::$server->getCommentsManager();
41
		$us = \OC::$server->getUserSession();
42
43
		$user = $us->getUser();
44
		if (!$user instanceof IUser) {
45
			return [];
46
		}
47
		$uf = \OC::$server->getUserFolder($user->getUID());
48
49
		if ($uf === null) {
50
			return [];
51
		}
52
53
		$result = [];
54
		$numComments = 50;
55
		$offset = 0;
56
57
		while (\count($result) < $numComments) {
58
			/** @var IComment[] $comments */
59
			$comments = $cm->search($query, 'files', '', 'comment', $offset, $numComments);
60
61
			foreach ($comments as $comment) {
62
				if ($comment->getActorType() !== 'users') {
63
					continue;
64
				}
65
66
				$displayName = $cm->resolveDisplayName('user', $comment->getActorId());
67
68
				try {
69
					$file = $this->getFileForComment($uf, $comment);
70
					$result[] = new Result($query,
71
						$comment,
72
						$displayName,
73
						$file->getPath()
74
					);
75
				} catch (NotFoundException $e) {
76
					continue;
77
				}
78
			}
79
80
			if (\count($comments) < $numComments) {
81
				// Didn't find more comments when we tried to get, so there are no more comments.
82
				return $result;
83
			}
84
85
			$offset += $numComments;
86
			$numComments = 50 - \count($result);
87
		}
88
89
		return $result;
90
	}
91
92
	/**
93
	 * @param Folder $userFolder
94
	 * @param IComment $comment
95
	 * @return Node
96
	 * @throws NotFoundException
97
	 */
98
	protected function getFileForComment(Folder $userFolder, IComment $comment): Node {
99
		$nodes = $userFolder->getById((int) $comment->getObjectId());
100
		if (empty($nodes)) {
101
			throw new NotFoundException('File not found');
102
		}
103
104
		return array_shift($nodes);
105
	}
106
}
107