Skip to content

Commit

Permalink
feat: add a way to get post requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
ekrzeptowski committed Mar 12, 2024
1 parent 052a2f7 commit c36e742
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/reddit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type {
TextPostOptions,
} from "./subreddit/controls";
export { SubredditControls } from "./subreddit/controls";
export type { SubredditData } from "./subreddit/object";
export type { PostRequirements, SubredditData } from "./subreddit/object";
export { Subreddit } from "./subreddit/object";
export type { SubredditFlair, SubredditType } from "./subreddit/types";
export type { SearchSort, SearchSyntax, Size, TimeRange } from "./types";
Expand Down
14 changes: 13 additions & 1 deletion src/reddit/subreddit/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
} from "../types";
import type { BannedUser } from "../user/moderator-actioned/banned";
import type { ModeratorActionedUser } from "../user/moderator-actioned/base";
import type { SubredditData } from "./object";
import type { PostRequirements, SubredditData } from "./object";
import type { FileDetails, SubredditFlair } from "./types";

import * as fs from "fs";
Expand Down Expand Up @@ -1353,6 +1353,18 @@ export class SubredditControls extends BaseControls {
}
}

/**
* Get the post requirements for this subreddit.
* @param subreddit The name of the subreddit.
* @returns A promise that resolves to the {@link PostRequirements} for this subreddit.
*/
async getPostRequirements(subreddit: string): Promise<PostRequirements> {
const raw: RedditObject = await this.gateway.get(
`api/v1/${subreddit}/post_requirements`
);
return fromRedditData(raw);
}

private async initializeWebSocket(websocketUrl: string): Promise<unknown> {
const ws = new webSocket(websocketUrl);
await new Promise((resolve, reject) => {
Expand Down
56 changes: 56 additions & 0 deletions src/reddit/subreddit/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,54 @@ export interface SubredditData extends ContentData {
// wls: number;
}

/** The attributes specific to post requirements. */
export interface PostRequirements {
/** An array of strings that are not allowed in the body of a post. */
bodyBlacklistedStrings: string[];
/** An array of regular expressions that the body of a post must match. */
bodyRegexes: string[];
/** An array of strings that the body of a post must contain. */
bodyRequiredStrings: string[];
/** The policy for the body of a post. */
bodyRestrictionPolicy: "none" | "required" | "notAllowed";
/** The maximum length of the body of a post. */
bodyTextMaxLength: Maybe<number>;
/** The minimum length of the body of a post. */
bodyTextMixLength: Maybe<number>;
/** An array of domains that are not allowed in the link of a post. */
domainBlacklist: string[];
/** An array of domains that are allowed in the link of a post. */
domainWhitelist: string[];
/** The policy for gallery captions. */
galleryCaptionsRequirement: "none" | "optional" | "required";
/** The maximum number of items in a gallery. */
galleryMaxItems: Maybe<number>;
/** The minimum number of items in a gallery. */
galleryMinItems: Maybe<number>;
/** The policy for gallery URLs. */
galleryUrlsRequirement: "none" | "optional" | "required";
/** The policy for the guidelines. */
guidelinesDisplayPolicy: "required" | "optional" | "hidden";
/** The text of the guidelines. */
guidelinesText: Maybe<string>;
/** Whether a flair is required. */
isFlairRequired: boolean;
/** The minimum days required for a repost of a link. */
linkRepostAge: Maybe<number>;
/** The policy for the link of a post. */
linkRestrictionPolicy: "none" | "whitelist" | "blacklist";
/** An array of strings that are not allowed in the title of a post. */
titleBlacklistedStrings: string[];
/** An array of regular expressions that the title of a post must match. */
titleRegexes: string[];
/** An array of strings that the title of a post must contain. */
titleRequiredStrings: string[];
/** Maximum length of the title of a post. */
titleTextMaxLength: Maybe<number>;
/** Minimum length of the title of a post. */
titleTextMixLength: Maybe<number>;
}

/** A single subreddit. */
export class Subreddit extends Content implements SubredditData {
accountsActiveIsFuzzed: boolean;
Expand Down Expand Up @@ -1137,4 +1185,12 @@ export class Subreddit extends Content implements SubredditData {
getBannedUsers(): Listing<BannedUser> {
return this.controls.getBannedUsers(this.displayName);
}

/**
* Get the post requirements for this subreddit.
* @returns A promise that resolves to the {@link PostRequirements} for this subreddit.
*/
getPostRequirements(): Promise<PostRequirements> {
return this.controls.getPostRequirements(this.displayName);
}
}

0 comments on commit c36e742

Please sign in to comment.