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
|
|
|
namespace OCA\Circles\Service; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
use daita\MySmallPhpTools\ActivityPub\Nextcloud\nc22\NC22Signature; |
35
|
|
|
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Request; |
36
|
|
|
use daita\MySmallPhpTools\Traits\TStringTools; |
37
|
|
|
use Exception; |
38
|
|
|
use OCA\Circles\Db\EventWrapperRequest; |
39
|
|
|
use OCA\Circles\Model\Federated\EventWrapper; |
40
|
|
|
use OCA\Circles\Model\Federated\FederatedEvent; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Class EventWrapperService |
45
|
|
|
* |
46
|
|
|
* @package OCA\Circles\Service |
47
|
|
|
*/ |
48
|
|
|
class EventWrapperService extends NC22Signature { |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
use TNC22Request; |
52
|
|
|
use TStringTools; |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** @var EventWrapperRequest */ |
56
|
|
|
private $eventWrapperRequest; |
57
|
|
|
|
58
|
|
|
/** @var FederatedEventService */ |
59
|
|
|
private $federatedEventService; |
60
|
|
|
|
61
|
|
|
/** @var RemoteUpstreamService */ |
62
|
|
|
private $remoteUpstreamService; |
63
|
|
|
|
64
|
|
|
/** @var ConfigService */ |
65
|
|
|
private $configService; |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* EventWrapperService constructor. |
70
|
|
|
* |
71
|
|
|
* @param EventWrapperRequest $eventWrapperRequest |
72
|
|
|
* @param FederatedEventService $federatedEventService |
73
|
|
|
* @param RemoteUpstreamService $remoteUpstreamService |
74
|
|
|
* @param ConfigService $configService |
75
|
|
|
*/ |
76
|
|
|
public function __construct( |
77
|
|
|
EventWrapperRequest $eventWrapperRequest, |
78
|
|
|
FederatedEventService $federatedEventService, |
79
|
|
|
RemoteUpstreamService $remoteUpstreamService, |
80
|
|
|
ConfigService $configService |
81
|
|
|
) { |
82
|
|
|
$this->eventWrapperRequest = $eventWrapperRequest; |
83
|
|
|
$this->federatedEventService = $federatedEventService; |
84
|
|
|
$this->remoteUpstreamService = $remoteUpstreamService; |
85
|
|
|
$this->configService = $configService; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $token |
91
|
|
|
* @param bool $refresh |
92
|
|
|
*/ |
93
|
|
|
public function confirmStatus(string $token, bool $refresh = false): void { |
94
|
|
|
$wrappers = $this->eventWrapperRequest->getByToken($token); |
95
|
|
|
|
96
|
|
|
foreach ($wrappers as $wrapper) { |
97
|
|
|
$status = $wrapper->getStatus(); |
98
|
|
|
if ($refresh && $status === EventWrapper::STATUS_FAILED) { |
99
|
|
|
$wrapper->setStatus(EventWrapper::STATUS_INIT); |
100
|
|
|
$this->eventWrapperRequest->update($wrapper); |
101
|
|
|
$status = $this->manageWrapper($wrapper); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($status !== EventWrapper::STATUS_DONE) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->federatedEventService->manageResults($token); |
110
|
|
|
$this->eventWrapperRequest->updateAll($token, EventWrapper::STATUS_OVER); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param EventWrapper $wrapper |
116
|
|
|
* |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
|
|
public function manageWrapper(EventWrapper $wrapper): int { |
120
|
|
|
if ($wrapper->getStatus() !== EventWrapper::STATUS_INIT) { |
121
|
|
|
return $wrapper->getStatus(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$status = EventWrapper::STATUS_FAILED; |
125
|
|
|
|
126
|
|
|
try { |
127
|
|
|
if ($this->configService->isLocalInstance($wrapper->getInstance())) { |
128
|
|
|
$gs = $this->federatedEventService->getFederatedItem($wrapper->getEvent(), false); |
129
|
|
|
$gs->manage($wrapper->getEvent()); |
130
|
|
|
} else { |
131
|
|
|
$this->remoteUpstreamService->broadcastEvent($wrapper); |
132
|
|
|
} |
133
|
|
|
$status = EventWrapper::STATUS_DONE; |
134
|
|
|
} catch (Exception $e) { |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ($wrapper->getSeverity() !== FederatedEvent::SEVERITY_HIGH) { |
138
|
|
|
$status = EventWrapper::STATUS_OVER; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$wrapper->setStatus($status); |
142
|
|
|
$wrapper->setResult($wrapper->getEvent()->getResult()); |
143
|
|
|
|
144
|
|
|
$this->eventWrapperRequest->update($wrapper); |
145
|
|
|
|
146
|
|
|
return $status; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* retry failed High Severity FederatedEvent |
152
|
|
|
*/ |
153
|
|
|
public function retry() { |
154
|
|
|
$tokens = $this->getFailedEvents(); |
155
|
|
|
echo json_encode($tokens) . "\n"; |
156
|
|
|
foreach ($tokens as $token) { |
157
|
|
|
$this->confirmStatus($token, true); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* returns token from failed FederatedEvent |
164
|
|
|
*/ |
165
|
|
|
private function getFailedEvents(): array { |
166
|
|
|
$token = array_map( |
167
|
|
|
function(EventWrapper $event): string { |
168
|
|
|
return $event->getToken(); |
169
|
|
|
}, $this->eventWrapperRequest->getFailedEvents() |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
return array_values(array_unique($token)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|