Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
feat: add a way to get the list of moderators
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Mar 27, 2022
1 parent 7e4e603 commit 7cad99f
Show file tree
Hide file tree
Showing 4 changed files with 58 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 @@ -33,6 +33,8 @@ 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 { ModeratorData } from "./user/moderator-actioned/moderator";
export { Moderator } from "./user/moderator-actioned/moderator";
export type { UserData } from "./user/object/base-object";
export { User } from "./user/object/base-object";
export type { MyUserData } from "./user/object/my-user";
Expand Down
17 changes: 17 additions & 0 deletions src/reddit/subreddit/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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 { Moderator } from "../user/moderator-actioned/moderator";
import { assertKind, fromRedditData } from "../util";
import { Subreddit } from "./object";

Expand Down Expand Up @@ -352,6 +353,22 @@ export class SubredditControls extends BaseControls {
});
}

/**
* Get the list of moderators for a subreddit.
*
* @param subreddit The name of the subreddit to get moderators for.
*
* @returns A listing of moderators.
*/
async getModerators(subreddit: string): Promise<Moderator[]> {
const result = await this.gateway.get<RedditObject>(
`r/${subreddit}/about/moderators`
);
assertKind("UserList", result);
const moderators = result.data.children as Data[];
return moderators.map(m => new Moderator(fromRedditData(m)));
}

/** @internal */
protected getSortedPosts(
subreddit: string | undefined,
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 @@ -6,6 +6,7 @@ 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 { Moderator } from "../user/moderator-actioned/moderator";
import type {
BanOptions,
LinkPostOptions,
Expand Down Expand Up @@ -979,6 +980,18 @@ export class Subreddit extends Content implements SubredditData {
return this.controls.getWikibannedUsers(this.displayName);
}

/**
* Get the list of moderators for this subreddit.
*
* @note Due to the way Reddit implements Listings, this will only contain the
* first 1000 moderators.
*
* @returns A listing of moderators.
*/
async getModerators(): Promise<Moderator[]> {
return this.controls.getModerators(this.displayName);
}

/**
* Ban a user from this subreddit.
*
Expand Down
26 changes: 26 additions & 0 deletions src/reddit/user/moderator-actioned/moderator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { ModeratorActionedUserData } from "./base";

import { ModeratorActionedUser } from "./base";

/** The data specific to a moderator. */
export interface ModeratorData extends ModeratorActionedUserData {
// TODO:
// authorFlairCssClass: null // TODO
// authorFlairText: null, // TODO

/** The permissions the moderator has. */
// TODO: enumerate values
modPermissions: string[];
}

/** A moderator. */
export class Moderator extends ModeratorActionedUser implements ModeratorData {
modPermissions: string[];

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

this.modPermissions = data.modPermissions;
}
}

0 comments on commit 7cad99f

Please sign in to comment.