CI / Typecheck, test, build (pull_request) Successful in 9s
Two changes. They share the `Segment` model, which is why they arrive
together.
## Reddit
A new adapter under /reddit, plus the thread beneath the post -- on Reddit the
conversation is usually the reason the link was shared, so a viewer that showed
only the post would be showing the wrong half.
The `.json` twin of a post URL is the post and the whole first page of comments
in one response, far better than anything the page gives up, so it is the only
layer that normally runs. Reddit refuses it to a browser it has never seen and
answers with a JavaScript challenge, which any ordinary navigation solves by
itself; the adapter navigates once and retries, and the cookie left behind
serves every later post. Below that, `shreddit-comment` elements are read from
the rendered page -- flat, each carrying its own `depth`, so `treeFromDepths`
rebuilds the nesting.
`Post.comments` is a tree rather than a flat list with depths, because folding
a comment has to take everything under it along and nesting is what makes that
free. Each comment renders as a `<details open>`, so collapsing works with the
stylesheet off and from the keyboard, and a collapsed one says how many replies
it is hiding. "Collapse all" is the only part that needs the script, so it
ships hidden and appears once the script has run. What sits behind a "load
more" is not fetched -- that is a second page and often a third -- but it is
counted and said out loud rather than quietly dropped.
Four things the payloads got wrong on the first try, each now with a fixture:
`fallback_url` is the video track alone whenever `has_audio` is true, so a post
with sound has to use `hls_url` and only a silent one gets the proxied MP4;
`scrubber_media_url` looks like a poster and is a second MP4 for the timeline
thumbnails, while the still is in `preview.images`; a gallery's pictures live
in `media_metadata` keyed and unordered, with their order only in
`gallery_data`; and `replies` is the string "" rather than an object when there
are none. Comment bodies are Markdown, rendered by a new render/markdown.ts
that escapes first and then puts back only the constructs we chose to support
-- never Reddit's own `body_html`, which would mean trusting markup a stranger
caused to be generated.
An app share link (/r/<sub>/s/<code>) is a plain 301, so one request told not
to follow it is enough. The permalink it resolves to is what the copy button
hands back, since an opaque share code is a tracking parameter by another name.
## Quoted posts
Both X and Bluesky lifted the quoted post's media out and showed it as the
quoter's own, dropping the quoted words and the quoted author entirely. A quote
of a photo post therefore rendered as somebody else's picture under the wrong
name with nothing to say so, and a quote that had a picture of its own dropped
the quoted one instead -- the two could never both appear. Half the quote posts
people share are someone answering a stranger and the other half are someone
continuing a thought from an earlier post; neither reads with only one side of
it on the page.
`Segment.quoted` now carries the whole thing -- author, words, pictures, time
and a link to it -- and renders as a post inside the post. Neither payload
carries a usable address for it: X has no permalink and it is rebuilt from the
handle and `id_str`, Bluesky has an `at://` URI nobody can open and it is
rebuilt from the handle and the record key. On Bluesky the record sits at
`embed.record` for a plain quote and at `embed.record.record` when the quoting
post has media of its own, and a quote can also point at a feed, a list or a
post since deleted, which arrive in the same slot under a different `$type` --
only `app.bsky.embed.record#viewRecord` is taken.
Two things about X's text, both visible on any post and not only a quote. It
arrives pre-escaped, so an ampersand someone typed was reaching the page as the
literal `&`; it is decoded in the adapter, where the encoding comes from,
leaving the escape-on-the-way-out rule alone. And every link is a `t.co`, which
tells the reader nothing and routes them through X's click tracker to find out
-- `entities.urls` carries the real address alongside, so it is put back. The
shortlink X staples onto the end of a quote post is dropped rather than
expanded, since the post it points at is already on the page;
`display_text_range` is where that boundary is and it keeps a link the author
put there deliberately. Its indices are UTF-16 units into the escaped text, so
the slice happens before decoding and before expanding, and splitting to
codepoints first overshoots past an emoji -- checked against a post carrying
one.
A quote is context, and context that fills the screen has stopped being
context, so a segment carrying one gives up the window-filling cap on its own
media.
## Along the way
setupMedia only ever wired `document.querySelector('.media')`, the first rail
on the page. That was already wrong for a Bluesky or Threads chain with media
in more than one post, and a quoted carousel would have hit it too. It now runs
per rail.
## Verified
108 tests, typecheck and build clean. Resolved end to end against the live
platforms: Reddit self, gallery, video, link and share-link posts; X quotes
with no media, with media on the quoted side, and with media on both; Bluesky
quotes in both embed shapes, including a ten-post chain where every post quotes
a different account and all nine quotes come back under the right name.
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017nMQ2eDKnqALYhAibpTKTu
179 lines
6.0 KiB
TypeScript
179 lines
6.0 KiB
TypeScript
/** The platforms antisocial understands. */
|
|
export type PlatformId = 'x' | 'threads' | 'instagram' | 'tiktok' | 'bluesky' | 'reddit';
|
|
|
|
/**
|
|
* Something fetchable that lives on someone else's CDN.
|
|
*
|
|
* Most of these get rewritten to a `/m/` proxy URL before they reach the
|
|
* page, because Instagram and TikTok reject requests that arrive without the
|
|
* right `Referer` and cookies, and because proxying keeps the viewer's
|
|
* browser from talking to the platform at all.
|
|
*/
|
|
export type Asset = {
|
|
url: string;
|
|
/** Headers the upstream CDN insists on. Held server-side, never in the page. */
|
|
fetchHeaders?: Record<string, string>;
|
|
/** Point the page straight at the CDN instead of proxying. Used for HLS,
|
|
* where proxying would mean rewriting playlists and every segment. */
|
|
direct?: boolean;
|
|
};
|
|
|
|
type MediaBase = Asset & {
|
|
width?: number;
|
|
height?: number;
|
|
};
|
|
|
|
export type Media =
|
|
| (MediaBase & { kind: 'image'; alt?: string })
|
|
| (MediaBase & {
|
|
kind: 'video';
|
|
poster?: Asset;
|
|
durationSec?: number;
|
|
/** An HLS playlist rather than a progressive file. Safari plays these
|
|
* natively; other browsers get a note. */
|
|
hls?: boolean;
|
|
});
|
|
|
|
export type Author = {
|
|
handle: string;
|
|
displayName?: string;
|
|
avatar?: Asset;
|
|
};
|
|
|
|
/**
|
|
* A post that the post being shown is talking about.
|
|
*
|
|
* Held apart from the segment quoting it rather than folded into it. The
|
|
* words and the pictures belong to someone else, and showing them under the
|
|
* quoter's name — which is what folding them in amounts to — tells the
|
|
* reader something untrue about who said what.
|
|
*/
|
|
export type Quoted = {
|
|
author: Author;
|
|
text?: string;
|
|
media: Media[];
|
|
/** ISO 8601. */
|
|
postedAt?: string;
|
|
/** The quoted post's own URL, so it can be opened on its own. */
|
|
url?: string;
|
|
};
|
|
|
|
/**
|
|
* One post. Usually a whole `Post` is a single segment, but on the platforms
|
|
* where people write in chains — Bluesky and Threads — the author's own
|
|
* follow-ups belong with the one that was linked, and other people's replies
|
|
* do not.
|
|
*/
|
|
export type Segment = {
|
|
/** Only Reddit gives a post a headline of its own. Everywhere else the
|
|
* first words of the body do that job, so this is left unset. */
|
|
title?: string;
|
|
text?: string;
|
|
media: Media[];
|
|
/** ISO 8601. */
|
|
postedAt?: string;
|
|
/** The post this one quotes, where there is one. */
|
|
quoted?: Quoted;
|
|
/** The post the link actually pointed at. Only meaningful when a thread
|
|
* has more than one segment. */
|
|
isAnchor?: boolean;
|
|
};
|
|
|
|
/**
|
|
* One comment, with the replies that hang off it.
|
|
*
|
|
* A tree rather than a flat list with depths: collapsing a comment has to
|
|
* take everything under it along, and nesting is what makes that free.
|
|
*/
|
|
export type Comment = {
|
|
/** Already prefixed, e.g. `u/someone`. `[deleted]` is left as it came. */
|
|
author: string;
|
|
text?: string;
|
|
/** ISO 8601. */
|
|
postedAt?: string;
|
|
/** Absent when the platform is still hiding it on a new comment. */
|
|
score?: number;
|
|
/** The author of the post, replying under it. */
|
|
isAuthor?: boolean;
|
|
/** Marked by the platform as a moderator or admin comment. */
|
|
distinguished?: string;
|
|
replies: Comment[];
|
|
/** Replies that exist upstream but were not on the page we were given.
|
|
* Shown as a count, since following them means going to the platform. */
|
|
moreReplies?: number;
|
|
};
|
|
|
|
/**
|
|
* The single shape every adapter produces and the renderer consumes. Adding
|
|
* a platform means producing one of these; nothing downstream changes.
|
|
*/
|
|
export type Post = {
|
|
platform: PlatformId;
|
|
/** Human label for the badge: "X", "Threads", ... */
|
|
platformLabel: string;
|
|
/** Canonical original URL, cleaned of tracking parameters. This is what
|
|
* the copy button hands back. */
|
|
originalUrl: string;
|
|
author: Author;
|
|
/** Fixed per platform: the ones that lead with words put the text above
|
|
* the media, the ones that lead with pictures put it below. */
|
|
textPosition: 'above' | 'below';
|
|
/** In the order they were written. Never empty. */
|
|
segments: Segment[];
|
|
/**
|
|
* The conversation under the post, where the platform has one worth
|
|
* showing. Only Reddit fills this in: elsewhere the replies are strangers
|
|
* arguing beneath something that was shared for its own sake, but on
|
|
* Reddit the thread is usually the point of the link.
|
|
*/
|
|
comments?: Comment[];
|
|
/** Top-level comments the first page did not carry. */
|
|
moreComments?: number;
|
|
/** What the platform says the total is, which is larger than what we
|
|
* show whenever `moreComments` is set. */
|
|
commentCount?: number;
|
|
};
|
|
|
|
/** Most platforms have no notion of a chain, so their adapters use this. */
|
|
export function oneSegment(segment: Segment): Segment[] {
|
|
return [{ ...segment, isAnchor: true }];
|
|
}
|
|
|
|
/** The segment the link pointed at, or the first one. */
|
|
export function anchorOf(post: Post): Segment | undefined {
|
|
return post.segments.find((s) => s.isAnchor) ?? post.segments[0];
|
|
}
|
|
|
|
/**
|
|
* Thrown when a post cannot be resolved. Carries enough for the error card
|
|
* to still be useful: which platform, and the link to hand back.
|
|
*/
|
|
export class ResolveError extends Error {
|
|
readonly platform: PlatformId;
|
|
readonly originalUrl: string;
|
|
|
|
constructor(message: string, platform: PlatformId, originalUrl: string, options?: ErrorOptions) {
|
|
super(message, options);
|
|
this.name = 'ResolveError';
|
|
this.platform = platform;
|
|
this.originalUrl = originalUrl;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A platform answered with a human-verification puzzle rather than the post.
|
|
*
|
|
* The page showing it is parked and still open, so the viewer can be handed
|
|
* the puzzle to solve. Solving it leaves the resulting cookie in the shared
|
|
* browser context, which is what makes the retry work.
|
|
*/
|
|
export class ChallengeError extends ResolveError {
|
|
readonly challengeId: string;
|
|
|
|
constructor(challengeId: string, platform: PlatformId, originalUrl: string) {
|
|
super('A verification puzzle is in the way.', platform, originalUrl);
|
|
this.name = 'ChallengeError';
|
|
this.challengeId = challengeId;
|
|
}
|
|
}
|