2025-03-03 04:26:14 -05:00
< ? 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 EncryptionCloseChannel extends Method
{
/**
* @ inheritDoc
*/
public static function execute ( ClientRequest $request , RpcRequest $rpcRequest ) : ? SerializableInterface
{
if ( ! $rpcRequest -> containsParameter ( 'channel_uuid' ))
{
throw new MissingRpcArgumentException ( 'channel_uuid' );
}
2025-03-11 15:03:26 -04:00
try
2025-03-03 04:26:14 -05:00
{
2025-03-11 15:03:26 -04:00
if ( $request -> isExternal ())
{
return self :: handleExternal ( $request , $rpcRequest );
}
2025-03-03 04:26:14 -05:00
}
2025-03-11 15:03:26 -04:00
catch ( DatabaseOperationException $e )
2025-03-03 04:26:14 -05:00
{
2025-03-11 15:03:26 -04:00
throw new StandardRpcException ( 'There was an error while handling the external request' , StandardError :: INTERNAL_SERVER_ERROR , $e );
2025-03-03 04:26:14 -05:00
}
return self :: handleInternal ( $request , $rpcRequest );
}
/**
* @ param ClientRequest $request
* @ param RpcRequest $rpcRequest
* @ return SerializableInterface | null
* @ throws StandardRpcException
*/
private static function handleInternal ( ClientRequest $request , RpcRequest $rpcRequest ) : ? SerializableInterface
{
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 :: closeChannel ( $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 );
}
/**
* @ param ClientRequest $request
* @ param RpcRequest $rpcRequest
* @ return SerializableInterface | null
* @ throws StandardRpcException
*/
private static function handleExternal ( ClientRequest $request , RpcRequest $rpcRequest ) : ? SerializableInterface
{
if ( $request -> getIdentifyAs () === null )
{
throw new StandardRpcException ( 'The IdentifyAs field is required for external requests' , StandardError :: UNAUTHORIZED );
}
try
{
$encryptionChannel = EncryptionChannelManager :: getChannel ( $rpcRequest -> getParameter ( 'channel_uuid' ));
}
catch ( DatabaseOperationException $e )
{
throw new StandardRpcException ( 'An error occurred 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' );
}
2025-03-11 15:03:26 -04:00
elseif ( ! $encryptionChannel -> isParticipant ( $request -> getIdentifyAs ()))
2025-03-03 04:26:14 -05:00
{
return $rpcRequest -> produceError ( StandardError :: UNAUTHORIZED , 'The requested encryption channel is not accessible' );
}
2025-03-11 15:03:26 -04:00
elseif ( $encryptionChannel -> getStatus () === EncryptionChannelStatus :: CLOSED )
2025-03-03 04:26:14 -05:00
{
return $rpcRequest -> produceResponse ( false );
}
try
{
EncryptionChannelManager :: closeChannel ( $encryptionChannel -> getUuid ());
}
catch ( DatabaseOperationException $e )
{
throw new StandardRpcException ( 'An error occurred while trying to close the encryption channel' , StandardError :: INTERNAL_SERVER_ERROR , $e );
}
return $rpcRequest -> produceResponse ( true );
}
}