8000 [Notifier] [Bluesky] Allow to attach website preview card · symfony/symfony@de0c847 · GitHub
[go: up one dir, main page]

Skip to content

Commit de0c847

Browse files
committed
[Notifier] [Bluesky] Allow to attach website preview card
1 parent eff9b52 commit de0c847

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

src/Symfony/Component/Notifier/Bridge/Bluesky/BlueskyOptions.php

+12
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,16 @@ public function attachMedia(File $file, string $description = ''): static
4343

4444
return $this;
4545
}
46+
47+
public function attachCard(string $uri, File $thumb, string $title = '', string $description = ''): static
48+
{
49+
$this->options['external'] = [
50+
'uri' => $uri,
51+
'thumb' => $thumb,
52+
'title' => $title,
53+
'description' => $description,
54+
];
55+
56+
return $this;
57+
}
4658
}

src/Symfony/Component/Notifier/Bridge/Bluesky/BlueskyTransport.php

+23
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ protected function doSend(MessageInterface $message): SentMessage
9191
unset($options['attach']);
9292
}
9393

94+
if(isset($options['external'])) {
95+
96+
$uploadedMedia = $this->uploadMedia(
97+
[
98+
[
99+
'file' => $options['external']['thumb'],
100+
'description' => $options['external']['description']
101+
]
102+
]
103+
);
104+
105+
$options['record']['embed'] = [
106+
'$type' => 'app.bsky.embed.external',
107+
'external' => [
108+
'uri' => $options['external']['uri'],
109+
'title' => $options['external']['title'],
110+
'description' => $options['external']['description'],
111+
'thumb' => current($uploadedMedia)['image'],
112+
]
113+
];
114+
unset($options['external']);
115+
}
116+
94117
$response = $this->client->request('POST', \sprintf('https://%s/xrpc/com.atproto.repo.createRecord', $this->getEndpoint()), [
95118
'auth_bearer' => $this->authSession['accessJwt'] ?? null,
96119
'json' => $options,

src/Symfony/Component/Notifier/Bridge/Bluesky/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add option to attach a website preview card
8+
49
7.2
510
---
611

src/Symfony/Component/Notifier/Bridge/Bluesky/Tests/BlueskyTransportTest.php

+36-7
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,12 @@ public function testParseFacetsUrlWithTrickyRegex()
274274
$this->assertEquals($expected, $this->parseFacets($input));
275275
}
276276

277-
public function testWithMedia()
277+
/**
278+
* @dataProvider sendMessageWithEmbedDataProvider
279+
*/
280+
public function testWithEmbed(BlueskyOptions $blueskyOptions, string $expectedJsonResponse)
278281
{
279-
$transport = $this->createTransport(new MockHttpClient((function () {
282+
$transport = $this->createTransport(new MockHttpClient((function () use ($expectedJsonResponse) {
280283
yield function (string $method, string $url, array $options) {
281284
$this->assertSame('POST', $method);
282285
$this->assertSame('https://bsky.social/xrpc/com.atproto.server.createSession', $url);
@@ -299,23 +302,49 @@ public function testWithMedia()
299302
]]);
300303
};
301304

302-
yield function (string $method, string $url, array $options) {
305+
yield function (string $method, string $url, array $options) use ($expectedJsonResponse) {
303306
$this->assertSame('POST', $method);
304307
$this->assertSame('https://bsky.social/xrpc/com.atproto.repo.createRecord', $url);
305308
$this->assertArrayHasKey('authorization', $options['normalized_headers']);
306-
$this->assertSame('{"repo":null,"collection":"app.bsky.feed.post","record":{"$type":"app.bsky.feed.post","text":"Hello World!","createdAt":"2024-04-28T08:40:17.000000Z","embed":{"$type":"app.bsky.embed.images","images":[{"alt":"A fixture","image":{"$type":"blob","ref":{"$link":"bafkreibabalobzn6cd366ukcsjycp4yymjymgfxcv6xczmlgpemzkz3cfa"},"mimeType":"image\/png","size":760898}}]}}}', $options['body']);
309+
$this->assertSame($expectedJsonResponse, $options['body']);
307310

308311
return new JsonMockResponse(['cid' => '103254962155278888']);
309312
};
310313
})()));
311314

312-
$options = (new BlueskyOptions())
313-
->attachMedia(new File(__DIR__.'/fixtures.gif'), 'A fixture');
314-
$result = $transport->send(new ChatMessage('Hello World!', $options));
315+
$result = $transport->send(new ChatMessage('Hello World!', $blueskyOptions));
315316

316317
$this->assertSame('103254962155278888', $result->getMessageId());
317318
}
318319

320+
public function sendMessageWithEmbedDataProvider(): iterable
321+
{
322+
yield "With media" => [
323+
'options' => (new BlueskyOptions())->attachMedia(new File(__DIR__.'/fixtures.gif'), 'A fixture'),
324+
'expectedResponse' => '{"repo":null,"collection":"app.bsky.feed.post","record":{"$type":"app.bsky.feed.post","text":"Hello World!","createdAt":"2024-04-28T08:40:17.000000Z","embed":{"$type":"app.bsky.embed.images","images":[{"alt":"A fixture","image":{"$type":"blob","ref":{"$link":"bafkreibabalobzn6cd366ukcsjycp4yymjymgfxcv6xczmlgpemzkz3cfa"},"mimeType":"image\/png","size":760898}}]}}}'
325+
];
326+
327+
yield "With website preview card and all optionnal informations" => [
328+
'options' => (new BlueskyOptions())
329+
->attachCard(
330+
'https://example.com',
331+
new File(__DIR__.'/fixtures.gif'),
332+
'Fork me im famous',
333+
'Click here to go to website!'
334+
),
335+
'expectedResponse' => '{"repo":null,"collection":"app.bsky.feed.post","record":{"$type":"app.bsky.feed.post","text":"Hello World!","createdAt":"2024-04-28T08:40:17.000000Z","embed":{"$type":"app.bsky.embed.external","external":{"uri":"https:\/\/example.com","title":"Fork me im famous","description":"Click here to go to website!","thumb":{"$type":"blob","ref":{"$link":"bafkreibabalobzn6cd366ukcsjycp4yymjymgfxcv6xczmlgpemzkz3cfa"},"mimeType":"image\/png","size":760898}}}}}'
336+
];
337+
338+
yield "With website preview card and minimal information" => [
339+
'options' => (new BlueskyOptions())
340+
->attachCard(
341+
'https://example.com',
342+
new File(__DIR__.'/fixtures.gif')
343+
),
344+
'expectedResponse' => '{"repo":null,"collection":"app.bsky.feed.post","record":{"$type":"app.bsky.feed.post","text":"Hello World!","createdAt":"2024-04-28T08:40:17.000000Z","embed":{"$type":"app.bsky.embed.external","external":{"uri":"https:\/\/example.com","title":"","description":"","thumb":{"$type":"blob","ref":{"$link":"bafkreibabalobzn6cd366ukcsjycp4yymjymgfxcv6xczmlgpemzkz3cfa"},"mimeType":"image\/png","size":760898}}}}}'
345+
];
346+
}
347+
319348
/**
320349
* A small helper function to test BlueskyTransport::parseFacets().
321350
*/

0 commit comments

Comments
 (0)
0