Compare commits

..

No commits in common. "631689b489209f7bc592f613945473a82f3d9386" and "f080775701cc2be1dfcc567527b5e9055644ca69" have entirely different histories.

2 changed files with 44 additions and 60 deletions

View file

@ -30,7 +30,7 @@
*/ */
public static function usernameExists(string $username): bool public static function usernameExists(string $username): bool
{ {
Logger::getLogger()->debug(sprintf("Checking if username %s already exists", $username)); Logger::getLogger()->verbose(sprintf("Checking if username %s already exists", $username));
try try
{ {
@ -57,7 +57,7 @@
*/ */
public static function createPeer(PeerAddress $peerAddress, bool $enabled=false): string public static function createPeer(PeerAddress $peerAddress, bool $enabled=false): string
{ {
Logger::getLogger()->debug(sprintf("Registering peer %s", $peerAddress->getAddress())); Logger::getLogger()->verbose(sprintf("Registering peer %s", $peerAddress->getAddress()));
$uuid = Uuid::v4()->toRfc4122(); $uuid = Uuid::v4()->toRfc4122();
$server = $peerAddress->getDomain(); $server = $peerAddress->getDomain();
@ -88,23 +88,23 @@
* Deletes a peer from the database based on the given UUID or RegisteredPeerRecord. * Deletes a peer from the database based on the given UUID or RegisteredPeerRecord.
* WARNING: This operation is cascading and will delete all associated data. * WARNING: This operation is cascading and will delete all associated data.
* *
* @param string|PeerDatabaseRecord $ppeerUuid The UUID or RegisteredPeerRecord instance representing the peer to be deleted. * @param string|PeerDatabaseRecord $uuid The UUID or RegisteredPeerRecord instance representing the peer to be deleted.
* @return void * @return void
* @throws DatabaseOperationException If the operation fails. * @throws DatabaseOperationException If the operation fails.
*/ */
public static function deletePeer(string|PeerDatabaseRecord $ppeerUuid): void public static function deletePeer(string|PeerDatabaseRecord $uuid): void
{ {
if($ppeerUuid instanceof PeerDatabaseRecord) if($uuid instanceof PeerDatabaseRecord)
{ {
$ppeerUuid = $ppeerUuid->getUuid(); $uuid = $uuid->getUuid();
} }
Logger::getLogger()->debug(sprintf("Deleting peer %s", $ppeerUuid)); Logger::getLogger()->verbose(sprintf("Deleting peer %s", $uuid));
try try
{ {
$statement = Database::getConnection()->prepare('DELETE FROM peers WHERE uuid=?'); $statement = Database::getConnection()->prepare('DELETE FROM peers WHERE uuid=?');
$statement->bindParam(1, $ppeerUuid); $statement->bindParam(1, $uuid);
$statement->execute(); $statement->execute();
} }
catch(PDOException $e) catch(PDOException $e)
@ -116,30 +116,30 @@
/** /**
* Retrieves a registered peer record based on the given unique identifier or RegisteredPeerRecord object. * Retrieves a registered peer record based on the given unique identifier or RegisteredPeerRecord object.
* *
* @param string|PeerDatabaseRecord $peerUuid The unique identifier of the registered peer, or an instance of RegisteredPeerRecord. * @param string|PeerDatabaseRecord $uuid The unique identifier of the registered peer, or an instance of RegisteredPeerRecord.
* @return PeerDatabaseRecord|null Returns a RegisteredPeerRecord object containing the peer's information. null if the peer does not exist. * @return PeerDatabaseRecord Returns a RegisteredPeerRecord object containing the peer's information.
* @throws DatabaseOperationException If there is an error during the database operation. * @throws DatabaseOperationException If there is an error during the database operation.
*/ */
public static function getPeer(string|PeerDatabaseRecord $peerUuid): ?PeerDatabaseRecord public static function getPeer(string|PeerDatabaseRecord $uuid): PeerDatabaseRecord
{ {
if($peerUuid instanceof PeerDatabaseRecord) if($uuid instanceof PeerDatabaseRecord)
{ {
$peerUuid = $peerUuid->getUuid(); $uuid = $uuid->getUuid();
} }
Logger::getLogger()->debug(sprintf("Retrieving peer %s from the database", $peerUuid)); Logger::getLogger()->verbose(sprintf("Retrieving peer %s from the database", $uuid));
try try
{ {
$statement = Database::getConnection()->prepare('SELECT * FROM peers WHERE uuid=?'); $statement = Database::getConnection()->prepare('SELECT * FROM peers WHERE uuid=?');
$statement->bindParam(1, $peerUuid); $statement->bindParam(1, $uuid);
$statement->execute(); $statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC); $result = $statement->fetch(PDO::FETCH_ASSOC);
if($result === false) if($result === false)
{ {
return null; throw new DatabaseOperationException(sprintf("The requested peer '%s' does not exist", $uuid));
} }
return new PeerDatabaseRecord($result); return new PeerDatabaseRecord($result);
@ -153,20 +153,20 @@
/** /**
* Retrieves a peer record by the given username. * Retrieves a peer record by the given username.
* *
* @param PeerAddress $oeerAddress The address of the peer to be retrieved. * @param PeerAddress $address The address of the peer to be retrieved.
* @return PeerDatabaseRecord|null The record of the peer associated with the given username. * @return PeerDatabaseRecord|null The record of the peer associated with the given username.
* @throws DatabaseOperationException If there is an error while querying the database. * @throws DatabaseOperationException If there is an error while querying the database.
*/ */
public static function getPeerByAddress(PeerAddress $oeerAddress): ?PeerDatabaseRecord public static function getPeerByAddress(PeerAddress $address): ?PeerDatabaseRecord
{ {
Logger::getLogger()->debug(sprintf("Retrieving peer %s from the database", $oeerAddress->getAddress())); Logger::getLogger()->verbose(sprintf("Retrieving peer %s from the database", $address->getAddress()));
try try
{ {
$statement = Database::getConnection()->prepare('SELECT * FROM peers WHERE username=:username AND server=:server'); $statement = Database::getConnection()->prepare('SELECT * FROM peers WHERE username=:username AND server=:server');
$username = $oeerAddress->getUsername(); $username = $address->getUsername();
$statement->bindParam(':username', $username); $statement->bindParam(':username', $username);
$server = $oeerAddress->getDomain(); $server = $address->getDomain();
// Convert to 'host' if the domain is the same as the server's host // Convert to 'host' if the domain is the same as the server's host
if($server === Configuration::getInstanceConfiguration()->getDomain()) if($server === Configuration::getInstanceConfiguration()->getDomain())
@ -181,7 +181,6 @@
if($result === false) if($result === false)
{ {
Logger::getLogger()->debug(sprintf("Peer %s not found in the database", $oeerAddress->getAddress()));
return null; return null;
} }
@ -292,23 +291,23 @@
/** /**
* Enables a peer identified by the given UUID or RegisteredPeerRecord. * Enables a peer identified by the given UUID or RegisteredPeerRecord.
* *
* @param string|PeerDatabaseRecord $peerUuid The UUID or RegisteredPeerRecord instance representing the peer to be enabled. * @param string|PeerDatabaseRecord $uuid The UUID or RegisteredPeerRecord instance representing the peer to be enabled.
* @return void * @return void
* @throws DatabaseOperationException If there is an error while updating the database. * @throws DatabaseOperationException If there is an error while updating the database.
*/ */
public static function enablePeer(string|PeerDatabaseRecord $peerUuid): void public static function enablePeer(string|PeerDatabaseRecord $uuid): void
{ {
if($peerUuid instanceof PeerDatabaseRecord) if($uuid instanceof PeerDatabaseRecord)
{ {
$peerUuid = $peerUuid->getUuid(); $uuid = $uuid->getUuid();
} }
Logger::getLogger()->verbose(sprintf("Enabling peer %s", $peerUuid)); Logger::getLogger()->verbose(sprintf("Enabling peer %s", $uuid));
try try
{ {
$statement = Database::getConnection()->prepare('UPDATE peers SET enabled=1 WHERE uuid=?'); $statement = Database::getConnection()->prepare('UPDATE peers SET enabled=1 WHERE uuid=?');
$statement->bindParam(1, $peerUuid); $statement->bindParam(1, $uuid);
$statement->execute(); $statement->execute();
} }
catch(PDOException $e) catch(PDOException $e)
@ -320,23 +319,23 @@
/** /**
* Disables the peer identified by the given UUID or RegisteredPeerRecord. * Disables the peer identified by the given UUID or RegisteredPeerRecord.
* *
* @param string|PeerDatabaseRecord $peerUuid The UUID or RegisteredPeerRecord instance representing the peer. * @param string|PeerDatabaseRecord $uuid The UUID or RegisteredPeerRecord instance representing the peer.
* @return void * @return void
* @throws DatabaseOperationException If there is an error while updating the peer's status in the database. * @throws DatabaseOperationException If there is an error while updating the peer's status in the database.
*/ */
public static function disablePeer(string|PeerDatabaseRecord $peerUuid): void public static function disablePeer(string|PeerDatabaseRecord $uuid): void
{ {
if($peerUuid instanceof PeerDatabaseRecord) if($uuid instanceof PeerDatabaseRecord)
{ {
$peerUuid = $peerUuid->getUuid(); $uuid = $uuid->getUuid();
} }
Logger::getLogger()->verbose(sprintf("Disabling peer %s", $peerUuid)); Logger::getLogger()->verbose(sprintf("Disabling peer %s", $uuid));
try try
{ {
$statement = Database::getConnection()->prepare('UPDATE peers SET enabled=0 WHERE uuid=?'); $statement = Database::getConnection()->prepare('UPDATE peers SET enabled=0 WHERE uuid=?');
$statement->bindParam(1, $peerUuid); $statement->bindParam(1, $uuid);
$statement->execute(); $statement->execute();
} }
catch(PDOException $e) catch(PDOException $e)
@ -348,30 +347,25 @@
/** /**
* Adds a specific flag to the peer identified by the given UUID or RegisteredPeerRecord. * Adds a specific flag to the peer identified by the given UUID or RegisteredPeerRecord.
* *
* @param string|PeerDatabaseRecord $peerUuid The UUID or RegisteredPeerRecord instance representing the peer. * @param string|PeerDatabaseRecord $uuid The UUID or RegisteredPeerRecord instance representing the peer.
* @param PeerFlags|array $peerFlags The flag or array of flags to be added to the peer. * @param PeerFlags|array $flags The flag or array of flags to be added to the peer.
* @return void * @return void
* @throws DatabaseOperationException If there is an error while updating the database. * @throws DatabaseOperationException If there is an error while updating the database.
*/ */
public static function addFlag(string|PeerDatabaseRecord $peerUuid, PeerFlags|array $peerFlags): void public static function addFlag(string|PeerDatabaseRecord $uuid, PeerFlags|array $flags): void
{ {
if($peerUuid instanceof PeerDatabaseRecord) if($uuid instanceof PeerDatabaseRecord)
{ {
$peerUuid = $peerUuid->getUuid(); $uuid = $uuid->getUuid();
} }
Logger::getLogger()->verbose(sprintf("Adding flag(s) %s to peer %s", implode(',', $peerFlags), $peerUuid)); Logger::getLogger()->verbose(sprintf("Adding flag(s) %s to peer %s", implode(',', $flags), $uuid));
$peer = self::getPeer($peerUuid);
if($peer === null)
{
throw new DatabaseOperationException('Peer does not exist');
}
$peer = self::getPeer($uuid);
$existingFlags = $peer->getFlags(); $existingFlags = $peer->getFlags();
$peerFlags = is_array($peerFlags) ? $peerFlags : [$peerFlags]; $flags = is_array($flags) ? $flags : [$flags];
foreach($peerFlags as $flag) foreach($flags as $flag)
{ {
if(!in_array($flag, $existingFlags)) if(!in_array($flag, $existingFlags))
{ {
@ -384,7 +378,7 @@
$implodedFlags = implode(',', array_map(fn($flag) => $flag->name, $existingFlags)); $implodedFlags = implode(',', array_map(fn($flag) => $flag->name, $existingFlags));
$statement = Database::getConnection()->prepare('UPDATE peers SET flags=? WHERE uuid=?'); $statement = Database::getConnection()->prepare('UPDATE peers SET flags=? WHERE uuid=?');
$statement->bindParam(1, $implodedFlags); $statement->bindParam(1, $implodedFlags);
$statement->bindParam(2, $peerUuid); $statement->bindParam(2, $uuid);
$statement->execute(); $statement->execute();
} }
catch(PDOException $e) catch(PDOException $e)
@ -408,11 +402,6 @@
$peer = self::getPeer($peer); $peer = self::getPeer($peer);
} }
if($peer === null)
{
throw new DatabaseOperationException('Peer does not exist');
}
Logger::getLogger()->verbose(sprintf("Removing flag %s from peer %s", $flag->value, $peer->getUuid())); Logger::getLogger()->verbose(sprintf("Removing flag %s from peer %s", $flag->value, $peer->getUuid()));
if(!$peer->flagExists($flag)) if(!$peer->flagExists($flag))

View file

@ -281,12 +281,7 @@
*/ */
public function isExternal(): bool public function isExternal(): bool
{ {
$peer = RegisteredPeerManager::getPeer($this->peerUuid); return RegisteredPeerManager::getPeer($this->peerUuid)->isExternal();
if($peer === null)
{
return false;
}
return $peer->isExternal();
} }
/** /**