Enhance message acknowledgment handling in EncryptionChannelReceive with improved error management

https://github.com/nosial/Socialbox-PHP/issues/16
This commit is contained in:
netkas 2025-03-07 19:32:03 -05:00
parent 7165734aef
commit 0ff277ed4c

View file

@ -76,57 +76,64 @@
try
{
$messages = EncryptionChannelManager::receiveData(
$rpcRequest->getParameter('channel_uuid'), $encryptionChannel->determineRecipient($requestingPeer->getAddress(), true)
);
$messageUuids = array_map(fn($message) => $message->getUuid(), $messages);
if($acknowledge)
{
EncryptionChannelManager::acknowledgeMessagesBatch(
channelUuid: $rpcRequest->getParameter('channel_uuid'),
messageUuids: $messageUuids,
);
$externalPeer = $encryptionChannel->getExternalPeer();
if($externalPeer !== null)
{
try
{
$rpcClient = Socialbox::getExternalSession($externalPeer->getDomain());
$rpcClient->encryptionChannelAcknowledgeMessages(
channelUuid: (string)$rpcRequest->getParameter('channel_uuid'),
messageUuids: $messageUuids,
identifiedAs: $requestingPeer->getAddress()
);
}
catch(Exception $e)
{
try
{
EncryptionChannelManager::rejectMessage($rpcRequest->getParameter('channel_uuid'), $rpcRequest->getParameter('message_uuid'), true);
}
catch (DatabaseOperationException $e)
{
Logger::getLogger()->error('Error rejecting message as server', $e);
}
if($e instanceof RpcException)
{
throw StandardRpcException::fromRpcException($e);
}
throw new StandardRpcException('Failed to acknowledge the message with the external server', StandardError::INTERNAL_SERVER_ERROR, $e);
}
}
}
}
catch(DatabaseOperationException $e)
{
throw new StandardRpcException('Failed to retrieve the messages', StandardError::INTERNAL_SERVER_ERROR, $e);
}
if($acknowledge)
{
$messageUuids = array_map(fn($message) => $message->getUuid(), $messages);
try
{
EncryptionChannelManager::acknowledgeMessagesBatch(
channelUuid: $rpcRequest->getParameter('channel_uuid'),
messageUuids: $messageUuids,
);
}
catch (DatabaseOperationException $e)
{
throw new StandardRpcException('Failed to acknowledge the messages locally', StandardError::INTERNAL_SERVER_ERROR, $e);
}
$externalPeer = $encryptionChannel->getExternalPeer();
if($externalPeer !== null)
{
try
{
$rpcClient = Socialbox::getExternalSession($externalPeer->getDomain());
$rpcClient->encryptionChannelAcknowledgeMessages(
channelUuid: (string)$rpcRequest->getParameter('channel_uuid'),
messageUuids: $messageUuids,
identifiedAs: $requestingPeer->getAddress()
);
}
catch(Exception $e)
{
try
{
EncryptionChannelManager::rejectMessage($rpcRequest->getParameter('channel_uuid'), $rpcRequest->getParameter('message_uuid'), true);
}
catch (DatabaseOperationException $e)
{
Logger::getLogger()->error('Error rejecting message as server', $e);
}
if($e instanceof RpcException)
{
throw StandardRpcException::fromRpcException($e);
}
throw new StandardRpcException('Failed to acknowledge the message with the external server', StandardError::INTERNAL_SERVER_ERROR, $e);
}
}
}
return $rpcRequest->produceResponse(array_map(fn($message) => $message->toStandard(), $messages));
}
}