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 support for message reactions #111

Merged
merged 5 commits into from
Jan 2, 2025
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
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"cp sdk-specifications/features/history/history-custom-mssg-type.feature tests/Acceptance/CustomMessageType/history-custom-mssg-type.feature",
"cp sdk-specifications/features/subscribe/subscribe-custom-mssg-type.feature tests/Acceptance/Subscribe/subscribe-custom-mssg-type.feature",
"vendor/bin/behat"
],
"lint": [
"vendor/bin/phpstan analyze --memory-limit 256M",
"git diff --name-only --diff-filter=d origin/master HEAD | grep -E '\\.php$' | xargs vendor/bin/phpcs --standard=PSR12"
]
},
"require": {
Expand Down
9 changes: 0 additions & 9 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -605,10 +605,6 @@ parameters:
count: 1
path: src/PubNub/Endpoints/Endpoint.php

-
message: "#^Method PubNub\\\\Endpoints\\\\Endpoint\\:\\:createStatus\\(\\) has parameter \\$category with no type specified\\.$#"
count: 1
path: src/PubNub/Endpoints/Endpoint.php

-
message: "#^Method PubNub\\\\Endpoints\\\\Endpoint\\:\\:createStatus\\(\\) has parameter \\$response with no type specified\\.$#"
Expand Down Expand Up @@ -700,11 +696,6 @@ parameters:
count: 2
path: src/PubNub/Endpoints/Endpoint.php

-
message: "#^PHPDoc tag @param has invalid value \\(int\\{PNStatusCategory\\:\\:PNUnknownCategory\\.\\.PNStatusCategory\\:\\:PNRequestMessageCountExceededCategory\\} \\$category\\)\\: Unexpected token \"\\{\", expected variable at offset 21$#"
count: 1
path: src/PubNub/Endpoints/Endpoint.php

-
message: "#^Parameter \\#1 \\$result of method PubNub\\\\Endpoints\\\\Endpoint\\:\\:createResponse\\(\\) expects array, WpOrg\\\\Requests\\\\Response given\\.$#"
count: 1
Expand Down
8 changes: 4 additions & 4 deletions src/PubNub/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected function validateSubscribeKey()
{
$subscribeKey = $this->pubnub->getConfiguration()->getSubscribeKey();

if ($subscribeKey == null || empty($subscribeKey)) {
marcin-cebo marked this conversation as resolved.
Show resolved Hide resolved
if ($subscribeKey == null) {
throw new PubNubValidationException("Subscribe Key not configured");
}
}
Expand All @@ -142,7 +142,7 @@ protected function validatePublishKey()
{
$publishKey = $this->pubnub->getConfiguration()->getPublishKey();

if ($publishKey == null || empty($publishKey)) {
if ($publishKey == null) {
throw new PubNubValidationException("Publish Key not configured");
}
}
Expand All @@ -154,7 +154,7 @@ protected function validateSecretKey()
{
$secretKey = $this->pubnub->getConfiguration()->getSecretKey();

if ($secretKey === null || empty($secretKey)) {
if ($secretKey === null) {
throw new PubNubValidationException("Secret key not configured");
}
}
Expand Down Expand Up @@ -554,7 +554,7 @@ protected function invokeRequest()
}

/**
* @param int{PNStatusCategory} $category
* @param int $category
* @param $response
* @param ResponseInfo | null $responseInfo
* @param PubNubException | null $exception
Expand Down
24 changes: 13 additions & 11 deletions src/PubNub/Endpoints/MessageActions/AddMessageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace PubNub\Endpoints\MessageActions;

use PubNub\PubNub;
use PubNub\Endpoints\Endpoint;
use PubNub\Enums\PNHttpMethod;
use PubNub\Enums\PNOperationType;
use PubNub\Exceptions\PubNubValidationException;
use PubNub\Exceptions\PubNubBuildRequestException;
use PubNub\Models\Consumer\MessageActions\PNMessageAction;
use PubNub\Models\Consumer\MessageActions\PNAddMessageActionResult;
use PubNub\PubNubUtil;
Expand All @@ -23,7 +25,7 @@ class AddMessageAction extends Endpoint
protected string $channel;
protected PNMessageAction $messageAction;

public function __construct($pubnub)
public function __construct(PubNub $pubnub)
{
parent::__construct($pubnub);
$this->endpointConnectTimeout = $this->pubnub->getConfiguration()->getConnectTimeout();
Expand Down Expand Up @@ -57,7 +59,7 @@ public function messageAction(PNMessageAction $messageAction): self
/**
* @throws PubNubValidationException
*/
protected function validateParams()
protected function validateParams(): void
{
if (!$this->channel) {
throw new PubNubValidationException("Channel Missing");
Expand All @@ -72,13 +74,13 @@ protected function validateParams()
*/
protected function validateMessageAction(): void
{
if (!$this->messageAction) {
if (!isset($this->messageAction)) {
throw new PubNubValidationException("Message Action Missing");
}
if (!$this->messageAction->type) {
if (!isset($this->messageAction->type)) {
throw new PubNubValidationException("Message Action Type Missing");
}
if (!$this->messageAction->value) {
if (!isset($this->messageAction->value)) {
throw new PubNubValidationException("Message Action Value Missing");
}
if (!$this->messageAction->messageTimetoken) {
Expand All @@ -87,7 +89,7 @@ protected function validateMessageAction(): void
}

/**
* @return array
* @return array<string, string>
*/
protected function customParams()
marcin-cebo marked this conversation as resolved.
Show resolved Hide resolved
{
Expand All @@ -97,7 +99,7 @@ protected function customParams()
}

/**
* @return array
* @return array<string, string>
*/
protected function customHeaders()
marcin-cebo marked this conversation as resolved.
Show resolved Hide resolved
{
Expand All @@ -108,7 +110,7 @@ protected function customHeaders()
}

/**
* @return array
* @return string | null
*/
protected function buildData()
marcin-cebo marked this conversation as resolved.
Show resolved Hide resolved
{
Expand Down Expand Up @@ -141,11 +143,11 @@ public function sync(): PNAddMessageActionResult
}

/**
* @param array $json Decoded json
* @return PNPublishResult
* @param array<string, string> $json Decoded json
* @return PNAddMessageActionResult
*/
protected function createResponse($json): PNAddMessageActionResult
{
return PNAddMessageActionResult::fromJson($json, $this->pubnub->getCrypto());
return PNAddMessageActionResult::fromJson($json);
}
}
18 changes: 10 additions & 8 deletions src/PubNub/Endpoints/MessageActions/GetMessageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace PubNub\Endpoints\MessageActions;

use PubNub\PubNub;
use PubNub\Endpoints\Endpoint;
use PubNub\Enums\PNHttpMethod;
use PubNub\Enums\PNOperationType;
use PubNub\Exceptions\PubNubValidationException;
use PubNub\Exceptions\PubNubBuildRequestException;
use PubNub\Models\Consumer\MessageActions\PNGetMessageActionResult;

/** @package PubNub\Endpoints\MessageActions */
Expand All @@ -24,7 +26,7 @@ class GetMessageAction extends Endpoint
protected ?string $end;
protected ?int $limit;

public function __construct($pubnub)
public function __construct(PubNub $pubnub)
{
parent::__construct($pubnub);
$this->endpointConnectTimeout = $this->pubnub->getConfiguration()->getConnectTimeout();
Expand Down Expand Up @@ -82,7 +84,7 @@ public function setLimit(int $limit): self
/**
* @throws PubNubValidationException
*/
protected function validateParams()
protected function validateParams(): void
{
if (!$this->channel) {
throw new PubNubValidationException("Channel Missing");
Expand All @@ -92,7 +94,7 @@ protected function validateParams()
}

/**
* @return array
* @return array<string, string>
*/
protected function customParams()
marcin-cebo marked this conversation as resolved.
Show resolved Hide resolved
{
Expand All @@ -112,11 +114,11 @@ protected function customParams()
}

/**
* @return array
* @return string | null
*/
protected function buildData()
marcin-cebo marked this conversation as resolved.
Show resolved Hide resolved
{
return [];
return null;
}

/**
Expand All @@ -141,11 +143,11 @@ public function sync(): PNGetMessageActionResult
}

/**
* @param array $json Decoded json
* @return PNPublishResult
* @param array<string, string> $json Decoded json
* @return PNGetMessageActionResult
*/
protected function createResponse($json): PNGetMessageActionResult
{
return PNGetMessageActionResult::fromJson($json, $this->pubnub->getCrypto());
return PNGetMessageActionResult::fromJson($json);
}
}
28 changes: 15 additions & 13 deletions src/PubNub/Endpoints/MessageActions/RemoveMessageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace PubNub\Endpoints\MessageActions;

use PubNub\PubNub;
use PubNub\Endpoints\Endpoint;
use PubNub\Enums\PNHttpMethod;
use PubNub\Enums\PNOperationType;
use PubNub\Exceptions\PubNubValidationException;
use PubNub\Exceptions\PubNubBuildRequestException;
use PubNub\Models\Consumer\MessageActions\PNRemoveMessageActionResult;

/** @package PubNub\Endpoints\MessageActions */
Expand All @@ -20,10 +22,10 @@ class RemoveMessageAction extends Endpoint

protected const DELETE_PATH = "/v1/message-actions/%s/channel/%s/message/%s/action/%s";
protected string $channel;
protected string $messageTimetoken;
protected string $actionTimetoken;
protected int | float $messageTimetoken;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why float is used here?
In other operations like AddMessageAction or GeMessageAction there is no float for timetokens definitions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int or float is compatible with PNMessageAction DTO class

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is float needed in PNMessageAction DTO class?
What value it brings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is carried from publish response where it's cast to float. int is also valid hence the union type. it is later cast to string to concat with other requests value

protected int | float $actionTimetoken;

public function __construct($pubnub)
public function __construct(PubNub $pubnub)
{
parent::__construct($pubnub);
$this->endpointConnectTimeout = $this->pubnub->getConfiguration()->getConnectTimeout();
Expand All @@ -45,10 +47,10 @@ public function channel(string $channel): self
/**
* The publish timetoken of a parent message.
*
* @param string $messageTimetoken
* @param int | float $messageTimetoken
* @return RemoveMessageAction
*/
public function messageTimetoken(string $messageTimetoken): self
public function messageTimetoken(int | float $messageTimetoken): self
{
$this->messageTimetoken = $messageTimetoken;
return $this;
Expand All @@ -57,10 +59,10 @@ public function messageTimetoken(string $messageTimetoken): self
/**
* The publish timetoken of the reaction.
*
* @param string $actionTimetoken
* @param int | float $actionTimetoken
* @return RemoveMessageAction
*/
public function actionTimetoken(string $actionTimetoken): self
public function actionTimetoken(int | float $actionTimetoken): self
{
$this->actionTimetoken = $actionTimetoken;
return $this;
Expand All @@ -69,7 +71,7 @@ public function actionTimetoken(string $actionTimetoken): self
/**
* @throws PubNubValidationException
*/
protected function validateParams()
protected function validateParams(): void
{
if (!$this->channel) {
throw new PubNubValidationException("Channel Missing");
Expand All @@ -86,19 +88,19 @@ protected function validateParams()
}

/**
* @return array
* @return array<string, string>
*/
protected function customParams()
{
return [];
}

/**
* @return array
* @return null|string
*/
protected function buildData()
{
return [];
return null;
}

/**
Expand All @@ -125,8 +127,8 @@ public function sync(): PNRemoveMessageActionResult
}

/**
* @param array $json Decoded json
* @return PNPublishResult
* @param mixed $json Decoded json
* @return PNRemoveMessageActionResult
*/
protected function createResponse($json): PNRemoveMessageActionResult
{
Expand Down
1 change: 1 addition & 0 deletions src/PubNub/Enums/PNOperationType.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

//phpcs:disable
namespace PubNub\Enums;

class PNOperationType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

class PNAddMessageActionResult extends PNMessageAction
{
public static function fromJson($json): PNMessageAction
/**
*
* @param mixed $json
* @return PNAddMessageActionResult
*/
public static function fromJson(mixed $json): self
{
$action = new self($json['data']);
return $action;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

class PNGetMessageActionResult extends PNMessageAction
{
/**
*
* @var PNMessageAction[] $actions
*/
public array $actions;

/**
*
* @param mixed $json
* @return PNGetMessageActionResult
*/
public static function fromJson($json): self
public static function fromJson(mixed $json): self
{

$actions = [];
foreach ($json['data'] as $action) {
$actions[] = new self($action);
Expand Down
17 changes: 11 additions & 6 deletions src/PubNub/Models/Consumer/MessageActions/PNMessageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

class PNMessageAction
{
public $type;
public $value;
public $messageTimetoken;
public $uuid;
public $actionTimetoken;
public ?string $type;
public ?string $value;
public int | float | null $messageTimetoken;
public ?string $uuid;
public int | float | null $actionTimetoken;

public function __construct($messageAction = null)
/**
*
* @param null|array<string, mixed> $messageAction
* @return void
*/
public function __construct(?array $messageAction = null)
{
if ($messageAction != null) {
$this->type = $messageAction['type'];
Expand Down
Loading
Loading