Skip to content

Commit

Permalink
refactor(facets): do not expose resolveFacets from BlueskyPost
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi committed Feb 10, 2024
1 parent 92449f1 commit cfba893
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
5 changes: 4 additions & 1 deletion src/BlueskyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use NotificationChannels\Bluesky\Exceptions\CouldNotCreateSession;
use NotificationChannels\Bluesky\Exceptions\CouldNotRefreshSession;
use NotificationChannels\Bluesky\Exceptions\CouldNotResolveHandle;
use NotificationChannels\Bluesky\RichText\Facets\Facet;

final class BlueskyClient
{
Expand Down Expand Up @@ -90,7 +91,9 @@ public function createPost(BlueskyIdentity $identity, BlueskyPost|string $post):
'collection' => 'app.bsky.feed.post',
'record' => [
'createdAt' => now()->toIso8601ZuluString(),
...$post->resolveFacets($this)->toArray(),
...$post
->facets(facets: Facet::resolveFacets($post->text, $this))
->toArray(),
],
]);

Expand Down
26 changes: 20 additions & 6 deletions src/BlueskyPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class BlueskyPost
{
private function __construct(
public string $text = '',
public readonly array $facets = [],
public array $facets = [],
) {
}

Expand All @@ -28,12 +28,26 @@ public static function make(): static
return new static();
}

public function resolveFacets(BlueskyClient $client): static
/**
* Adds a facet to the post. Note that most facets are resolved automatically.
*/
public function facet(Facet $facet): static
{
return new static(
text: $this->text,
facets: Facet::resolveFacets($this->text, $client),
);
$this->facets[] = $facet;

return $this;
}

/**
* Adds multiple facets to the post. Note that most facets are resolved automatically.
*
* @param Facet[] $facet
*/
public function facets(array $facets): static
{
$this->facets = array_merge($this->facets, $facets);

return $this;
}

/**
Expand Down
53 changes: 22 additions & 31 deletions tests/BlueskyPostTest.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,21 @@
<?php

use NotificationChannels\Bluesky\BlueskyClient;
use NotificationChannels\Bluesky\BlueskyPost;
use NotificationChannels\Bluesky\Tests\Factories\BlueskyClientResponseFactory;
use NotificationChannels\Bluesky\RichText\Facets\Facet;
use NotificationChannels\Bluesky\RichText\Facets\Features\Mention;

it('can be converted to an array', function () {
$post = BlueskyPost::make()->text('foo');

expect($post->toArray())->toBe([
'text' => 'foo',
'facets' => [],
]);
});

it('has an accessble `text` property', function () {
$post = BlueskyPost::make()->text('foo');

expect($post->text)->toBe('foo');
});

it('has an accessble `facets` property', function () {
$post = BlueskyPost::make()->text('foo');

expect($post->facets)->toBe([]);
});

it('can generate facets given the `BlueskyClient` instance', function () {
ray()->showHttpClientRequests();
BlueskyClientResponseFactory::fake();

$client = resolve(BlueskyClient::class);

$post = BlueskyPost::make()
->text('Hello @innocenzi.dev')
->resolveFacets($client);
->text('foo')
->facet(new Facet(
range: [6, 20],
features: [
new Mention('did:plc:sa57ykejomjswkuoktilt3sz'),
],
));

expect($post->toArray())->toBe([
'text' => 'Hello @innocenzi.dev',
'text' => 'foo',
'facets' => [
[
'$type' => 'app.bsky.richtext.facet',
Expand All @@ -54,3 +33,15 @@
],
]);
});

it('has an accessble `text` property', function () {
$post = BlueskyPost::make()->text('foo');

expect($post->text)->toBe('foo');
});

it('has an accessble `facets` property', function () {
$post = BlueskyPost::make()->text('foo');

expect($post->facets)->toBe([]);
});

0 comments on commit cfba893

Please sign in to comment.