refactor: unify streaming
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -16,6 +16,8 @@ use KTXC\Http\Response\StreamedNdJsonResponse;
|
||||
use KTXC\SessionIdentity;
|
||||
use KTXC\SessionTenant;
|
||||
use KTXF\Controller\ControllerAbstract;
|
||||
use KTXF\Json\JsonSerializable;
|
||||
use KTXF\Resource\Provider\ResourceServiceLocationInterface;
|
||||
use KTXF\Resource\Selector\SourceSelector;
|
||||
use KTXF\Routing\Attributes\AuthenticatedRoute;
|
||||
use KTXM\MailManager\Manager;
|
||||
@@ -133,7 +135,7 @@ class DefaultController extends ControllerAbstract {
|
||||
'service.create' => $this->serviceCreate($tenantId, $userId, $data),
|
||||
'service.update' => $this->serviceUpdate($tenantId, $userId, $data),
|
||||
'service.delete' => $this->serviceDelete($tenantId, $userId, $data),
|
||||
'service.discover' => $this->serviceDiscover($tenantId, $userId, $data),
|
||||
'service.discover' => $this->serviceDiscover($tenantId, $userId, $data, $version, $transaction),
|
||||
'service.test' => $this->serviceTest($tenantId, $userId, $data),
|
||||
|
||||
// Collection operations
|
||||
@@ -346,18 +348,48 @@ class DefaultController extends ControllerAbstract {
|
||||
);
|
||||
}
|
||||
|
||||
private function serviceDiscover(string $tenantId, string $userId, array $data): mixed {
|
||||
private function serviceDiscover(string $tenantId, string $userId, array $data, int $version, string $transaction): StreamedNdJsonResponse {
|
||||
|
||||
if (!isset($data['identity']) || empty($data['identity']) || !is_string($data['identity'])) {
|
||||
throw new InvalidArgumentException(self::ERR_INVALID_DATA);
|
||||
}
|
||||
|
||||
$provider = $data['provider'] ?? null;
|
||||
$identity = $data['identity'];
|
||||
$location = $data['location'] ?? null;
|
||||
$secret = $data['secret'] ?? null;
|
||||
|
||||
return $this->mailManager->serviceDiscover($tenantId, $userId, $provider, $identity, $location, $secret);
|
||||
$provider = $data['provider'] ?? null;
|
||||
$identity = $data['identity'];
|
||||
$location = $data['location'] ?? null;
|
||||
$secret = $data['secret'] ?? null;
|
||||
|
||||
$discoverGenerator = $this->mailManager->serviceDiscover($tenantId, $userId, $provider, $identity, $location, $secret);
|
||||
$logger = $this->logger;
|
||||
|
||||
$response = (function () use ($discoverGenerator, $version, $transaction, $logger): \Generator {
|
||||
yield ['type' => 'control', 'status' => 'start', 'version' => $version, 'transaction' => $transaction];
|
||||
|
||||
$total = 0;
|
||||
try {
|
||||
foreach ($discoverGenerator as $providerId => $serviceLocation) {
|
||||
if (!$serviceLocation instanceof ResourceServiceLocationInterface) {
|
||||
continue;
|
||||
}
|
||||
yield [
|
||||
'type' => 'data',
|
||||
'data' => [
|
||||
'provider' => $providerId,
|
||||
'location' => $serviceLocation->jsonSerialize()
|
||||
]
|
||||
];
|
||||
$total++;
|
||||
}
|
||||
} catch (\Throwable $t) {
|
||||
$logger->error('Error streaming service discovery', ['exception' => $t]);
|
||||
yield ['type' => 'error', 'message' => $t->getMessage()];
|
||||
return;
|
||||
}
|
||||
|
||||
yield ['type' => 'control', 'status' => 'end', 'total' => $total];
|
||||
})();
|
||||
|
||||
return new StreamedNdJsonResponse($response, 1, 200, ['Content-Type' => 'application/json']);
|
||||
}
|
||||
|
||||
// ==================== Collection Operations ====================
|
||||
@@ -647,16 +679,19 @@ class DefaultController extends ControllerAbstract {
|
||||
$entityGenerator = $this->mailManager->entityStream($tenantId, $userId, $sources, $filter, $sort, $range);
|
||||
$logger = $this->logger;
|
||||
|
||||
$lines = (function () use ($entityGenerator, $version, $transaction, $logger): \Generator {
|
||||
yield ['type' => 'meta', 'version' => $version, 'transaction' => $transaction, 'status' => 'success'];
|
||||
$responseGenerator = (function () use ($entityGenerator, $version, $transaction, $logger): \Generator {
|
||||
yield ['type' => 'control', 'status' => 'start', 'version' => $version, 'transaction' => $transaction];
|
||||
|
||||
$total = 0;
|
||||
try {
|
||||
foreach ($entityGenerator as $entity) {
|
||||
$entityData = is_array($entity)
|
||||
? $entity
|
||||
: (method_exists($entity, 'jsonSerialize') ? $entity->jsonSerialize() : (array) $entity);
|
||||
yield ['type' => 'entity'] + $entityData;
|
||||
if (!$entity instanceof JsonSerializable) {
|
||||
continue;
|
||||
}
|
||||
yield [
|
||||
'type' => 'data',
|
||||
'data' => $entity->jsonSerialize()
|
||||
];
|
||||
$total++;
|
||||
}
|
||||
} catch (\Throwable $t) {
|
||||
@@ -665,10 +700,10 @@ class DefaultController extends ControllerAbstract {
|
||||
return;
|
||||
}
|
||||
|
||||
yield ['type' => 'done', 'total' => $total];
|
||||
yield ['type' => 'control', 'status' => 'end', 'total' => $total];
|
||||
})();
|
||||
|
||||
return new StreamedNdJsonResponse($lines, self::STREAM_FLUSH_INTERVAL, 200, ['Content-Type' => 'application/json']);
|
||||
return new StreamedNdJsonResponse($responseGenerator, 1, 200, ['Content-Type' => 'application/json']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -303,7 +303,7 @@ class Manager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Discover mail service settings from identity
|
||||
* Discover mail service settings from identity, yielding results as each provider completes
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*
|
||||
@@ -314,12 +314,7 @@ class Manager {
|
||||
* @param string|null $location Optional hostname to test directly (bypasses DNS SRV lookup)
|
||||
* @param string|null $secret Optional password/token to validate discovered service
|
||||
*
|
||||
* @return array<string,ResourceServiceLocationInterface> Array of discovered service locations keyed by provider ID
|
||||
* [
|
||||
* 'jmap' => ResourceServiceLocationInterface,
|
||||
* 'smtp' => ResourceServiceLocationInterface,
|
||||
* // Only providers that successfully discovered (non-null)
|
||||
* ]
|
||||
* @return \Generator Yields providerId => ResourceServiceLocationInterface pairs as each provider completes
|
||||
*/
|
||||
public function serviceDiscover(
|
||||
string $tenantId,
|
||||
@@ -328,32 +323,28 @@ class Manager {
|
||||
string $identity,
|
||||
string|null $location = null,
|
||||
string|null $secret = null
|
||||
): array {
|
||||
$locations = [];
|
||||
|
||||
): \Generator {
|
||||
$providers = $this->providerList($tenantId, $userId, $providerId !== null ? new SourceSelector([$providerId => true]) : null);
|
||||
|
||||
foreach ($providers as $providerId => $provider) {
|
||||
|
||||
foreach ($providers as $currentProviderId => $provider) {
|
||||
if (!($provider instanceof ProviderServiceDiscoverInterface)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
$location = $provider->serviceDiscover($tenantId, $userId, $identity, $location, $secret);
|
||||
|
||||
if ($location !== null) {
|
||||
$locations[$providerId] = $location;
|
||||
$result = $provider->serviceDiscover($tenantId, $userId, $identity, $location, $secret);
|
||||
|
||||
if ($result !== null) {
|
||||
yield $currentProviderId => $result;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->warning('Provider autodiscovery failed', [
|
||||
'provider' => $providerId,
|
||||
'provider' => $currentProviderId,
|
||||
'identity' => $identity,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return $locations;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user