refacto: use microcosm endpoint and change types

This commit is contained in:
Julien Calixte
2026-02-17 09:26:26 +01:00
parent 8c7503abac
commit 103b23884f
11 changed files with 171 additions and 74 deletions

View File

@@ -1,47 +0,0 @@
export type Author = { alias: string; endpoint: string }
const correspondanceCache = new Map<string, Author>()
export const getUniqueAka = async (did: string): Promise<Author> => {
if (correspondanceCache.has(did)) {
return correspondanceCache.get(did) as Author
}
const response = await fetch(`https://plc.directory/${did}`)
const {
alsoKnownAs: [aka],
service: [{ serviceEndpoint }],
} = await response.json()
const alias = aka.replace("at://", "")
const author = { alias, endpoint: serviceEndpoint }
correspondanceCache.set(did, author)
return author
}
export const getAka = async (dids: Set<string>) => {
const correspondance = await Promise.all(
[...dids].map(async (did) => {
if (correspondanceCache.has(did)) {
return [did, correspondanceCache.get(did)] as [string, Author]
}
const response = await fetch(`https://plc.directory/${did}`)
const {
alsoKnownAs: [aka],
service: [{ serviceEndpoint }],
} = await response.json()
const alias = aka.replace("at://", "")
const author = { alias, endpoint: serviceEndpoint }
correspondanceCache.set(did, author)
return [did, author] as [string, Author]
}),
)
return new Map(correspondance)
}

View File

@@ -0,0 +1,79 @@
import { createSchema, createFetch } from "@better-fetch/fetch"
import { type } from "arktype"
export type Author = { handle: string; pds: string }
const correspondanceCache = new Map<string, Author>()
const schema = createSchema(
{
"/xrpc/blue.microcosm.identity.resolveMiniDoc": {
output: type({
did: "string",
handle: "string",
pds: "string",
signing_key: "string",
}),
query: type({
identifier: "string",
}),
},
},
{ strict: true },
)
const microcosmSlingshot = createFetch({
baseURL: "https://slingshot.microcosm.blue",
// plugins: [logger()],
schema,
})
export const getAuthor = async (did: string): Promise<Author | null> => {
if (correspondanceCache.has(did)) {
return correspondanceCache.get(did) as Author
}
try {
const { data: author, error } = await microcosmSlingshot(
"/xrpc/blue.microcosm.identity.resolveMiniDoc",
{ query: { identifier: did } },
)
if (!author) {
return null
}
correspondanceCache.set(did, author)
return author
} catch (e) {
console.warn(e)
return null
}
}
export const getAuthors = async (dids: Set<string>) => {
const correspondance = await Promise.all(
[...dids].map(async (did) => {
if (correspondanceCache.has(did)) {
return [did, correspondanceCache.get(did)] as [string, Author | null]
}
const { data: author } = await microcosmSlingshot(
"/xrpc/blue.microcosm.identity.resolveMiniDoc",
{ query: { identifier: did } },
)
if (!author) {
return [did, null] as [string, Author | null]
}
correspondanceCache.set(did, author)
return [did, author] as [string, Author | null]
}),
)
return new Map(correspondance)
}

View File

@@ -1,3 +1,5 @@
import { getAuthor } from "@/modules/atproto/getAuthor"
const endpointCache = new Map<string, string>()
const getEndpoint = async (did: string) => {
@@ -15,10 +17,13 @@ const getEndpoint = async (did: string) => {
}
export const getUrl = async ({ did, rkey }: { did: string; rkey: string }) => {
const url = new URL(
"/xrpc/com.atproto.repo.getRecord",
await getEndpoint(did),
)
const author = await getAuthor(did)
if (!author) {
return null
}
const url = new URL("/xrpc/com.atproto.repo.getRecord", author.pds)
url.searchParams.set("repo", did)
url.searchParams.set("collection", "space.remanso.note")
url.searchParams.set("rkey", rkey)

View File

@@ -1,11 +1,11 @@
export const withATProtoImages = (
markdown: string,
{ endpoint, did }: { endpoint: string; did: string },
{ pds, did }: { pds: string; did: string },
): string => {
const imageLinkPattern = /!\[([^\]]*)\]\((bafkrei[a-z0-9]+)\)/g
return markdown.replace(imageLinkPattern, (_, altText, cid) => {
const imageUrl = new URL("/xrpc/com.atproto.sync.getBlob", endpoint)
const imageUrl = new URL("/xrpc/com.atproto.sync.getBlob", pds)
imageUrl.searchParams.set("did", did)
imageUrl.searchParams.set("cid", cid)
return `![${altText}](${imageUrl.toString()})`