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 all commits
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
11 changes: 8 additions & 3 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
name: php
version: 7.1.0
version: 7.2.0
schema: 1
scm: github.com/pubnub/php
changelog:
- date: 2025-01-02
version: 7.2.0
changes:
- type: feature
text: "Support for adding, getting and deleting message reactions."
- date: 2024-11-20
version: 7.1.0
changes:
Expand Down Expand Up @@ -432,8 +437,8 @@ sdks:
- x86-64
- distribution-type: library
distribution-repository: GitHub release
package-name: php-7.1.0.zip
location: https://github.com/pubnub/php/releases/tag/7.1.0
package-name: php-7.2.0.zip
location: https://github.com/pubnub/php/releases/tag/7.2.0
requires:
- name: rmccue/requests
min-version: 1.0.0
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 7.2.0
January 02 2025

#### Added
- Support for adding, getting and deleting message reactions.

## 7.1.0
November 20 2024

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ You will need the publish and subscribe keys to authenticate your app. Get your
{
"require": {
<!-- include the latest version from the badge at the top -->
"pubnub/pubnub": "7.1.0"
"pubnub/pubnub": "7.2.0"
}
}
```
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"keywords": ["api", "real-time", "realtime", "real time", "ajax", "push"],
"homepage": "http://www.pubnub.com/",
"license": "proprietary",
"version": "7.1.0",
"version": "7.2.0",
"authors": [
{
"name": "PubNub",
Expand All @@ -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
38 changes: 30 additions & 8 deletions src/PubNub/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@

abstract class Endpoint
{
protected bool $endpointAuthRequired;
protected int $endpointConnectTimeout;
protected int $endpointRequestTimeout;
protected string $endpointHttpMethod;
protected int $endpointOperationType;
protected string $endpointName;

protected const RESPONSE_IS_JSON = true;

/** @var PubNub */
Expand Down Expand Up @@ -56,12 +63,18 @@ abstract protected function createResponse($result);
/**
* @return int
*/
abstract protected function getOperationType();
protected function getOperationType()
{
return $this->endpointOperationType;
}

/**
* @return bool
*/
abstract protected function isAuthRequired();
protected function isAuthRequired()
{
return $this->endpointAuthRequired;
}

/**
* @return null|string
Expand All @@ -81,24 +94,33 @@ abstract protected function customParams();
/**
* @return int
*/
abstract protected function getRequestTimeout();
protected function getRequestTimeout()
{
return $this->endpointRequestTimeout;
}

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

/**
* @return string PNHttpMethod
*/
abstract protected function httpMethod();
protected function httpMethod()
{
return $this->endpointHttpMethod;
}

/**
* @return string
*/
protected function getName()
{
return substr(strrchr(get_class($this), '\\'), 1);
return !empty($this->endpointName) ? $this->endpointName : substr(strrchr(get_class($this), '\\'), 1);
}

/**
Expand Down Expand Up @@ -532,7 +554,7 @@ protected function invokeRequest()
}

/**
* @param int{PNStatusCategory::PNUnknownCategory..PNStatusCategory::PNRequestMessageCountExceededCategory} $category
* @param int $category
* @param $response
* @param ResponseInfo | null $responseInfo
* @param PubNubException | null $exception
Expand Down Expand Up @@ -598,7 +620,7 @@ private function getDefaultTransport()
self::$cachedTransports[$method] = [];
}

if (isset(self::$cachedTransports[$method][$cap_string]) && self::$cachedTransports[$method][$cap_string] !== null) {
if (isset(self::$cachedTransports[$method][$cap_string])) {
return self::$cachedTransports[$method][$cap_string];
}

Expand Down
153 changes: 153 additions & 0 deletions src/PubNub/Endpoints/MessageActions/AddMessageAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

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;

class AddMessageAction extends Endpoint
{
protected bool $endpointAuthRequired = true;
protected int $endpointConnectTimeout;
protected int $endpointRequestTimeout;
protected string $endpointHttpMethod = PNHttpMethod::POST;
protected int $endpointOperationType = PNOperationType::PNAddMessageActionOperation;
protected string $endpointName = "Set Message Actions";

protected const POST_PATH = "/v1/message-actions/%s/channel/%s/message/%s";
protected string $channel;
protected PNMessageAction $messageAction;

public function __construct(PubNub $pubnub)
{
parent::__construct($pubnub);
$this->endpointConnectTimeout = $this->pubnub->getConfiguration()->getConnectTimeout();
$this->endpointRequestTimeout = $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout();
}

/**
* Set a channel for the message action
*
* @param string $channel
* @return AddMessageAction
*/
public function channel(string $channel): self
{
$this->channel = $channel;
return $this;
}

/**
* Set the message action with instance of PNMessageAction
*
* @param PNMessageAction $messageAction
* @return AddMessageAction
*/
public function messageAction(PNMessageAction $messageAction): self
{
$this->messageAction = $messageAction;
return $this;
}

/**
* @throws PubNubValidationException
*/
protected function validateParams(): void
{
if (!$this->channel) {
throw new PubNubValidationException("Channel Missing");
}
$this->validateMessageAction();
$this->validateSubscribeKey();
$this->validatePublishKey();
}

/**
* @throws PubNubValidationException
*/
protected function validateMessageAction(): void
{
if (!isset($this->messageAction)) {
throw new PubNubValidationException("Message Action Missing");
}
if (!isset($this->messageAction->type)) {
throw new PubNubValidationException("Message Action Type Missing");
}
if (!isset($this->messageAction->value)) {
throw new PubNubValidationException("Message Action Value Missing");
}
if (!$this->messageAction->messageTimetoken) {
throw new PubNubValidationException("Message Action Message Timetoken Missing");
}
}

/**
* @return array<string, string>
*/
protected function customParams()
marcin-cebo marked this conversation as resolved.
Show resolved Hide resolved
{
return [
'uuid' => $this->pubnub->getConfiguration()->getUuid()
];
}

/**
* @return array<string, string>
*/
protected function customHeaders()
marcin-cebo marked this conversation as resolved.
Show resolved Hide resolved
{
return [
'Content-Type' => 'application/json',
'Accept' => 'application/json'
];
}

/**
* @return string | null
*/
protected function buildData()
marcin-cebo marked this conversation as resolved.
Show resolved Hide resolved
{
return PubNubUtil::writeValueAsString([
'type' => $this->messageAction->type,
'value' => $this->messageAction->value,
]);
}

/**
* @return string
* @throws PubNubBuildRequestException
*/
protected function buildPath()
{
return sprintf(
self::POST_PATH,
$this->pubnub->getConfiguration()->getSubscribeKey(),
$this->channel,
(int)$this->messageAction->messageTimetoken
);
}

/**
* @return PNAddMessageActionResult
*/
public function sync(): PNAddMessageActionResult
{
return parent::sync();
}

/**
* @param array<string, string> $json Decoded json
* @return PNAddMessageActionResult
*/
protected function createResponse($json): PNAddMessageActionResult
{
return PNAddMessageActionResult::fromJson($json);
}
}
Loading
Loading