Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/Service/FederatedEventService.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@
use OCA\Circles\Tools\Model\Request;
use OCA\Circles\Tools\Traits\TNCRequest;
use OCA\Circles\Tools\Traits\TStringTools;
use OCP\AppFramework\Http;
use OCP\AppFramework\Services\IAppConfig;
use OCP\Server;
use Psr\Log\LoggerInterface;
use ReflectionClass;
use ReflectionException;

Expand Down Expand Up @@ -411,10 +413,44 @@ public function initBroadcast(FederatedEvent $event): bool {
$this->e($e);
}

if (!$isLoopbackTest && !$this->isLoopbackSuccessful($request)) {
Server::get(LoggerInterface::class)->warning(
'Loopback request failed, processing event synchronously. Please check the loopback setup with occ circles:check',
['url' => $request->getCompleteUrl(), 'status' => $request->hasResult() ? $request->getResult()->getStatusCode() : 0]
);
$this->manageWrappersSynchronously($wrapper->getToken());
}

return true;
}


/**
* asyncBroadcast() closes the connection with an empty 200 before processing the event
*/
private function isLoopbackSuccessful(NCRequest $request): bool {
if (!$request->hasResult() || $request->getResult()->hasException()) {
return false;
}

$result = $request->getResult();

return $result->getStatusCode() === Http::STATUS_OK && $result->getContent() === '';
}

/**
* same process as EventWrapperController::asyncBroadcast()
*/
private function manageWrappersSynchronously(string $token): void {
// EventWrapperService depends on this service
$eventWrapperService = Server::get(EventWrapperService::class);
foreach ($this->eventWrapperRequest->getByToken($token) as $wrapper) {
$eventWrapperService->manageWrapper($wrapper);
}

$eventWrapperService->confirmStatus($token);
}

/**
* @param FederatedEvent $event
*
Expand Down
4 changes: 3 additions & 1 deletion lib/Service/MiscService.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ public function asyncAndLeaveClientOutOfThis($result = '') {
ob_start();
echo(json_encode($result));
$size = ob_get_length();
header('Content-Length: ' . $size);
if ($size !== false) {
header('Content-Length: ' . $size);
}
ob_end_flush();
flush();
}
Expand Down
4 changes: 3 additions & 1 deletion lib/Tools/Traits/TAsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public function async(string $result = ''): void {
echo($result);

$size = ob_get_length();
header('Content-Length: ' . $size);
if ($size !== false) {
header('Content-Length: ' . $size);
}
ob_end_flush();
flush();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Tools/Traits/TNCLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ trait TNCLogger {
public static $INFO = 1;
public static $DEBUG = 0;


/**
* @param Throwable $t
* @param array $serializable
Expand Down Expand Up @@ -172,7 +171,8 @@ public function log(int $level, string $message, bool $trace = false, array $ser
* @return LoggerInterface
*/
public function logger(): LoggerInterface {
if (isset($this->logger)) {
/** @psalm-suppress UndefinedThisPropertyFetch Ugly but works */
if (isset($this->logger) && $this->logger instanceof LoggerInterface) {
return $this->logger;
} else {
return Server::get(LoggerInterface::class);
Expand Down
25 changes: 10 additions & 15 deletions lib/Tools/Traits/TNCRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
trait TNCRequest {
use TNCLogger;


/**
* @param int $size
*/
Expand Down Expand Up @@ -86,6 +85,7 @@ public function doRequest(NCRequest $request, bool $exceptionOnIssue = true): vo
* @return IClientService
*/
public function clientService(): IClientService {
/** @psalm-suppress UndefinedThisPropertyFetch Ugly but works */
if (isset($this->clientService) && $this->clientService instanceof IClientService) {
return $this->clientService;
} else {
Expand Down Expand Up @@ -140,19 +140,14 @@ private function generationClientOptions(NCRequest $request) {
*/
private function useClient(NCRequest $request): IResponse {
$client = $request->getClient();
switch ($request->getType()) {
case Request::TYPE_POST:
return $client->post($request->getCompleteUrl(), $request->getClientOptions());
case Request::TYPE_PUT:
return $client->put($request->getCompleteUrl(), $request->getClientOptions());
case Request::TYPE_DELETE:
return $client->delete($request->getCompleteUrl(), $request->getClientOptions());
case Request::TYPE_GET:
return $client->get(
$request->getCompleteUrl() . $request->getQueryString(), $request->getClientOptions()
);
default:
throw new Exception('unknown request type ' . json_encode($request));
}
return match ($request->getType()) {
Request::TYPE_POST => $client->post($request->getCompleteUrl(), $request->getClientOptions()),
Request::TYPE_PUT => $client->put($request->getCompleteUrl(), $request->getClientOptions()),
Request::TYPE_DELETE => $client->delete($request->getCompleteUrl(), $request->getClientOptions()),
Request::TYPE_GET => $client->get(
$request->getCompleteUrl() . $request->getQueryString(), $request->getClientOptions()
),
default => throw new Exception('unknown request type ' . json_encode($request)),
};
}
}
2 changes: 1 addition & 1 deletion lib/Tools/Traits/TStringTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function humanReadable(int $bytes): string {
$s = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
$e = floor(log($bytes, 1024));

return round($bytes / pow(1024, $e), 2) . ' ' . $s[$e];
return (string)round((float)$bytes / pow(1024, $e), 2) . ' ' . $s[$e];
}


Expand Down
2 changes: 1 addition & 1 deletion tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="6.5.0@38fc8444edf0cebc9205296ee6e30e906ade783b">
<files psalm-version="6.14.3@d0b040a91f280f071c1abcb1b77ce3822058725a">
<file src="lib/Activity/ProviderParser.php">
<TypeDoesNotContainType>
<code><![CDATA[str_starts_with($sK, '_')]]></code>
Expand Down
Loading
Loading