Discuss: threading logic
discuss/logic.ts: pure functions that turn a ListComments answer into a discussion summary and ordered thread rows, plus hrefs and reply versions.

src/discuss/logic.ts is pure: "No clock, no randomness, no fetch." It takes what listComments returns and produces the rows the screens draw. Keeping it pure lets Bun tests run it against a captured production answer.

Types

Type

Fields

RawComment

The API's comment: id (author/tsid), version, author, targetAccount, targetPath, replyParent, replyParentVersion, threadRoot, threadRootVersion, content (block nodes), createTime, visibility

Authors

uid to {metadata: {name, icon}}, as ListComments returns it

CommentRow

id, href, author, name, avatar, authorHref, at, md, depth, replies, parentName (Contract shape DiscussComment)

Discussion

key (the target as asked), uid, path, ref, count, error, threads

ThreadPage

id, ref, docTitle, docHref, error, comments (Contract shape DiscussThread)

Hrefs and ids

Rows

rowOf(c, authors, server, depth, replies, parentName) builds one CommentRow:

    name from authorName: the trimmed profile name, else the short id.

    avatar through mediaUrl at size S.

    at from timeMs, which accepts an ISO string, a Date, a number or {seconds}.

commentMarkdown runs the comment's blocks through read's blocksToRows and joins the rows into one Markdown string with blank lines between them: text, headings and tables as they are (indented two spaces per depth, with the list marker), code and math inside a fence at least three backticks long and longer than any run in the body, images and embeds as links. The bubble renders it with markup="markdown".

isPublic(c) drops any comment whose visibility contains PRIVATE.

Summary of a document's discussion

summarize(key, ref, comments, authors, server):

    count is the number of public comments, replies included.

    threads are the comments without a threadRoot, each with its reply count.

    Order: newest activity first, where a thread's activity is the latest createTime among its root and replies. Ties break by id.

A thread in reading order

threadRows(rootId, comments, authors, server) returns "the root, then each reply under the comment it answers (depth-first, oldest first)".

    Members are public comments whose threadRoot is the root.

    "A reply whose parent is missing hangs off the root."

    Depth starts at 1 for direct replies and is capped at MAX_DEPTH = 4.

    parentName is set when the parent is not the root, to "show who a reply answers when indentation alone can't".

    The root row's replies is the member count; each reply's is its direct children.

    Returns an empty list when the root is not among the comments; loadThread then falls back to direct lookups.

Reply versions

replyVersions(parent) returns what a reply to parent signs:

    replyCommentVersion = parent.version

    rootReplyCommentVersion = parent.threadRootVersion || parent.version

The comment marks this as a fix over the React Native app: "a reply attaches to the tapped comment". The values become the Comment blob's replyParent and threadRoot CIDs, see Crypto: changes, refs, comments and profiles and the history page.

paragraphContent(text) is the single-paragraph body the composer sends: one Paragraph block with id c1.

Tests

logic.test.ts has 6 Bun tests. The summary test runs on hypermedia-home-comments.json, a captured ListComments answer for the hyper.media home. The others cover ordering by latest activity and dropping private comments, nesting oldest first, the reply target and thread root, signing a reply to a reply (replyParent is that reply, threadRoot is the root), and ids and Markdown. See Bun unit tests.

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime

Comments