Add EncryptionDeleteChannel method and RPC integration for channel deletion

https://github.com/nosial/Socialbox-PHP/issues/72
This commit is contained in:
netkas 2025-03-13 14:11:43 -04:00
parent 458d1bd9f9
commit c0a1aec7f7
Signed by: netkas
GPG key ID: 4D8629441B76E4CC
3 changed files with 107 additions and 0 deletions

View file

@ -0,0 +1,88 @@
<?php
namespace Socialbox\Classes\StandardMethods\EncryptionChannel;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\StandardError;
use Socialbox\Enums\Status\EncryptionChannelStatus;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\RpcException;
use Socialbox\Exceptions\Standard\MissingRpcArgumentException;
use Socialbox\Exceptions\Standard\StandardRpcException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\EncryptionChannelManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
use Socialbox\Socialbox;
class EncryptionDeleteChannel extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->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);
}
}

View file

@ -27,6 +27,7 @@
use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionCloseChannel; use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionCloseChannel;
use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionCreateChannel; use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionCreateChannel;
use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionDeclineChannel; use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionDeclineChannel;
use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionDeleteChannel;
use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionGetChannel; use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionGetChannel;
use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionGetChannelRequests; use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionGetChannelRequests;
use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionGetChannels; use Socialbox\Classes\StandardMethods\EncryptionChannel\EncryptionGetChannels;
@ -101,6 +102,7 @@
case ENCRYPTION_CLOSE_CHANNEL = 'encryptionCloseChannel'; case ENCRYPTION_CLOSE_CHANNEL = 'encryptionCloseChannel';
case ENCRYPTION_CREATE_CHANNEL = 'encryptionCreateChannel'; case ENCRYPTION_CREATE_CHANNEL = 'encryptionCreateChannel';
case ENCRYPTION_DECLINE_CHANNEL = 'encryptionDeclineChannel'; case ENCRYPTION_DECLINE_CHANNEL = 'encryptionDeclineChannel';
case ENCRYPTION_DELETE_CHANNEL = 'encryptionDeleteChannel';
case ENCRYPTION_GET_CHANNEL = 'encryptionGetChannel'; case ENCRYPTION_GET_CHANNEL = 'encryptionGetChannel';
case ENCRYPTION_GET_CHANNEL_REQUESTS = 'encryptionGetChannelRequests'; case ENCRYPTION_GET_CHANNEL_REQUESTS = 'encryptionGetChannelRequests';
case ENCRYPTION_GET_CHANNELS = 'encryptionGetChannels'; case ENCRYPTION_GET_CHANNELS = 'encryptionGetChannels';
@ -224,6 +226,7 @@
self::ENCRYPTION_CLOSE_CHANNEL => EncryptionCloseChannel::execute($request, $rpcRequest), self::ENCRYPTION_CLOSE_CHANNEL => EncryptionCloseChannel::execute($request, $rpcRequest),
self::ENCRYPTION_CREATE_CHANNEL => EncryptionCreateChannel::execute($request, $rpcRequest), self::ENCRYPTION_CREATE_CHANNEL => EncryptionCreateChannel::execute($request, $rpcRequest),
self::ENCRYPTION_DECLINE_CHANNEL => EncryptionDeclineChannel::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 => EncryptionGetChannel::execute($request, $rpcRequest),
self::ENCRYPTION_GET_CHANNEL_REQUESTS => EncryptionGetChannelRequests::execute($request, $rpcRequest), self::ENCRYPTION_GET_CHANNEL_REQUESTS => EncryptionGetChannelRequests::execute($request, $rpcRequest),
self::ENCRYPTION_GET_CHANNELS => EncryptionGetChannels::execute($request, $rpcRequest), self::ENCRYPTION_GET_CHANNELS => EncryptionGetChannels::execute($request, $rpcRequest),

View file

@ -759,6 +759,22 @@
)->getResponse()->getResult(); )->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. * Retrieves an encryption channel by its UUID.
* *