Refactor AddressBook methods to streamline peer address handling and improve error management

https://github.com/nosial/Socialbox-PHP/issues/29
This commit is contained in:
netkas 2025-03-11 22:48:38 -04:00
parent 81b3dcf3ea
commit a842818175
Signed by: netkas
GPG key ID: 4D8629441B76E4CC
7 changed files with 23 additions and 58 deletions

View file

@ -32,14 +32,7 @@
throw new MissingRpcArgumentException('peer'); throw new MissingRpcArgumentException('peer');
} }
try $peerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
{
$address = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
}
catch(InvalidArgumentException $e)
{
throw new InvalidRpcArgumentException('peer', $e);
}
if($rpcRequest->containsParameter('relationship') && $rpcRequest->getParameter('relationship') !== null) if($rpcRequest->containsParameter('relationship') && $rpcRequest->getParameter('relationship') !== null)
{ {
@ -57,22 +50,22 @@
try try
{ {
$peer = $request->getPeer(); $peer = $request->getPeer();
if($peer->getAddress() == $address) if($peer->getAddress() == $peerAddress)
{ {
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Cannot add self as contact'); return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Cannot add self as contact');
} }
// Resolve the peer, this would throw a StandardException if something goes wrong // Resolve the peer, this would throw a StandardException if something goes wrong
Socialbox::resolvePeer($address); Socialbox::resolvePeer($peerAddress);
// Check if the contact already exists // Check if the contact already exists
if(ContactManager::isContact($peer, $address)) if(ContactManager::isContact($peer, $peerAddress))
{ {
return $rpcRequest->produceResponse(false); return $rpcRequest->produceResponse(false);
} }
// Create the contact // Create the contact
ContactManager::createContact($peer, $address, $relationship); ContactManager::createContact($peer, $peerAddress, $relationship);
} }
catch (DatabaseOperationException $e) catch (DatabaseOperationException $e)
{ {

View file

@ -29,19 +29,12 @@
throw new MissingRpcArgumentException('peer'); throw new MissingRpcArgumentException('peer');
} }
try $peerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
{
$address = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
}
catch(InvalidArgumentException $e)
{
throw new InvalidRpcArgumentException('peer', $e);
}
try try
{ {
$peer = $request->getPeer(); $peer = $request->getPeer();
return $rpcRequest->produceResponse(ContactManager::isContact($peer, $address)); return $rpcRequest->produceResponse(ContactManager::isContact($peer, $peerAddress));
} }
catch (DatabaseOperationException $e) catch (DatabaseOperationException $e)
{ {

View file

@ -30,26 +30,19 @@
throw new MissingRpcArgumentException('peer'); throw new MissingRpcArgumentException('peer');
} }
try $peerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
{
$address = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
}
catch(InvalidArgumentException $e)
{
throw new InvalidRpcArgumentException('peer', $e);
}
try try
{ {
// Check if the contact already exists // Check if the contact already exists
$peer = $request->getPeer(); $peer = $request->getPeer();
if(!ContactManager::isContact($peer, $address)) if(!ContactManager::isContact($peer, $peerAddress))
{ {
return $rpcRequest->produceResponse(false); return $rpcRequest->produceResponse(false);
} }
// Create the contact // Create the contact
ContactManager::deleteContact($peer, $address); ContactManager::deleteContact($peer, $peerAddress);
} }
catch (DatabaseOperationException $e) catch (DatabaseOperationException $e)
{ {

View file

@ -29,16 +29,16 @@
throw new MissingRpcArgumentException('peer'); throw new MissingRpcArgumentException('peer');
} }
$address = PeerAddress::fromAddress($rpcRequest->getParameter('peer')); $peerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
try try
{ {
if(!ContactManager::isContact($request->getPeer(), $address)) if(!ContactManager::isContact($request->getPeer(), $peerAddress))
{ {
return $rpcRequest->produceResponse(); return $rpcRequest->produceResponse();
} }
return $rpcRequest->produceResponse(ContactManager::getStandardContact($request->getPeer(), $address)); return $rpcRequest->produceResponse(ContactManager::getStandardContact($request->getPeer(), $peerAddress));
} }
catch(DatabaseOperationException $e) catch(DatabaseOperationException $e)
{ {

View file

@ -28,14 +28,7 @@
throw new MissingRpcArgumentException('peer'); throw new MissingRpcArgumentException('peer');
} }
try $peerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
{
$address = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
}
catch(InvalidArgumentException $e)
{
throw new InvalidRpcArgumentException('peer', $e);
}
if(!$rpcRequest->containsParameter('signature_uuid')) if(!$rpcRequest->containsParameter('signature_uuid'))
{ {
@ -45,7 +38,7 @@
try try
{ {
// Check if the contact already exists // Check if the contact already exists
$contact = ContactManager::getContact($request->getPeer(), $address); $contact = ContactManager::getContact($request->getPeer(), $peerAddress);
} }
catch (DatabaseOperationException $e) catch (DatabaseOperationException $e)
{ {

View file

@ -27,7 +27,7 @@
throw new MissingRpcArgumentException('peer'); throw new MissingRpcArgumentException('peer');
} }
$address = PeerAddress::fromAddress($rpcRequest->getParameter('peer')); $peerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
if(!$rpcRequest->containsParameter('signature_uuid')) if(!$rpcRequest->containsParameter('signature_uuid'))
{ {
@ -35,18 +35,18 @@
} }
$signatureUuid = (string)$rpcRequest->getParameter('signature_uuid'); $signatureUuid = (string)$rpcRequest->getParameter('signature_uuid');
$signingKey = Socialbox::resolvePeerSignature($address, $signatureUuid); $signingKey = Socialbox::resolvePeerSignature($peerAddress, $signatureUuid);
try try
{ {
// Check if the contact already exists // Check if the contact already exists
$peer = $request->getPeer(); $peer = $request->getPeer();
if(!ContactManager::isContact($peer, $address)) if(!ContactManager::isContact($peer, $peerAddress))
{ {
ContactManager::createContact($peer, $address); ContactManager::createContact($peer, $peerAddress);
} }
$contact = ContactManager::getContact($peer, $address); $contact = ContactManager::getContact($peer, $peerAddress);
if(ContactManager::contactGetSigningKeysCount($contact) > Configuration::getPoliciesConfiguration()->getMaxContactSigningKeys()) if(ContactManager::contactGetSigningKeysCount($contact) > Configuration::getPoliciesConfiguration()->getMaxContactSigningKeys())
{ {
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The contact has exceeded the maximum amount of trusted signatures'); return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The contact has exceeded the maximum amount of trusted signatures');

View file

@ -28,14 +28,7 @@
throw new MissingRpcArgumentException('peer'); throw new MissingRpcArgumentException('peer');
} }
try $peerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
{
$receivingPeerAddress = PeerAddress::fromAddress($rpcRequest->getParameter('peer'));
}
catch(InvalidArgumentException $e)
{
throw new InvalidRpcArgumentException('peer', $e);
}
if(!$rpcRequest->containsParameter('relationship')) if(!$rpcRequest->containsParameter('relationship'))
{ {
@ -51,13 +44,13 @@
{ {
// Check if the contact already exists // Check if the contact already exists
$requestingPeer = $request->getPeer(); $requestingPeer = $request->getPeer();
if(!ContactManager::isContact($requestingPeer->getUuid(), $receivingPeerAddress)) if(!ContactManager::isContact($requestingPeer->getUuid(), $peerAddress))
{ {
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact does not exist'); return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact does not exist');
} }
// Create the contact // Create the contact
ContactManager::updateContactRelationship($requestingPeer->getUuid(), $receivingPeerAddress, $newRelationship); ContactManager::updateContactRelationship($requestingPeer->getUuid(), $peerAddress, $newRelationship);
} }
catch (DatabaseOperationException $e) catch (DatabaseOperationException $e)
{ {