Skip to content

Commit

Permalink
fix: unbreak search()ed post comments yet again
Browse files Browse the repository at this point in the history
The previous fix for comment replies broke top level comments. This
fixes that issue. I hate Reddit's REST API so much.

Fixes thislooksfun#71
  • Loading branch information
thislooksfun committed Dec 4, 2022
1 parent ed33872 commit 09876c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/reddit/comment/listing/listing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { makeDebug } from "../../../helper/debug";
import { Listing } from "../../listing/listing";
import { MoreComments } from "./more";
import { CommentPager } from "./pager";
import { PostComments } from "./post";

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

Expand All @@ -21,15 +22,10 @@ function makeFetcher(
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),
});
// HACK: Some posts, notably the ones returned by search, don't have their
// own comments listing. To ensure we can still get comments for those posts
// we use a custom fetcher that just jump-starts the fetching process.
return new PostComments();
}

return new CommentPager(after);
Expand Down
28 changes: 28 additions & 0 deletions src/reddit/comment/listing/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type {
Fetcher,
ListingContext,
ListingObject,
} from "../../listing/listing";
import type { Comment } from "../object";

import { emptyRedditListing } from "../../listing/util";
import { CommentListing } from "./listing";

/** @internal */
export class PostComments implements Fetcher<Comment> {
async fetch(context: ListingContext): Promise<CommentListing> {
if (!context.post) {
// This should never happen, but just in case...
throw new Error("Precondition failed: context.post is falsy");
}

const pth = `comments/${context.post}`;
const childrenResponse: [unknown, ListingObject] =
await context.client.gateway.get(pth, { comment: context.post });

return new CommentListing(
childrenResponse[1].data ?? emptyRedditListing,
context
);
}
}

0 comments on commit 09876c6

Please sign in to comment.