Refactor session handling and improve data reliability.

This commit is contained in:
netkas 2024-12-19 21:10:45 -05:00
parent 014b63705b
commit ea3de13cf8
5 changed files with 84 additions and 20 deletions

View file

@ -83,7 +83,6 @@ class Utilities
if ($decoded === false) if ($decoded === false)
{ {
var_dump($data);
throw new InvalidArgumentException('Failed to decode data from Base64'); throw new InvalidArgumentException('Failed to decode data from Base64');
} }

View file

@ -81,10 +81,11 @@ class ResolvedServersManager
* Retrieves the resolved server record from the database for a given domain. * Retrieves the resolved server record from the database for a given domain.
* *
* @param string $domain The domain name for which to retrieve the resolved server record. * @param string $domain The domain name for which to retrieve the resolved server record.
* @return ResolvedServerRecord The resolved server record associated with the given domain. * @return ResolvedServerRecord|null The resolved server record associated with the given domain.
* @throws DatabaseOperationException If there is an error retrieving the resolved server record from the database. * @throws DatabaseOperationException If there is an error retrieving the resolved server record from the database.
* @throws \DateMalformedStringException If the date string is malformed.
*/ */
public static function getResolvedServer(string $domain): ResolvedServerRecord public static function getResolvedServer(string $domain): ?ResolvedServerRecord
{ {
try try
{ {
@ -92,7 +93,13 @@ class ResolvedServersManager
$statement->bindParam(1, $domain); $statement->bindParam(1, $domain);
$statement->execute(); $statement->execute();
$result = $statement->fetch(); $result = $statement->fetch();
return new ResolvedServerRecord($result);
if($result === false)
{
return null;
}
return ResolvedServerRecord::fromArray($result);
} }
catch(PDOException $e) catch(PDOException $e)
{ {

View file

@ -15,7 +15,7 @@
* @var SessionFlags[]|null * @var SessionFlags[]|null
*/ */
private ?array $flags; private ?array $flags;
private DateTime $created; private int $created;
/** /**
* Constructor for initializing the object with the provided data. * Constructor for initializing the object with the provided data.
@ -25,7 +25,6 @@
* - 'identified_as': mixed, The identity information. * - 'identified_as': mixed, The identity information.
* - 'authenticated': bool, Whether the object is authenticated. * - 'authenticated': bool, Whether the object is authenticated.
* - 'flags': string|null, Optional flags in * - 'flags': string|null, Optional flags in
* @throws \DateMalformedStringException
*/ */
public function __construct(array $data) public function __construct(array $data)
{ {
@ -48,49 +47,101 @@
if(is_int($data['created'])) if(is_int($data['created']))
{ {
$this->created = new DateTime(); $this->created = $data['created'];
$this->created->setTimestamp($data['created']);
} }
elseif($data['created'] instanceof DateTime) elseif($data['created'] instanceof DateTime)
{ {
$this->created = $data['created']; $this->created = $data['created']->getTimestamp();
} }
else else
{ {
$this->created = new DateTime($data['created']); $this->created = time();
} }
} }
/**
* Retrieves the UUID of the current instance.
*
* @return string The UUID as a string.
*/
public function getUuid(): string public function getUuid(): string
{ {
return $this->uuid; return $this->uuid;
} }
/**
*
* @return string The identifier associated with the entity.
*/
public function getIdentifiedAs(): string public function getIdentifiedAs(): string
{ {
return $this->identifiedAs; return $this->identifiedAs;
} }
/**
* Checks if the user is authenticated.
*
* @return bool Returns true if the user is authenticated, otherwise false.
*/
public function isAuthenticated(): bool public function isAuthenticated(): bool
{ {
return $this->authenticated; return $this->authenticated;
} }
/**
*
* @return array|null
*/
public function getFlags(): ?array public function getFlags(): ?array
{ {
return $this->flags; return $this->flags;
} }
public function getCreated(): DateTime /**
* Checks if the provided flag exists within the current flags.
*
* @param string|SessionFlags $flag The flag to check, either as a string or an instance of SessionFlags.
* @return bool Returns true if the flag is found in the current flags, otherwise false.
*/
public function containsFlag(string|SessionFlags $flag): bool
{
if($this->flags === null || count($this->flags) === 0)
{
return false;
}
if($flag instanceof SessionFlags)
{
$flag = $flag->value;
}
return in_array($flag, $this->flags);
}
/**
*
* @return int Returns the created timestamp as an integer.
*/
public function getCreated(): int
{ {
return $this->created; return $this->created;
} }
/**
* Creates a new instance of SessionState from the provided array.
*
* @param array $data The input array containing data to initialize the SessionState instance.
* @return SessionState A new instance of the SessionState class.
*/
public static function fromArray(array $data): SessionState public static function fromArray(array $data): SessionState
{ {
return new self($data); return new self($data);
} }
/**
*
* @return array An associative array representation of the object's properties, including 'uuid', 'identified_as', 'authenticated', 'flags', and 'created'.
*/
public function toArray(): array public function toArray(): array
{ {
return [ return [
@ -98,7 +149,7 @@
'identified_as' => $this->identifiedAs, 'identified_as' => $this->identifiedAs,
'authenticated' => $this->authenticated, 'authenticated' => $this->authenticated,
'flags' => $this->flags, 'flags' => $this->flags,
'created' => $this->created->getTimestamp() 'created' => $this->created,
]; ];
} }
} }

View file

@ -5,14 +5,12 @@
use Socialbox\Classes\RpcClient; use Socialbox\Classes\RpcClient;
use Socialbox\Classes\Utilities; use Socialbox\Classes\Utilities;
use Socialbox\Exceptions\CryptographyException; use Socialbox\Exceptions\CryptographyException;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\ResolutionException; use Socialbox\Exceptions\ResolutionException;
use Socialbox\Exceptions\RpcException; use Socialbox\Exceptions\RpcException;
use Socialbox\Objects\ExportedSession; use Socialbox\Objects\ExportedSession;
use Socialbox\Objects\KeyPair;
use Socialbox\Objects\PeerAddress; use Socialbox\Objects\PeerAddress;
use Socialbox\Objects\RpcError;
use Socialbox\Objects\RpcRequest; use Socialbox\Objects\RpcRequest;
use Socialbox\Objects\Standard\SessionState;
class SocialClient extends RpcClient class SocialClient extends RpcClient
{ {
@ -43,6 +41,19 @@
)->getResponse()->getResult(); )->getResponse()->getResult();
} }
/**
* Retrieves the current state of the session from the server.
*
* @return SessionState Returns an instance of SessionState representing the session's current state.
* @throws RpcException Thrown if the RPC request fails.
*/
public function getSessionState(): SessionState
{
return SessionState::fromArray($this->sendRequest(
new RpcRequest('getSessionState', Utilities::randomCrc32())
)->getResponse()->getResult());
}
/** /**
* Retrieves the privacy policy from the server. * Retrieves the privacy policy from the server.
* *

View file

@ -4,11 +4,7 @@
import('net.nosial.socialbox'); import('net.nosial.socialbox');
$client = new \Socialbox\SocialClient(generateRandomPeer()); $client = new \Socialbox\SocialClient(generateRandomPeer());
var_dump($client->ping()); var_dump($client->getSessionState());
var_dump($client->getPrivacyPolicy());
var_dump($client->acceptPrivacyPolicy());
var_dump($client->getTermsOfService());
var_dump($client->acceptTermsOfService());
function generateRandomPeer() function generateRandomPeer()
{ {