Skip to content

Commit

Permalink
Passing BlueskyMessage directly
Browse files Browse the repository at this point in the history
  • Loading branch information
kawax committed Sep 7, 2024
1 parent 78f8c60 commit 0afc890
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
8 changes: 4 additions & 4 deletions docs/bluesky-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dump($response->json());
```

### TextBuilder
You can use `BlueskyMessage` class from [Laravel Notifications](./notification.md) as a text builder.
You can use `BlueskyMessage` class from [Notifications](./notification.md) as a text builder.

```php
use Revolution\Bluesky\Facades\Bluesky;
Expand All @@ -55,7 +55,7 @@ $message = BlueskyMessage::create(text: 'test')

/** @var \Illuminate\Http\Client\Response $response */
$response = Bluesky::login(identifier: config('bluesky.identifier'), password: config('bluesky.password'))
->post(text: $message->text, facets: $message->facets);
->post($message);

dump($response->json());
```
Expand All @@ -73,7 +73,7 @@ $message = BlueskyMessage::create(text: 'test')

/** @var \Illuminate\Http\Client\Response $response */
$response = Bluesky::login(identifier: config('bluesky.identifier'), password: config('bluesky.password'))
->post(text: $message->text, embed: $message->embed);
->post($message);

dump($response->json());
```
Expand All @@ -99,7 +99,7 @@ $message = BlueskyMessage::create(text: 'test')
->embed($images);

/** @var \Illuminate\Http\Client\Response $response */
$response = Bluesky::post(text: $message->text, embed: $message->embed);
$response = Bluesky::post($message);

dump($response->json());
```
Expand Down
14 changes: 7 additions & 7 deletions src/BlueskyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Support\Traits\Macroable;
use Revolution\Bluesky\Contracts\Factory;
use Revolution\Bluesky\Enums\AtProto;
use Revolution\Bluesky\Notifications\BlueskyMessage;

class BlueskyClient implements Factory
{
Expand Down Expand Up @@ -92,14 +93,13 @@ public function timeline(int $limit = 50, string $cursor = ''): Response
* Create new post.
* @throws ConnectionException
*/
public function post(string $text, ?array $facets = null, ?array $embed = null): Response
public function post(string|BlueskyMessage $text): Response
{
$record = collect([
'text' => $text,
'facets' => $facets,
'embed' => $embed,
'createdAt' => now()->toISOString(),
])->reject(fn ($item) => is_null($item))
$message = $text instanceof BlueskyMessage ? $text : BlueskyMessage::create($text);

$record = collect($message->toArray())
->put('createdAt', now()->toISOString())
->reject(fn ($item) => is_null($item))
->toArray();

return Http::baseUrl($this->baseUrl())
Expand Down
3 changes: 2 additions & 1 deletion src/Facades/Bluesky.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Facade;
use Revolution\Bluesky\Contracts\Factory;
use Revolution\Bluesky\Notifications\BlueskyMessage;

/**
* @method static static service(string $service)
Expand All @@ -16,7 +17,7 @@
* @method static bool check()
* @method static Response feed(int $limit = 50, string $cursor = '', string $filter = 'posts_with_replies')
* @method static Response timeline(int $limit = 50, string $cursor = '')
* @method static Response post(string $text, ?array $facets = null, ?array $embed = null)
* @method static Response post(string|BlueskyMessage $text)
* @method static Response uploadBlob(mixed $data, string $type = 'image/png')
* @method static void macro(string $name, object|callable $macro)
* @method static \Revolution\Bluesky\BlueskyClient|Response|mixed when(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null)
Expand Down
2 changes: 1 addition & 1 deletion src/Notifications/BlueskyChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function send(mixed $notifiable, Notification $notification): void

Bluesky::service($route->service)
->login($route->identifier, $route->password)
->post($message->text, $message->facets, $message->embed)
->post($message)
->throw();
}
}
17 changes: 16 additions & 1 deletion tests/Feature/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Http;
use Revolution\Bluesky\BlueskyClient;
use Revolution\Bluesky\Facades\Bluesky;
use Revolution\Bluesky\Notifications\BlueskyMessage;
use Tests\TestCase;

class ClientTest extends TestCase
Expand Down Expand Up @@ -94,7 +95,21 @@ public function test_post()
->push(['uri' => 'at']);

$response = Bluesky::login(identifier: 'identifier', password: 'password')
->post(text: 'test', facets: [], embed: []);
->post(text: 'test');

$this->assertTrue($response->collect()->has('uri'));
}

public function test_post_message()
{
Http::fakeSequence()
->push(['accessJwt' => 'test', 'did' => 'test'])
->push(['uri' => 'at']);

$m = BlueskyMessage::create('text');

$response = Bluesky::login(identifier: 'identifier', password: 'password')
->post(text: $m);

$this->assertTrue($response->collect()->has('uri'));
}
Expand Down

0 comments on commit 0afc890

Please sign in to comment.