refacto: use microcosm endpoint and change types
This commit is contained in:
@@ -7,7 +7,7 @@ import { markdownBuilder } from "@/hooks/useMarkdown.hook"
|
||||
import { computedAsync } from "@vueuse/core"
|
||||
import { getUrl } from "@/modules/atproto/getUrl"
|
||||
import { withATProtoImages } from "@/modules/atproto/withATProtoImages"
|
||||
import { getUniqueAka } from "@/modules/atproto/getAka"
|
||||
import { getAuthor } from "@/modules/atproto/getAuthor"
|
||||
import { PublicNoteRecord } from "@/modules/atproto/publicNote.types"
|
||||
import { parseAtUri } from "@/modules/atproto/parseAtUri"
|
||||
|
||||
@@ -24,7 +24,7 @@ const rkey = computed(() => atUriProps.value.rkey)
|
||||
|
||||
const index = computed(() => props.index)
|
||||
|
||||
const author = computedAsync(async () => getUniqueAka(did.value))
|
||||
const author = computedAsync(async () => getAuthor(did.value))
|
||||
const url = computedAsync(async () =>
|
||||
getUrl({ did: did.value, rkey: rkey.value }),
|
||||
)
|
||||
@@ -47,7 +47,7 @@ const content = computed(() =>
|
||||
noteRecord.value?.value.content && author.value
|
||||
? toHTML(
|
||||
withATProtoImages(noteRecord.value.value.content, {
|
||||
endpoint: author.value.endpoint,
|
||||
pds: author.value.pds,
|
||||
did: did.value,
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Author, getAka } from "@/modules/atproto/getAka"
|
||||
import { Author, getAuthors } from "@/modules/atproto/getAuthor"
|
||||
import { PublicNoteListItem } from "@/modules/note/models/Note"
|
||||
import { computedAsync } from "@vueuse/core"
|
||||
import { computed, ref, Ref } from "vue"
|
||||
@@ -28,16 +28,23 @@ export function usePublicNoteList(did?: Ref<string | undefined>) {
|
||||
isLoading.value = false
|
||||
}
|
||||
|
||||
const aka = computedAsync<Map<string, Author>>(async () => {
|
||||
const authors = computedAsync<Map<string, Author>>(async () => {
|
||||
if (notes.value.length === 0) {
|
||||
return new Map()
|
||||
}
|
||||
|
||||
return getAka(new Set(notes.value.map((n) => n.did)))
|
||||
return getAuthors(new Set(notes.value.map((n) => n.did)))
|
||||
}, new Map())
|
||||
|
||||
const getAlias = (did: string) =>
|
||||
aka.value.has(did) ? aka.value.get(did)?.alias : ""
|
||||
const getAuthor = (did: string) =>
|
||||
authors.value.has(did) ? authors.value.get(did)?.handle : ""
|
||||
|
||||
return { notes, isLoading, canLoadMore, onLoadMore, aka, getAlias }
|
||||
return {
|
||||
notes,
|
||||
isLoading,
|
||||
canLoadMore,
|
||||
onLoadMore,
|
||||
authors,
|
||||
getAuthor,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
79
src/modules/atproto/getAuthor.ts
Normal file
79
src/modules/atproto/getAuthor.ts
Normal 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)
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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 `})`
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import BackButton from "@/components/BackButton.vue"
|
||||
import { usePublicNoteList } from "@/hooks/usePublicNoteList.hook"
|
||||
import { getUniqueAka } from "@/modules/atproto/getAka"
|
||||
import { getAuthor } from "@/modules/atproto/getAuthor"
|
||||
import { computedAsync } from "@vueuse/core"
|
||||
import { computed } from "vue"
|
||||
import { vInfiniteScroll } from "@vueuse/components"
|
||||
@@ -11,14 +11,14 @@ const did = computed(() => props.did)
|
||||
|
||||
const { notes, isLoading, canLoadMore, onLoadMore } = usePublicNoteList(did)
|
||||
|
||||
const author = computedAsync(async () => getUniqueAka(did.value))
|
||||
const author = computedAsync(async () => getAuthor(did.value))
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main class="public-note-list-view">
|
||||
<div class="header">
|
||||
<back-button class="back-button" :fallback="{ name: 'Home' }" />
|
||||
<h1>{{ author?.alias ?? did }}</h1>
|
||||
<h1>{{ author?.handle ?? did }}</h1>
|
||||
</div>
|
||||
<div v-if="isLoading"></div>
|
||||
<div v-else>
|
||||
|
||||
@@ -3,7 +3,7 @@ import BackButton from "@/components/BackButton.vue"
|
||||
import { usePublicNoteList } from "@/hooks/usePublicNoteList.hook"
|
||||
import { vInfiniteScroll } from "@vueuse/components"
|
||||
|
||||
const { notes, isLoading, canLoadMore, onLoadMore, getAlias } =
|
||||
const { notes, isLoading, canLoadMore, onLoadMore, getAuthor } =
|
||||
usePublicNoteList()
|
||||
</script>
|
||||
|
||||
@@ -32,14 +32,14 @@ const { notes, isLoading, canLoadMore, onLoadMore, getAlias } =
|
||||
|
||||
<div class="text-xs opacity-80 alias">
|
||||
<router-link
|
||||
v-if="getAlias(note.did)"
|
||||
v-if="getAuthor(note.did)"
|
||||
:to="{
|
||||
name: 'PublicNoteListByDidView',
|
||||
params: { did: note.did },
|
||||
}"
|
||||
class="link link-hover"
|
||||
>
|
||||
{{ getAlias(note.did) }}
|
||||
{{ getAuthor(note.did) }}
|
||||
</router-link>
|
||||
<span v-if="note.publishedAt"
|
||||
> • {{
|
||||
|
||||
@@ -4,7 +4,7 @@ import { markdownBuilder } from "@/hooks/useMarkdown.hook"
|
||||
import BackButton from "@/components/BackButton.vue"
|
||||
import StackedPublicNote from "@/components/StackedPublicNote.vue"
|
||||
import { useRouteQueryStackedNotes } from "@/hooks/useRouteQueryStackedNotes.hook"
|
||||
import { getUniqueAka } from "@/modules/atproto/getAka"
|
||||
import { getAuthor } from "@/modules/atproto/getAuthor"
|
||||
import type { PublicNoteRecord } from "@/modules/atproto/publicNote.types"
|
||||
import { withATProtoImages } from "@/modules/atproto/withATProtoImages"
|
||||
import { getUrl } from "@/modules/atproto/getUrl"
|
||||
@@ -18,9 +18,10 @@ const props = defineProps<{ did: string; rkey: string }>()
|
||||
const did = computed(() => props.did)
|
||||
const rkey = computed(() => props.rkey)
|
||||
|
||||
const author = computedAsync(async () => getUniqueAka(did.value))
|
||||
const url = computedAsync(async () =>
|
||||
getUrl({ did: did.value, rkey: rkey.value }),
|
||||
const author = computedAsync(async () => getAuthor(did.value))
|
||||
const url = computedAsync(
|
||||
async () => getUrl({ did: did.value, rkey: rkey.value }),
|
||||
null,
|
||||
)
|
||||
|
||||
const noteRecord = computedAsync(async () =>
|
||||
@@ -50,7 +51,7 @@ const content = computed(() =>
|
||||
noteRecord.value?.value.content && author.value
|
||||
? toHTML(
|
||||
withATProtoImages(noteRecord.value.value.content, {
|
||||
endpoint: author.value.endpoint,
|
||||
pds: author.value.pds,
|
||||
did: did.value,
|
||||
}),
|
||||
)
|
||||
@@ -95,7 +96,7 @@ watch(
|
||||
:to="{ name: 'PublicNoteListByDidView', params: { did: did } }"
|
||||
class="link link-hover"
|
||||
>
|
||||
{{ author.alias }}
|
||||
{{ author.handle }}
|
||||
</router-link>
|
||||
<span v-if="publishedAt"> • {{ publishedAt }}</span>
|
||||
</span>
|
||||
|
||||
Reference in New Issue
Block a user