import { bsky } from "./bsky.ts"; type threadPost = { authorHandle: string; message: string; uri: string; authorDID: string; postedDateTime: string; bookmarks: number; replies: number; reposts: number; likes: number; quotes: number; }; export const getCleanThread = async (uri: string): Promise => { const res = await bsky.getPostThread({ uri: uri }); const { thread } = res.data; const postsThread: threadPost[] = []; // Type guard to check if thread is a ThreadViewPost if (thread && "post" in thread) { postsThread.push({ authorHandle: `@${thread.post.author.handle}`, message: (thread.post.record as { text: string }).text, uri: thread.post.uri, authorDID: thread.post.author.did, postedDateTime: (thread.post.record as { createdAt: string }).createdAt, bookmarks: thread.post.bookmarkCount ?? 0, replies: thread.post.replyCount ?? 0, reposts: thread.post.repostCount ?? 0, likes: thread.post.likeCount ?? 0, quotes: thread.post.quoteCount ?? 0, }); // Now traverse the parent chain if ("parent" in thread) { let current = thread.parent; while (current && "post" in current) { postsThread.push({ authorHandle: `@${current.post.author.handle}`, message: (current.post.record as { text: string }).text, uri: current.post.uri, authorDID: current.post.author.did, postedDateTime: (current.post.record as { createdAt: string }).createdAt, bookmarks: current.post.bookmarkCount ?? 0, replies: current.post.replyCount ?? 0, reposts: current.post.repostCount ?? 0, likes: current.post.likeCount ?? 0, quotes: current.post.quoteCount ?? 0, }); current = "parent" in current ? current.parent : undefined; } postsThread.reverse(); } } return postsThread; };