Refactor AddressBookAddContact to improve relationship parameter handling and return contact UUID on success

This commit is contained in:
netkas 2025-03-25 12:58:21 -04:00
parent 631689b489
commit c981ed3d9d
Signed by: netkas
GPG key ID: 4D8629441B76E4CC

View file

@ -2,7 +2,6 @@
namespace Socialbox\Classes\StandardMethods\AddressBook; namespace Socialbox\Classes\StandardMethods\AddressBook;
use InvalidArgumentException;
use Socialbox\Abstracts\Method; use Socialbox\Abstracts\Method;
use Socialbox\Enums\ReservedUsernames; use Socialbox\Enums\ReservedUsernames;
use Socialbox\Enums\StandardError; use Socialbox\Enums\StandardError;
@ -35,7 +34,7 @@
$peerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer')); $peerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
if($rpcRequest->containsParameter('relationship') && $rpcRequest->getParameter('relationship') !== null) if($rpcRequest->containsParameter('relationship'))
{ {
$relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship'))); $relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship')));
if($relationship === null) if($relationship === null)
@ -66,11 +65,11 @@
// Check if the contact already exists // Check if the contact already exists
if(ContactManager::isContact($peer, $peerAddress)) if(ContactManager::isContact($peer, $peerAddress))
{ {
return $rpcRequest->produceResponse(false); return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact already exists');
} }
// Create the contact // Create the contact
ContactManager::createContact($peer, $peerAddress, $relationship); $contactUuid = ContactManager::createContact($peer, $peerAddress, $relationship);
} }
catch (DatabaseOperationException $e) catch (DatabaseOperationException $e)
{ {
@ -78,6 +77,6 @@
} }
// Return success // Return success
return $rpcRequest->produceResponse(true); return $rpcRequest->produceResponse($contactUuid);
} }
} }