Passed
Push — find-similar ( f7a85f...776de5 )
by Matias
04:52
created

RelationController::updateByPersonsBatch()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
nc 5
nop 2
dl 0
loc 23
ccs 0
cts 19
cp 0
crap 20
rs 9.7666
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 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
28
use OCP\AppFramework\Http;
29
use OCP\AppFramework\Http\DataResponse;
30
use OCP\AppFramework\Http\JSONResponse;
31
use OCP\AppFramework\Http\DataDisplayResponse;
32
use OCP\AppFramework\Controller;
33
34
use OCA\FaceRecognition\Db\Person;
35
use OCA\FaceRecognition\Db\PersonMapper;
36
37
use OCA\FaceRecognition\Db\Relation;
38
use OCA\FaceRecognition\Db\RelationMapper;
39
40
use OCA\FaceRecognition\Service\SettingsService;
41
42
class RelationController extends Controller {
43
44
	/** @var PersonMapper */
45
	private $personMapper;
46
47
	/** @var RelationMapper */
48
	private $relationMapper;
49
50
	/** @var SettingsService */
51
	private $settingsService;
52
53
	/** @var string */
54
	private $userId;
55
56
	public function __construct($AppName,
57
	                            IRequest        $request,
58
	                            PersonMapper    $personMapper,
59
	                            RelationMapper  $relationMapper,
60
	                            SettingsService $settingsService,
61
	                            $UserId)
62
	{
63
		parent::__construct($AppName, $request);
64
65
		$this->personMapper    = $personMapper;
66
		$this->relationMapper  = $relationMapper;
67
		$this->settingsService = $settingsService;
68
		$this->userId          = $UserId;
69
	}
70
71
	/**
72
	 * @NoAdminRequired
73
	 * @param int $personId
74
	 */
75
	public function findByPerson(int $personId) {
76
		$deviation = $this->settingsService->getDeviation();
77
78
		$enabled = (version_compare(phpversion('pdlib'), '1.0.2', '>=') && ($deviation > 0.0));
79
80
		$resp = array();
81
		$resp['enabled'] = $enabled;
82
		$resp['proposed'] = array();
83
84
		if (!$enabled)
85
			return new DataResponse($resp);
86
87
		$mainPerson = $this->personMapper->find($this->userId, $personId);
88
89
		$proposed = array();
90
		$relations = $this->relationMapper->findFromPerson($this->userId, $personId, RELATION::PROPOSED);
91
		foreach ($relations as $relation) {
92
			$person1 = $this->personMapper->findFromFace($this->userId, $relation->face1);
93
			if ($person1->getId() !== $personId) {
94
				$proffer = array();
95
				$proffer['origId'] = $mainPerson->getId();
96
				$proffer['id'] = $person1->getId();
97
				$proffer['name'] = $person1->getName();
98
				$proposed[] = $proffer;
99
			}
100
			$person2 = $this->personMapper->findFromFace($this->userId, $relation->face2);
101
			if ($person2->getId() !== $personId) {
102
				$proffer = array();
103
				$proffer['origId'] = $mainPerson->getId();
104
				$proffer['id'] = $person2->getId();
105
				$proffer['name'] = $person2->getName();
106
				$proposed[] = $proffer;
107
			}
108
		}
109
		$resp['proposed'] = $proposed;
110
111
		return new DataResponse($resp);
112
	}
113
114
	/**
115
	 * @NoAdminRequired
116
	 * @param int $personId
117
	 * @param int $toPersonId
118
	 * @param int $state
119
	 */
120
	public function updateByPersons(int $personId, int $toPersonId, int $state) {
121
		$relations = $this->relationMapper->findFromPersons($personId, $toPersonId);
122
123
		foreach ($relations as $relation) {
124
			$relation->setState($state);
125
			$this->relationMapper->update($relation);
126
		}
127
128
		if ($state === RELATION::ACCEPTED) {
129
			$person = $this->personMapper->find($this->userId, $personId);
130
			$name = $person->getName();
131
132
			$toPerson = $this->personMapper->find($this->userId, $toPersonId);
133
			$toPerson->setName($name);
134
			$this->personMapper->update($toPerson);
135
		}
136
137
		$relations = $this->relationMapper->findFromPersons($personId, $toPersonId);
138
		return new DataResponse($relations);
139
	}
140
141
	/**
142
	 * @NoAdminRequired
143
	 * @param array $personsRelations
144
	 * @param string $personName
145
	 */
146
	public function updateByPersonsBatch(array $personsRelations, string $personName) {
147
		foreach ($personsRelations as $personRelation) {
148
			$origId = $personRelation['origId'];
149
			$id = $personRelation['id'];
150
			$state = $personRelation['state'];
151
152
			$faceRelations = $this->relationMapper->findFromPersons($origId, $id);
153
			foreach ($faceRelations as $faceRelation) {
154
				$faceRelation->setState($state);
155
				$this->relationMapper->update($faceRelation);
156
			}
157
158
			if ($state === RELATION::ACCEPTED) {
159
				$toPerson = $this->personMapper->find($this->userId, $id);
160
				$toPerson->setName($personName);
161
				$this->personMapper->update($toPerson);
162
			}
163
		}
164
165
		$modelId = $this->settingsService->getCurrentFaceModel();
166
		$persons = $this->personMapper->findByName($this->userId, $modelId, $personName);
167
168
		return new DataResponse($persons);
169
	}
170
171
}
172