From 8e89d6d0c90f799aae264e4be605016bb6f03411 Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Thu, 28 May 2026 15:35:22 -0400 Subject: [PATCH] add associated refs to embed in bsky publish flow --- .../[leaflet_id]/publish/publishBskyPost.ts | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/app/(app)/[leaflet_id]/publish/publishBskyPost.ts b/app/(app)/[leaflet_id]/publish/publishBskyPost.ts index 2c5fdf4a..c26198e9 100644 --- a/app/(app)/[leaflet_id]/publish/publishBskyPost.ts +++ b/app/(app)/[leaflet_id]/publish/publishBskyPost.ts @@ -1,6 +1,7 @@ "use server"; import { + AppBskyEmbedExternal, AppBskyRichtextFacet, Agent as BskyAgent, UnicodeString, @@ -20,6 +21,12 @@ import { import { fetchAtprotoBlob } from "app/api/atproto_images/route"; import { maybeOffloadPagesToBlob } from "src/utils/offloadPagesToBlob"; +type StrongRef = { + $type: "com.atproto.repo.strongRef"; + uri: string; + cid: string; +}; + type PublishBskyResult = | { success: true } | { success: false; error: OAuthSessionError }; @@ -107,13 +114,25 @@ export async function publishPostToBsky(args: { .eq("document", documentUri) .maybeSingle(); - let associatedRefs: { uri: string; cid: string }[] = []; + let associatedRefs: StrongRef[] = []; for (let uri of [documentUri, docInPub?.publication]) { if (!uri) continue; let ref = await getRecordStrongRef(agent, uri); if (ref) associatedRefs.push(ref); } + // associatedRefs hangs off the external embed card alongside uri/title/etc. + // It isn't in the published @atproto/api types yet, so widen External here. + let external: AppBskyEmbedExternal.External & { + associatedRefs?: StrongRef[]; + } = { + uri: args.url, + title: args.title, + description: args.description, + thumb: blob.data.blob, + }; + if (associatedRefs.length > 0) external.associatedRefs = associatedRefs; + let bsky = new BskyAgent(credentialSession); let post = await bsky.app.bsky.feed.post.create( { @@ -126,14 +145,8 @@ export async function publishPostToBsky(args: { facets: args.facets, embed: { $type: "app.bsky.embed.external", - external: { - uri: args.url, - title: args.title, - description: args.description, - thumb: blob.data.blob, - }, + external, }, - ...(associatedRefs.length > 0 && { associatedRefs }), }, ); let record = args.document_record; @@ -166,7 +179,7 @@ export async function publishPostToBsky(args: { async function getRecordStrongRef( agent: AtpBaseClient, uri: string, -): Promise<{ uri: string; cid: string } | null> { +): Promise { try { let { host, collection, rkey } = new AtUri(uri); let { data } = await agent.com.atproto.repo.getRecord({ @@ -175,7 +188,11 @@ async function getRecordStrongRef( rkey, }); if (!data.cid) return null; - return { uri: data.uri, cid: data.cid }; + return { + $type: "com.atproto.repo.strongRef", + uri: data.uri, + cid: data.cid, + }; } catch { return null; } -- 2.51.2