Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Fetch Messages endpoint #99

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
234 changes: 234 additions & 0 deletions src/PubNub/Endpoints/MessagePersistance/FetchMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
<?php

namespace PubNub\Endpoints\MessagePersistance;

use PubNub\Endpoints\Endpoint;
use PubNub\Enums\PNOperationType;
use PubNub\Enums\PNHttpMethod;
use PubNub\Exceptions\PubNubBuildRequestException;
use PubNub\Exceptions\PubNubValidationException;
use PubNub\Models\Consumer\MessagePersistence\PNFetchMessagesResult;
use PubNub\PubNubUtil;

class FetchMessages extends Endpoint
{
protected const GET_PATH = "/v3/history%s/sub-key/%s/channel/%s";

protected const SINGLE_CHANNEL_MAX_MESSAGES = 100;
protected const DEFAULT_SINGLE_CHANNEL_MESSAGES = 100;

protected const MULTIPLE_CHANNELS_MAX_MESSAGES = 25;
protected const DEFAULT_MULTIPLE_CHANNELS_MESSAGES = 25;

protected const MAX_MESSAGES_ACTIONS = 25;
protected const DEFAULT_MESSAGES_ACTIONS = 25;

protected array $channels;

protected int $start;
protected int $end;
protected int $count;

protected bool $includeMeta = false;
protected bool $includeUuid = false;
protected bool $includeMessageType = false;
protected bool $includeMessageActions = false;
protected bool $includeType = false;
seba-aln marked this conversation as resolved.
Show resolved Hide resolved
protected bool $includeSpaceId = false;
seba-aln marked this conversation as resolved.
Show resolved Hide resolved

protected array $customParamMapping = [
'start' => 'start',
'end' => 'end',
'count' => 'max',
'includeMeta' => 'include_meta',
'includeUuid' => 'include_uuid',
'includeMessageType' => 'include_message_type',
'includeType' => 'include_type',
'includeSpaceId' => 'include_space_id',
];

public function channels(...$channel)
{
if (is_array($channel[0])) {
$this->channels = $channel[0];
} elseif (strpos($channel[0], ',')) {
$this->channels = array_map('trim', explode(',', $channel[0]));
} else {
$this->channels = $channel;
}
return $this;
}

public function start($start)
{
$this->start = $start;
return $this;
}

public function end($end)
{
$this->end = $end;
return $this;
}

public function count($count)
{
$this->count = $count;
return $this;
}

public function includeMeta($includeMeta)
{
$this->includeMeta = $includeMeta;
return $this;
}

public function includeUuid($includeUuid)
{
$this->includeUuid = $includeUuid;
return $this;
}

public function includeMessageType($includeMessageType)
{
$this->includeMessageType = $includeMessageType;
return $this;
}

public function includeMessageActions($includeMessageActions)
{
$this->includeMessageActions = $includeMessageActions;
return $this;
}

public function includeType($includeType)
{
$this->includeType = $includeType;
return $this;
}

public function includeSpaceId($includeSpaceId)
{
$this->includeSpaceId = $includeSpaceId;
return $this;
}

/**
* @throws PubNubValidationException
*/
protected function validateParams()
{
if (!is_array($this->channels) || count($this->channels) === 0) {
throw new PubNubValidationException("Channel Missing");
}

$this->validateSubscribeKey();
$this->validatePublishKey();
}

/**
* @return array
*/
protected function customParams()
{
$params = [];
foreach ($this->customParamMapping as $customParam => $requestParam) {
if (isset($this->$customParam)) {
$params[$requestParam] = $this->$customParam;
}
}

return $params;
}

/**
* @return string
* @throws PubNubBuildRequestException
*/
protected function buildPath()
{
$withActions = $this->includeMessageActions ? '-with-actions' : '';
$channelList = $this->includeMessageActions
? PubNubUtil::urlEncode($this->channels[0])
: implode(',', array_map(fn($channel) => PubNubUtil::urlEncode($channel), $this->channels));

return sprintf(
self::GET_PATH,
$withActions,
$this->pubnub->getConfiguration()->getSubscribeKey(),
$channelList,
);
}

public function sync(): PNFetchMessagesResult
{
return parent::sync();
}

/**
* @param array $json Decoded json
* @return PNPublishResult
*/
protected function createResponse($json)
{
return PNFetchMessagesResult::fromJson(
$json,
$this->pubnub->getCrypto(),
isset($this->start) ? $this->start : null,
isset($this->end) ? $this->end : null
);
}

/**
* @return bool
*/
protected function isAuthRequired()
{
return true;
}

protected function buildData()
{
return null;
}

/**
* @return int
*/
protected function getRequestTimeout()
{
return $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout();
}

/**
* @return int
*/
protected function getConnectTimeout()
{
return $this->pubnub->getConfiguration()->getConnectTimeout();
}

/**
* @return string
*/
protected function httpMethod()
{
return PNHttpMethod::GET;
}

/**
* @return int
*/
protected function getOperationType()
{
return PNOperationType::PNFetchMessagesOperation;
}

/**
* @return string
*/
protected function getName()
{
return "Fetch Messages";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace PubNub\Models\Consumer\MessagePersistence;

class PNFetchMessagesItemResult
{
protected mixed $message;
protected string $timetoken;
protected mixed $metadata;
protected mixed $actions;
protected string $uuid;
protected string $messageType;


public function __construct(mixed $message, string $timetoken)
{
$this->message = $message;
$this->timetoken = $timetoken;
}

public function setMetadata(mixed $metadata)
{
$this->metadata = $metadata;
return $this;
}

public function setActions(mixed $actions)
{
$this->actions = $actions;
return $this;
}

public function setUuid(string $uuid)
{
$this->uuid = $uuid;
return $this;
}

public function setMessageType(string $messageType)
{
$this->messageType = $messageType;
return $this;
}

public function getMessage(): mixed
{
return $this->message;
}

public function getTimetoken(): string
{
return $this->timetoken;
}

public function getMetadata(): mixed
{
return $this->metadata;
}

public function getActions(): mixed
{
return $this->actions;
}

public function getUuid(): string
{
return $this->uuid;
}

public function getMessageType(): string
{
return $this->messageType;
}

public static function fromJson($json, $crypto): static
{
$message = $json['message'];
if ($crypto) {
$message = $crypto->decrypt($message);
}
$item = new static(
$message,
$json['timetoken'],
);

if (isset($json['uuid'])) {
$item->setUuid($json['uuid']);
}

if (isset($json['message_type'])) {
$item->setMessageType($json['message_type']);
}

if (isset($json['meta'])) {
$item->setMetadata($json['meta']);
}

if (isset($json['actions'])) {
$item->setActions($json['actions']);
} else {
$item->setActions([]);
}

return $item;
}

public function __toString(): string
{
return sprintf("Fetch message item with tt: %s and content: %s", $this->timetoken, $this->message);
}
}
Loading
Loading