Skip to content

Commit

Permalink
fix: properly handle comment replies
Browse files Browse the repository at this point in the history
Turns out that when fixing thislooksfun#67 I accidentally broke comment replies. Now
they are working again.

Fixes thislooksfun#71.
  • Loading branch information
thislooksfun committed Nov 12, 2022
1 parent cfc33da commit e754d3f
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/reddit/comment/listing/listing.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Maybe } from "../../../helper/types";
import type {
Fetcher,
ListingContext,
Expand All @@ -13,24 +14,31 @@ import { CommentPager } from "./pager";

const debug = makeDebug("listing:comment");

function makeFetcher(
after: RedditListing["after"],
context: ListingContext
): Maybe<Fetcher<Comment>> {
if (after == undefined) return undefined;

if (after === "" && context.post) {
return new MoreComments({
children: [],
count: 0,
depth: 0,
id: "_",
name: "t1__",
// eslint-disable-next-line @typescript-eslint/naming-convention
parent_id: context.client.posts.namespace(context.post),
});
}

return new CommentPager(after);
}

/** @internal */
export class CommentListing extends Listing<Comment> {
constructor(l: RedditListing, context: ListingContext) {
let fetcher: Fetcher<Comment> | undefined;

if (context.post) {
fetcher = new MoreComments({
children: [],
count: 0,
depth: 0,
id: "_",
name: "t1__",
// eslint-disable-next-line @typescript-eslint/naming-convention
parent_id: context.client.posts.namespace(context.post),
});
} else if (l.after != undefined) {
fetcher = new CommentPager(l.after);
}
let fetcher: Maybe<Fetcher<Comment>>;

const comments: Comment[] = [];
for (const c of l.children) {
Expand All @@ -47,6 +55,8 @@ export class CommentListing extends Listing<Comment> {
}
}

fetcher ??= makeFetcher(l.after, context);

super(context, comments, fetcher);
}
}

0 comments on commit e754d3f

Please sign in to comment.