Skip to content

Commit

Permalink
Add Images
Browse files Browse the repository at this point in the history
  • Loading branch information
kawax committed Sep 4, 2024
1 parent 31cee04 commit 8e421eb
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
38 changes: 32 additions & 6 deletions docs/bluesky-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ dump($response->json());
use Revolution\Bluesky\Facades\Bluesky;

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

dump($response);
dump($response->json());
```

### TextBuilder
Expand All @@ -54,10 +54,10 @@ $message = BlueskyMessage::create(text: 'test')
->tag(text: '#Laravel', tag: 'Laravel');

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

dump($response);
dump($response->json());
```

### Social Card
Expand All @@ -72,10 +72,36 @@ $message = BlueskyMessage::create(text: 'test')
->embed($external);

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

dump($response);
dump($response->json());
```

### Upload Images
You can upload up to 4 images at a time.

```php
use Illuminate\Support\Facades\Storage;
use Revolution\Bluesky\Facades\Bluesky;
use Revolution\Bluesky\Notifications\BlueskyMessage;
use Revolution\Bluesky\Embed\Images;

Bluesky::login(identifier: config('bluesky.identifier'), password: config('bluesky.password'));

$images = Images::create()
->add(alt: 'ALT TEXT', blob: function (): array {
return Bluesky::uploadBlob(Storage::get('test.png'), Storage::mimeType('test.png'))->json('blob');
})
->add(alt: 'image 2', blob: []);

$message = BlueskyMessage::create(text: 'test')
->embed($images);

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

dump($response->json());
```

## Login
Expand Down
38 changes: 38 additions & 0 deletions src/Embed/Images.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Revolution\Bluesky\Embed;

use Illuminate\Contracts\Support\Arrayable;
use Revolution\Bluesky\Enums\AtProto;

class Images implements Arrayable
{
private array $images = [];

public function __construct()
{
}

public static function create(): static
{
return new static();
}

public function add(string $alt, array|callable $blob): static
{
$this->images[] = [
'image' => is_callable($blob) ? call_user_func($blob) : $blob,
'alt' => $alt,
];

return $this;
}

public function toArray(): array
{
return [
'$type' => AtProto::Images->value,
'images' => collect($this->images)->take(4)->toArray(),
];
}
}
2 changes: 2 additions & 0 deletions src/Enums/AtProto.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ enum AtProto: string
{
case getAuthorFeed = 'app.bsky.feed.getAuthorFeed';
case getTimeline = 'app.bsky.feed.getTimeline';

case External = 'app.bsky.embed.external';
case Images = 'app.bsky.embed.images';

case createSession = 'com.atproto.server.createSession';
case createRecord = 'com.atproto.repo.createRecord';
Expand Down
17 changes: 17 additions & 0 deletions tests/Feature/Notifications/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Notification;
use Revolution\Bluesky\Embed\External;
use Revolution\Bluesky\Embed\Images;
use Revolution\Bluesky\Enums\AtProto;
use Revolution\Bluesky\Notifications\BlueskyChannel;
use Revolution\Bluesky\Notifications\BlueskyMessage;
Expand Down Expand Up @@ -122,6 +123,22 @@ public function test_message_embed_external()
$this->assertSame(AtProto::External->value, $m->embed['$type']);
}

public function test_message_embed_images()
{
$images = Images::create()
->add(alt: 'alt', blob: ['blob'])
->add('alt2', fn () => ['blob2']);

$m = BlueskyMessage::create(text: 'test')
->embed($images);

$this->assertIsArray($m->embed);
$this->assertSame('alt', $m->embed['images'][0]['alt']);
$this->assertSame('alt2', $m->embed['images'][1]['alt']);
$this->assertSame(['blob2'], $m->embed['images'][1]['image']);
$this->assertSame(AtProto::Images->value, $m->embed['$type']);
}

public function test_message_new_line()
{
$m = BlueskyMessage::create(text: 'test')
Expand Down

0 comments on commit 8e421eb

Please sign in to comment.