1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017, Matias De lellis <[email protected]> |
4
|
|
|
* @copyright Copyright (c) 2018, Branko Kokanovic <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @author Branko Kokanovic <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
namespace OCA\FaceRecognition; |
25
|
|
|
|
26
|
|
|
use OCP\IConfig; |
27
|
|
|
use OCP\IUser; |
28
|
|
|
use OCP\IUserManager; |
29
|
|
|
|
30
|
|
|
use OCA\FaceRecognition\Db\FaceMapper; |
31
|
|
|
use OCA\FaceRecognition\Db\ImageMapper; |
32
|
|
|
use OCA\FaceRecognition\Db\PersonMapper; |
33
|
|
|
|
34
|
|
|
use OCA\FaceRecognition\BackgroundJob\Tasks\AddMissingImagesTask; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Background service. Both command and cron job are calling this service for long-running background operations. |
38
|
|
|
* Background processing for face recognition is comprised of several steps, called tasks. Each task is independent, |
39
|
|
|
* idempotent, DI-aware logic unit that yields. Since tasks are non-preemptive, they should yield from time to time, so we son't end up |
40
|
|
|
* working for more than given timeout. |
41
|
|
|
* |
42
|
|
|
* Tasks can be seen as normal sequential functions, but they are easier to work with, |
43
|
|
|
* reason about them and test them independently. Other than that, they are really glorified functions. |
44
|
|
|
*/ |
45
|
|
|
class FaceManagementService { |
46
|
|
|
|
47
|
|
|
/** @var IConfig */ |
48
|
|
|
private $config; |
49
|
|
|
|
50
|
|
|
/** @var IUserManager */ |
51
|
|
|
private $userManager; |
52
|
|
|
|
53
|
|
|
/** @var FaceMapper */ |
54
|
|
|
private $faceMapper; |
55
|
|
|
|
56
|
|
|
/** @var ImageMapper */ |
57
|
|
|
private $imageMapper; |
58
|
|
|
|
59
|
|
|
/** @var PersonMapper */ |
60
|
|
|
private $personMapper; |
61
|
|
|
|
62
|
8 |
|
public function __construct(IConfig $config, IUserManager $userManager, |
63
|
|
|
FaceMapper $faceMapper, ImageMapper $imageMapper, PersonMapper $personMapper) { |
64
|
8 |
|
$this->config = $config; |
65
|
8 |
|
$this->userManager = $userManager; |
66
|
8 |
|
$this->faceMapper = $faceMapper; |
67
|
8 |
|
$this->imageMapper = $imageMapper; |
68
|
8 |
|
$this->personMapper = $personMapper; |
69
|
8 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Deletes all faces, images and persons found. IF no user is given, resetting is executed for all users. |
73
|
|
|
* |
74
|
|
|
* @param IUser|null $user Optional user to execute resetting for |
75
|
|
|
*/ |
76
|
|
|
public function resetAll(IUser $user = null) { |
77
|
|
|
$eligable_users = array(); |
78
|
|
|
if (is_null($user)) { |
79
|
|
|
$this->userManager->callForAllUsers(function (IUser $user) use (&$eligable_users) { |
80
|
|
|
$eligable_users[] = $user->getUID(); |
81
|
|
|
}); |
82
|
|
|
} else { |
83
|
|
|
$eligable_users[] = $user->getUID(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach($eligable_users as $user) { |
87
|
|
|
$this->resetAllForUser($user); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Deletes all faces, images and persons found for a given user. |
93
|
|
|
* |
94
|
|
|
* @param string $user ID of user to execute resetting for |
95
|
|
|
*/ |
96
|
8 |
|
public function resetAllForUser(string $userId) { |
97
|
8 |
|
$this->faceMapper->deleteUserFaces($userId); |
98
|
8 |
|
$this->personMapper->deleteUserPersons($userId); |
99
|
8 |
|
$this->imageMapper->deleteUserImages($userId); |
100
|
8 |
|
$this->config->deleteUserValue($userId, 'facerecognition', AddMissingImagesTask::FULL_IMAGE_SCAN_DONE_KEY); |
101
|
|
|
} |
102
|
|
|
} |