diff --git a/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionChannelReceive.php b/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionChannelReceive.php index 3989e67..4eed656 100644 --- a/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionChannelReceive.php +++ b/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionChannelReceive.php @@ -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)); } } \ No newline at end of file