Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Notifier] [Mercure] Add options #54961

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

Closed
wants to merge 21 commits into from
Closed
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Mercure/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add `content` option

5.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ final class MercureOptions implements MessageOptionsInterface
private ?string $id;
private ?string $type;
private ?int $retry;
private ?array $content;

/**
* @param string|string[]|null $topics
*/
public function __construct(string|array|null $topics = null, bool $private = false, ?string $id = null, ?string $type = null, ?int $retry = null)
public function __construct(string|array|null $topics = null, bool $private = false, ?string $id = null, ?string $type = null, ?int $retry = null, ?array $content = null)
{
$this->topics = null !== $topics ? (array) $topics : null;
$this->private = $private;
$this->id = $id;
$this->type = $type;
$this->retry = $retry;
$this->content = $content;
}

/**
Expand Down Expand Up @@ -64,6 +66,11 @@ public function getRetry(): ?int
return $this->retry;
}

public function getContent(): ?array
{
return $this->content;
}

public function toArray(): array
{
return [
Expand All @@ -72,6 +79,7 @@ public function toArray(): array
'id' => $this->id,
'type' => $this->type,
'retry' => $this->retry,
'content' => $this->content,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ protected function doSend(MessageInterface $message): SentMessage
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Announce',
'summary' => $message->getSubject(),
'mediaType' => 'application/json',
'content' => $options->getContent(),
Copy link
Contributor

@chapterjason chapterjason May 29, 2024

Choose a reason for hiding this comment

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

To make it fit correctly it should be serialized to json here. (Only the value of the content entry)

For now I think it is enough to just inject the Serializer and always encode to json. To keep this PR simple.

I would also add an test to cover this case.

Copy link
Member

Choose a reason for hiding this comment

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

@ernie76 What about this comment?

]), $options->isPrivate(), $options->getId(), $options->getType(), $options->getRetry());

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ public function testConstructWithDefaults()
'id' => null,
'type' => null,
'retry' => null,
'content' => null
]);
}

public function testConstructWithParameters()
{
$options = (new MercureOptions('/topic/1', true, 'id', 'type', 1));
$options = (new MercureOptions('/topic/1', true, 'id', 'type', 1, ['tag' => '1234', 'body' => 'TEST']));

$this->assertSame($options->toArray(), [
'topics' => ['/topic/1'],
'private' => true,
'id' => 'id',
'type' => 'type',
'retry' => 1,
'content' => ['tag' => '1234', 'body' => 'TEST'],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testSendWithMercureOptions()
{
$hub = new MockHub('https://foo.com/.well-known/mercure', new StaticTokenProvider('foo'), function (Update $update): string {
$this->assertSame(['/topic/1', '/topic/2'], $update->getTopics());
$this->assertSame('{"@context":"https:\/\/www.w3.org\/ns\/activitystreams","type":"Announce","summary":"subject"}', $update->getData());
$this->assertSame('{"@context":"https:\/\/www.w3.org\/ns\/activitystreams","type":"Announce","summary":"subject","mediaType":"application\/json","content":null}', $update->getData());
Copy link
Member

Choose a reason for hiding this comment

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

I think we should add a test where content is not null as this is the goal off this PR.

$this->assertSame('id', $update->getId());
$this->assertSame('type', $update->getType());
$this->assertSame(1, $update->getRetry());
Expand All @@ -130,7 +130,7 @@ public function testSendWithMercureOptionsButWithoutOptionTopic()
{
$hub = new MockHub('https://foo.com/.well-known/mercure', new StaticTokenProvider('foo'), function (Update $update): string {
$this->assertSame(['https://symfony.com/notifier'], $update->getTopics());
$this->assertSame('{"@context":"https:\/\/www.w3.org\/ns\/activitystreams","type":"Announce","summary":"subject"}', $update->getData());
$this->assertSame('{"@context":"https:\/\/www.w3.org\/ns\/activitystreams","type":"Announce","summary":"subject","mediaType":"application\/json","content":null}', $update->getData());
$this->assertSame('id', $update->getId());
$this->assertSame('type', $update->getType());
$this->assertSame(1, $update->getRetry());
Expand All @@ -146,7 +146,7 @@ public function testSendWithoutMercureOptions()
{
$hub = new MockHub('https://foo.com/.well-known/mercure', new StaticTokenProvider('foo'), function (Update $update): string {
$this->assertSame(['https://symfony.com/notifier'], $update->getTopics());
$this->assertSame('{"@context":"https:\/\/www.w3.org\/ns\/activitystreams","type":"Announce","summary":"subject"}', $update->getData());
$this->assertSame('{"@context":"https:\/\/www.w3.org\/ns\/activitystreams","type":"Announce","summary":"subject","mediaType":"application\/json","content":null}', $update->getData());
$this->assertFalse($update->isPrivate());

return 'id';
Expand Down