Skip to content

Commit

Permalink
feat: add a way to get banned users
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Mar 27, 2022
1 parent f4d9500 commit de2f41b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/reddit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export { Subreddit } from "./subreddit/object";
export type { SubredditType } from "./subreddit/types";
export type { SearchSort, SearchSyntax, Size, TimeRange } from "./types";
export { UserControls } from "./user/controls";
export type { BannedUserData } from "./user/moderator-actioned/banned";
export { BannedUser } from "./user/moderator-actioned/banned";
export type { ModeratorActionedUserData } from "./user/moderator-actioned/base";
export { ModeratorActionedUser } from "./user/moderator-actioned/base";
export type { UserData } from "./user/object/base-object";
Expand Down
19 changes: 19 additions & 0 deletions src/reddit/subreddit/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
SearchSyntax,
TimeRange,
} from "../types";
import type { BannedUser } from "../user/moderator-actioned/banned";
import type { ModeratorActionedUser } from "../user/moderator-actioned/base";
import type { SubredditData } from "./object";

Expand All @@ -19,6 +20,7 @@ import { CommentListing } from "../comment/listing/listing";
import { fakeListingAfter } from "../listing/util";
import { PostListing } from "../post/listing";
import { PostOrCommentListing } from "../post-or-comment/listing";
import { BannedUserListing } from "../user/moderator-actioned/banned";
import { ModeratorActionedUserListing } from "../user/moderator-actioned/base";
import { assertKind, fromRedditData } from "../util";
import { Subreddit } from "./object";
Expand Down Expand Up @@ -257,6 +259,23 @@ export class SubredditControls extends BaseControls {
await this.unfriend(subreddit, username, "banned");
}

/**
* Get the list of banned users for a subreddit.
*
* @note Due to the way Reddit implements Listings, this will only contain the
* first 1000 banned users.
*
* @param subreddit The name of the subreddit to get banned users for.
*
* @returns A listing of banned users.
*/
getBannedUsers(subreddit: string): Listing<BannedUser> {
return new BannedUserListing(fakeListingAfter(""), {
request: { url: `r/${subreddit}/about/banned`, query: {} },
client: this.client,
});
}

/**
* Mute a user in a subreddit.
*
Expand Down
13 changes: 13 additions & 0 deletions src/reddit/subreddit/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ContentData } from "../content";
import type { Listing } from "../listing/listing";
import type { Post } from "../post/object";
import type { SearchSort, SearchSyntax, Size, TimeRange } from "../types";
import type { BannedUser } from "../user/moderator-actioned/banned";
import type { ModeratorActionedUser } from "../user/moderator-actioned/base";
import type {
BanOptions,
Expand Down Expand Up @@ -988,4 +989,16 @@ export class Subreddit extends Content implements SubredditData {
async unbanUser(username: string): Promise<void> {
return this.controls.unbanUser(this.displayName, username);
}

/**
* Get the list of banned users for this subreddit.
*
* @note Due to the way Reddit implements Listings, this will only contain the
* first 1000 banned users.
*
* @returns A listing of banned users.
*/
getBannedUsers(): Listing<BannedUser> {
return this.controls.getBannedUsers(this.displayName);
}
}
62 changes: 62 additions & 0 deletions src/reddit/user/moderator-actioned/banned.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type {
Fetcher,
ListingContext,
RedditListing,
} from "../../listing/listing";
import type { ModeratorActionedUserData } from "./base";

import { Listing, Pager } from "../../listing/listing";
import { fromRedditData } from "../../util";
import { ModeratorActionedUser } from "./base";

/** The data specific to a banned user. */
export interface BannedUserData extends ModeratorActionedUserData {
/** The number of days left on the ban. If `null` the ban is permanent. */
daysLeft: number | null;
/** The mod note. */
note: string;
}

/** A banned user. */
export class BannedUser
extends ModeratorActionedUser
implements BannedUserData
{
daysLeft: number | null;
note: string;

/** @internal */
constructor(data: BannedUserData) {
super(data);

this.daysLeft = data.daysLeft;
this.note = data.note;
}
}

// #region listing
class BannedUserPager extends Pager<BannedUser> {
async fetch(context: ListingContext): Promise<BannedUserListing> {
const pg = await this.nextPage(context);
return new BannedUserListing(pg, context);
}
}

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

if (l.after != undefined) {
fetcher = new BannedUserPager(l.after);
}

const users: BannedUser[] = [];
for (const c of l.children) {
users.push(new BannedUser(fromRedditData(c)));
}

super(context, users, fetcher);
}
}
// #endregion listing

0 comments on commit de2f41b

Please sign in to comment.