Something went wrong. Try again.
softfork of @juliet.paris parfait forked from juliet.paris/parfait
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187import type { AppBskyFeedDefs, AppBskyUnspeccedDefs, AppBskyUnspeccedGetPostThreadV2,} from "@atcute/bluesky";import { type Client, ok } from "@atcute/client";
import { noteHiddenReply } from "./hidden-replies";
export type ThreadViewPost = AppBskyFeedDefs.ThreadViewPost;
export const isPost = (node: unknown): node is ThreadViewPost => (node as { $type?: string } | undefined)?.$type === "app.bsky.feed.defs#threadViewPost";
export type Node = ThreadViewPost["parent"];
/** Why a post in the thread can't be shown, if it can't. */export const missing = (node: Node) => { if ((node as { signedInOnly?: boolean } | undefined)?.signedInOnly) return "Only for people signed in"; if (node?.$type === "app.bsky.feed.defs#blockedPost") return "Blocked post"; if (node?.$type === "app.bsky.feed.defs#notFoundPost") return "Deleted post";};
type FlatItem = AppBskyUnspeccedGetPostThreadV2.ThreadItem;type PostItem = AppBskyUnspeccedDefs.ThreadItemPost;
export const asPostItem = (item: { value: unknown }) => (item.value as { $type?: string }).$type === "app.bsky.unspecced.defs#threadItemPost" ? (item.value as PostItem) : undefined;
/** * What stands in for a post that can't be shown, in the shape the rest of the page reads: gone, * hidden by a block, or kept by its author for people signed in, seen as a guest or signed out. */const stub = (item: FlatItem): Node => { const type = (item.value as { $type?: string }).$type; return { $type: type === "app.bsky.unspecced.defs#threadItemNotFound" ? "app.bsky.feed.defs#notFoundPost" : "app.bsky.feed.defs#blockedPost", uri: item.uri, ...(type === "app.bsky.unspecced.defs#threadItemNoUnauthenticated" ? { signedInOnly: true } : {}), } as Node;};
/** * Bluesky's thread, which comes as a flat list (the posts above at negative depths, the post * itself at 0, its replies at 1), in the nested shape the page reads. Replies keep Bluesky's * order, its "top" ranking, which puts people you follow first, with the author's own replies * ahead of everyone's. Replies the author hid are left for "Show more replies"; replies from * accounts you've muted stay, folded away as anywhere else. */const fromFlat = (items: FlatItem[]): Node => { // The replies the thread's author hid, listed on its first post: known, so the menu offers to // unhide them wherever they show (to their own authors, they still do). for (const item of items) { const hidden = (asPostItem(item)?.post.threadgate?.record as { hiddenReplies?: string[] }) ?.hiddenReplies; hidden?.forEach(noteHiddenReply); } const anchor = items.find((item) => item.depth === 0); const post = anchor && asPostItem(anchor); if (!anchor || !post) return anchor ? stub(anchor) : ({ $type: "app.bsky.feed.defs#notFoundPost" } as Node);
let parent: Node | undefined; for (const item of items.filter((item) => item.depth < 0).sort((a, b) => a.depth - b.depth)) { const value = asPostItem(item); parent = value ? ({ $type: "app.bsky.feed.defs#threadViewPost", post: value.post, parent, } as unknown as Node) : stub(item); }
// The replies, each with the replies under it nested inside, as far down as Bluesky sent // them. They come in reading order: each reply right before the replies to it. const author = post.post.author.did; const top: Reply[] = []; const last: Reply[] = []; /** Under a reply that can't be shown, nothing is: its own replies go with it. */ let skipBelow: number | undefined; for (const item of items) { if (item.depth < 1) continue; if (skipBelow !== undefined && item.depth > skipBelow) continue; skipBelow = undefined; const value = asPostItem(item); if (!value || value.hiddenByThreadgate) { skipBelow = item.depth; continue; } const reply: Reply = { $type: "app.bsky.feed.defs#threadViewPost", post: value.post, replies: [], more: (value as { moreReplies?: number }).moreReplies || undefined, } as Reply; if (item.depth === 1) top.push(reply); else if (last[item.depth - 1]) (last[item.depth - 1].replies as Reply[]).push(reply); else continue; last[item.depth] = reply; last.length = item.depth + 1; } // A stable sort: the author's replies move ahead, everything else keeps Bluesky's order. const replies = top.sort( (a, b) => Number(b.post.author.did === author) - Number(a.post.author.did === author), );
return { $type: "app.bsky.feed.defs#threadViewPost", post: post.post, parent, replies, } as unknown as Node;};
/** * A reply in a thread, with the replies under it that came with it, and how many more it has * that didn't (in a tree, past the few Bluesky sends at each step). */export type Reply = ThreadViewPost & { more?: number };
/** Posts that each reply to the one before, oldest first, as what the last one replies to. */export const chainOf = (posts: AppBskyFeedDefs.PostView[]): Node | undefined => posts.reduce<Node | undefined>( (parent, post) => ({ $type: "app.bsky.feed.defs#threadViewPost", post, parent }) as Node, undefined, );
/** How many steps down a thread shown as a tree goes before "Continue thread". */export const TREE_DEPTH = 6;
/** A thread as the page shows it, with whether Bluesky has replies it left out. */export type LoadedThread = { thread: Node; hasOther: boolean; uri: string; /** Loaded as a tree: every branch, not one per reply, which a line can be read from too. */ tree?: boolean;};
export const fetchThread = async ( rpc: Client, uri: string, tree = false, /** The order its replies come in (see reply-order). */ sort: "top" | "newest" | "oldest" = "top",): Promise<LoadedThread> => { const data = await ok( rpc.get("app.bsky.unspecced.getPostThreadV2", { // Its replies, and two levels of the conversation under each: one branch at each level, // the one Bluesky ranks first. As a tree, the conversation as it branches: several // replies at each step, six steps down, as Bluesky's own tree view has it. params: tree ? { anchor: uri as any, above: true, below: TREE_DEPTH, branchingFactor: 6, sort } : { anchor: uri as any, above: true, below: 3, branchingFactor: 1, sort }, }), ); return { thread: fromFlat(data.thread), hasOther: data.hasOtherReplies, uri, tree };};
/** * What a post's page can show before its thread arrives: the post and, as far as it's known, * what it replies to, from the page it's opened from. Its replies come with the thread. */const seeds = new Map<string, { at: number; thread: ThreadViewPost }>();const SEED_FRESH = 10_000;
export const seedThread = (path: string, thread: ThreadViewPost) => { const now = Date.now(); for (const [key, kept] of seeds) if (now - kept.at > SEED_FRESH) seeds.delete(key); seeds.set(path, { at: now, thread });};
export const takeSeed = (path: string) => { const kept = seeds.get(path); seeds.delete(path); return kept && Date.now() - kept.at <= SEED_FRESH ? kept.thread : undefined;};