Passed
Push — visibilities ( 5173e6...c549fc )
by Matias
05:12
created

PersonController::setVisibility()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018-2020 Matias De lellis <[email protected]>
4
 *
5
 * @author Matias De lellis <[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 OCA\FaceRecognition\Controller;
25
26
use OCP\IRequest;
27
use OCP\Files\File;
28
29
use OCP\AppFramework\Http;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\AppFramework\Http\JSONResponse;
32
use OCP\AppFramework\Http\DataDisplayResponse;
33
use OCP\AppFramework\Controller;
34
35
use OCA\FaceRecognition\Db\Face;
36
use OCA\FaceRecognition\Db\FaceMapper;
37
38
use OCA\FaceRecognition\Db\Image;
39
use OCA\FaceRecognition\Db\ImageMapper;
40
41
use OCA\FaceRecognition\Db\Person;
42
use OCA\FaceRecognition\Db\PersonMapper;
43
44
use OCA\FaceRecognition\Service\SettingsService;
45
use OCA\FaceRecognition\Service\UrlService;
46
47
48
class PersonController extends Controller {
49
50
	/** @var FaceMapper */
51
	private $faceMapper;
52
53
	/** @var ImageMapper */
54
	private $imageMapper;
55
56
	/** @var PersonMapper */
57
	private $personMapper;
58
59
	/** @var SettingsService */
60
	private $settingsService;
61
62
	/** @var UrlService */
63
	private $urlService;
64
65
	/** @var string */
66
	private $userId;
67
68
	public function __construct($AppName,
69
	                            IRequest        $request,
70
	                            FaceMapper      $faceMapper,
71
	                            ImageMapper     $imageMapper,
72
	                            PersonMapper    $personmapper,
73
	                            SettingsService $settingsService,
74
	                            UrlService      $urlService,
75
	                            $UserId)
76
	{
77
		parent::__construct($AppName, $request);
78
79
		$this->faceMapper      = $faceMapper;
80
		$this->imageMapper     = $imageMapper;
81
		$this->personMapper    = $personmapper;
82
		$this->settingsService = $settingsService;
83
		$this->urlService      = $urlService;
84
		$this->userId          = $UserId;
85
	}
86
87
	/**
88
	 * @NoAdminRequired
89
	 *
90
	 * @return DataResponse
91
	 */
92
	public function index(): DataResponse {
93
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
94
95
		$resp = array();
96
		$resp['enabled'] = $userEnabled;
97
		$resp['persons'] = array();
98
99
		if (!$userEnabled)
100
			return new DataResponse($resp);
101
102
		$modelId = $this->settingsService->getCurrentFaceModel();
103
104
		$personsNames = $this->personMapper->findDistinctNames($this->userId, $modelId);
105
		foreach ($personsNames as $personNamed) {
106
			$facesCount = 0;
107
			$faceUrl = null;
108
			$persons = $this->personMapper->findByName($this->userId, $modelId, $personNamed->getName());
109
			foreach ($persons as $person) {
110
				$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $modelId);
111
				if (is_null($faceUrl)) {
112
					$faceUrl = $this->urlService->getThumbUrl($personFaces[0]->getId(), 128);
113
				}
114
				$facesCount += count($personFaces);
115
			}
116
117
			$person = [];
118
			$person['name'] = $personNamed->getName();
119
			$person['thumbUrl'] = $faceUrl;
120
			$person['count'] = $facesCount;
121
122
			$resp['persons'][] = $person;
123
		}
124
125
		return new DataResponse($resp);
126
	}
127
128
	/**
129
	 * @NoAdminRequired
130
	 *
131
	 * @return DataResponse
132
	 */
133
	public function find(string $personName): DataResponse {
134
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
135
136
		$resp = array();
137
		$resp['enabled'] = $userEnabled;
138
		$resp['name'] = $personName;
139
		$resp['thumbUrl'] = null;
140
		$resp['clusters'] = 0;
141
		$resp['images'] = array();
142
143
		if (!$userEnabled)
144
			return new DataResponse($resp);
145
146
		$faceUrl = null;
147
		$modelId = $this->settingsService->getCurrentFaceModel();
148
149
		$clusters = $this->personMapper->findByName($this->userId, $modelId, $personName);
150
		foreach ($clusters as $cluster) {
151
			$resp['clusters']++;
152
153
			$faces = $this->faceMapper->findFacesFromPerson($this->userId, $cluster->getId(), $modelId);
154
			if (is_null($faceUrl)) {
155
				$faceUrl = $this->urlService->getThumbUrl($faces[0]->getId(), 128);
156
				$resp['thumbUrl'] = $faceUrl;
157
			}
158
			foreach ($faces as $face) {
159
				$image = $this->imageMapper->find($this->userId, $face->getImage());
160
161
162
				$fileId = $image->getFile();
163
				if ($fileId === null) continue;
164
165
				$fileUrl = $this->urlService->getRedirectToFileUrl($fileId);
166
				if ($fileUrl === null) continue;
167
168
				$thumbUrl = $this->urlService->getPreviewUrl($fileId, 256);
169
				if ($thumbUrl === null) continue;
170
171
				$image = [];
172
				$image['thumbUrl'] = $thumbUrl;
173
				$image['fileUrl'] = $fileUrl;
174
175
				$resp['images'][] = $image;
176
			}
177
		}
178
179
		return new DataResponse($resp);
180
	}
181
182
	/**
183
	 * @NoAdminRequired
184
	 *
185
	 * @param string $personName
186
	 * @param string $name
187
	 *
188
	 * @return DataResponse
189
	 */
190
	public function updateName($personName, $name): DataResponse {
191
		$modelId = $this->settingsService->getCurrentFaceModel();
192
		$clusters = $this->personMapper->findByName($this->userId, $modelId, $personName);
193
		foreach ($clusters as $person) {
194
			$person->setName($name);
195
			$this->personMapper->update($person);
196
		}
197
		return $this->find($name);
198
	}
199
200
	/**
201
	 * @NoAdminRequired
202
	 *
203
	 * @param string $personName
204
	 * @param bool $visible
205
	 *
206
	 * @return DataResponse
207
	*/
208
	public function setVisibility ($personName, bool $visible): DataResponse {
209
		$modelId = $this->settingsService->getCurrentFaceModel();
210
		$clusters = $this->personMapper->findByName($this->userId, $modelId, $personName);
211
		foreach ($clusters as $cluster) {
212
			$this->personMapper->setVisibility($cluster->getId(), $visible);
213
		}
214
		return $this->find($personName);
215
	}
216
217
	/**
218
	 * @NoAdminRequired
219
	 *
220
	 * @return DataResponse
221
	 */
222
	public function autocomplete(string $query): DataResponse {
223
		$resp = array();
224
225
		if (!$this->settingsService->getUserEnabled($this->userId))
226
			return new DataResponse($resp);
227
228
		$modelId = $this->settingsService->getCurrentFaceModel();
229
230
		$persons = $this->personMapper->findPersonsLike($this->userId, $modelId, $query);
231
		foreach ($persons as $person) {
232
			$name = [];
233
			$name['name'] = $person->getName();
234
			$name['value'] = $person->getName();
235
			$resp[] = $name;
236
		}
237
		return new DataResponse($resp);
238
	}
239
240
}
241