import type { CommentListPage } from "#lib/api/comment.ts"; import type { BobbinContext } from "#lib/api/client.ts"; import { INVALID_HANDLE } from "#lib/api/identity.ts"; import { orNull } from "#lib/api/load.ts"; import type { Sidecar } from "#lib/api/enrich.ts"; import { rkeyFromUri } from "#lib/api/uri.ts"; import { buildCommentThreads, commentAuthorOf, commentModerationOf, listCommentItems, type CommentThread, type CommentView, type ThreadInput } from "#lib/components/comment/comments.ts"; import type { MarkupContext } from "#lib/markup/paths.ts"; import { renderMarkup } from "#lib/markup/render.ts"; export const toThreadInputs = async ( items: CommentListPage["items"], docs: Sidecar, markupCtx: MarkupContext, extra?: (uri: string) => Partial ): Promise => Promise.all( items.map(async (item): Promise => { const author = commentAuthorOf(docs, item); const body = item.body?.text ?? ""; return { comment: { uri: item.uri, cid: item.cid, rkey: rkeyFromUri(item.uri), authorDid: author.did, authorHandle: author.handle ?? INVALID_HANDLE, moderation: commentModerationOf(docs, item), createdAt: item.createdAt, body, bodyHtml: body ? await orNull(renderMarkup(body, markupCtx)) : null, ...extra?.(item.uri) }, replyTo: item.replyTo?.uri ?? null, replyToCid: item.replyTo?.cid }; }) ); export const loadCommentThreads = async ( ctx: BobbinContext, subjectUri: string, markupCtx: MarkupContext, extra?: (uri: string) => Partial ): Promise => { const { items, docs } = await listCommentItems(ctx, subjectUri); return buildCommentThreads(await toThreadInputs(items, docs, markupCtx, extra)); };