Added method ADDRESS_BOOK_GET_CONTACTS

This commit is contained in:
netkas 2025-01-22 22:01:07 -05:00
parent d56483119c
commit 75de51c910
7 changed files with 185 additions and 11 deletions

View file

@ -157,6 +157,10 @@
// When a peer's external address is resolved, it is cached for this amount of time before resolving again.
// This reduces the amount of times a resolution request is made to the external server.
$config->setDefault('policies.peer_sync_interval', 3600);
// The maximum number of contacts a peer can retrieve from the server at once, if the client puts a
// value that exceeds this limit, the server will use this limit instead.
// recommendation: 100
$config->setDefault('policies.get_contacts_limit', 100);
// Storage configuration
$config->setDefault('storage.path', '/etc/socialbox'); // The main path for file storage

View file

@ -8,6 +8,7 @@
private int $sessionInactivityExpires;
private int $imageCaptchaExpires;
private int $peerSyncInterval;
private int $getContactsLimit;
public function __construct(array $data)
{
@ -15,6 +16,7 @@
$this->sessionInactivityExpires = $data['session_inactivity_expires'];
$this->imageCaptchaExpires = $data['image_captcha_expires'];
$this->peerSyncInterval = $data['peer_sync_interval'];
$this->getContactsLimit = $data['get_contacts_limit'];
}
/**
@ -58,4 +60,14 @@
{
return $this->peerSyncInterval;
}
/**
* Returns the maximum amount of contacts that can be retrieved in a single request
*
* @return int
*/
public function getGetContactsLimit(): int
{
return $this->getContactsLimit;
}
}

View file

@ -0,0 +1,63 @@
<?php
namespace Socialbox\Classes\StandardMethods;
use Socialbox\Abstracts\Method;
use Socialbox\Classes\Configuration;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Managers\ContactManager;
use Socialbox\Objects\ClientRequest;
use Socialbox\Objects\RpcRequest;
class AddressBookGetContacts extends Method
{
/**
* @inheritDoc
*/
public static function execute(ClientRequest $request, RpcRequest $rpcRequest): ?SerializableInterface
{
$limit = Configuration::getPoliciesConfiguration()->getGetContactsLimit();
if($rpcRequest->containsParameter('limit'))
{
$limit = (int)$rpcRequest->getParameter('limit');
if($limit < 0)
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Invalid limit');
}
$limit = min($limit, Configuration::getPoliciesConfiguration()->getGetContactsLimit());
}
$page = 0;
if($rpcRequest->containsParameter('page'))
{
$page = (int)$rpcRequest->getParameter('page');
if($page < 0)
{
return $rpcRequest->produceError(StandardError::RPC_INVALID_ARGUMENTS, 'Invalid page');
}
$page = max($page, 0);
}
try
{
$contacts = ContactManager::getContacts($request->getPeer(), $limit, $page);
}
catch(DatabaseOperationException $e)
{
throw new StandardException('Failed to get contacts', StandardError::INTERNAL_SERVER_ERROR, $e);
}
$results = [];
foreach($contacts as $contact)
{
$results[] = $contact->toStandard();
}
return $rpcRequest->produceResponse($results);
}
}