Refactored Peer Information to use InformationFields rather than being hard-coded into the peer record

This commit is contained in:
netkas 2025-01-24 15:10:20 -05:00
parent 75de51c910
commit f689e36378
45 changed files with 1422 additions and 1337 deletions

View file

@ -0,0 +1,147 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\PrivacyState;
use Socialbox\Enums\StandardError;
use Socialbox\Enums\Types\InformationFieldName;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\PeerInformationManager;
use Socialbox\Managers\SessionManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsAddInformationField extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
// Field parameter is required
if(!$rpcRequest->containsParameter('field'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The required field parameter is missing');
}
$fieldName = InformationFieldName::tryFrom(strtoupper($rpcRequest->getParameter('field')));
if($fieldName === null)
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided field parameter is invalid');
}
// Value parameter is required
if(!$rpcRequest->containsParameter('value'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The required value parameter is missing');
}
$value = $rpcRequest->getParameter('value');
if(!$fieldName->validate($value))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided value parameter is invalid');
}
// Privacy parameter is optional
$privacy = null;
if($rpcRequest->containsParameter('privacy') && $rpcRequest->getParameter('privacy') !== null)
{
$privacy = PrivacyState::tryFrom(strtoupper($rpcRequest->getParameter('privacy')));
if($privacy === null)
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided privacy parameter is invalid');
}
}
try
{
$peer = $request->getPeer();
}
catch (DatabaseOperationException $e)
{
throw new StandardException('Failed to retrieve current peer information', StandardError::INTERNAL_SERVER_ERROR, $e);
}
try
{
if (PeerInformationManager::fieldExists($peer, $fieldName))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided field parameter is already registered, use settingsUpdateInformationField or settingsUpdateInformationPrivacy instead');
}
PeerInformationManager::addField($peer, $fieldName, $value, $privacy);
}
catch (DatabaseOperationException $e)
{
throw new StandardException('Failed to add the information field', StandardError::INTERNAL_SERVER_ERROR, $e);
}
// Update the session flow if necessary
try
{
switch($fieldName)
{
case InformationFieldName::DISPLAY_NAME:
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_DISPLAY_NAME]);
break;
case InformationFieldName::FIRST_NAME:
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_FIRST_NAME]);
break;
case InformationFieldName::MIDDLE_NAME:
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_MIDDLE_NAME]);
break;
case InformationFieldName::LAST_NAME:
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_LAST_NAME]);
break;
case InformationFieldName::BIRTHDAY:
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_BIRTHDAY]);
break;
case InformationFieldName::PHONE_NUMBER:
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_PHONE]);
break;
case InformationFieldName::EMAIL_ADDRESS:
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_EMAIL]);
break;
case InformationFieldName::URL:
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_URL]);
break;
default:
break;
}
}
catch (Exception $e)
{
try
{
// Rollback the information field otherwise the peer will be stuck with an incomplete session flow
PeerInformationManager::deleteField($peer, $fieldName);
}
catch (DatabaseOperationException $e)
{
// Something is seriously wrong if we can't roll back the information field
throw new StandardException('Failed to rollback the information field', StandardError::INTERNAL_SERVER_ERROR, $e);
}
if($e instanceof StandardException)
{
throw $e;
}
throw new StandardException('Failed to update the session flow', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -1,38 +0,0 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\RegisteredPeerManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsDeleteBirthday extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(Configuration::getRegistrationConfiguration()->isBirthdayRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'A birthday is required for this server');
}
try
{
RegisteredPeerManager::deleteBirthday($request->getPeer());
}
catch(Exception $e)
{
throw new StandardException('Failed to delete birthday ' . $e->getMessage(), StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -1,38 +0,0 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\RegisteredPeerManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsDeleteDisplayName extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(Configuration::getRegistrationConfiguration()->isDisplayNameRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'A display name is required for this server');
}
try
{
RegisteredPeerManager::deleteDisplayName($request->getPeer());
}
catch(Exception $e)
{
throw new StandardException('Failed to delete display name due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -1,38 +0,0 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\RegisteredPeerManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsDeleteDisplayPicture extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(Configuration::getRegistrationConfiguration()->isDisplayPictureRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'A display picture is required for this server');
}
try
{
RegisteredPeerManager::deleteDisplayPicture($request->getPeer());
}
catch(Exception $e)
{
throw new StandardException('Failed to delete display picture: ' . $e->getMessage(), StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -1,38 +0,0 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\RegisteredPeerManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsDeleteEmailAddress extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(Configuration::getRegistrationConfiguration()->isEmailAddressRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'A email address is required for this server');
}
try
{
RegisteredPeerManager::deleteEmailAddress($request->getPeer());
}
catch(Exception $e)
{
throw new StandardException('Failed to delete email address: ' . $e->getMessage(), StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -0,0 +1,119 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
use Socialbox\Enums\StandardError;
use Socialbox\Enums\Types\InformationFieldName;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\PeerInformationManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsDeleteInformationField extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
// Field parameter is required
if(!$rpcRequest->containsParameter('field'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The required field parameter is missing');
}
$fieldName = InformationFieldName::tryFrom(strtoupper($rpcRequest->getParameter('field')));
if($fieldName === null)
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided field parameter is invalid');
}
try
{
if(!PeerInformationManager::fieldExists($request->getPeer(), $fieldName))
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The information field does not exist');
}
}
catch(DatabaseOperationException $e)
{
throw new StandardException('Failed to check if the information field exists', StandardError::INTERNAL_SERVER_ERROR, $e);
}
switch($fieldName)
{
case InformationFieldName::DISPLAY_NAME:
if(Configuration::getRegistrationConfiguration()->isDisplayNameRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'A display name is required for this server');
}
break;
case InformationFieldName::FIRST_NAME:
if(Configuration::getRegistrationConfiguration()->isFirstNameRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'A first name is required for this server');
}
break;
case InformationFieldName::MIDDLE_NAME:
if(Configuration::getRegistrationConfiguration()->isMiddleNameRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'A middle name is required for this server');
}
break;
case InformationFieldName::LAST_NAME:
if(Configuration::getRegistrationConfiguration()->isLastNameRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'A last name is required for this server');
}
break;
case InformationFieldName::BIRTHDAY:
if(Configuration::getRegistrationConfiguration()->isBirthdayRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'A birthday is required for this server');
}
break;
case InformationFieldName::PHONE_NUMBER:
if(Configuration::getRegistrationConfiguration()->isPhoneNumberRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'A phone number is required for this server');
}
break;
case InformationFieldName::EMAIL_ADDRESS:
if(Configuration::getRegistrationConfiguration()->isEmailAddressRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'An email address is required for this server');
}
break;
case InformationFieldName::URL:
if(Configuration::getRegistrationConfiguration()->isUrlRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'A URL is required for this server');
}
break;
default:
break;
}
try
{
PeerInformationManager::deleteField($request->getPeer(), $fieldName);
}
catch(DatabaseOperationException $e)
{
throw new StandardException('Failed to delete the information field', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -1,38 +0,0 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\RegisteredPeerManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsDeletePhoneNumber extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(Configuration::getRegistrationConfiguration()->isPhoneNumberRequired())
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'A phone number is required for this server');
}
try
{
RegisteredPeerManager::deletePhoneNumber($request->getPeer());
}
catch(Exception $e)
{
throw new StandardException('Failed to delete phone number: ' . $e->getMessage(), StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\PeerInformationManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsGetInformationFields extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
try
{
$fieldRecords = PeerInformationManager::getFields($request->getPeer());
}
catch(DatabaseOperationException $e)
{
throw new StandardException('Failed to retrieve existing information fields', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(array_map(fn($result) => $result->toInformationFieldState(), $fieldRecords));
}
}

View file

@ -1,63 +0,0 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Validator;
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\RegisteredPeerManager;
use Socialbox\Managers\SessionManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsSetBirthday extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('month'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'month' parameter");
}
if(!$rpcRequest->containsParameter('day'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'day' parameter");
}
if(!$rpcRequest->containsParameter('year'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'year' parameter");
}
$month = $rpcRequest->getParameter('month');
$day = $rpcRequest->getParameter('day');
$year = $rpcRequest->getParameter('year');
if(!Validator::validateDate($month, $day, $year))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Invalid date provided, must be a valid gregorian calender date.");
}
try
{
// Set the password
RegisteredPeerManager::updateBirthday($request->getPeer(), $month, $day, $year);
// Check & update the session flow
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_BIRTHDAY]);
}
catch(Exception $e)
{
throw new StandardException('Failed to set birthday due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -1,48 +0,0 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Exception;
use ncc\ThirdParty\Symfony\Process\Exception\InvalidArgumentException;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\RegisteredPeerManager;
use Socialbox\Managers\SessionManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsSetDisplayName extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('name'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'name' parameter");
}
try
{
// Update the display name
RegisteredPeerManager::updateDisplayName($request->getPeer(), $rpcRequest->getParameter('name'));
// Check & update the session flow
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_DISPLAY_NAME]);
}
catch(InvalidArgumentException)
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Invalid display name');
}
catch(Exception $e)
{
throw new StandardException('Failed to set password due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -1,65 +0,0 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\Utilities;
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\RegisteredPeerManager;
use Socialbox\Managers\SessionManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsSetDisplayPicture extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('image'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'image' parameter");
}
if(strlen($rpcRequest->getParameter('image')) > Configuration::getStorageConfiguration()->getUserDisplayImagesMaxSize())
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Image size exceeds the maximum allowed size of " . Configuration::getStorageConfiguration()->getUserDisplayImagesMaxSize() . " bytes");
}
try
{
$decodedImage = @base64_decode($rpcRequest->getParameter('image'));
if($decodedImage === false)
{
return $rpcRequest->produceError(StandardError::RPC_BAD_REQUEST, "Failed to decode JPEG image base64 data");
}
$sanitizedImage = Utilities::resizeImage(Utilities::sanitizeJpeg($decodedImage), 256, 256);
}
catch(Exception $e)
{
throw new StandardException('Failed to process JPEG image: ' . $e->getMessage(), StandardError::RPC_BAD_REQUEST, $e);
}
try
{
// Set the password
RegisteredPeerManager::updateDisplayPicture($request->getPeer(), $sanitizedImage);
// Check & update the session flow
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_DISPLAY_PICTURE]);
}
catch(Exception $e)
{
throw new StandardException('Failed to update display picture: ' . $e->getMessage(), StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -1,50 +0,0 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Validator;
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\RegisteredPeerManager;
use Socialbox\Managers\SessionManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class
SettingsSetEmailAddress extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('email_address'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'email_address' parameter");
}
if(!Validator::validateEmailAddress($rpcRequest->getParameter('email_address')))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Invalid 'email_address' parameter, must be a valid email address");
}
try
{
// Set the password
RegisteredPeerManager::updateEmailAddress($request->getPeer(), $rpcRequest->getParameter('email_address'));
// Check & update the session flow
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_EMAIL]);
}
catch(Exception $e)
{
throw new StandardException('Failed to set email address due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -1,49 +0,0 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Exception;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Validator;
use Socialbox\Enums\Flags\SessionFlags;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\RegisteredPeerManager;
use Socialbox\Managers\SessionManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsSetPhoneNumber extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
if(!$rpcRequest->containsParameter('phone_number'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Missing 'phone_number' parameter");
}
if(!Validator::validatePhoneNumber($rpcRequest->getParameter('phone_number')))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, "Invalid 'phone_number' parameter, must be a valid phone number");
}
try
{
// Set the phone number
RegisteredPeerManager::updatePhoneNumber($request->getPeer(), $rpcRequest->getParameter('phone_number'));
// Check & update the session flow
SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_PHONE]);
}
catch(Exception $e)
{
throw new StandardException('Failed to set phone number due to an internal exception', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -0,0 +1,62 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\StandardError;
use Socialbox\Enums\Types\InformationFieldName;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\PeerInformationManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsUpdateInformationField extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
// Field parameter is required
if(!$rpcRequest->containsParameter('field'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The required field parameter is missing');
}
$fieldName = InformationFieldName::tryFrom(strtoupper($rpcRequest->getParameter('field')));
if($fieldName === null)
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided field parameter is invalid');
}
// Value parameter is required
if(!$rpcRequest->containsParameter('value'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The required value parameter is missing');
}
$value = $rpcRequest->getParameter('value');
if(!$fieldName->validate($value))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided value parameter is invalid');
}
try
{
$peer = $request->getPeer();
if(!PeerInformationManager::fieldExists($peer, $fieldName))
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The information field does not exist');
}
PeerInformationManager::updateField($peer, $fieldName, $value);
}
catch(DatabaseOperationException $e)
{
throw new StandardException('Failed to update the information field', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}

View file

@ -0,0 +1,64 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Socialbox\Abstracts\Method;
use Socialbox\Enums\PrivacyState;
use Socialbox\Enums\StandardError;
use Socialbox\Enums\Types\InformationFieldName;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\PeerInformationManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class SettingsUpdateInformationPrivacy extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
// Field parameter is required
if(!$rpcRequest->containsParameter('field'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The required field parameter is missing');
}
$fieldName = InformationFieldName::tryFrom(strtoupper($rpcRequest->getParameter('field')));
if($fieldName === null)
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided field parameter is invalid');
}
// Privacy parameter is required
$privacy = null;
if(!$rpcRequest->containsParameter('privacy'))
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The required privacy parameter is missing');
}
$privacy = PrivacyState::tryFrom(strtoupper($rpcRequest->getParameter('privacy')));
if($privacy === null)
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'The provided privacy parameter is invalid');
}
try
{
$peer = $request->getPeer();
if(!PeerInformationManager::fieldExists($peer, $fieldName))
{
return $rpcRequest->produceError(StandardError::FORBIDDEN, 'The information field does not exist');
}
PeerInformationManager::updatePrivacyState($peer, $fieldName, $privacy);
}
catch(DatabaseOperationException $e)
{
throw new StandardException('Failed to update the information field', StandardError::INTERNAL_SERVER_ERROR, $e);
}
return $rpcRequest->produceResponse(true);
}
}