Skip to content

Commit

Permalink
feat: add getters for moderation listings
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Apr 12, 2021
1 parent a386903 commit 1c3876e
Show file tree
Hide file tree
Showing 3 changed files with 359 additions and 0 deletions.
187 changes: 187 additions & 0 deletions src/controls/subreddit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import BaseControls from "./base";
import CommentListing from "../listings/comment";
import PostListing from "../listings/post";
import Subreddit from "../objects/subreddit";
import PostOrCommentListing from "../listings/postOrComment";

/** A single capcha identifier and response. */
export interface Capcha {
Expand Down Expand Up @@ -302,6 +303,192 @@ export default class SubredditControls extends BaseControls {
return this.getSortedPosts(subreddit, "controversial", { time });
}

/** @internal */
protected getAboutListing(sr: string, ext: string): Listing<Comment | Post> {
const req = { url: `r/${sr}/about/${ext}`, query: {} };
const ctx = { req, client: this.client };
return new PostOrCommentListing(fakeListingAfter(""), ctx);
}

/** @internal */
protected getAboutListingComments(sr: string, ext: string): Listing<Comment> {
const req = { url: `r/${sr}/about/${ext}`, query: { only: "comments" } };
const ctx = { req, client: this.client };
return new CommentListing(fakeListingAfter(""), ctx);
}

/** @internal */
protected getAboutListingPosts(sr: string, ext: string): Listing<Post> {
const req = { url: `r/${sr}/about/${ext}`, query: { only: "links" } };
const ctx = { req, client: this.client };
return new PostListing(fakeListingAfter(""), ctx);
}

/**
* Get the list of items that have been removed from a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of items that have been removed.
*/
getSpam(subreddit: string): Listing<Post | Comment> {
return this.getAboutListing(subreddit, "spam");
}

/**
* Get the list of comments that have been removed from a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of comments that have been removed.
*/
getSpamComments(subreddit: string): Listing<Comment> {
return this.getAboutListingComments(subreddit, "spam");
}

/**
* Get the list of posts that have been removed from a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of posts that have been removed.
*/
getSpamPosts(subreddit: string): Listing<Post> {
return this.getAboutListingPosts(subreddit, "spam");
}

/**
* Get the list of items that have been edited from a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of items that have been edited.
*/
getEdited(subreddit: string): Listing<Post | Comment> {
return this.getAboutListing(subreddit, "edited");
}

/**
* Get the list of comments that have been edited from a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of comments that have been edited.
*/
getEditedComments(subreddit: string): Listing<Comment> {
return this.getAboutListingComments(subreddit, "edited");
}

/**
* Get the list of posts that have been edited from a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of posts that have been edited.
*/
getEditedPosts(subreddit: string): Listing<Post> {
return this.getAboutListingPosts(subreddit, "edited");
}

/**
* Get the list of items that have been reported from a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of items that have been reported.
*/
getReported(subreddit: string): Listing<Post | Comment> {
return this.getAboutListing(subreddit, "reports");
}

/**
* Get the list of comments that have been reported from a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of comments that have been reported.
*/
getReportedComments(subreddit: string): Listing<Comment> {
return this.getAboutListingComments(subreddit, "reports");
}

/**
* Get the list of posts that have been reported from a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of posts that have been reported.
*/
getReportedPosts(subreddit: string): Listing<Post> {
return this.getAboutListingPosts(subreddit, "reports");
}

/**
* Get the list of items that have not been moderated from a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of items that have not been moderated.
*/
getUnmoderated(subreddit: string): Listing<Post | Comment> {
return this.getAboutListing(subreddit, "unmoderated");
}

/**
* Get the list of comments that have not been moderated from a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of comments that have not been moderated.
*/
getUnmoderatedComments(subreddit: string): Listing<Comment> {
return this.getAboutListingComments(subreddit, "unmoderated");
}

/**
* Get the list of posts that have not been moderated from a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of posts that have not been moderated.
*/
getUnmoderatedPosts(subreddit: string): Listing<Post> {
return this.getAboutListingPosts(subreddit, "unmoderated");
}

/**
* Get the list of items that are in the modqueue of a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of items that are in the modqueue.
*/
getModqueue(subreddit: string): Listing<Post | Comment> {
return this.getAboutListing(subreddit, "modqueue");
}

/**
* Get the list of comments that are in the modqueue of a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of comments that are in the modqueue.
*/
getModqueueComments(subreddit: string): Listing<Comment> {
return this.getAboutListingComments(subreddit, "modqueue");
}

/**
* Get the list of posts that are in the modqueue of a subreddit.
*
* @param subreddit The name of the subreddit.
*
* @returns A listing of posts that are in the modqueue.
*/
getModqueuePosts(subreddit: string): Listing<Post> {
return this.getAboutListingPosts(subreddit, "modqueue");
}

/**
* Get a random post.
*
Expand Down
37 changes: 37 additions & 0 deletions src/listings/postOrComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { InvalidKindError } from "../helper/errors";
import type { Comment, Post } from "../objects";
import type { _Listing, Context, Fetcher } from "./listing";
import Listing, { Pager } from "./listing";

export class PostOrCommentPager extends Pager<Post | Comment> {
async fetch(ctx: Context): Promise<PostOrCommentListing> {
const pg = await this.nextPage(ctx);
return new PostOrCommentListing(pg, ctx);
}
}

export default class PostOrCommentListing extends Listing<Post | Comment> {
constructor(l: _Listing, ctx: Context) {
let fetcher: Fetcher<Post | Comment> | undefined;

if (l.after != null) {
fetcher = new PostOrCommentPager(l.after);
}

const arr: (Post | Comment)[] = [];
for (const c of l.children) {
switch (c.kind) {
case "t1":
arr.push(ctx.client.comments.fromRaw(c));
break;
case "t3":
arr.push(ctx.client.posts.fromRaw(c));
break;
default:
throw new InvalidKindError("t1 or t3", c.kind);
}
}

super(ctx, arr, fetcher);
}
}
135 changes: 135 additions & 0 deletions src/objects/subreddit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,141 @@ export default class Subreddit extends Content implements SubredditData {
return this.controls.getControversialPosts(this.displayName, time);
}

/**
* Get the list of items that have been removed from this subreddit.
*
* @returns A listing of items that have been removed.
*/
getSpam(): Listing<Post | Comment> {
return this.controls.getSpam(this.displayName);
}

/**
* Get the list of comments that have been removed from this subreddit.
*
* @returns A listing of comments that have been removed.
*/
getSpamComments(): Listing<Comment> {
return this.controls.getSpamComments(this.displayName);
}

/**
* Get the list of posts that have been removed from this subreddit.
*
* @returns A listing of posts that have been removed.
*/
getSpamPosts(): Listing<Post> {
return this.controls.getSpamPosts(this.displayName);
}

/**
* Get the list of items that have been edited from this subreddit.
*
* @returns A listing of items that have been edited.
*/
getEdited(): Listing<Post | Comment> {
return this.controls.getEdited(this.displayName);
}

/**
* Get the list of comments that have been edited from this subreddit.
*
* @returns A listing of comments that have been edited.
*/
getEditedComments(): Listing<Comment> {
return this.controls.getEditedComments(this.displayName);
}

/**
* Get the list of posts that have been edited from this subreddit.
*
* @returns A listing of posts that have been edited.
*/
getEditedPosts(): Listing<Post> {
return this.controls.getEditedPosts(this.displayName);
}

/**
* Get the list of items that have been reported from this subreddit.
*
* @returns A listing of items that have been reported.
*/
getReported(): Listing<Post | Comment> {
return this.controls.getReported(this.displayName);
}

/**
* Get the list of comments that have been reported from this subreddit.
*
* @returns A listing of comments that have been reported.
*/
getReportedComments(): Listing<Comment> {
return this.controls.getReportedComments(this.displayName);
}

/**
* Get the list of posts that have been reported from this subreddit.
*
* @returns A listing of posts that have been reported.
*/
getReportedPosts(): Listing<Post> {
return this.controls.getReportedPosts(this.displayName);
}

/**
* Get the list of items that have not been moderated from this subreddit.
*
* @returns A listing of items that have not been moderated.
*/
getUnmoderated(): Listing<Post | Comment> {
return this.controls.getUnmoderated(this.displayName);
}

/**
* Get the list of comments that have not been moderated from this subreddit.
*
* @returns A listing of comments that have not been moderated.
*/
getUnmoderatedComments(): Listing<Comment> {
return this.controls.getUnmoderatedComments(this.displayName);
}

/**
* Get the list of posts that have not been moderated from this subreddit.
*
* @returns A listing of posts that have not been moderated.
*/
getUnmoderatedPosts(): Listing<Post> {
return this.controls.getUnmoderatedPosts(this.displayName);
}

/**
* Get the list of items that are in the modqueue of this subreddit.
*
* @returns A listing of items that are in the modqueue.
*/
getModqueue(): Listing<Post | Comment> {
return this.controls.getModqueue(this.displayName);
}

/**
* Get the list of comments that are in the modqueue of this subreddit.
*
* @returns A listing of comments that are in the modqueue.
*/
getModqueueComments(): Listing<Comment> {
return this.controls.getModqueueComments(this.displayName);
}

/**
* Get the list of posts that are in the modqueue of this subreddit.
*
* @returns A listing of posts that are in the modqueue.
*/
getModqueuePosts(): Listing<Post> {
return this.controls.getModqueuePosts(this.displayName);
}

/**
* Get a random post from this subreddit.
*
Expand Down

0 comments on commit 1c3876e

Please sign in to comment.