Skip to content

Commit

Permalink
Resolve the issue that external does not serialize by atproto/bsky specs
Browse files Browse the repository at this point in the history
- Add Client dependency to External constructor for future use
- Restructure jsonSerialize output to match required format
- Wrap external data in 'external' property according to spec
- Update unit tests to reflect new structure
- Fix array formatting and indentation in tests

This commit addresses an issue where external embeds weren't being
structured according to AT Protocol specifications, which requires
external data to be nested under an 'external' property.
  • Loading branch information
shahmal1yev committed Nov 11, 2024
1 parent 7d0f06f commit 58bc75d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
17 changes: 10 additions & 7 deletions src/Lexicons/App/Bsky/Embed/External.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Atproto\Lexicons\App\Bsky\Embed;

use Atproto\Client;
use Atproto\Contracts\Lexicons\App\Bsky\Embed\EmbedInterface;
use Atproto\Contracts\Lexicons\App\Bsky\Embed\MediaContract;
use Atproto\DataModel\Blob\Blob;
Expand All @@ -17,7 +18,7 @@ class External implements EmbedInterface, MediaContract
private string $description;
private ?Blob $blob = null;

public function __construct(string $uri, string $title, string $description)
public function __construct(Client $bsky, string $uri, string $title, string $description)
{
$this->uri($uri)
->title($title)
Expand Down Expand Up @@ -94,12 +95,14 @@ public function thumb(Blob $blob = null)

public function jsonSerialize(): array
{
return array_filter([
return [
'$type' => $this->nsid(),
'uri' => $this->uri,
'title' => $this->title,
'description' => $this->description,
'blob' => ($b = $this->blob) ? $b : null,
]);
'external' => array_filter([
'uri' => $this->uri,
'title' => $this->title,
'description' => $this->description,
'blob' => ($b = $this->blob) ? $b : null,
])
];
}
}
21 changes: 13 additions & 8 deletions tests/Unit/Lexicons/App/Bsky/Embed/ExternalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Unit\Lexicons\App\Bsky\Embed;

use Atproto\Client;
use Atproto\DataModel\Blob\Blob;
use Atproto\Exceptions\InvalidArgumentException;
use Atproto\Lexicons\App\Bsky\Embed\External;
Expand All @@ -17,7 +18,7 @@ class ExternalTest extends TestCase

public function setUp(): void
{
$this->external = new External('https://shahmal1yev.dev', 'foo', 'bar');
$this->external = new External($this->createMock(Client::class), 'https://shahmal1yev.dev', 'foo', 'bar');

$this->blob = $this->getMockBuilder(Blob::class)
->disableOriginalConstructor()
Expand Down Expand Up @@ -107,9 +108,11 @@ public function testJsonSerializeWithoutSetBlob(): void
{
$expected = [
'$type' => 'app.bsky.embed.external',
'uri' => 'https://shahmal1yev.dev',
'title' => 'foo',
'description' => 'bar',
'external' => [
'uri' => 'https://shahmal1yev.dev',
'title' => 'foo',
'description' => 'bar',
]
];

$this->assertSame($expected, json_decode($this->external, true));
Expand All @@ -124,10 +127,12 @@ public function testJsonSerializeWithSetBlob(): void

$expected = [
'$type' => 'app.bsky.embed.external',
'uri' => 'https://shahmal1yev.dev',
'title' => 'foo',
'description' => 'bar',
'blob' => $this->blob->jsonSerialize(),
'external' => [
'uri' => 'https://shahmal1yev.dev',
'title' => 'foo',
'description' => 'bar',
'blob' => $this->blob->jsonSerialize()
],
];

$this->assertSame($expected, json_decode($this->external, true));
Expand Down

0 comments on commit 58bc75d

Please sign in to comment.