diff --git a/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionChannelReceive.php b/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionChannelReceive.php new file mode 100644 index 0000000..fa0db4e --- /dev/null +++ b/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionChannelReceive.php @@ -0,0 +1,98 @@ +containsParameter('channel_uuid')) + { + throw new MissingRpcArgumentException('channel_uuid'); + } + elseif(!Validator::validateUuid($rpcRequest->getParameter('channel_uuid'))) + { + throw new InvalidRpcArgumentException('channel_uuid', 'The given channel uuid is not a valid UUID V4'); + } + + try + { + $channelUuid = $rpcRequest->getParameter('channel_uuid'); + $encryptionChannel = EncryptionChannelManager::getChannel($channelUuid); + } + catch(DatabaseOperationException $e) + { + throw new StandardRpcException('Failed to retrieve the encryption channel', StandardError::INTERNAL_SERVER_ERROR, $e); + } + + if($encryptionChannel === null) + { + return $rpcRequest->produceError(StandardError::NOT_FOUND, 'The encryption channel does not exist'); + } + + try + { + $requestingPeer = $request->getPeer(); + } + catch (DatabaseOperationException $e) + { + throw new StandardRpcException('Failed to retrieve the peer', StandardError::INTERNAL_SERVER_ERROR, $e); + } + + if($requestingPeer === null) + { + return $rpcRequest->produceError(StandardError::UNAUTHORIZED, 'The peer is not authorized'); + } + + if(!$encryptionChannel->isParticipant($requestingPeer->getAddress())) + { + return $rpcRequest->produceError(StandardError::UNAUTHORIZED, 'The encryption channel is not accessible'); + } + elseif($encryptionChannel->getStatus() !== EncryptionChannelStatus::OPENED) + { + return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The encryption channel is not opened'); + } + + $acknowledge = false; + if($rpcRequest->containsParameter('acknowledge') && is_bool($rpcRequest->getParameter('acknowledge')) && $rpcRequest->getParameter('acknowledge')) + { + $acknowledge = true; + } + + try + { + + $messages = EncryptionChannelManager::receiveData( + $rpcRequest->getParameter('channel_uuid'), $encryptionChannel->determineRecipient($requestingPeer->getAddress(), true) + ); + + if($acknowledge) + { + EncryptionChannelManager::acknowledgeMessagesBatch($rpcRequest->getParameter('channel_uuid'), array_map(fn($message) => $message->getUuid(), $messages)); + } + } + catch(DatabaseOperationException $e) + { + throw new StandardRpcException('Failed to retrieve the messages', StandardError::INTERNAL_SERVER_ERROR, $e); + } + + return $rpcRequest->produceResponse(array_map(fn($message) => $message->toStandard(), $messages)); + } + } \ No newline at end of file