-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support resolving website embeds
- Loading branch information
Showing
21 changed files
with
520 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace NotificationChannels\Bluesky; | ||
|
||
final class Blob | ||
{ | ||
public function __construct( | ||
public readonly array $blob, | ||
) { | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->blob; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace NotificationChannels\Bluesky\Embeds; | ||
|
||
use NotificationChannels\Bluesky\RichText\SerializesIntoPost; | ||
|
||
abstract class Embed | ||
{ | ||
use SerializesIntoPost; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace NotificationChannels\Bluesky\Embeds; | ||
|
||
use NotificationChannels\Bluesky\BlueskyPost; | ||
use NotificationChannels\Bluesky\BlueskyService; | ||
|
||
interface EmbedResolver | ||
{ | ||
/** | ||
* Resolves an embed from the given post. | ||
*/ | ||
public function resolve(BlueskyService $bluesky, BlueskyPost $post): ?Embed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace NotificationChannels\Bluesky\Embeds; | ||
|
||
final class External extends Embed | ||
{ | ||
public function __construct( | ||
public readonly string $uri, | ||
public readonly string $title, | ||
public readonly string $description, | ||
public readonly array $thumb, | ||
) { | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return 'app.bsky.embed.external'; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'$type' => $this->getType(), | ||
'external' => $this->serializeProperties(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace NotificationChannels\Bluesky\Embeds; | ||
|
||
use Illuminate\Support\Facades\Http; | ||
use NotificationChannels\Bluesky\BlueskyPost; | ||
use NotificationChannels\Bluesky\BlueskyService; | ||
use NotificationChannels\Bluesky\RichText\Facets\Facet; | ||
use NotificationChannels\Bluesky\RichText\Facets\FacetFeature; | ||
|
||
final class LinkEmbedResolverUsingCardyb implements EmbedResolver | ||
{ | ||
public function resolve(BlueskyService $bluesky, BlueskyPost $post): ?Embed | ||
{ | ||
if (\count($post->facets) === 0) { | ||
return null; | ||
} | ||
|
||
/** @var Facet */ | ||
$firstLink = collect($post->facets)->first( | ||
callback: fn (Facet $facet) => collect($facet->getFeatures())->first( | ||
callback: fn (FacetFeature $feature) => $feature->getType() === 'app.bsky.richtext.facet#link', | ||
), | ||
); | ||
|
||
if (!$firstLink) { | ||
return null; | ||
} | ||
|
||
$embed = Http::get('https://cardyb.bsky.app/v1/extract', [ | ||
'url' => $firstLink->getFeatures()[0]->uri, | ||
]); | ||
|
||
if ($embed->json('error')) { | ||
return null; | ||
} | ||
|
||
return new External( | ||
uri: $embed->json('url'), | ||
title: $embed->json('title'), | ||
description: $embed->json('description'), | ||
thumb: $bluesky | ||
->uploadBlob($embed->json('image')) | ||
->toArray(), | ||
); | ||
} | ||
} |
Oops, something went wrong.