1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Circles - Bring cloud-users closer together. |
8
|
|
|
* |
9
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
10
|
|
|
* later. See the COPYING file. |
11
|
|
|
* |
12
|
|
|
* @author Maxence Lange <[email protected]> |
13
|
|
|
* @copyright 2021 |
14
|
|
|
* @license GNU AGPL version 3 or any later version |
15
|
|
|
* |
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Affero General Public License as |
18
|
|
|
* published by the Free Software Foundation, either version 3 of the |
19
|
|
|
* License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU Affero General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Affero General Public License |
27
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
namespace OCA\Circles\Service; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use ArtificialOwl\MySmallPhpTools\Model\SimpleDataStore; |
36
|
|
|
use Exception; |
37
|
|
|
use OCA\Circles\Db\CircleRequest; |
38
|
|
|
use OCA\Circles\Db\MemberRequest; |
39
|
|
|
use OCA\Circles\Exceptions\InitiatorNotFoundException; |
40
|
|
|
use OCA\Circles\Exceptions\RequestBuilderException; |
41
|
|
|
use OCA\Circles\Model\Circle; |
42
|
|
|
use OCA\Circles\Model\Member; |
43
|
|
|
use OCP\IUserManager; |
44
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Class MaintenanceService |
49
|
|
|
* |
50
|
|
|
* @package OCA\Circles\Service |
51
|
|
|
*/ |
52
|
|
|
class MaintenanceService { |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** @var IUserManager */ |
56
|
|
|
private $userManager; |
57
|
|
|
|
58
|
|
|
/** @var CircleRequest */ |
59
|
|
|
private $circleRequest; |
60
|
|
|
|
61
|
|
|
/** @var MemberRequest */ |
62
|
|
|
private $memberRequest; |
63
|
|
|
|
64
|
|
|
/** @var FederatedUserService */ |
65
|
|
|
private $federatedUserService; |
66
|
|
|
|
67
|
|
|
/** @var EventWrapperService */ |
68
|
|
|
private $eventWrapperService; |
69
|
|
|
|
70
|
|
|
/** @var CircleService */ |
71
|
|
|
private $circleService; |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** @var OutputInterface */ |
75
|
|
|
private $output; |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* MaintenanceService constructor. |
80
|
|
|
* |
81
|
|
|
* @param IUserManager $userManager |
82
|
|
|
* @param CircleRequest $circleRequest |
83
|
|
|
* @param MemberRequest $memberRequest |
84
|
|
|
* @param FederatedUserService $federatedUserService |
85
|
|
|
* @param EventWrapperService $eventWrapperService |
86
|
|
|
* @param CircleService $circleService |
87
|
|
|
*/ |
88
|
|
|
public function __construct( |
89
|
|
|
IUserManager $userManager, |
90
|
|
|
CircleRequest $circleRequest, |
91
|
|
|
MemberRequest $memberRequest, |
92
|
|
|
FederatedUserService $federatedUserService, |
93
|
|
|
EventWrapperService $eventWrapperService, |
94
|
|
|
CircleService $circleService |
95
|
|
|
) { |
96
|
|
|
$this->userManager = $userManager; |
97
|
|
|
$this->circleRequest = $circleRequest; |
98
|
|
|
$this->memberRequest = $memberRequest; |
99
|
|
|
$this->federatedUserService = $federatedUserService; |
100
|
|
|
$this->eventWrapperService = $eventWrapperService; |
101
|
|
|
$this->circleService = $circleService; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param OutputInterface $output |
107
|
|
|
*/ |
108
|
|
|
public function setOccOutput(OutputInterface $output): void { |
109
|
|
|
$this->output = $output; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* |
115
|
|
|
*/ |
116
|
|
|
public function runMaintenance(int $level = 0): void { |
117
|
|
|
$this->federatedUserService->bypassCurrentUserCondition(true); |
118
|
|
|
|
119
|
|
|
try { |
120
|
|
|
$this->output('remove circles with no owner'); |
121
|
|
|
$this->removeCirclesWithNoOwner(); |
122
|
|
|
} catch (Exception $e) { |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
try { |
126
|
|
|
$this->output('remove members with no circles'); |
127
|
|
|
$this->removeMembersWithNoCircles(); |
|
|
|
|
128
|
|
|
} catch (Exception $e) { |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
try { |
133
|
|
|
// TODO: waiting for confirmation of a good migration before cleaning orphan shares |
134
|
|
|
// $this->output('remove deprecated shares'); |
135
|
|
|
// $this->removeDeprecatedShares(); |
136
|
|
|
} catch (Exception $e) { |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
try { |
140
|
|
|
$this->output('retry failed FederatedEvents'); |
141
|
|
|
$this->eventWrapperService->retry(); |
142
|
|
|
} catch (Exception $e) { |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($level < 1) { |
146
|
|
|
return; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ($level < 2) { |
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ($level < 3) { |
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// if ($level < 5) { |
158
|
|
|
// $this->output('refresh displayNames older than 7d'); |
159
|
|
|
// // $this->refreshOldDisplayNames(); |
160
|
|
|
// } |
161
|
|
|
// |
162
|
|
|
if ($level < 4) { |
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if ($level < 5) { |
167
|
|
|
return; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
try { |
171
|
|
|
$this->output('refresh DisplayNames'); |
172
|
|
|
$this->refreshDisplayName(); |
173
|
|
|
} catch (Exception $e) { |
|
|
|
|
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @throws InitiatorNotFoundException |
180
|
|
|
* @throws RequestBuilderException |
181
|
|
|
*/ |
182
|
|
|
private function removeCirclesWithNoOwner(): void { |
183
|
|
|
$circles = $this->circleService->getCircles(); |
184
|
|
|
foreach ($circles as $circle) { |
185
|
|
|
if (!$circle->hasOwner()) { |
186
|
|
|
$this->circleRequest->delete($circle); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* |
194
|
|
|
*/ |
195
|
|
|
private function removeMembersWithNoCircles(): void { |
196
|
|
|
// $members = $this->membersRequest->forceGetAllMembers(); |
197
|
|
|
// |
198
|
|
|
// foreach ($members as $member) { |
199
|
|
|
// try { |
200
|
|
|
// $this->circlesRequest->forceGetCircle($member->getCircleId()); |
201
|
|
|
// } catch (CircleDoesNotExistException $e) { |
202
|
|
|
// $this->membersRequest->removeMember($member); |
203
|
|
|
// } |
204
|
|
|
// } |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
private function removeDeprecatedShares(): void { |
|
|
|
|
209
|
|
|
// $circles = array_map( |
210
|
|
|
// function(DeprecatedCircle $circle) { |
211
|
|
|
// return $circle->getUniqueId(); |
212
|
|
|
// }, $this->circlesRequest->forceGetCircles() |
213
|
|
|
// ); |
214
|
|
|
// |
215
|
|
|
// $shares = array_unique( |
216
|
|
|
// array_map( |
217
|
|
|
// function($share) { |
218
|
|
|
// return $share['share_with']; |
219
|
|
|
// }, $this->fileSharesRequest->getShares() |
220
|
|
|
// ) |
221
|
|
|
// ); |
222
|
|
|
// |
223
|
|
|
// foreach ($shares as $share) { |
224
|
|
|
// if (!in_array($share, $circles)) { |
225
|
|
|
// $this->fileSharesRequest->removeSharesToCircleId($share); |
226
|
|
|
// } |
227
|
|
|
// } |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @throws RequestBuilderException |
233
|
|
|
* @throws InitiatorNotFoundException |
234
|
|
|
*/ |
235
|
|
|
private function refreshDisplayName(): void { |
236
|
|
|
$params = new SimpleDataStore(['includeSystemCircles' => true]); |
237
|
|
|
$circleFilter = new Circle(); |
238
|
|
|
$circleFilter->setConfig(Circle::CFG_SINGLE); |
239
|
|
|
$circles = $this->circleService->getCircles($circleFilter, null, $params); |
240
|
|
|
|
241
|
|
|
foreach ($circles as $circle) { |
242
|
|
|
$owner = $circle->getOwner(); |
243
|
|
|
if ($owner->getUserType() === Member::TYPE_USER) { |
244
|
|
|
$user = $this->userManager->get($owner->getUserId()); |
245
|
|
|
$this->memberRequest->updateDisplayName($owner->getSingleId(), $user->getDisplayName()); |
246
|
|
|
$this->circleRequest->updateDisplayName($owner->getSingleId(), $user->getDisplayName()); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param string $message |
253
|
|
|
*/ |
254
|
|
|
private function output(string $message): void { |
255
|
|
|
if (!is_null($this->output)) { |
256
|
|
|
$this->output->writeln('- ' . $message); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
|