(slug: MaybeRefOrGetter<string | null | undefined>)
| 10 | } |
| 11 | |
| 12 | export function useBlogPostBlueskyLink(slug: MaybeRefOrGetter<string | null | undefined>) { |
| 13 | const cachedFetch = useCachedFetch() |
| 14 | |
| 15 | const blogUrl = computed(() => { |
| 16 | const s = toValue(slug) |
| 17 | if (!s) return null |
| 18 | return `${NPMX_SITE}/blog/${s}` |
| 19 | }) |
| 20 | |
| 21 | return useAsyncData<BlogPostBlueskyLink | null>( |
| 22 | () => (blogUrl.value ? `blog-bsky-link:${blogUrl.value}` : 'blog-bsky-link:none'), |
| 23 | async () => { |
| 24 | const url = blogUrl.value |
| 25 | if (!url) return null |
| 26 | |
| 27 | const constellation = new Constellation(cachedFetch) |
| 28 | |
| 29 | try { |
| 30 | // Try embed.external.uri first (link card embeds) |
| 31 | const { data: embedBacklinks } = await constellation.getBackLinks( |
| 32 | url, |
| 33 | 'app.bsky.feed.post', |
| 34 | 'embed.external.uri', |
| 35 | 1, |
| 36 | undefined, |
| 37 | true, |
| 38 | [[NPMX_DID]], |
| 39 | BLOG_BACKLINK_TTL_IN_SECONDS, |
| 40 | ) |
| 41 | |
| 42 | const embedRecord = embedBacklinks.records[0] |
| 43 | if (embedRecord) { |
| 44 | return { |
| 45 | did: embedRecord.did, |
| 46 | rkey: embedRecord.rkey, |
| 47 | postUri: `at://${embedRecord.did}/app.bsky.feed.post/${embedRecord.rkey}`, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Try facets.features.uri (URLs in post text) |
| 52 | const { data: facetBacklinks } = await constellation.getBackLinks( |
| 53 | url, |
| 54 | 'app.bsky.feed.post', |
| 55 | 'facets[].features[app.bsky.richtext.facet#link].uri', |
| 56 | 1, |
| 57 | undefined, |
| 58 | true, |
| 59 | [[NPMX_DID]], |
| 60 | BLOG_BACKLINK_TTL_IN_SECONDS, |
| 61 | ) |
| 62 | |
| 63 | const facetRecord = facetBacklinks.records[0] |
| 64 | if (facetRecord) { |
| 65 | return { |
| 66 | did: facetRecord.did, |
| 67 | rkey: facetRecord.rkey, |
| 68 | postUri: `at://${facetRecord.did}/app.bsky.feed.post/${facetRecord.rkey}`, |
| 69 | } |
nothing calls this directly
no test coverage detected