First unit tests, misc fixes
This commit is contained in:
@ -23,11 +23,11 @@ class Message
|
||||
|
||||
public function __construct(
|
||||
array $topic,
|
||||
?string $type,
|
||||
?string $data,
|
||||
?bool $private,
|
||||
?string $id,
|
||||
?int $retry
|
||||
?string $data=null,
|
||||
?string $type=null,
|
||||
?bool $private=null,
|
||||
?string $id=null,
|
||||
?int $retry=null
|
||||
)
|
||||
{
|
||||
$this->topic = $topic;
|
||||
@ -60,7 +60,7 @@ class Message
|
||||
topic: (array)$data['topic'],
|
||||
type: $data['type']??null,
|
||||
data: $data['data']??null,
|
||||
private: match ($data['private']??null) { "on" => true, null => null, default => false },
|
||||
private: match ($data['private']??null) { "on" => true, true => true, null => null, default => false },
|
||||
id: $data['id']??null,
|
||||
retry: $data['retry']??null,
|
||||
);
|
||||
|
@ -7,4 +7,6 @@ interface SubscriberInterface
|
||||
public function deliver(Message $message): void;
|
||||
|
||||
public function isAuthorized(): bool;
|
||||
|
||||
public function getPayload(): ?array;
|
||||
}
|
@ -108,5 +108,19 @@ class Configuration
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getOverwriteMessageIds(): bool
|
||||
{
|
||||
return $this->config['publish.overwrite_ids']??true;
|
||||
}
|
||||
|
||||
public function getRejectDuplicateMessages(): bool
|
||||
{
|
||||
return $this->config['publish.reject_duplicates']??true;
|
||||
}
|
||||
|
||||
public function getDuplicateIdHistorySize(): int
|
||||
{
|
||||
return 50;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace NoccyLabs\Mercureact\Http\Middleware;
|
||||
|
||||
use LDAP\Result;
|
||||
use NoccyLabs\Mercureact\Broker\Message;
|
||||
use NoccyLabs\Mercureact\Broker\SseSubscriber;
|
||||
use NoccyLabs\Mercureact\Broker\TopicManager;
|
||||
@ -24,6 +25,10 @@ class MercureHandler
|
||||
{
|
||||
private LoopInterface $loop;
|
||||
|
||||
private array $seenMessageIds = [];
|
||||
|
||||
private int $seenIdHistorySize = 100;
|
||||
|
||||
public function __construct(
|
||||
private Configuration $config,
|
||||
private TopicManager $topicManager,
|
||||
@ -31,6 +36,7 @@ class MercureHandler
|
||||
)
|
||||
{
|
||||
$this->loop = $loop ?? Loop::get();
|
||||
$this->seenIdHistorySize = $this->config->getDuplicateIdHistorySize();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -166,9 +172,16 @@ class MercureHandler
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->config->getRejectDuplicateMessages() && !empty($data['id'])) {
|
||||
if (in_array($data['id'], $this->seenMessageIds)) {
|
||||
return Response::plaintext("Duplicate message id")->withStatus(Response::STATUS_BAD_REQUEST);
|
||||
}
|
||||
array_push($this->seenMessageIds, $data['id']);
|
||||
$this->seenMessageIds = array_slice($this->seenMessageIds, -100, 100);
|
||||
}
|
||||
|
||||
// Put an id in there if none already
|
||||
// TODO add a configurable for this
|
||||
if (!isset($data['id'])) {
|
||||
if (empty($data['id']) || $this->config->getOverwriteMessageIds()) {
|
||||
$data['id'] = (string)Uuid::v7();
|
||||
}
|
||||
|
||||
@ -176,7 +189,7 @@ class MercureHandler
|
||||
$message = Message::fromData($data);
|
||||
|
||||
$this->loop->futureTick(function () use ($message) {
|
||||
$this->publishMercureMessage($message);
|
||||
$this->topicManager->publish($message);
|
||||
});
|
||||
|
||||
return Response::plaintext("urn:uuid:".$message->id."\n");
|
||||
@ -201,15 +214,4 @@ class MercureHandler
|
||||
return ($matched == count($topic));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param Message $message
|
||||
* @return void
|
||||
*/
|
||||
private function publishMercureMessage(Message $message): void
|
||||
{
|
||||
$this->topicManager->publish($message);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user