Comments and community content
Invidious brings discussion content from YouTube and Reddit into the pages that show videos and community posts. It retrieves source data on demand, maps YouTube data into a stable comment shape, keeps Reddit thread data in its source form, and turns both into HTML or JSON for browser and API clients.
Content model
Comments are discussion records attached to either a YouTube video or a YouTube community post. A community post is also represented as a comment-like item so that its author, text, time, reactions, replies, and attachments can use the same display path.
The main source relationships are:
| Content | Parent | Main link |
|---|---|---|
| YouTube video comments | Video ID | /api/v1/comments/:id |
| YouTube community posts | Channel ID | /channel/:ucid/community and /api/v1/channels/:ucid/community |
| Comments on one community post | Post ID and channel ID | /post/:id and /api/v1/post/:id/comments |
| Reddit discussion | Matching video URL | /api/v1/comments/:id?source=reddit |
YouTube responses expose common fields such as author identity, author links, text, time, likes, reply information, and source identifiers. Community posts add attachment data for media and interactive post content. Reddit responses retain thread and comment fields such as the thread title, permalink, author, score, nesting depth, body, and replies.
Common comment presentation
The comment presentation layer turns normalized YouTube records and Reddit records into readable HTML. It keeps source links visible so that a reader can open the original YouTube comment, community post, or Reddit thread.
YouTube display includes author details, verification or channel-owner markers, sponsor and creator-heart markers, relative and exact times, likes, replies, and supported attachments. Community post content can link back to the post, while video comments link back to the video comment on YouTube. Reddit display preserves nested depth and provides links to the Reddit user and comment permalink.
References: src/…/comments_youtube.cr• L4-46 src/…/comments_youtube.cr• L148-195 src/…/comments_reddit.cr• L4-49
YouTube comment flow
YouTube comments are fetched through the YouTube continuation flow. An initial request creates a continuation for the video and sort order. Later requests reuse the returned continuation to fetch more top-level comments or replies.
The server accepts a source, format, sort order, and continuation through the comments API. It returns JSON for programmatic clients or rendered HTML for the browser. Missing comment sections produce an empty result rather than a fabricated comment list.
flowchart LR
A[Video or post page] --> B[Comments API]
B --> C[YouTube browse response]
C --> D[YouTube comment normalizer]
D --> E{Output format}
E -->|JSON| F[Comment data and continuation]
E -->|HTML| G[Comment presentation]
G --> H[Browser comment area]
F --> I[API consumer]
Reply links and load-more links carry continuation values. The browser sends those values back to the same API family, which lets the server fetch the next slice without keeping comment state in the page.
References: src/…/youtube.cr• L4-16 src/…/youtube.cr• L64-101 src/…/youtube.cr• L266-315 src/…/videos.cr• L324-358
YouTube comment normalization
The normalizer hides changes between YouTube response shapes. It reads both the current entity-based comment form and the older renderer form, then presents shared fields to the HTML and API layers.
It also carries state that affects presentation. This includes pinned status, edited status, creator hearts, channel-owner status, sponsor status, reply counts, and a continuation for replies or more comments. Text is exposed as both content data and display HTML so API consumers and browser pages can choose the form they need.
References: src/…/youtube.cr• L111-190 src/…/youtube.cr• L191-280
Reddit comment flow
Reddit comments provide a fallback and an alternate discussion source for a video. The server searches Reddit for threads whose URLs match the video, selects the highest-scoring matching thread, and retrieves its comments with the requested Reddit sort order.
The Reddit API path returns either the thread with its comments as JSON or a small presentation response as HTML. The HTML path renders nested comments, then adjusts links so links in comment text resolve against Reddit and remain safe to display in Invidious.
sequenceDiagram
participant B as Browser or API client
participant I as Invidious
participant R as Reddit
B->>I: Request comments with source=reddit
I->>R: Search for matching video threads
R-->>I: Matching thread list
I->>R: Fetch selected thread and comments
R-->>I: Thread and nested comment data
I-->>B: JSON thread or rendered HTML
Reddit replies are nested in the returned listing rather than loaded through the YouTube continuation mechanism. The display layer uses comment depth to indent replies and recursively renders available child listings.
References: src/…/reddit.cr• L5-40 src/…/reddit_types.cr• L1-57 src/…/videos.cr• L359-389
Community post flow
Community content has two related views. The channel community view lists posts and supports more posts through a channel continuation. The post view shows one post and its comment thread, with a channel ID used when YouTube needs the post owner to resolve the post.
The community fetch path turns post data into the same broad comment display contract used by YouTube comments. This lets the page show post text, author information, time, reactions, replies, and attachments without a separate comment renderer.
Community post retrieval
The channel route retrieves a page of community posts from YouTube and returns a continuation when more posts exist. A direct post route can resolve the channel ID from a YouTube post URL when the caller did not provide it.
The post-comment route uses the channel and post IDs for its first request. It then uses the returned continuation for later replies or additional comment data. The route also supports JSON and HTML output, which keeps the browser flow and API flow aligned.
References: src/…/community.cr• L4-25 src/…/community.cr• L27-57 src/…/channels.cr• L220-261 src/…/channels.cr• L264-299 src/…/channels.cr• L400-475
Community post attachments
Community posts may carry images, video, multiple images, polls, quizzes, or linked channel content. The normalized post keeps the attachment type and the data needed by the presentation layer, while unknown types produce a visible error marker instead of stopping the full post list.
The HTML view presents images and multi-image posts as media content, embeds supported videos, and displays an error when an attached video cannot be used. This keeps the post text and discussion available even when one attachment is not supported.
References: src/…/community.cr• L141-257 src/…/comments_youtube.cr• L86-145
Presentation flows
The watch page places comments below the video details and can switch between YouTube and Reddit without leaving the page. The channel community page renders the current post list directly, while a post page places the selected post above its own comment area.
Watch page comments
The watch page uses user comment preferences to choose its first source and its fallback source. JavaScript loads the chosen source after the page loads, replaces the comment area with the returned HTML, and keeps a link for switching to the other source.
If YouTube comments are disabled or unavailable, the page can offer Reddit. If the selected source fails and a fallback is configured, the browser tries that source; otherwise it restores the prior content or shows the page's disabled state.
References: src/…/watch.cr• L80-118 src/…/watch.ecr• L53-79 src/…/watch.ecr• L307-344 assets/…/watch.js• L14-22 assets/…/watch.js• L114-160 assets/…/watch.js• L181-200
Community and post pages
The community page renders the post list on the server and uses a small browser helper for reply expansion and more-comment requests. A post page renders the selected post first, then loads its comments when JavaScript is enabled or prepares the comments during the no-JavaScript request.
Reply expansion keeps the surrounding post or comment visible while the browser fetches the next HTML fragment. The page can hide or show replies without refetching the already loaded fragment.
References: src/…/community.ecr• L18-46 src/…/post.ecr• L5-48 assets/…/community.js• L4-81 src/…/channels.cr• L281-299
API and legacy links
The API exposes separate paths for video comments, channel community content, one post, and one post's comments. Older channel comment paths return a redirect to the community path so clients can move to the current content model without losing the channel query.
The same API parameters control the main presentation choices across these paths: format selects JSON or HTML, continuation requests the next page, sort_by selects the source order where supported, and thin_mode reduces image detail for browser output.
References: src/…/routing.cr• L242-280 src/…/videos.cr• L324-389 src/…/channels.cr• L400-475
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the normalized YouTube fields expected by both
template_youtubeand JSON clients aligned when YouTube changes its comment response shape. - Keep continuation creation and continuation extraction paired for video comments, post comments, community posts, and replies.
- Keep
source,format,sort_by,ucid, andthin_modebehavior consistent between the route handlers and the browser data objects. - Keep source-switch links and fallback behavior aligned with the two-entry
Preferences.commentscontract. - Keep community attachment types synchronized between community extraction and the HTML renderer so unsupported content remains visible as an error.
- Keep old channel-comment redirects pointed at the current community API path when the route shape changes.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| YouTube returns no comment body | The comment API returns an empty list or empty HTML instead of a partial invalid record. | parse_youtube checks for missing contents before normalization. |
| YouTube changes the comment renderer shape | Comments may fail to normalize or lose fields such as authors, likes, or replies. | The parser supports both entity-based and renderer-based forms, but each new source shape needs matching field handling. |
| A continuation is missing or invalid | Reply expansion or load-more stops at the current page. | Each response carries a continuation only when the source provides one. |
| Reddit has no matching thread | The Reddit API returns a not-found response and the watch page can restore or try its fallback source. | Thread search and selected-thread checks reject an empty match. |
| YouTube comments are disabled | The watch page does not load YouTube comments and offers Reddit when the browser flow allows it. | The page passes comments_enabled into the client and checks the configured source order. |
| A community attachment type is unknown | The post remains visible but shows an attachment error marker. | Community extraction emits an explicit unknown type rather than aborting the post. |
Key files
| File | Why to read it |
|---|---|
src/invidious/routes/api/v1/videos.cr | Main API entry point for YouTube and Reddit video comments. |
src/invidious/comments/youtube.cr | YouTube fetch, normalization, continuation, and HTML handoff. |
src/invidious/comments/reddit.cr | Reddit thread discovery and comment retrieval. |
src/invidious/comments/reddit_types.cr | Source data model for Reddit threads, listings, comments, and nested replies. |
src/invidious/channels/community.cr | Community post retrieval, post normalization, attachment handling, and continuation flow. |
src/invidious/routes/api/v1/channels.cr | Community, post, and post-comment API routes. |
src/invidious/frontend/comments_youtube.cr | HTML presentation for YouTube comments and community posts. |
src/invidious/frontend/comments_reddit.cr | Nested Reddit comment presentation. |
| assets/…/comments.js | Browser loading and reply expansion for video and post comments. |
| assets/…/watch.js | YouTube/Reddit source switching and watch-page fallback flow. |
| assets/…/community.js | Community-page reply and continuation behavior. |
src/invidious/views/watch.ecr | Watch-page comment area and client data contract. |
src/invidious/views/community.ecr | Community post list presentation and client data contract. |
src/invidious/views/post.ecr | Single post and post-comment presentation. |
Channel content and subscriptions | Related channel and community content context. |
Video watch and playback | Related watch-page and video context. |