Skip to content

Commit

Permalink
feat: add a method to get the approved contributors in a subreddit
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Mar 27, 2022
1 parent a79b3d7 commit c1edba2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/reddit/subreddit/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import type {
SearchSyntax,
TimeRange,
} from "../types";
import type { User } from "../user/object/base-object";
import type { SubredditData } from "./object";

import { BaseControls } from "../base-controls";
import { CommentListing } from "../comment/listing/listing";
import { fakeListingAfter } from "../listing/util";
import { PostListing } from "../post/listing";
import { PostOrCommentListing } from "../post-or-comment/listing";
import { UserListing } from "../user/listing";
import { assertKind, fromRedditData } from "../util";
import { Subreddit } from "./object";

Expand Down Expand Up @@ -157,6 +159,20 @@ export class SubredditControls extends BaseControls {
await this.gateway.post("api/leavecontributor", { id: subredditId });
}

/**
* Get the list of approved contributors for a subreddit.
*
* @note Due to the way Reddit implements Listings, this will only contain the
* first 1000 contributors.
*
* @param subreddit The name of the subreddit to get contributors for.
*
* @returns A listing of approved contributors.
*/
getContributors(subreddit: string): Listing<User> {
return this.getAboutListingUsers(subreddit, "contributors");
}

/**
* Add a user to the list of approved wiki editors.
*
Expand Down Expand Up @@ -714,6 +730,16 @@ export class SubredditControls extends BaseControls {
});
}

/** @internal */
protected getAboutListingUsers(
subreddit: string,
type: string
): Listing<User> {
const request = { url: `r/${subreddit}/${type}`, query: {} };
const context = { request, client: this.client };
return new UserListing(fakeListingAfter(""), context);
}

/** @internal */
fromRaw(raw: RedditObject): Subreddit {
assertKind("t5", raw);
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 { User } from "../user/object/base-object";
import type {
BanOptions,
LinkPostOptions,
Expand Down Expand Up @@ -869,6 +870,18 @@ export class Subreddit extends Content implements SubredditData {
await this.controls.leaveContributor(`t5_${this.id}`);
}

/**
* Get the list of approved contributors for this subreddit.
*
* @note Due to the way Reddit implements Listings, this will only contain the
* first 1000 contributors.
*
* @returns A listing of approved contributors.
*/
getContributors(): Listing<User> {
return this.controls.getContributors(this.displayName);
}

/**
* Add a user to the list of approved wiki editors.
*
Expand Down
33 changes: 33 additions & 0 deletions src/reddit/user/listing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type {
Fetcher,
ListingContext,
RedditListing,
} from "../listing/listing";
import type { User } from "./object/base-object";

import { Listing, Pager } from "../listing/listing";

class UserPager extends Pager<User> {
async fetch(context: ListingContext): Promise<UserListing> {
const pg = await this.nextPage(context);
return new UserListing(pg, context);
}
}

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

if (l.after) {
fetcher = new UserPager(l.after);
}

const users: User[] = [];
for (const c of l.children) {
users.push(context.client.users.fromRaw(c));
}

super(context, users, fetcher);
}
}

0 comments on commit c1edba2

Please sign in to comment.