Passed
Push — visibilities ( 425312 )
by Matias
12:05
created

ClusterController::setVisibility()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
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 ClusterController 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 find(int $id): DataResponse {
93
		$person = $this->personMapper->find($this->userId, $id);
94
95
		$resp = [];
96
		$faces = [];
97
		$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $this->settingsService->getCurrentFaceModel());
98
		foreach ($personFaces as $personFace) {
99
			$image = $this->imageMapper->find($this->userId, $personFace->getImage());
100
			$fileId = $image->getFile();
101
			if ($fileId === null) continue;
102
103
			$fileUrl = $this->urlService->getRedirectToFileUrl($fileId);
104
			if ($fileUrl === null) continue;
105
106
			$face = [];
107
			$face['thumbUrl'] = $this->urlService->getThumbUrl($personFace->getId(), 50);
108
			$face['fileUrl'] = $fileUrl;
109
			$faces[] = $face;
110
		}
111
		$resp['name'] = $person->getName();
112
		$resp['id'] = $person->getId();
113
		$resp['faces'] = $faces;
114
115
		return new DataResponse($resp);
116
	}
117
118
	/**
119
	 * @NoAdminRequired
120
	 *
121
	 * @return DataResponse
122
	 */
123
	public function findByName(string $personName): DataResponse {
124
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
125
126
		$resp = array();
127
		$resp['enabled'] = $userEnabled;
128
		$resp['clusters'] = array();
129
130
		if (!$userEnabled)
131
			return new DataResponse($resp);
132
133
		$modelId = $this->settingsService->getCurrentFaceModel();
134
135
		$persons = $this->personMapper->findByName($this->userId, $modelId, $personName);
136
		foreach ($persons as $person) {
137
			$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $modelId);
138
139
			$faces = [];
140
			foreach ($personFaces as $personFace) {
141
				$image = $this->imageMapper->find($this->userId, $personFace->getImage());
142
				$fileId = $image->getFile();
143
				if ($fileId === null) continue;
144
145
				$fileUrl = $this->urlService->getRedirectToFileUrl($fileId);
146
				if ($fileUrl === null) continue;
147
148
				$face = [];
149
				$face['thumbUrl'] = $this->urlService->getThumbUrl($personFace->getId(), 50);
150
				$face['fileUrl'] = $fileUrl;
151
				$faces[] = $face;
152
			}
153
154
			$cluster = [];
155
			$cluster['name'] = $person->getName();
156
			$cluster['count'] = count($personFaces);
157
			$cluster['id'] = $person->getId();
158
			$cluster['faces'] = $faces;
159
			$resp['clusters'][] = $cluster;
160
		}
161
162
		return new DataResponse($resp);
163
	}
164
165
	/**
166
	 * @NoAdminRequired
167
	 *
168
	 * @return DataResponse
169
	 */
170
	public function findUnassigned(): DataResponse {
171
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
172
173
		$resp = array();
174
		$resp['enabled'] = $userEnabled;
175
		$resp['clusters'] = array();
176
177
		if (!$userEnabled)
178
			return new DataResponse($resp);
179
180
		$modelId = $this->settingsService->getCurrentFaceModel();
181
182
		$persons = $this->personMapper->findUnassigned($this->userId, $modelId);
183
		foreach ($persons as $person) {
184
			$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $modelId, 40);
185
			if (count($personFaces) === 1)
186
				continue;
187
188
			$faces = [];
189
			foreach ($personFaces as $personFace) {
190
				$image = $this->imageMapper->find($this->userId, $personFace->getImage());
191
192
				$fileId = $image->getFile();
193
				if ($fileId === null) continue;
194
195
				$fileUrl = $this->urlService->getRedirectToFileUrl($fileId);
196
				if ($fileUrl === null) continue;
197
198
				$thumbUrl = $this->urlService->getThumbUrl($personFace->getId(), 50);
199
200
				$face = [];
201
				$face['thumbUrl'] = $thumbUrl;
202
				$face['fileUrl'] = $fileUrl;
203
204
				$faces[] = $face;
205
			}
206
207
			$cluster = [];
208
			$cluster['count'] = count($personFaces);
209
			$cluster['id'] = $person->getId();
210
			$cluster['faces'] = $faces;
211
			$resp['clusters'][] = $cluster;
212
		}
213
214
		return new DataResponse($resp);
215
	}
216
217
	/**
218
	 * @NoAdminRequired
219
	 *
220
	 * @param int $id
221
	 * @param bool $visible
222
	 *
223
	 * @return DataResponse
224
	 */
225
	public function setVisibility (int $id, bool $visible): DataResponse {
226
		$resp = array();
227
		$this->personMapper->setVisibility($id, $visible);
228
		return new DataResponse($resp);
229
	}
230
231
	/**
232
	 * @NoAdminRequired
233
	 *
234
	 * @param int $id
235
	 * @param string $name
236
	 *
237
	 * @return DataResponse
238
	 */
239
	public function updateName($id, $name): DataResponse {
240
		$person = $this->personMapper->find ($this->userId, $id);
241
		$person->setName($name);
242
		$this->personMapper->update($person);
243
244
		$newPerson = $this->personMapper->find($this->userId, $id);
245
		return new DataResponse($newPerson);
246
	}
247
248
}
249