From c0a1aec7f70f3c07bc39cea3909f864d6f08cc08 Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 13 Mar 2025 14:11:43 -0400 Subject: [PATCH] Add EncryptionDeleteChannel method and RPC integration for channel deletion https://github.com/nosial/Socialbox-PHP/issues/72 --- .../EncryptionDeleteChannel.php | 88 +++++++++++++++++++ src/Socialbox/Enums/StandardMethods.php | 3 + src/Socialbox/SocialClient.php | 16 ++++ 3 files changed, 107 insertions(+) create mode 100644 src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionDeleteChannel.php diff --git a/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionDeleteChannel.php b/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionDeleteChannel.php new file mode 100644 index 0000000..f826d4b --- /dev/null +++ b/src/Socialbox/Classes/StandardMethods/EncryptionChannel/EncryptionDeleteChannel.php @@ -0,0 +1,88 @@ +containsParameter('channel_uuid')) + { + throw new MissingRpcArgumentException('channel_uuid'); + } + + try + { + $requestingPeer = $request->getPeer(); + $encryptionChannel = EncryptionChannelManager::getChannel($rpcRequest->getParameter('channel_uuid')); + } + catch(DatabaseOperationException $e) + { + throw new StandardRpcException('There was an error while trying to obtain the encryption channel', StandardError::INTERNAL_SERVER_ERROR, $e); + } + + if($encryptionChannel === null) + { + return $rpcRequest->produceError(StandardError::NOT_FOUND, 'The requested encryption channel was not found'); + } + elseif(!$encryptionChannel->isParticipant($requestingPeer->getAddress())) + { + return $rpcRequest->produceError(StandardError::UNAUTHORIZED, 'The requested encryption channel is not accessible'); + } + elseif($encryptionChannel->getStatus() === EncryptionChannelStatus::CLOSED) + { + return $rpcRequest->produceResponse(false); + } + + try + { + EncryptionChannelManager::deleteChannel($encryptionChannel->getUuid()); + } + catch(DatabaseOperationException $e) + { + throw new StandardRpcException('An error occurred while trying to close the encryption channel', StandardError::INTERNAL_SERVER_ERROR, $e); + } + + $externalPeer = $encryptionChannel->getExternalPeer(); + if($externalPeer !== null) + { + try + { + $rpcClient = Socialbox::getExternalSession($encryptionChannel->getCallingPeerAddress()->getDomain()); + $rpcClient->encryptionCloseChannel( + channelUuid: $rpcRequest->getParameter('channel_uuid'), + identifiedAs: $requestingPeer->getAddress() + ); + } + catch(Exception $e) + { + if($e instanceof RpcException) + { + throw StandardRpcException::fromRpcException($e); + } + + throw new StandardRpcException('There was an error while trying to notify the external server of the encryption channel', StandardError::INTERNAL_SERVER_ERROR, $e); + } + } + + return $rpcRequest->produceResponse(true); + } + } \ No newline at end of file diff --git a/src/Socialbox/Enums/StandardMethods.php b/src/Socialbox/Enums/StandardMethods.php index 8109b44..1f385a3 100644 --- a/src/Socialbox/Enums/StandardMethods.php +++ b/src/Socialbox/Enums/StandardMethods.php @@ -27,6 +27,7 @@ use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionCloseChannel; use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionCreateChannel; use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionDeclineChannel; + use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionDeleteChannel; use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionGetChannel; use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionGetChannelRequests; use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionGetChannels; @@ -101,6 +102,7 @@ case ENCRYPTION_CLOSE_CHANNEL = 'encryptionCloseChannel'; case ENCRYPTION_CREATE_CHANNEL = 'encryptionCreateChannel'; case ENCRYPTION_DECLINE_CHANNEL = 'encryptionDeclineChannel'; + case ENCRYPTION_DELETE_CHANNEL = 'encryptionDeleteChannel'; case ENCRYPTION_GET_CHANNEL = 'encryptionGetChannel'; case ENCRYPTION_GET_CHANNEL_REQUESTS = 'encryptionGetChannelRequests'; case ENCRYPTION_GET_CHANNELS = 'encryptionGetChannels'; @@ -224,6 +226,7 @@ self::ENCRYPTION_CLOSE_CHANNEL => EncryptionCloseChannel::execute($request, $rpcRequest), self::ENCRYPTION_CREATE_CHANNEL => EncryptionCreateChannel::execute($request, $rpcRequest), self::ENCRYPTION_DECLINE_CHANNEL => EncryptionDeclineChannel::execute($request, $rpcRequest), + self::ENCRYPTION_DELETE_CHANNEL => EncryptionDeleteChannel::execute($request, $rpcRequest), self::ENCRYPTION_GET_CHANNEL => EncryptionGetChannel::execute($request, $rpcRequest), self::ENCRYPTION_GET_CHANNEL_REQUESTS => EncryptionGetChannelRequests::execute($request, $rpcRequest), self::ENCRYPTION_GET_CHANNELS => EncryptionGetChannels::execute($request, $rpcRequest), diff --git a/src/Socialbox/SocialClient.php b/src/Socialbox/SocialClient.php index 0adeb54..6670a91 100644 --- a/src/Socialbox/SocialClient.php +++ b/src/Socialbox/SocialClient.php @@ -759,6 +759,22 @@ )->getResponse()->getResult(); } + /** + * Deletes an encryption channel by its UUID. + * + * @param string $channelUuid The UUID of the encryption channel to delete. + * @return bool True if the channel was successfully deleted, false otherwise. + * @throws RpcException Thrown if there was an error with the RPC request + */ + public function encryptionDeleteChannel(string $channelUuid): bool + { + return $this->sendRequest( + new RpcRequest(StandardMethods::ENCRYPTION_DELETE_CHANNEL, parameters: [ + 'channel_uuid' => $channelUuid + ]) + )->getResponse()->getResult(); + } + /** * Retrieves an encryption channel by its UUID. *