Updated Standard Peer object to include standard information fields and updated PeerInformationManager to retrieve filtered fields per privacy condition

This commit is contained in:
netkas 2025-01-24 15:20:19 -05:00
parent f689e36378
commit a826f4f3a9
3 changed files with 86 additions and 22 deletions

View file

@ -20,9 +20,7 @@
* @param InformationFieldName $property The name of the property to add.
* @param string $value The value of the property to add.
* @param PrivacyState|null $privacyState The privacy state of the property to add.
*
* @return void
*
* @throws DatabaseOperationException Thrown if the operation fails.
*/
public static function addField(string|PeerRecord $peerUuid, InformationFieldName $property, string $value, ?PrivacyState $privacyState=null): void
@ -70,9 +68,7 @@
* @param string|PeerRecord $peerUuid The UUID of the peer to update the property for.
* @param InformationFieldName $property The name of the property to update.
* @param string $value The new value of the property.
*
* @return void
*
* @throws DatabaseOperationException Thrown if the operation fails.
*/
public static function updateField(string|PeerRecord $peerUuid, InformationFieldName $property, string $value): void
@ -108,9 +104,7 @@
* @param string|PeerRecord $peerUuid The UUID of the peer to update the privacy state for.
* @param InformationFieldName $property The name of the property to update the privacy state for.
* @param PrivacyState $privacyState The new privacy state of the property.
*
* @return void
*
* @throws DatabaseOperationException Thrown if the operation fails.
*/
public static function updatePrivacyState(string|PeerRecord $peerUuid, InformationFieldName $property, PrivacyState $privacyState): void
@ -146,9 +140,7 @@
*
* @param string|PeerRecord $peerUuid The UUID of the peer to check for the property.
* @param InformationFieldName $property The name of the property to check for.
*
* @return bool
*
* @throws DatabaseOperationException Thrown if the operation fails.
*/
public static function fieldExists(string|PeerRecord $peerUuid, InformationFieldName $property): bool
@ -179,9 +171,7 @@
*
* @param string|PeerRecord $peerUuid The UUID of the peer to get the property from.
* @param InformationFieldName $property The name of the property to get.
*
* @return PeerInformationFieldRecord
*
* @throws DatabaseOperationException Thrown if the operation fails.
*/
public static function getField(string|PeerRecord $peerUuid, InformationFieldName $property): PeerInformationFieldRecord
@ -217,9 +207,7 @@
* Gets all properties from a peer's information record.
*
* @param string|PeerRecord $peerUuid The UUID of the peer to get the properties from.
*
* @return PeerInformationFieldRecord[]
*
* @throws DatabaseOperationException Thrown if the operation fails.
*/
public static function getFields(string|PeerRecord $peerUuid): array
@ -249,14 +237,53 @@
}
}
/**
* Gets all properties from a peer's information record that match the provided privacy filters.
*
* @param string|PeerRecord $peerUuid The UUID of the peer to get the properties from.
* @param PrivacyState[] $privacyFilters The privacy filters to apply.
* @return PeerInformationFieldRecord[] The filtered properties.
* @throws DatabaseOperationException Thrown if the operation fails.
*/
public static function getFilteredFields(string|PeerRecord $peerUuid, array $privacyFilters): array
{
if($peerUuid instanceof PeerRecord)
{
$peerUuid = $peerUuid->getUuid();
}
$results = [];
/** @var PrivacyState $privacyState */
foreach($privacyFilters as $privacyState)
{
try
{
$stmt = Database::getConnection()->prepare('SELECT * FROM peer_information WHERE peer_uuid=:peer_uuid AND privacy_state=:privacy_state');
$stmt->bindValue(':peer_uuid', $peerUuid);
$stmt->bindValue(':privacy_state', $privacyState->value);
$stmt->execute();
$results = array_merge($results, $stmt->fetchAll());
}
catch(PDOException $e)
{
throw new DatabaseOperationException(sprintf('Failed to get properties for peer %s with privacy state %s', $peerUuid, $privacyState->value), $e);
}
}
if(!$results)
{
return [];
}
return array_map(fn($result) => PeerInformationFieldRecord::fromArray($result), $results);
}
/**
* Deletes a property from a peer's information record.
*
* @param string|PeerRecord $peerUuid The UUID of the peer to delete the property from.
* @param InformationFieldName $property The name of the property to delete.
*
* @return void
*
* @throws DatabaseOperationException Thrown if the operation fails.
*/
public static function deleteField(string|PeerRecord $peerUuid, InformationFieldName $property): void

View file

@ -59,4 +59,17 @@
'privacy_state' => $this->privacyState->value
];
}
/**
* Converts the object to an InformationField instance.
*
* @return InformationField Returns the converted InformationField instance.
*/
public function toInformationField(): InformationField
{
return new InformationField([
'name' => $this->name->value,
'value' => $this->value,
]);
}
}

View file

@ -4,12 +4,16 @@
use InvalidArgumentException;
use Socialbox\Interfaces\SerializableInterface;
use Socialbox\Objects\Database\PeerInformationFieldRecord;
use Socialbox\Objects\PeerAddress;
class Peer implements SerializableInterface
{
private PeerAddress $address;
private string $displayName;
/**
* @var InformationField[]
*/
private array $informationFields;
private array $flags;
private int $registered;
@ -27,7 +31,6 @@
*/
public function __construct(array $data)
{
// TODO: Bug: PHP message: PHP Warning: Undefined array key "address" in /var/ncc/packages/net.nosial.socialbox=1.0.0/bin/src/Socialbox/Objects/Standard/Peer.php on line 28
if(is_string($data['address']))
{
$this->address = PeerAddress::fromAddress($data['address']);
@ -41,7 +44,28 @@
throw new InvalidArgumentException('Invalid address value type, got type ' . gettype($data['address']));
}
$this->displayName = $data['display_name'];
$informationFields = [];
foreach($data['information_fields'] as $field)
{
if($field instanceof PeerInformationFieldRecord)
{
$informationFields[] = $field->toInformationField();
}
elseif($field instanceof InformationFieldState)
{
$informationFields[] = $field->toInformationField();
}
elseif(is_array($field))
{
$informationFields[] = new InformationField($field);
}
else
{
throw new InvalidArgumentException('Invalid information field type, got type ' . gettype($field));
}
}
$this->informationFields = $informationFields;
$this->flags = $data['flags'];
$this->registered = $data['registered'];
}
@ -57,13 +81,13 @@
}
/**
* Retrieves the display name of the entity.
* Retrieves the information fields associated with the peer.
*
* @return string The display name associated with the entity.
* @return InformationField[] An array containing the information fields.
*/
public function getDisplayName(): string
public function getInformationFields(): array
{
return $this->displayName;
return $this->informationFields;
}
/**
@ -101,7 +125,7 @@
{
return [
'address' => $this->address->getAddress(),
'display_name' => $this->displayName,
'information_fields' => array_map(fn($field) => $field->toArray(), $this->informationFields),
'flags' => $this->flags,
'registered' => $this->registered
];