Restructured StandardMethod Namespaces

This commit is contained in:
netkas 2025-01-29 15:52:38 -05:00
parent d6e397247a
commit 1e10e761db
38 changed files with 72 additions and 82 deletions

View file

@ -0,0 +1,80 @@
<?php
namespace Socialbox\Classes\StandardMethods\AddressBook;
use InvalidArgumentException;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\StandardError;
use Socialbox\Enums\Types\ContactRelationshipType;
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;
use Socialbox\Socialbox;
class AddressBookAddContact 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);
}
if($rpcRequest->containsParameter('relationship'))
{
$relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship')));
if($relationship === null)
{
throw new StandardException('Invalid relationship type', StandardError::RPC_INVALID_ARGUMENTS);
}
}
else
{
$relationship = ContactRelationshipType::MUTUAL;
}
try
{
$peer = $request->getPeer();
if($peer->getAddress() == $address)
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Cannot add self as contact');
}
// Resolve the peer, this would throw a StandardException if something goes wrong
Socialbox::resolvePeer($address);
// Check if the contact already exists
if(ContactManager::isContact($peer, $address))
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact already exists');
}
// Create the contact
ContactManager::createContact($peer, $address, $relationship);
}
catch (DatabaseOperationException $e)
{
throw new StandardException('Failed to add contact', StandardError::INTERNAL_SERVER_ERROR, $e);
}
// Return success
return $rpcRequest->produceResponse(true);
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace Socialbox\Classes\StandardMethods\AddressBook;
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 AddressBookDeleteContact 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
{
// Check if the contact already exists
$peer = $request->getPeer();
if(!ContactManager::isContact($peer, $address))
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact does not exist');
}
// Create the contact
ContactManager::deleteContact($peer, $address);
}
catch (DatabaseOperationException $e)
{
throw new StandardException('Failed to remove contact', StandardError::INTERNAL_SERVER_ERROR, $e);
}
// Return success
return $rpcRequest->produceResponse(true);
}
}

View file

@ -0,0 +1,53 @@
<?php
namespace Socialbox\Classes\StandardMethods\AddressBook;
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());
}
}

View file

@ -0,0 +1,58 @@
<?php
namespace Socialbox\Classes\StandardMethods\AddressBook;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
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\RpcRequest;
class AddressBookGetContacts extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
$limit = Configuration::getPoliciesConfiguration()->getGetContactsLimit();
if($rpcRequest->containsParameter('limit'))
{
$limit = (int)$rpcRequest->getParameter('limit');
if($limit < 1)
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Invalid limit, must be greater than 0');
}
$limit = min($limit, Configuration::getPoliciesConfiguration()->getGetContactsLimit());
}
$page = 0;
if($rpcRequest->containsParameter('page'))
{
$page = (int)$rpcRequest->getParameter('page');
if($page < 0)
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Invalid page, must be greater than or equal to 0');
}
$page = max($page, 0);
}
try
{
$contacts = ContactManager::getContacts($request->getPeer(), $limit, $page);
}
catch(DatabaseOperationException $e)
{
throw new StandardException('Failed to get contacts', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(array_map(function($contact) {return $contact->toStandard();}, $contacts));
}
}

View file

@ -0,0 +1,72 @@
<?php
namespace Socialbox\Classes\StandardMethods\AddressBook;
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;
use Symfony\Component\Uid\Uuid;
class AddressBookTrustSignature 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);
}
if(!$rpcRequest->containsParameter('uuid'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'uuid' parameter");
}
try
{
$uuid = Uuid::fromString($rpcRequest->getParameter('uuid'));
}
catch(InvalidArgumentException $e)
{
throw new StandardException('Invalid UUID', StandardError::RPC_INVALID_ARGUMENTS, $e);
}
try
{
// Check if the contact already exists
$peer = $request->getPeer();
if(ContactManager::isContact($peer, $address))
{
}
// Create the contact
ContactManager::updateContactRelationship($peer, $address, $relationship);
}
catch (DatabaseOperationException $e)
{
throw new StandardException('Failed to update contact relationship', StandardError::INTERNAL_SERVER_ERROR, $e);
}
// Return success
return $rpcRequest->produceResponse(true);
}
}

View file

@ -0,0 +1,68 @@
<?php
namespace Socialbox\Classes\StandardMethods\AddressBook;
use InvalidArgumentException;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\StandardError;
use Socialbox\Enums\Types\ContactRelationshipType;
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 AddressBookUpdateRelationship 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);
}
if(!$rpcRequest->containsParameter('relationship'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Missing required relationship parameter');
}
$relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship')));
if($relationship === null)
{
throw new StandardException('Invalid relationship type', StandardError::RPC_INVALID_ARGUMENTS);
}
try
{
// Check if the contact already exists
$peer = $request->getPeer();
if(!ContactManager::isContact($peer, $address))
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact does not exist');
}
// Create the contact
ContactManager::updateContactRelationship($peer, $address, $relationship);
}
catch (DatabaseOperationException $e)
{
throw new StandardException('Failed to update contact relationship', StandardError::INTERNAL_SERVER_ERROR, $e);
}
// Return success
return $rpcRequest->produceResponse(true);
}
}