First unit tests, misc fixes
This commit is contained in:
		@@ -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