Skip to content

Commit

Permalink
feat: add getLinkFlairTemplates
Browse files Browse the repository at this point in the history
  • Loading branch information
ekrzeptowski committed Mar 6, 2024
1 parent d893730 commit 9b89463
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
9 changes: 6 additions & 3 deletions src/reddit/post/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ export interface PostData extends LockableData {
// linkFlairBackgroundColor: string;
// linkFlairCssClass: Maybe<string>;
// linkFlairRichtext: RichTextFlair[];
// linkFlairTemplateId: Maybe<string>;

/** The ID of the link flair template, or `undefined` if there is no flair. */
linkFlairTemplateId: Maybe<string>;

// linkFlairText: Maybe<string>;
// linkFlairTextColor: "dark" | "light";
// linkFlairType: "text" | "richtext";
Expand Down Expand Up @@ -265,7 +268,7 @@ export class Post extends Lockable implements PostData {
// linkFlairBackgroundColor: string;
// linkFlairCssClass: Maybe<string>;
// linkFlairRichtext: RichTextFlair[];
// linkFlairTemplateId: Maybe<string>;
linkFlairTemplateId: Maybe<string>;
// linkFlairText: Maybe<string>;
// linkFlairTextColor: "dark" | "light";
// linkFlairType: "text" | "richtext";
Expand Down Expand Up @@ -325,7 +328,7 @@ export class Post extends Lockable implements PostData {
// this.linkFlairBackgroundColor = data.linkFlairBackgroundColor;
// this.linkFlairCssClass = data.linkFlairCssClass;
// this.linkFlairRichtext = data.linkFlairRichtext;
// this.linkFlairTemplateId = data.linkFlairTemplateId;
this.linkFlairTemplateId = data.linkFlairTemplateId;
// this.linkFlairText = data.linkFlairText;
// this.linkFlairTextColor = data.linkFlairTextColor;
// this.linkFlairType = data.linkFlairType;
Expand Down
22 changes: 18 additions & 4 deletions 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 { SubredditData, SubredditFlair } from "./object";

import { BaseControls } from "../base-controls";
import { CommentListing } from "../comment/listing/listing";
Expand Down Expand Up @@ -56,7 +56,8 @@ export interface BanOptions {
/** Extra options for submitting a post. */
export interface TextPostOptions {
/**
* Whether or not to send inbox replies for the new post (defaults to `true`).
* Whether or not to send inbox replies for the new post (defaults to
* `true`).
*
* If you want to change this later you can use
* {@link Post.enableInboxReplies} and {@link Post.disableInboxReplies}.
Expand All @@ -79,8 +80,8 @@ export interface TextPostOptions {
/** Extra options for submitting a link post. */
export interface LinkPostOptions extends TextPostOptions {
/**
* Whether or not to error if this link has been submitted before (defaults to
* `false`).
* Whether or not to error if this link has been submitted before (defaults
* to `false`).
*/
unique?: boolean;
}
Expand Down Expand Up @@ -567,6 +568,19 @@ export class SubredditControls extends BaseControls {
return new PostListing(fakeListingAfter(""), context);
}

/**
* Get the list of flair templates for a subreddit.
* @param subreddit
*
* @returns A promise that resolves to a list of flair templates.
*/
async getLinkFlairTemplates(subreddit: string): Promise<SubredditFlair[]> {
const raw = await this.gateway.get<Data[]>(
`r/${subreddit}/api/link_flair_v2.json`
);
return raw.map((flair: Data) => fromRedditData(flair));
}

/**
* Get the list of items that have been removed from a subreddit.
*
Expand Down
55 changes: 38 additions & 17 deletions src/reddit/subreddit/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ export interface SubredditData extends ContentData {
displayName: string;

/**
* The name of the subreddit prefixed with `r/` (or `u/` for user subreddits).
* The name of the subreddit prefixed with `r/` (or `u/` for user
* subreddits).
*/
displayNamePrefixed: string;

/**
* The custom emoji size if 'Custom sized emojis' has been configured for this
* subreddit, or `undefined` if no custom size was set.
* The custom emoji size if 'Custom sized emojis' has been configured for
* this subreddit, or `undefined` if no custom size was set.
*/
emojisCustomSize: Maybe<Size>;

Expand All @@ -141,8 +142,8 @@ export interface SubredditData extends ContentData {
headerSize: Maybe<Size>;

/**
* The text to display when hovering over {@link headerImg}, or `undefined` if
* no such title has been set.
* The text to display when hovering over {@link headerImg}, or `undefined`
* if no such title has been set.
*/
headerTitle: Maybe<string>;

Expand All @@ -165,9 +166,8 @@ export interface SubredditData extends ContentData {
// TODO: Document or remove SubredditData.lang
// lang: string;

// TODO: Document or remove SubredditData.linkFlair*
// linkFlairEnabled: boolean;
// linkFlairPosition: "" | "left" | "right";
linkFlairEnabled: boolean;
linkFlairPosition: "" | "left" | "right";

// TODO: Document or remove SubredditData.mobileBannerImage
// mobileBannerImage: string;
Expand Down Expand Up @@ -245,8 +245,8 @@ export interface SubredditData extends ContentData {
submitText: string;

/**
* The custom submit text rendered as HTML, or `undefined` if no such text was
* configured.
* The custom submit text rendered as HTML, or `undefined` if no such text
* was configured.
*/
submitTextHtml: Maybe<string>;

Expand All @@ -260,8 +260,8 @@ export interface SubredditData extends ContentData {
subscribers: number;

/**
* The suggested way to sort the comments, or `undefined` if no suggestion has
* been configured.
* The suggested way to sort the comments, or `undefined` if no suggestion
* has been configured.
*/
// TODO:
// suggestedCommentSort: Maybe<Sort>;
Expand Down Expand Up @@ -328,6 +328,22 @@ export interface SubredditData extends ContentData {
// wls: number;
}

export interface SubredditFlair {
text: string;
cssClass: string;
id: string;
textEditable: boolean;
type: "text" | "richtext";
allowableContent: string;
textColor: "dark" | "light";
modOnly: boolean;
richtext?: {
e: "text";
t: string;
}[];
backgroundColor: string;
}

/** A single subreddit. */
export class Subreddit extends Content implements SubredditData {
accountsActiveIsFuzzed: boolean;
Expand Down Expand Up @@ -374,8 +390,8 @@ export class Subreddit extends Content implements SubredditData {
// isEnrolledInNewModmail: boolean,
// keyColor: string;
// lang: string;
// linkFlairEnabled: boolean;
// linkFlairPosition: "" | "left" | "right";
linkFlairEnabled: boolean;
linkFlairPosition: "" | "left" | "right";
// mobileBannerImage: string;
// notificationLevel: Maybe<string>;
originalContentTagEnabled: boolean;
Expand Down Expand Up @@ -474,8 +490,8 @@ export class Subreddit extends Content implements SubredditData {
// this.isEnrolledInNewModmail = data.isEnrolledInNewModmail;
// this.keyColor = data.keyColor;
// this.lang = data.lang;
// this.linkFlairEnabled = data.linkFlairEnabled;
// this.linkFlairPosition = data.linkFlairPosition;
this.linkFlairEnabled = data.linkFlairEnabled;
this.linkFlairPosition = data.linkFlairPosition;
// this.mobileBannerImage = data.mobileBannerImage;
// this.notificationLevel = data.notificationLevel;
this.originalContentTagEnabled = data.originalContentTagEnabled;
Expand Down Expand Up @@ -599,6 +615,10 @@ export class Subreddit extends Content implements SubredditData {
return this.controls.getControversialPosts(this.displayName, time);
}

getLinkFlairTemplates(): Promise<SubredditFlair[]> {
return this.controls.getLinkFlairTemplates(this.displayName);
}

/**
* Get the list of items that have been removed from this subreddit.
*
Expand Down Expand Up @@ -921,7 +941,8 @@ export class Subreddit extends Content implements SubredditData {
/**
* Mute a user in this subreddit.
*
* This prevents the user from sending modmail to this subreddit for 72 hours.
* This prevents the user from sending modmail to this subreddit for 72
* hours.
*
* @param username The username of the user to mute.
*/
Expand Down

0 comments on commit 9b89463

Please sign in to comment.