Skip to content

Commit

Permalink
TextBuilder: Make the second parameter optional
Browse files Browse the repository at this point in the history
  • Loading branch information
puklipo committed Nov 25, 2024
1 parent ddf1cee commit 107ab14
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ use Revolution\Bluesky\RichText\TextBuilder;

Route::get('text-builder', function () {
$post = Post::build(function (TextBuilder $builder) {
$builder->text(text: 'Hello Bluesky')
$builder->text('Hello Bluesky')
->newLine(count: 2)
->link(text: 'https://bsky.app/', uri: 'https://bsky.app/')
->link('https://bsky.app/')
->newLine()
->tag(text: '#Bluesky', tag: 'Bluesky');
->tag('#Bluesky');
});

$response = Bluesky::login(identifier: config('bluesky.identifier'), password: config('bluesky.password'))
Expand Down
14 changes: 7 additions & 7 deletions docs/basic-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ use Revolution\Bluesky\RichText\TextBuilder;

$builder = TextBuilder::make(text: 'test')
->newLine()
->link(text: 'https://', uri: 'https://')
->link('https://')
->newLine()
->tag(text: '#Laravel', tag: 'Laravel');
->tag('#Laravel');

$post = Post::create(text: $builder->text, facets: $builder->facets);

Expand All @@ -157,9 +157,9 @@ use Revolution\Bluesky\RichText\TextBuilder;

$post = TextBuilder::make(text: 'test')
->newLine()
->link(text: 'https://', uri: 'https://')
->link('https://')
->newLine()
->tag(text: '#Laravel', tag: 'Laravel')
->tag('#Laravel')
->toPost();

/** @var \Illuminate\Http\Client\Response $response */
Expand All @@ -175,11 +175,11 @@ use Revolution\Bluesky\Record\Post;
use Revolution\Bluesky\RichText\TextBuilder;

$post = Post::build(function (TextBuilder $builder) {
$builder->text(text: 'test')
$builder->text('test')
->newLine()
->link(text: 'https://', uri: 'https://')
->link('https://')
->newLine()
->tag(text: '#Laravel', tag: 'Laravel')
->tag('#Laravel')
});
```

Expand Down
8 changes: 4 additions & 4 deletions docs/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class TestNotification extends Notification
$external = External::create(title: 'Title', description: 'test', uri: 'https://');

return Post::build(function (TextBuilder $builder) {
$builder->text(text: 'test')
$builder->text('test')
->newLine()
->tag(text: '#Laravel', tag: 'Laravel');
->tag('#Laravel');
})->embed($external);
}
}
Expand Down Expand Up @@ -68,9 +68,9 @@ class TestNotification extends Notification
$quote = QuoteRecord::create(StrongRef::to(uri: 'at://', cid: 'cid'));

return BlueskyPrivateMessage::build(function (TextBuilder $builder) {
$builder->text(text: 'test')
$builder->text('test')
->newLine()
->tag(text: '#Laravel', tag: 'Laravel');
->tag('#Laravel');
})->embed($quote);
}
}
Expand Down
68 changes: 57 additions & 11 deletions src/RichText/TextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Revolution\AtProto\Lexicon\Attributes\Format;
use Revolution\AtProto\Lexicon\Attributes\Ref;
use Revolution\AtProto\Lexicon\Enum\Facet;
use Revolution\Bluesky\Facades\Bluesky;
use Revolution\Bluesky\Record\Post;

final class TextBuilder implements Arrayable
Expand Down Expand Up @@ -83,18 +84,35 @@ public function newLine(int $count = 1): self

/**
* Add mention facets.
*
* ```
* $builder->mention(text: '@***.bsky.social', did: 'did:plc:***');
* ```
* If did is not passed, did will be automatically resolved from handle.
* ```
* $builder->mention('@***.bsky.social');
* ```
*/
public function mention(string $text, #[Format('did')] string $did): self
public function mention(string $text, #[Format('did')] ?string $did = null): self
{
$this->facets[] = [
'index' => $this->buildFacetIndex($text),
'features' => [
[
'$type' => Facet::Mention->value,
'did' => $did,
$text = Str::rtrim($text);

if (is_null($did) && Str::startsWith($text, '@')) {
$handle = Str::of($text)->after('@')->trim()->toString();
$did = Bluesky::resolveHandle($handle)->json('did');
}

if (filled($did)) {
$this->facets[] = [
'index' => $this->buildFacetIndex($text),
'features' => [
[
'$type' => Facet::Mention->value,
'did' => $did,
],
],
],
];
];
}

$this->text .= $text;

Expand All @@ -103,9 +121,23 @@ public function mention(string $text, #[Format('did')] string $did): self

/**
* Add link facets.
*
* ```
* $builder->link(text: 'https://example.com', uri: 'https://example.com');
* ```
* If uri is not passed, input text will be used as the uri.
* ```
* $builder->link('https://example.com');
* ```
*/
public function link(string $text, string $uri): self
public function link(string $text, ?string $uri = null): self
{
$text = Str::rtrim($text);

if (is_null($uri)) {
$uri = $text;
}

$this->facets[] = [
'index' => $this->buildFacetIndex($text),
'features' => [
Expand All @@ -123,9 +155,23 @@ public function link(string $text, string $uri): self

/**
* Add tag facets.
*
* ```
* $builder->tag(text: '#alice', tag: 'alice');
* ```
* If tag is not passed, tag will be automatically set from input text.
* ```
* $builder->tag('#alice');
* ```
*/
public function tag(string $text, string $tag): self
public function tag(string $text, ?string $tag = null): self
{
$text = Str::rtrim($text);

if (is_null($tag) && Str::startsWith($text, '#')) {
$tag = Str::of($text)->after('#')->trim()->toString();
}

$this->facets[] = [
'index' => $this->buildFacetIndex($text),
'features' => [
Expand Down
40 changes: 40 additions & 0 deletions tests/Feature/RichText/TextBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

class TextBuilderTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

Http::preventStrayRequests();
}

public function test_detect_facets_mention()
{
Http::fakeSequence()
Expand Down Expand Up @@ -104,4 +111,37 @@ public function test_detect_facets_post_build()
$this->assertSame('https://localhost', collect($facets)->dot()->get('1.features.0.uri'));
$this->assertSame('alice', collect($facets)->dot()->get('2.features.0.tag'));
}

public function test_resolve_mention()
{
Http::fakeSequence()
->push(['did' => 'did:plc:alice']);

$builder = TextBuilder::make('test')
->mention('@alice.test ');

$this->assertSame(4, data_get($builder->facets, '0.index.byteStart'));
$this->assertSame(15, data_get($builder->facets, '0.index.byteEnd'));
$this->assertSame('did:plc:alice', collect($builder->facets)->dot()->get('0.features.0.did'));
}

public function test_resolve_link()
{
$builder = TextBuilder::make('test')
->link('https://localhost ');

$this->assertSame(4, data_get($builder->facets, '0.index.byteStart'));
$this->assertSame(21, data_get($builder->facets, '0.index.byteEnd'));
$this->assertSame('https://localhost', collect($builder->facets)->dot()->get('0.features.0.uri'));
}

public function test_resolve_tag()
{
$builder = TextBuilder::make('test')
->tag('#alice ');

$this->assertSame(4, data_get($builder->facets, '0.index.byteStart'));
$this->assertSame(10, data_get($builder->facets, '0.index.byteEnd'));
$this->assertSame('alice', collect($builder->facets)->dot()->get('0.features.0.tag'));
}
}

0 comments on commit 107ab14

Please sign in to comment.