From 1728d607d7ecb8ae41642bf909c0c7ec39a07aa0 Mon Sep 17 00:00:00 2001 From: Netkas Date: Wed, 21 Jun 2023 02:09:13 -0400 Subject: [PATCH] Cleanup --- src/FederationLib/Classes/Security.php | 19 +++++++++++ src/FederationLib/Classes/Utilities.php | 32 ----------------- src/FederationLib/Classes/Validate.php | 34 +++++++++++++++++++ src/FederationLib/Enums/CacheDriver.php | 9 ----- src/FederationLib/Enums/CommandApplets.php | 22 ------------ .../Enums/SerializationMethod.php | 9 ----- .../Enums/Standard/AttachmentDataType.php | 12 ------- .../Enums/Standard/ContentType.php | 15 -------- .../Enums/Standard/DocumentSubjectType.php | 12 ------- .../Enums/Standard/InternetPeerType.php | 3 -- src/FederationLib/Enums/Standard/PeerType.php | 8 ++--- src/FederationLib/Enums/Standard/ScanMode.php | 12 ------- .../Enums/UserEntityRelationType.php | 8 ----- .../PeerMetadataNotFoundException.php | 2 +- src/FederationLib/Managers/PeerManager.php | 6 ++-- .../PeerMetadata/TelegramUserMetadata.php | 20 +++++------ 16 files changed, 71 insertions(+), 152 deletions(-) delete mode 100644 src/FederationLib/Enums/CacheDriver.php delete mode 100644 src/FederationLib/Enums/CommandApplets.php delete mode 100644 src/FederationLib/Enums/SerializationMethod.php delete mode 100644 src/FederationLib/Enums/Standard/AttachmentDataType.php delete mode 100644 src/FederationLib/Enums/Standard/ContentType.php delete mode 100644 src/FederationLib/Enums/Standard/DocumentSubjectType.php delete mode 100644 src/FederationLib/Enums/Standard/ScanMode.php delete mode 100644 src/FederationLib/Enums/UserEntityRelationType.php diff --git a/src/FederationLib/Classes/Security.php b/src/FederationLib/Classes/Security.php index edc68df..a1cc9d2 100644 --- a/src/FederationLib/Classes/Security.php +++ b/src/FederationLib/Classes/Security.php @@ -99,4 +99,23 @@ } return false; } + + /** + * Same as gettype() but returns only what the user should see. + * + * @param mixed $value + * @return string + */ + public static function gettype(mixed $value): string + { + return match (strtolower(gettype($value))) + { + 'boolean' => 'boolean', + 'integer' => 'integer', + 'double' => 'float', + 'string' => 'string', + 'array' => 'array', + default => 'null', + }; + } } \ No newline at end of file diff --git a/src/FederationLib/Classes/Utilities.php b/src/FederationLib/Classes/Utilities.php index 52b1d20..ac03fe8 100644 --- a/src/FederationLib/Classes/Utilities.php +++ b/src/FederationLib/Classes/Utilities.php @@ -4,12 +4,8 @@ namespace FederationLib\Classes; - use Exception; - use FederationLib\Enums\SerializationMethod; use FederationLib\Interfaces\PeerMetadataInterface; - use FederationLib\Interfaces\SerializableObjectInterface; use InvalidArgumentException; - use LogLib\Log; use Throwable; class Utilities @@ -91,34 +87,6 @@ throw new InvalidArgumentException(sprintf('Invalid address provided: %s', $address)); } - /** - * Serializes an array into a string. - * - * @param array $data - * @param string $method - * @return string - */ - public static function serialize(array|SerializableObjectInterface $data, string $method): string - { - if($data instanceof SerializableObjectInterface) - { - $data = $data->toArray(); - } - - switch(strtolower($method)) - { - case SerializationMethod::JSON: - return json_encode($data); - - case SerializationMethod::MSGPACK: - return msgpack_pack($data); - - default: - Log::warning('net.nosial.federationlib', sprintf('Unknown serialization method: %s, defaulting to msgpack', $method)); - return msgpack_pack($data); - } - } - /** * Recursively converts a Throwable into an array representation. * diff --git a/src/FederationLib/Classes/Validate.php b/src/FederationLib/Classes/Validate.php index 2ed75df..4252669 100644 --- a/src/FederationLib/Classes/Validate.php +++ b/src/FederationLib/Classes/Validate.php @@ -7,6 +7,7 @@ use FederationLib\Enums\Standard\PeerAssociationType; use FederationLib\Enums\Standard\PermissionRole; use FederationLib\Enums\Standard\UserPeerType; + use FederationLib\Exceptions\Standard\InvalidPeerMetadataException; class Validate { @@ -102,4 +103,37 @@ return (int)$role >= 0 && (int)$role <= 5; } + /** + * Validates the given metadata for a peer. + * + * @param array $data + * @param array $required + * @param array $optional + * @return void + * @throws InvalidPeerMetadataException + */ + public static function validateMetadata(array $data, array $required, array $optional): void + { + foreach ($required as $property => $type) + { + if (!isset($data[$property])) + { + throw new InvalidPeerMetadataException(sprintf('The property "%s" is required in the metadata', $property)); + } + + if (gettype($data[$property]) !== $type) + { + throw new InvalidPeerMetadataException(sprintf('The property "%s" must be a %s in metadata, got %s', $property, $type, Security::gettype($data[$property]))); + } + } + + foreach ($optional as $property => $type) + { + if (isset($data[$property]) && gettype($data[$property]) !== $type) + { + throw new InvalidPeerMetadataException(sprintf('The property "%s" must be a %s in metadata, got %s', $property, $type, Security::gettype($data[$property]))); + } + } + } + } \ No newline at end of file diff --git a/src/FederationLib/Enums/CacheDriver.php b/src/FederationLib/Enums/CacheDriver.php deleted file mode 100644 index d9ee640..0000000 --- a/src/FederationLib/Enums/CacheDriver.php +++ /dev/null @@ -1,9 +0,0 @@ -updateLastSeen($client_uuid, $this->getPeer($federated_address)); + $this->updateLastSeen($client_uuid, $federated_address); } catch(PeerNotFoundException $e) { @@ -357,9 +357,9 @@ $client_uuid = $client_uuid->getUuid(); } - if($federated_address instanceof ParsedFederatedAddress) + if(!($federated_address instanceof ParsedFederatedAddress)) { - $federated_address = $federated_address->getAddress(); + $federated_address = new ParsedFederatedAddress($federated_address); } $qb = Database::getConnection()->createQueryBuilder(); diff --git a/src/FederationLib/Objects/Standard/PeerMetadata/TelegramUserMetadata.php b/src/FederationLib/Objects/Standard/PeerMetadata/TelegramUserMetadata.php index c37f446..55dcf89 100644 --- a/src/FederationLib/Objects/Standard/PeerMetadata/TelegramUserMetadata.php +++ b/src/FederationLib/Objects/Standard/PeerMetadata/TelegramUserMetadata.php @@ -140,6 +140,16 @@ return sprintf('%s:%s', UserPeerType::TELEGRAM_USER, $this->id); } + /** + * Returns the Client UUID that last updated the record + * + * @return ?string + */ + public function getUpdatedClient(): ?string + { + return $this->updated_client; + } + /** * Sets the client UUID that last updated the record * @@ -172,16 +182,6 @@ $this->updated_timestamp = $timestamp; } - /** - * Returns the Client UUID that last updated the record - * - * @return ?string - */ - public function getUpdatedClient(): ?string - { - return $this->updated_client; - } - /** * Unique identifier for this user or bot. This number may have more than 32 significant bits and some * programming languages may have difficulty/silent defects in interpreting it. But it has at most 52