forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseBlogPostBlueskyLink.ts
More file actions
82 lines (71 loc) · 2.41 KB
/
useBlogPostBlueskyLink.ts
File metadata and controls
82 lines (71 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Constellation } from '#shared/utils/constellation'
import { NPMX_SITE, NPMX_DID } from '#shared/utils/constants'
const BLOG_BACKLINK_TTL_IN_SECONDS = 60 * 5
export interface BlogPostBlueskyLink {
did: string
rkey: string
postUri: string
}
export function useBlogPostBlueskyLink(slug: MaybeRefOrGetter<string | null | undefined>) {
const cachedFetch = useCachedFetch()
const blogUrl = computed(() => {
const s = toValue(slug)
if (!s) return null
return `${NPMX_SITE}/blog/${s}`
})
return useAsyncData<BlogPostBlueskyLink | null>(
() => (blogUrl.value ? `blog-bsky-link:${blogUrl.value}` : 'blog-bsky-link:none'),
async () => {
const url = blogUrl.value
if (!url) return null
const constellation = new Constellation(cachedFetch)
try {
// Try embed.external.uri first (link card embeds)
const { data: embedBacklinks } = await constellation.getBackLinks(
url,
'app.bsky.feed.post',
'embed.external.uri',
1,
undefined,
true,
[[NPMX_DID]],
BLOG_BACKLINK_TTL_IN_SECONDS,
)
const embedRecord = embedBacklinks.records[0]
if (embedRecord) {
return {
did: embedRecord.did,
rkey: embedRecord.rkey,
postUri: `at://${embedRecord.did}/app.bsky.feed.post/${embedRecord.rkey}`,
}
}
// Try facets.features.uri (URLs in post text)
const { data: facetBacklinks } = await constellation.getBackLinks(
url,
'app.bsky.feed.post',
'facets[].features[app.bsky.richtext.facet#link].uri',
1,
undefined,
true,
[[NPMX_DID]],
BLOG_BACKLINK_TTL_IN_SECONDS,
)
const facetRecord = facetBacklinks.records[0]
if (facetRecord) {
return {
did: facetRecord.did,
rkey: facetRecord.rkey,
postUri: `at://${facetRecord.did}/app.bsky.feed.post/${facetRecord.rkey}`,
}
}
} catch (error: unknown) {
// TODO: Will need to remove this console error to satisfy linting scan
// Constellation unavailable or error - fail silently
// But during dev we will get an error
// oxlint-disable-next-line no-console
if (import.meta.dev) console.error('[Bluesky] Constellation error:', error)
}
return null
},
)
}