Added method ADDRESS_BOOK_GET_CONTACTS
This commit is contained in:
parent
d56483119c
commit
75de51c910
7 changed files with 185 additions and 11 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@
|
|||
use Socialbox\Classes\StandardMethods\AcceptTermsOfService;
|
||||
use Socialbox\Classes\StandardMethods\AddressBookAddContact;
|
||||
use Socialbox\Classes\StandardMethods\AddressBookDeleteContact;
|
||||
use Socialbox\Classes\StandardMethods\AddressBookGetContacts;
|
||||
use Socialbox\Classes\StandardMethods\Authenticate;
|
||||
use Socialbox\Classes\StandardMethods\GetAllowedMethods;
|
||||
use Socialbox\Classes\StandardMethods\GetCommunityGuidelines;
|
||||
|
@ -100,6 +101,7 @@
|
|||
|
||||
case ADDRESS_BOOK_ADD_CONTACT = 'addressBookAddContact';
|
||||
case ADDRESS_BOOK_DELETE_CONTACT = 'addressBookDeleteContact';
|
||||
case ADDRESS_BOOK_GET_CONTACTS = 'addressBookGetContacts';
|
||||
|
||||
case AUTHENTICATE = 'authenticate';
|
||||
case RESOLVE_PEER = 'resolvePeer';
|
||||
|
@ -152,6 +154,7 @@
|
|||
|
||||
self::ADDRESS_BOOK_ADD_CONTACT => AddressBookAddContact::execute($request, $rpcRequest),
|
||||
self::ADDRESS_BOOK_DELETE_CONTACT => AddressBookDeleteContact::execute($request, $rpcRequest),
|
||||
self::ADDRESS_BOOK_GET_CONTACTS => AddressBookGetContacts::execute($request, $rpcRequest),
|
||||
|
||||
self::AUTHENTICATE => Authenticate::execute($request, $rpcRequest),
|
||||
self::RESOLVE_PEER => ResolvePeer::execute($request, $rpcRequest),
|
||||
|
@ -286,6 +289,7 @@
|
|||
|
||||
self::ADDRESS_BOOK_ADD_CONTACT,
|
||||
self::ADDRESS_BOOK_DELETE_CONTACT,
|
||||
self::ADDRESS_BOOK_GET_CONTACTS,
|
||||
];
|
||||
|
||||
// Prevent the user from deleting their display name if it is required
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
use Socialbox\Classes\Database;
|
||||
use Socialbox\Enums\Types\ContactRelationshipType;
|
||||
use Socialbox\Exceptions\DatabaseOperationException;
|
||||
use Socialbox\Objects\Database\ContactRecord;
|
||||
use Socialbox\Objects\Database\ContactDatabaseRecord;
|
||||
use Socialbox\Objects\PeerAddress;
|
||||
|
||||
class ContactManager
|
||||
|
@ -109,10 +109,10 @@
|
|||
*
|
||||
* @param string $peerUuid The unique identifier for the peer whose contact is to be retrieved.
|
||||
* @param string|PeerAddress $contactAddress The address of the contact, either as a string or a PeerAddress object.
|
||||
* @return ContactRecord|null The retrieved ContactRecord instance if found, or null if no matching contact exists.
|
||||
* @return ContactDatabaseRecord|null The retrieved ContactRecord instance if found, or null if no matching contact exists.
|
||||
* @throws DatabaseOperationException If the database query fails.
|
||||
*/
|
||||
public static function getContact(string $peerUuid, string|PeerAddress $contactAddress): ?ContactRecord
|
||||
public static function getContact(string $peerUuid, string|PeerAddress $contactAddress): ?ContactDatabaseRecord
|
||||
{
|
||||
if($contactAddress instanceof PeerAddress)
|
||||
{
|
||||
|
@ -138,7 +138,7 @@
|
|||
return null;
|
||||
}
|
||||
|
||||
return ContactRecord::fromArray($result);
|
||||
return ContactDatabaseRecord::fromArray($result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -204,10 +204,10 @@
|
|||
* Retrieves a contact by its unique identifier.
|
||||
*
|
||||
* @param string $uuid The unique identifier of the contact to retrieve.
|
||||
* @return ContactRecord|null A ContactRecord instance if the contact is found, or null if no contact exists with the provided UUID.
|
||||
* @return ContactDatabaseRecord|null A ContactRecord instance if the contact is found, or null if no contact exists with the provided UUID.
|
||||
* @throws DatabaseOperationException If the database query fails.
|
||||
*/
|
||||
public static function getContactByUuid(string $uuid): ?ContactRecord
|
||||
public static function getContactByUuid(string $uuid): ?ContactDatabaseRecord
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -227,7 +227,7 @@
|
|||
return null;
|
||||
}
|
||||
|
||||
return ContactRecord::fromArray($result);
|
||||
return ContactDatabaseRecord::fromArray($result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -236,7 +236,7 @@
|
|||
* @param string $peerUuid The unique identifier for the peer whose contacts are to be retrieved.
|
||||
* @param int $limit The maximum number of contacts to retrieve per page. Defaults to 100.
|
||||
* @param int $page The page number to retrieve. Defaults to 1.
|
||||
* @return array An array of ContactRecord instances representing the contacts for the given peer.
|
||||
* @return ContactDatabaseRecord[] An array of ContactRecord instances representing the contacts for the given peer.
|
||||
* @throws DatabaseOperationException If the database query fails.
|
||||
*/
|
||||
public static function getContacts(string $peerUuid, int $limit=100, int $page=1): array
|
||||
|
@ -268,7 +268,7 @@
|
|||
// Convert results to ContactRecord instances
|
||||
foreach ($results as $result)
|
||||
{
|
||||
$contacts[] = ContactRecord::fromArray($result);
|
||||
$contacts[] = ContactDatabaseRecord::fromArray($result);
|
||||
}
|
||||
}
|
||||
catch (PDOException $e)
|
||||
|
|
|
@ -7,8 +7,9 @@
|
|||
use InvalidArgumentException;
|
||||
use Socialbox\Enums\Types\ContactRelationshipType;
|
||||
use Socialbox\Interfaces\SerializableInterface;
|
||||
use Socialbox\Objects\Standard\ContactRecord;
|
||||
|
||||
class ContactRecord implements SerializableInterface
|
||||
class ContactDatabaseRecord implements SerializableInterface
|
||||
{
|
||||
private string $uuid;
|
||||
private string $peerUuid;
|
||||
|
@ -117,11 +118,25 @@
|
|||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function fromArray(array $data): ContactRecord
|
||||
public static function fromArray(array $data): ContactDatabaseRecord
|
||||
{
|
||||
return new self($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the object to a standard contact record.
|
||||
*
|
||||
* @return ContactRecord The standard contact record.
|
||||
*/
|
||||
public function toStandard(): ContactRecord
|
||||
{
|
||||
return new ContactRecord([
|
||||
'address' => $this->contactPeerAddress,
|
||||
'relationship' => $this->relationship,
|
||||
'added_timestamp' => $this->created->getTimestamp()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
76
src/Socialbox/Objects/Standard/ContactRecord.php
Normal file
76
src/Socialbox/Objects/Standard/ContactRecord.php
Normal file
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox\Objects\Standard;
|
||||
|
||||
use Socialbox\Enums\Types\ContactRelationshipType;
|
||||
use Socialbox\Interfaces\SerializableInterface;
|
||||
use Socialbox\Objects\PeerAddress;
|
||||
|
||||
class ContactRecord implements SerializableInterface
|
||||
{
|
||||
private PeerAddress $address;
|
||||
private ContactRelationshipType $relationship;
|
||||
private int $addedTimestamp;
|
||||
|
||||
/**
|
||||
* Constructs a new instance with the provided parameters.
|
||||
*
|
||||
* @param array $data The array of data to use for the object.
|
||||
*/
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->address = PeerAddress::fromAddress($data['address']);
|
||||
$this->relationship = ContactRelationshipType::tryFrom($data['relationship']) ?? ContactRelationshipType::MUTUAL;
|
||||
$this->addedTimestamp = $data['added_timestamp'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the address of the contact.
|
||||
*
|
||||
* @return PeerAddress Returns the address of the contact.
|
||||
*/
|
||||
public function getAddress(): PeerAddress
|
||||
{
|
||||
return $this->address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the relationship of the contact.
|
||||
*
|
||||
* @return ContactRelationshipType Returns the relationship of the contact.
|
||||
*/
|
||||
public function getRelationship(): ContactRelationshipType
|
||||
{
|
||||
return $this->relationship;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the timestamp when the contact was added.
|
||||
*
|
||||
* @return int Returns the timestamp when the contact was added.
|
||||
*/
|
||||
public function getAddedTimestamp(): int
|
||||
{
|
||||
return $this->addedTimestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function fromArray(array $data): object
|
||||
{
|
||||
return new self($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'address' => $this->address->getAddress(),
|
||||
'relationship' => $this->relationship->value,
|
||||
'added_timestamp' => $this->addedTimestamp
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue