Enhance logging in ContactManager and improve error messages for invalid peer addresses

This commit is contained in:
netkas 2025-03-25 12:58:53 -04:00
parent 97ca09d7dd
commit 52985b6308
Signed by: netkas
GPG key ID: 4D8629441B76E4CC

View file

@ -9,6 +9,7 @@
use PDOException; use PDOException;
use Socialbox\Classes\Configuration; use Socialbox\Classes\Configuration;
use Socialbox\Classes\Database; use Socialbox\Classes\Database;
use Socialbox\Classes\Logger;
use Socialbox\Classes\Validator; use Socialbox\Classes\Validator;
use Socialbox\Enums\Types\ContactRelationshipType; use Socialbox\Enums\Types\ContactRelationshipType;
use Socialbox\Exceptions\DatabaseOperationException; use Socialbox\Exceptions\DatabaseOperationException;
@ -50,6 +51,8 @@
throw new InvalidArgumentException('The given peer internal UUID is not a valid UUID V4'); throw new InvalidArgumentException('The given peer internal UUID is not a valid UUID V4');
} }
Logger::getLogger()->debug(sprintf('Querying if %s is a contact of %s', $contactAddress, $peerUuid));
try try
{ {
// Check if the contact is already in the database // Check if the contact is already in the database
@ -89,7 +92,7 @@
} }
elseif(!Validator::validatePeerAddress($contactAddress)) elseif(!Validator::validatePeerAddress($contactAddress))
{ {
throw new InvalidArgumentException('The given contact address is not a valid peer address'); throw new InvalidArgumentException(sprintf('The given contact address %s is not a valid peer address', $contactAddress));
} }
if(!Validator::validateUuid($peerUuid)) if(!Validator::validateUuid($peerUuid))
@ -98,6 +101,7 @@
} }
$uuid = UuidV4::v4()->toRfc4122(); $uuid = UuidV4::v4()->toRfc4122();
Logger::getLogger()->debug(sprintf('Creating new contact (%s) for %s as UUID %s', $contactAddress, $peerUuid, $uuid));
try try
{ {
@ -132,6 +136,8 @@
$peerUuid = $peerUuid->getUuid(); $peerUuid = $peerUuid->getUuid();
} }
Logger::getLogger()->debug(sprintf('Querying contact count for %s', $peerUuid));
try try
{ {
// Get the contact count from the database // Get the contact count from the database
@ -167,7 +173,7 @@
} }
elseif(!Validator::validatePeerAddress($contactAddress)) elseif(!Validator::validatePeerAddress($contactAddress))
{ {
throw new InvalidArgumentException('The given contact address is not a valid peer address'); throw new InvalidArgumentException(sprintf('The given contact address %s is not a valid peer address', $contactAddress));
} }
if(!Validator::validateUuid($peerUuid)) if(!Validator::validateUuid($peerUuid))
@ -175,6 +181,8 @@
throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4'); throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4');
} }
Logger::getLogger()->debug(sprintf('Querying contact %s for %s', $contactAddress, $peerUuid));
try try
{ {
// Get the contact from the database // Get the contact from the database
@ -226,6 +234,8 @@
throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4'); throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4');
} }
Logger::getLogger()->debug(sprintf('Deleting contact %s for %s', $contactAddress, $peerUuid));
try try
{ {
$statement = Database::getConnection()->prepare('DELETE FROM contacts WHERE peer_uuid=:peer AND contact_peer_address=:address'); $statement = Database::getConnection()->prepare('DELETE FROM contacts WHERE peer_uuid=:peer AND contact_peer_address=:address');
@ -261,7 +271,7 @@
} }
elseif(!Validator::validatePeerAddress($contactAddress)) elseif(!Validator::validatePeerAddress($contactAddress))
{ {
throw new InvalidArgumentException('The given contact address is not a valid peer address'); throw new InvalidArgumentException(sprintf('The given contact address %s is not a valid peer address', $contactAddress));
} }
if(!Validator::validateUuid($peerUuid)) if(!Validator::validateUuid($peerUuid))
@ -411,7 +421,7 @@
try try
{ {
$statement = Database::getConnection()->prepare("SELECT uuid FROM contacts WHERE peer_uuid=:peer ORDER BY created DESC LIMIT :limit OFFSET :offset"); $statement = Database::getConnection()->prepare("SELECT contact_peer_address FROM contacts WHERE peer_uuid=:peer ORDER BY created DESC LIMIT :limit OFFSET :offset");
$offset = ($page - 1) * $limit; $offset = ($page - 1) * $limit;
$statement->bindParam(':peer', $peerUuid); $statement->bindParam(':peer', $peerUuid);
$statement->bindParam(':limit', $limit, PDO::PARAM_INT); $statement->bindParam(':limit', $limit, PDO::PARAM_INT);
@ -424,7 +434,7 @@
// Convert results to ContactRecord instances // Convert results to ContactRecord instances
foreach ($results as $result) foreach ($results as $result)
{ {
$contacts[] = self::getStandardContact($peerUuid, $result['uuid']); $contacts[] = self::getStandardContact($peerUuid, $result['contact_peer_address']);
} }
} }
catch (PDOException $e) catch (PDOException $e)
@ -465,11 +475,21 @@
$signatureKey = $signingKey->getPublicKey(); $signatureKey = $signingKey->getPublicKey();
$statement->bindParam(':signature_key', $signatureKey); $statement->bindParam(':signature_key', $signatureKey);
$expires = $signingKey->getExpires(); $expires = $signingKey->getExpires();
if($expires === 0)
{
$expires = null;
}
else
{
$expires = (new DateTime())->setTimestamp($expires)->format('Y-m-d H:i:s');
}
$statement->bindParam(':expires', $expires); $statement->bindParam(':expires', $expires);
$created = $signingKey->getCreated(); $created = (new DateTime())->setTimestamp($signingKey->getCreated())->format('Y-m-d H:i:s');
$statement->bindParam(':created', $created); $statement->bindParam(':created', $created);
$trustedOn = (new DateTime())->format('Y-m-d H:i:s'); $trustedOn = (new DateTime())->format('Y-m-d H:i:s');
$statement->bindParam(':trusted_on', $trustedOn); $statement->bindParam(':trusted_on', $trustedOn);
$statement->execute();
} }
catch(PDOException $e) catch(PDOException $e)
{ {