From bb3e0a5ffaca1837204cff00f3cbf3800d9e074b Mon Sep 17 00:00:00 2001 From: netkas Date: Wed, 22 Jan 2025 15:35:35 -0500 Subject: [PATCH] Updated PhpDoc, added method ADDRESS_BOOK_ADD_CONTACT, minor correction in getRegistrationMethods where $methods was not initialized --- .../StandardMethods/AddressBookAddContact.php | 75 +++++++++++++++++++ src/Socialbox/Enums/StandardMethods.php | 14 +++- 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/Socialbox/Classes/StandardMethods/AddressBookAddContact.php diff --git a/src/Socialbox/Classes/StandardMethods/AddressBookAddContact.php b/src/Socialbox/Classes/StandardMethods/AddressBookAddContact.php new file mode 100644 index 0000000..39b70d6 --- /dev/null +++ b/src/Socialbox/Classes/StandardMethods/AddressBookAddContact.php @@ -0,0 +1,75 @@ +containsParameter('peer')) + { + return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Missing required \'peer\' parameter'); + } + + try + { + $address = PeerAddress::fromAddress($rpcRequest->getParameter('peer')); + } + catch(InvalidArgumentException $e) + { + throw new StandardException('Invalid peer address', StandardError::RPC_INVALID_ARGUMENTS, $e); + } + + if($rpcRequest->containsParameter('relationship')) + { + $relationship = ContactRelationshipType::tryFrom(strtoupper($rpcRequest->getParameter('relationship'))); + if($relationship === null) + { + throw new StandardException('Invalid relationship type', StandardError::RPC_INVALID_ARGUMENTS); + } + } + else + { + $relationship = ContactRelationshipType::MUTUAL; + } + + try + { + // Resolve the peer, this would throw a StandardException if something goes wrong + Socialbox::resolvePeer($address); + + // Check if the contact already exists + $peer = $request->getPeer(); + if(ContactManager::isContact($peer, $address)) + { + return $rpcRequest->produceError(StandardError::FORBIDDEN, 'Contact already exists'); + } + + // Create the contact + ContactManager::createContact($peer, $address, $relationship); + } + catch (DatabaseOperationException $e) + { + throw new StandardException('Failed to add contact', StandardError::INTERNAL_SERVER_ERROR, $e); + } + + // Return success + return $rpcRequest->produceResponse(true); + } + } \ No newline at end of file diff --git a/src/Socialbox/Enums/StandardMethods.php b/src/Socialbox/Enums/StandardMethods.php index c4e8cf9..403fd03 100644 --- a/src/Socialbox/Enums/StandardMethods.php +++ b/src/Socialbox/Enums/StandardMethods.php @@ -6,6 +6,7 @@ use Socialbox\Classes\StandardMethods\AcceptCommunityGuidelines; use Socialbox\Classes\StandardMethods\AcceptPrivacyPolicy; use Socialbox\Classes\StandardMethods\AcceptTermsOfService; + use Socialbox\Classes\StandardMethods\AddressBookAddContact; use Socialbox\Classes\StandardMethods\Authenticate; use Socialbox\Classes\StandardMethods\GetAllowedMethods; use Socialbox\Classes\StandardMethods\GetCommunityGuidelines; @@ -96,6 +97,8 @@ case SETTINGS_ADD_SIGNING_KEY = 'settingsAddSigningKey'; case SETTINGS_GET_SIGNING_KEYS = 'settingsGetSigningKeys'; + case ADDRESS_BOOK_ADD_CONTACT = 'addressBookAddContact'; + case AUTHENTICATE = 'authenticate'; case RESOLVE_PEER = 'resolvePeer'; @@ -145,6 +148,8 @@ self::SETTINGS_ADD_SIGNING_KEY => SettingsAddSigningKey::execute($request, $rpcRequest), self::SETTINGS_GET_SIGNING_KEYS => SettingsGetSigningKeys::execute($request, $rpcRequest), + self::ADDRESS_BOOK_ADD_CONTACT => AddressBookAddContact::execute($request, $rpcRequest), + self::AUTHENTICATE => Authenticate::execute($request, $rpcRequest), self::RESOLVE_PEER => ResolvePeer::execute($request, $rpcRequest), @@ -234,7 +239,8 @@ /** * Retrieves a list of external methods based on the client's session state. * - * @param ClientRequest + * @param ClientRequest $clientRequest The client request object containing all the request parameters + * @return array Returns an array methods that are available for external sessions */ private static function getExternalMethods(ClientRequest $clientRequest): array { @@ -273,7 +279,9 @@ self::SETTINGS_SET_EMAIL, self::SETTINGS_SET_PHONE, self::SETTINGS_SET_BIRTHDAY, - self::RESOLVE_PEER + self::RESOLVE_PEER, + + self::ADDRESS_BOOK_ADD_CONTACT ]; // Prevent the user from deleting their display name if it is required @@ -335,6 +343,8 @@ return []; } + $methods = []; + // If the flag `VER_PRIVACY_POLICY` is set, then the user can accept the privacy policy if($session->flagExists(SessionFlags::VER_PRIVACY_POLICY)) {