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 daita\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
|
|
View Code Duplication |
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
|
|
|
try { |
132
|
|
|
$this->output('remove deprecated shares'); |
133
|
|
|
// $this->removeDeprecatedShares(); |
134
|
|
|
} catch (Exception $e) { |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
try { |
138
|
|
|
$this->output('retry failed FederatedEvents'); |
139
|
|
|
$this->eventWrapperService->retry(); |
140
|
|
|
} catch (Exception $e) { |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($level < 1) { |
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($level < 2) { |
148
|
|
|
return; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ($level < 3) { |
152
|
|
|
return; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ($level < 5) { |
156
|
|
|
$this->output('refresh displayNames older than 7d'); |
157
|
|
|
// $this->refreshOldDisplayNames(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ($level < 4) { |
161
|
|
|
return; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ($level < 5) { |
165
|
|
|
return; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
try { |
169
|
|
|
$this->output('refresh DisplayNames'); |
170
|
|
|
$this->refreshDisplayName(); |
171
|
|
|
} catch (Exception $e) { |
|
|
|
|
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @throws InitiatorNotFoundException |
178
|
|
|
* @throws RequestBuilderException |
179
|
|
|
*/ |
180
|
|
|
private function removeCirclesWithNoOwner(): void { |
181
|
|
|
$circles = $this->circleService->getCircles(); |
182
|
|
|
foreach ($circles as $circle) { |
183
|
|
|
if (!$circle->hasOwner()) { |
184
|
|
|
$this->circleRequest->delete($circle); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* |
192
|
|
|
*/ |
193
|
|
|
private function removeMembersWithNoCircles(): void { |
194
|
|
|
// $members = $this->membersRequest->forceGetAllMembers(); |
195
|
|
|
// |
196
|
|
|
// foreach ($members as $member) { |
197
|
|
|
// try { |
198
|
|
|
// $this->circlesRequest->forceGetCircle($member->getCircleId()); |
199
|
|
|
// } catch (CircleDoesNotExistException $e) { |
200
|
|
|
// $this->membersRequest->removeMember($member); |
201
|
|
|
// } |
202
|
|
|
// } |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
private function removeDeprecatedShares(): void { |
|
|
|
|
207
|
|
|
// $circles = array_map( |
208
|
|
|
// function(DeprecatedCircle $circle) { |
209
|
|
|
// return $circle->getUniqueId(); |
210
|
|
|
// }, $this->circlesRequest->forceGetCircles() |
211
|
|
|
// ); |
212
|
|
|
// |
213
|
|
|
// $shares = array_unique( |
214
|
|
|
// array_map( |
215
|
|
|
// function($share) { |
216
|
|
|
// return $share['share_with']; |
217
|
|
|
// }, $this->fileSharesRequest->getShares() |
218
|
|
|
// ) |
219
|
|
|
// ); |
220
|
|
|
// |
221
|
|
|
// foreach ($shares as $share) { |
222
|
|
|
// if (!in_array($share, $circles)) { |
223
|
|
|
// $this->fileSharesRequest->removeSharesToCircleId($share); |
224
|
|
|
// } |
225
|
|
|
// } |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @throws RequestBuilderException |
231
|
|
|
* @throws InitiatorNotFoundException |
232
|
|
|
*/ |
233
|
|
|
private function refreshDisplayName(): void { |
234
|
|
|
$params = new SimpleDataStore(['includeSystemCircles' => true]); |
235
|
|
|
$circleFilter = new Circle(); |
236
|
|
|
$circleFilter->setConfig(Circle::CFG_SINGLE); |
237
|
|
|
$circles = $this->circleService->getCircles($circleFilter, null, $params); |
238
|
|
|
|
239
|
|
|
foreach ($circles as $circle) { |
240
|
|
|
$owner = $circle->getOwner(); |
241
|
|
|
if ($owner->getUserType() === Member::TYPE_USER) { |
242
|
|
|
$user = $this->userManager->get($owner->getUserId()); |
243
|
|
|
$this->memberRequest->updateDisplayName($owner->getSingleId(), $user->getDisplayName()); |
244
|
|
|
$this->circleRequest->updateDisplayName($owner->getSingleId(), $user->getDisplayName()); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param string $message |
251
|
|
|
*/ |
252
|
|
|
private function output(string $message): void { |
253
|
|
|
if (!is_null($this->output)) { |
254
|
|
|
$this->output->writeln('- ' . $message); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.