Skip to content

Commit

Permalink
Merge branch 'tags' into post-builder
Browse files Browse the repository at this point in the history
Merged the 'tags' feature branch into 'post-builder', adding support for tags in the PostBuilder API.
  • Loading branch information
shahmal1yev committed Oct 13, 2024
2 parents 28a0544 + c2ef9b7 commit 7c0d52c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/Contracts/Lexicons/App/Bsky/Feed/PostBuilderContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public function reply(StrongRef $root, StrongRef $parent): PostBuilderContract;
public function langs(array $languages): PostBuilderContract;

public function labels(SelfLabels $labels): PostBuilderContract;

public function tags(array $tags): PostBuilderContract;
}
35 changes: 34 additions & 1 deletion src/Lexicons/App/Bsky/Feed/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Post implements PostBuilderContract
private ?array $reply = null;
private ?array $languages = null;
private ?SelfLabels $labels = null;
private ?array $tags = null;


public function __construct()
Expand Down Expand Up @@ -130,6 +131,37 @@ public function labels(SelfLabels $labels): PostBuilderContract
return $this;
}

/**
* @throws InvalidArgumentException
*/
public function tags(array $tags): PostBuilderContract
{
$maxLength = 8;
$maxLengthByTag = 640;

if (count($tags) > $maxLength) {
throw new InvalidArgumentException('A maximum of 8 tags is allowed.');
}

$invalid = array_filter($tags, function ($tag) {
if (mb_strlen($tag) > 640) {
return true;
}
});

if (! empty($invalid)) {
throw new InvalidArgumentException(sprintf(
"Invalid tags: %s. A tag maximum of %s characters is allowed.",
implode(', ', $invalid),
$maxLengthByTag
));
}

$this->tags = $tags;

return $this;
}

/**
* Validates the format of a language code.
*
Expand Down Expand Up @@ -175,7 +207,8 @@ public function jsonSerialize(): array
'embed' => $this->embed,
'replyRef' => $this->reply,
'langs' => $this->languages,
'labels' => $this->labels
'labels' => $this->labels,
'tags' => $this->tags,
]);
}

Expand Down
46 changes: 40 additions & 6 deletions tests/Unit/Lexicons/App/Bsky/Feed/PostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,41 @@ public function testLabelsMethod(): void
], $result['labels']);
}

public function testTagsThrowsExceptionWhenPassedTagExceedsAllowedLength(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Invalid tags: ");

$trigger = [str_pad('', 641, 'a')];
$this->post->tags($trigger);
}

public function testTagsMethodThrowsExceptionWhenPassedArrayExceedsAllowedLength(): void
{
$max = 8;

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("A maximum of $max tags is allowed.");

$trigger = str_split(str_pad('', ++$max, 'a'));

$this->post->tags($trigger);
}

/**
* @throws InvalidArgumentException
*/
public function testTags(): void
{
$tags = [str_pad('', 640, 'a')];
$this->post->tags($tags);

$result = json_decode($this->post, true);

$this->assertArrayHasKey('tags', $result);
$this->assertEquals($tags, $result['tags']);
}

/**
* @throws InvalidArgumentException
*/
Expand All @@ -241,15 +276,13 @@ public function testJsonSerialize()
$embed = $this->createMock(EmbedInterface::class);
$embed->method('jsonSerialize')->willReturn(['embedKey' => 'embedValue']);
$this->post->embed($embed);
$sRef = new StrongRef('foo', 'bar');
$this->post->reply($sRef, clone $sRef);
$this->post->reply($sRef = new StrongRef('foo', 'bar'), clone $sRef);
$this->post->langs(['en', 'fr', 'es']);
$this->post->labels(new SelfLabels([
'val 1',
'val 2'
]));
$this->post->labels(new SelfLabels(str_split(str_pad('', 2, 'val'))));
$this->post->tags(str_split(str_pad('', 2, 'tag')));

$result = $this->post->jsonSerialize();

$this->assertArrayHasKey('$type', $result);
$this->assertEquals('app.bsky.feed.post', $result['$type']);
$this->assertArrayHasKey('text', $result);
Expand All @@ -259,6 +292,7 @@ public function testJsonSerialize()
$this->assertArrayHasKey('replyRef', $result);
$this->assertArrayHasKey('langs', $result);
$this->assertArrayHasKey('labels', $result);
$this->assertArrayHasKey('tags', $result);
}

public function testConstructorWorksCorrectlyOnDirectBuild()
Expand Down

0 comments on commit 7c0d52c

Please sign in to comment.