Added the method AddressBookGetContact and minor improvements in other implementations

This commit is contained in:
netkas 2025-01-24 19:12:03 -05:00
parent e2a0e2f27f
commit aba9adf916
4 changed files with 62 additions and 8 deletions

View file

@ -0,0 +1,53 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use InvalidArgumentException;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\ContactManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\PeerAddress;
use Socialbox\Objects\RpcRequest;
class AddressBookGetContact extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('peer'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Missing required peer parameter');
}
try
{
$address = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
}
catch(InvalidArgumentException $e)
{
throw new StandardException('Invalid peer address', StandardError::RPC_INVALID_ARGUMENTS, $e);
}
try
{
if(!ContactManager::isContact($request->getPeer(), $address))
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact does not exist');
}
$contact = ContactManager::getContact($request->getPeer(), $address);
}
catch(DatabaseOperationException $e)
{
throw new StandardException('Failed to get contact', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse($contact->toStandard());
}
}