diff --git a/src/Socialbox/Objects/Database/EncryptionChannelMessageRecord.php b/src/Socialbox/Objects/Database/EncryptionChannelMessageRecord.php new file mode 100644 index 0000000..c5e04fb --- /dev/null +++ b/src/Socialbox/Objects/Database/EncryptionChannelMessageRecord.php @@ -0,0 +1,171 @@ +uuid = $data['uuid']; + $this->channelUuid = $data['channel_uuid']; + $this->recipient = EncryptionMessageRecipient::from($data['recipient']); + $this->status = EncryptionChannelMessageStatus::from($data['status']); + $this->hash = $data['hash']; + $this->data = $data['data']; + + if($data['timestamp'] instanceof DateTime) + { + $this->timestamp = $data['timestamp']; + } + elseif(is_int($data['timestamp'])) + { + $this->timestamp = (new DateTime())->setTimestamp($data['timestamp']); + } + elseif(is_string($data['timestamp'])) + { + try + { + $this->timestamp = new DateTime($data['timestamp']); + } + catch (DateMalformedStringException $e) + { + throw new InvalidArgumentException('Invalid DateTime format for timestamp, got: ' . $data['timestamp'], $e->getCode(), $e); + } + } + else + { + throw new InvalidArgumentException('Invalid timestamp type, got: ' . gettype($data['timestamp'])); + } + } + + /** + * Returns the Unique Universal Identifier for the message + * + * @return string The Message's Unique Universal Identifier + */ + public function getUuid(): string + { + return $this->uuid; + } + + /** + * Returns the Unique Universal Identifier of the channel that this message belongs to + * + * @return string The Channel's Unique Universal Identifier + */ + public function getChannelUuid(): string + { + return $this->channelUuid; + } + + /** + * Returns the recipient of the message + * + * @return EncryptionMessageRecipient The recipient of the message + */ + public function getRecipient(): EncryptionMessageRecipient + { + return $this->recipient; + } + + /** + * Returns the status of the message + * + * @return EncryptionChannelMessageStatus The status of the message + */ + public function getStatus(): EncryptionChannelMessageStatus + { + return $this->status; + } + + /**try { + switch($fieldName) { + case InformationFieldName::DISPLAY_NAME: + SessionManager::updateFlow($request->getSession(), [SessionFlags::SET_DISPLAY_NAME]); + break; + // Other cases... + } +} catch (Exception $e) { + try { + PeerInformationManager::deleteProperty($peer, $fieldName); + } catch (DatabaseOperationException $e) { + 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); +} + * Returns the SHA512 hash of the decrypted content + * + * @return string The SHA512 hash of the decrypted content + */ + public function getHash(): string + { + return $this->hash; + } + + /** + * Returns the encrypted content of the message + * + * @return string The encrypted content of the message + */ + public function getData(): string + { + return $this->data; + } + + /** + * Returns the Timestamp for when this message was created + * + * @return DateTime The Timestamp for when the message was created + */ + public function getTimestamp(): DateTime + { + return $this->timestamp; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): EncryptionChannelMessageRecord + { + return new self($data); + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'uuid' => $this->uuid, + 'channel_uuid' => $this->channelUuid, + 'recipient' => $this->recipient->value, + 'status' => $this->status->value, + 'hash' => $this->hash, + 'data' => $this->data, + 'timestamp' => $this->timestamp->getTimestamp() + ]; + } + } \ No newline at end of file diff --git a/src/Socialbox/Objects/Database/EncryptionChannelRecord.php b/src/Socialbox/Objects/Database/EncryptionChannelRecord.php new file mode 100644 index 0000000..c9440d2 --- /dev/null +++ b/src/Socialbox/Objects/Database/EncryptionChannelRecord.php @@ -0,0 +1,161 @@ +uuid = $data['uuid']; + $this->status = EncryptionChannelStatus::from($data['status']); + $this->callingPeerAddress = PeerAddress::fromAddress($data['calling_peer_address']); + $this->callingPublicEncryptionKey = $data['calling_public_encryption_key']; + $this->receivingPeerAddress = PeerAddress::fromAddress($data['receiving_peer_address']); + $this->receivingPublicEncryptionKey = $data['receiving_public_encryption_key'] ?? null; + + if($data['created'] instanceof DateTime) + { + $this->created = $data['created']; + } + elseif(is_int($data['created'])) + { + $this->created = (new DateTime())->setTimestamp($data['created']); + } + elseif(is_string($data['created'])) + { + try + { + $this->created = new DateTime($data['created']); + } + catch (DateMalformedStringException $e) + { + throw new InvalidArgumentException('Invalid DateTime given in created, got: ' . $data['created'], $e->getCode(), $e); + } + } + else + { + throw new InvalidArgumentException('Invalid created type, got: ' . gettype($data['created'])); + } + } + + /** + * Returns the Universal Unique Identifier of the encryption channel record + * + * @return string The UUID V4 + */ + public function getUuid(): string + { + return $this->uuid; + } + + /** + * Returns the current status of the encryption channel record + * + * @return EncryptionChannelStatus The current status of the encryption channel record + */ + public function getStatus(): EncryptionChannelStatus + { + return $this->status; + } + + /** + * Returns the PeerAddress of the calling peer for the encryption channel record + * + * @return PeerAddress The address of the calling peer + */ + public function getCallingPeerAddress(): PeerAddress + { + return $this->callingPeerAddress; + } + + /** + * Returns the public encryption key of the calling peer + * + * @return string The public encryption key of the caller + */ + public function getCallingPublicEncryptionKey(): string + { + return $this->callingPublicEncryptionKey; + } + + /** + * Returns the PeerAddress of the receiving peer for the encryption channel record + * + * @return PeerAddress + */ + public function getReceivingPeerAddress(): PeerAddress + { + return $this->receivingPeerAddress; + } + + /** + * Returns the public encryption key of the receiving peer + * + * @return string|null The public encryption key of the receiver + */ + public function getReceivingPublicEncryptionKey(): ?string + { + return $this->receivingPublicEncryptionKey; + } + + /** + * The DateTime object of when the record was created + * + * @return DateTime The DateTime object of the record's creation date + */ + public function getCreated(): DateTime + { + return $this->created; + } + + /** + * @inheritDoc + */ + public static function fromArray(array $data): EncryptionChannelRecord + { + return new self($data); + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'uuid' => $this->uuid, + 'status' => $this->status->value, + 'calling_peer_address' => $this->callingPeerAddress->getAddress(), + 'calling_public_encryption_key' => $this->callingPublicEncryptionKey, + 'receiving_peer_address' => $this->receivingPeerAddress->getAddress(), + 'created' => $this->created->getTimestamp() + ]; + } + } \ No newline at end of file