Skip to content

Commit 766084b

Browse files
committed
PS-916 novu: create topic even if no notification sent
1 parent e60a4e0 commit 766084b

File tree

10 files changed

+115
-7
lines changed

10 files changed

+115
-7
lines changed

databox/api/src/Asset/ObjectNotifier.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ public function notifyObject(
4242
}
4343

4444
if ($object->isAutoSubscribeOwner() && $object->getOwnerId()) {
45-
$this->notifier->addTopicSubscribers($topicKey, [$object->getOwnerId()]);
45+
$this->notifier->addTopicSubscribers($topicKey, [$object->getOwnerId()], direct: true);
4646
$shouldNotify = true;
4747
} else {
48+
$this->notifier->createTopic($topicKey);
4849
$shouldNotify = false;
4950
}
5051

lib/php/notify-bundle/composer.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77
],
88
"homepage": "https://www.alchemy.fr/",
99
"license": "MIT",
10+
"repositories": [
11+
{
12+
"type": "path",
13+
"url": "../auth-bundle",
14+
"options": {
15+
"symlink": true
16+
}
17+
}
18+
],
1019
"require": {
1120
"php": "^8.3",
12-
"alchemy/auth-bundle": "*",
21+
"alchemy/auth-bundle": "@dev",
1322
"symfony/notifier": "^6.4.13",
1423
"symfony/framework-bundle": "^6.4"
1524
},

lib/php/notify-bundle/src/AlchemyNotifyBundle.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Alchemy\NotifyBundle\Command\TestNotificationCommand;
99
use Alchemy\NotifyBundle\Controller\NotificationController;
1010
use Alchemy\NotifyBundle\Message\AddTopicSubscribersHandler;
11+
use Alchemy\NotifyBundle\Message\NotifyTopicHandler;
12+
use Alchemy\NotifyBundle\Message\RemoveTopicSubscribersHandler;
1113
use Alchemy\NotifyBundle\Message\UpdateSubscribersHandler;
1214
use Alchemy\NotifyBundle\Notification\MockNotifier;
1315
use Alchemy\NotifyBundle\Notification\NotifierInterface;
@@ -95,6 +97,8 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
9597
$services->set(TestNotificationCommand::class);
9698
$services->set(NotificationController::class);
9799
$services->set(AddTopicSubscribersHandler::class);
100+
$services->set(RemoveTopicSubscribersHandler::class);
101+
$services->set(NotifyTopicHandler::class);
98102
$services->set(UpdateSubscribersHandler::class);
99103
}
100104
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Alchemy\NotifyBundle\Message;
4+
5+
final readonly class NotifyTopic
6+
{
7+
public function __construct(
8+
public string $topicKey,
9+
public ?string $authorId,
10+
public string $notificationId,
11+
public array $parameters = [],
12+
public array $options = [],
13+
) {
14+
}
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Alchemy\NotifyBundle\Message;
6+
7+
use Alchemy\NotifyBundle\Service\NovuClient;
8+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
9+
10+
#[AsMessageHandler]
11+
readonly class NotifyTopicHandler
12+
{
13+
public function __construct(
14+
private NovuClient $novuClient,
15+
) {
16+
}
17+
18+
public function __invoke(NotifyTopic $message): void
19+
{
20+
$this->novuClient->notifyTopic(
21+
$message->topicKey,
22+
$message->authorId,
23+
$message->notificationId,
24+
$message->parameters,
25+
$message->options
26+
);
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Alchemy\NotifyBundle\Message;
4+
5+
final readonly class RemoveTopicSubscribers
6+
{
7+
public function __construct(
8+
private string $topicKey,
9+
private array $subscribers,
10+
) {
11+
}
12+
13+
public function getTopicKey(): string
14+
{
15+
return $this->topicKey;
16+
}
17+
18+
public function getSubscribers(): array
19+
{
20+
return $this->subscribers;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Alchemy\NotifyBundle\Message;
6+
7+
use Alchemy\NotifyBundle\Service\NovuClient;
8+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
9+
10+
#[AsMessageHandler]
11+
readonly class RemoveTopicSubscribersHandler
12+
{
13+
public function __construct(
14+
private NovuClient $novuClient,
15+
) {
16+
}
17+
18+
public function __invoke(RemoveTopicSubscribers $message): void
19+
{
20+
$this->novuClient->removeTopicSubscribers($message->getTopicKey(), $message->getSubscribers());
21+
}
22+
}

lib/php/notify-bundle/src/Notification/MockNotifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function notifyTopic(
5252
];
5353
}
5454

55-
public function addTopicSubscribers(string $topicKey, array $subscribers): void
55+
public function addTopicSubscribers(string $topicKey, array $subscribers, bool $direct = false): void
5656
{
5757
}
5858

lib/php/notify-bundle/src/Notification/NotifierInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function notifyTopic(
3434
public function addTopicSubscribers(
3535
string $topicKey,
3636
array $subscribers,
37+
bool $direct = false,
3738
): void;
3839

3940
public function createTopic(

lib/php/notify-bundle/src/Notification/SymfonyNotifier.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Alchemy\AuthBundle\Repository\UserRepository;
88
use Alchemy\NotifyBundle\Message\AddTopicSubscribers;
9+
use Alchemy\NotifyBundle\Message\NotifyTopic;
10+
use Alchemy\NotifyBundle\Message\RemoveTopicSubscribers;
911
use Alchemy\NotifyBundle\Message\UpdateSubscribers;
1012
use Alchemy\NotifyBundle\Service\NovuClient;
1113
use Psr\Log\LoggerAwareInterface;
@@ -73,12 +75,16 @@ public function notifyTopic(
7375
if ($this->notifyAuthor) {
7476
$authorId = null;
7577
}
76-
$this->novuClient->notifyTopic($topicKey, $authorId, $notificationId, $parameters, $options);
78+
$this->bus->dispatch(new NotifyTopic($topicKey, $authorId, $notificationId, $parameters, $options));
7779
}
7880

79-
public function addTopicSubscribers(string $topicKey, array $subscribers): void
81+
public function addTopicSubscribers(string $topicKey, array $subscribers, bool $direct = false): void
8082
{
81-
$this->bus->dispatch(new AddTopicSubscribers($topicKey, $subscribers));
83+
if ($direct) {
84+
$this->novuClient->addTopicSubscribers($topicKey, $subscribers);
85+
} else {
86+
$this->bus->dispatch(new AddTopicSubscribers($topicKey, $subscribers));
87+
}
8288
$this->bus->dispatch(new UpdateSubscribers($subscribers));
8389
}
8490

@@ -89,7 +95,7 @@ public function createTopic(string $topicKey): void
8995

9096
public function removeTopicSubscribers(string $topicKey, array $subscribers): void
9197
{
92-
$this->novuClient->removeTopicSubscribers($topicKey, $subscribers);
98+
$this->bus->dispatch(new RemoveTopicSubscribers($topicKey, $subscribers));
9399
}
94100

95101
public function getTopicSubscriptions(array $topicKeys, string $userId): array

0 commit comments

Comments
 (0)