Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
Cache unresolvable handles and tighten offset types
Paulo Castellano5227cde5c729
diff --git a/app/Services/Social/BlueskyPublisher.php b/app/Services/Social/BlueskyPublisher.phpindex dda56f9af..b44060ab0 100644--- a/app/Services/Social/BlueskyPublisher.php+++ b/app/Services/Social/BlueskyPublisher.php@@ -181,7 +181,7 @@ class BlueskyPublisherforeach ($urlMatches[0] as $match) {$url = $match[0];- $start = $this->getUtf8ByteOffset($text, $match[1]);+ $start = $this->getUtf8ByteOffset($text, (int) $match[1]);$end = $start + strlen($url);$facets[] = [@@ -212,12 +212,16 @@ class BlueskyPublisher$handle = substr($mention, 1); // Remove @// A mention facet needs the target's DID, not the handle; skip it if unresolvable.- $did = $didCache[$handle] ?? ($didCache[$handle] = $this->resolveHandleToDid($handle, $service, $account));+ // Cache by key (not ??) so an unresolvable handle is resolved once, not per occurrence.+ if (! array_key_exists($handle, $didCache)) {+ $didCache[$handle] = $this->resolveHandleToDid($handle, $service, $account);+ }+ $did = $didCache[$handle];if ($did === null) {continue;}- $start = $this->getUtf8ByteOffset($text, $match[1]);+ $start = $this->getUtf8ByteOffset($text, (int) $match[1]);$end = $start + strlen($mention);$facets[] = [@@ -245,7 +249,7 @@ class BlueskyPublisherforeach ($hashtagMatches[0] as $match) {$hashtag = $match[0];$tag = substr($hashtag, 1); // Remove #- $start = $this->getUtf8ByteOffset($text, $match[1]);+ $start = $this->getUtf8ByteOffset($text, (int) $match[1]);$end = $start + strlen($hashtag);$facets[] = [@@ -269,8 +273,8 @@ class BlueskyPublisher* Resolve a Bluesky handle to its DID via com.atproto.identity.resolveHandle.** Tries the account's own PDS first (authenticated), then falls back to the- * public AppView and the bsky.social entryway. Returns null on failure so the- * caller can skip the mention facet instead of sending an invalid record.+ * public AppView and the configured default service. Returns null on failure+ * so the caller can skip the mention facet instead of sending an invalid record.*/private function resolveHandleToDid(string $handle, ?string $service = null, ?SocialAccount $account = null): ?string{diff --git a/tests/Feature/Services/Social/BlueskyPublisherTest.php b/tests/Feature/Services/Social/BlueskyPublisherTest.phpindex d7a296e5e..db740e743 100644--- a/tests/Feature/Services/Social/BlueskyPublisherTest.php+++ b/tests/Feature/Services/Social/BlueskyPublisherTest.php@@ -243,6 +243,39 @@ test('bluesky publisher resolves some mentions and skips the unresolvable ones',});});+test('bluesky publisher resolves a repeated handle only once', function () {+ $this->post->update(['content' => 'thanks @dup.bsky.social, really @dup.bsky.social']);++ Http::fake([+ '*/xrpc/com.atproto.identity.resolveHandle*' => Http::response(['did' => 'did:plc:dup789'], 200),+ config('trypost.platforms.bluesky.default_service').'/xrpc/com.atproto.repo.createRecord' => Http::response([+ 'uri' => 'at://did:plc:testuser123/app.bsky.feed.post/3abc123xyz',+ 'cid' => 'bafyreiabc123',+ ], 200),+ ]);++ $this->publisher->publish($this->postPlatform);++ // The same handle appears twice but the per-post cache resolves it once.+ $resolveCalls = Http::recorded(fn ($request) => str_contains($request->url(), 'resolveHandle'))->count();+ expect($resolveCalls)->toBe(1);++ // Both occurrences still become mention facets carrying the DID.+ Http::assertSent(function ($request) {+ $record = $request->data()['record'] ?? null;++ if (! $record) {+ return false;+ }++ $mentions = collect($record['facets'] ?? [])+ ->filter(fn ($facet) => $facet['features'][0]['$type'] === 'app.bsky.richtext.facet#mention');++ return $mentions->count() === 2+ && $mentions->every(fn ($facet) => $facet['features'][0]['did'] === 'did:plc:dup789');+ });+});+test('bluesky publisher uploads images', function () {// Create a media item through the PostPlatform's media() relation$this->post->update([