Show the author's own chain on Bluesky and Threads
CI / Typecheck, test, build (pull_request) Successful in 46s
CI / Typecheck, test, build (pull_request) Successful in 46s
People write in chains on both, and a link into one arrives pointing at a single post out of several. Showing only that post loses the thing that was being said. Other people's replies are a different matter: they are a conversation rather than the thing that was shared, and on a busy post there are hundreds of them. A Post is now a list of Segments instead of one body. Most platforms produce exactly one and say so through oneSegment(); the two that thread produce the whole chain, with isAnchor marking the post that was actually linked, which need not be the first. Bluesky walks parent upward and the author's own replies downward, stopping at the first post by anyone else. That needs depth and parentHeight on getPostThread, which drags the entire reply tree along -- a few hundred KB on a popular post -- because there is no way to ask the API for one author's branch. Threads is harder to read. The page ships the linked post, the author's follow-ups, other people's replies and a pile of unrelated recommendations, all as flat thread_items containers with no nesting to go on. What separates a follow-up from a stranger's reply is that a follow-up is the author replying to themselves; a reply from someone else carries the same reply_to_author with a different name on it. The first post of a chain replies to nothing at all, so it is reachable only by walking backwards from the post that answers it -- a test caught that, when linking the second post of a thread returned just the one post. Fixtures for both are real captures. The Bluesky one keeps two of every level's outside replies rather than pruning them away, because a filter is only worth testing against the thing it is supposed to exclude. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BGkRmLfiWuJHx6tQ12EELY
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { test } from 'node:test';
|
||||
import { selfThread } from '../src/platforms/threads.ts';
|
||||
import { fixture } from './helpers.ts';
|
||||
|
||||
type Post = Parameters<typeof selfThread>[0][number];
|
||||
|
||||
// A real capture of a two-post NASA thread. The page ships eighteen posts:
|
||||
// the two that belong together, other people's replies to them, and a pile of
|
||||
// unrelated recommendations. Only the first two may survive.
|
||||
const page = () => fixture<Post[]>('threads/thread-chain.json');
|
||||
|
||||
test('the author\'s own follow-up is kept, in order', () => {
|
||||
const chain = selfThread(page(), 'DccGgKuEW-E');
|
||||
assert.equal(chain.length, 2);
|
||||
assert.deepEqual(chain.map((p) => p.code), ['DccGgKuEW-E', 'DccGhYSkXYY']);
|
||||
assert.match(chain[0]?.caption?.text ?? '', /Home improvement/);
|
||||
assert.match(chain[1]?.caption?.text ?? '', /where you can watch/);
|
||||
});
|
||||
|
||||
test('every other author on the page is dropped', () => {
|
||||
const chain = selfThread(page(), 'DccGgKuEW-E');
|
||||
assert.ok(chain.every((p) => p.user?.username === 'nasa'),
|
||||
'a post by someone else survived the filter');
|
||||
assert.ok(page().length > chain.length + 10, 'the fixture should be mostly noise');
|
||||
});
|
||||
|
||||
test('a reply from someone else to the same post is not a continuation', () => {
|
||||
// This one is the trap: it carries reply_to_author = nasa, exactly like a
|
||||
// real follow-up does. Only the author of the *post* separates them.
|
||||
const esa = page().find((p) => p.user?.username === 'europeanspaceagency');
|
||||
assert.ok(esa, 'fixture should contain a reply from another account');
|
||||
assert.equal(esa?.text_post_app_info?.reply_to_author?.username, 'nasa');
|
||||
assert.ok(!selfThread(page(), 'DccGgKuEW-E').includes(esa as Post));
|
||||
});
|
||||
|
||||
test('linking the second post still returns the whole chain', () => {
|
||||
const chain = selfThread(page(), 'DccGhYSkXYY');
|
||||
assert.deepEqual(chain.map((p) => p.code), ['DccGgKuEW-E', 'DccGhYSkXYY']);
|
||||
});
|
||||
|
||||
test('an unknown code yields nothing rather than guessing', () => {
|
||||
assert.deepEqual(selfThread(page(), 'NoSuchCode'), []);
|
||||
});
|
||||
|
||||
test('a lone post is a chain of one', () => {
|
||||
const chain = selfThread(
|
||||
[
|
||||
{ code: 'A', taken_at: 1, user: { username: 'me' }, caption: { text: 'only' } },
|
||||
{ code: 'B', taken_at: 2, user: { username: 'you' }, caption: { text: 'reply' },
|
||||
text_post_app_info: { reply_to_author: { username: 'me' } } },
|
||||
],
|
||||
'A',
|
||||
);
|
||||
assert.deepEqual(chain.map((p) => p.code), ['A']);
|
||||
});
|
||||
|
||||
test('the chain is ordered by when each was posted, not by payload order', () => {
|
||||
const chain = selfThread(
|
||||
[
|
||||
{ code: 'C', taken_at: 30, user: { username: 'me' },
|
||||
text_post_app_info: { reply_to_author: { username: 'me' } } },
|
||||
{ code: 'A', taken_at: 10, user: { username: 'me' } },
|
||||
{ code: 'B', taken_at: 20, user: { username: 'me' },
|
||||
text_post_app_info: { reply_to_author: { username: 'me' } } },
|
||||
],
|
||||
'A',
|
||||
);
|
||||
assert.deepEqual(chain.map((p) => p.code), ['A', 'B', 'C']);
|
||||
});
|
||||
Reference in New Issue
Block a user