Andrea Gandino

Commenting via Bluesky

How I've added comments fetched from the Bluesky API to my blog.

Published on Social, Laravel

I love that Bluesky is getting traction. I've abruptly phased out usage on the bird app, and started posting on my account on the new platform.

Things that I'm loving so far:

  1. You can customize your handle, by simply adding an entry to your domain DNS. Way better than any blue mark, for verification.
  2. The general vibes: people are genuinely happy to connect (starter packs, by the way, great idea!), and keep the place clean (moderation lists are also super cool), although I fear the moment when bots will start swarming in.
  3. The fact that you can use their API and do stuff with your content: so refreshing!

This last point is particularly interest, since you can use the API to embed threaded comments anywhere you want, by just specifying which Bluesky post you want to keep track of.

Leveraging the power of caching helpers in Laravel, I was able to quickly spin up a method to retrieve all the first-level responses to a given thread, cache the response for a configurable amount of time, and render whatever the result may be to HTML, below a post.

Here's what I'm using:

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

$comments = Cache::remember( 'bsky-comments-' . $url, config( 'website.bsky.commentsCache', 60 * 5 ), function () use ( $url ) {
	$atEndpoint = sprintf(
		'at://%s/app.bsky.feed.post/%s',
		str_replace( '@', '', config( 'website.bsky.handle' ) ),
		last( explode( '/', $url ) )
	);

	$response = Http::withHeaders( [
		'Accept' => 'application/json',
	] )->get( 'https://api.bsky.app/xrpc/app.bsky.feed.getPostThread?uri=' . $atEndpoint );

	$comments = $response->json();
	usort( $comments[ 'thread' ][ 'replies' ], function ( $a, $b ) {
		return strtotime( $a[ 'post' ][ 'record' ][ 'createdAt' ] ) > strtotime( $b[ 'post' ][ 'record' ][ 'createdAt' ] );
	} );

	return $comments;
} );

Comments are enabled for this very blog post, so head over to Bluesky and say "Hi", if you like!