Skip to content

Commit

Permalink
feat: add base type for votable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Mar 12, 2021
1 parent 8c08cbc commit 2da716c
Showing 1 changed file with 201 additions and 0 deletions.
201 changes: 201 additions & 0 deletions src/objects/voteable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import VoteableControls from "../controls/voteable";
import type { ContentData } from "./content";
import Content from "./content";

export interface RichTextFlair {
/** The string representation of the emoji */
a?: string;
/** The type of the flair entry */
e: "text" | "emoji";
/** URL of the emoji image */
u?: string;
/** The text content of a text flair */
t?: string;
}

export interface Gildings {
/** Number of Reddit Silver awarded */
gid_1: number;
/** Number of Reddit Gold awarded */
gid_2: number;
/** Number of Reddit Platinum awarded */
gid_3: number;
}

export type SubredditType =
| "gold_restricted"
| "archived"
| "restricted"
| "employees_only"
| "gold_only"
| "private"
| "user"
| "public";

export interface VoteableData extends ContentData {
approvedAtUtc: number | null;
approvedBy: string | null; // RedditUser | null
archived: boolean;
author: string; // RedditUser
authorFlairBackgroundColor: string | null;
authorFlairCssClass: string | null;
authorFlairRichtext: RichTextFlair[];
authorFlairTemplateId: string | null;
authorFlairText: string | null;
authorFlairTextColor: string | null;
authorFlairType: "text" | "richtext";
authorFullname: string;
authorPatreonFlair: boolean;
bannedAtUtc: number | null;
bannedBy: string | null; // RedditUser | null;
canGild: boolean;
canModPost: boolean;
distinguished: "admin" | "moderator" | null;
downs: number;
edited: number | boolean;
gilded: number;
gildings: Gildings;
/** true = upvoted, false = downvoted, null = hasn't voted */
likes: boolean | null;
modNote: string;
modReasonBy: string;
modReasonTitle: string;
modReports: string[];
noFollow: boolean;
numReports: number;
permalink: string;
removalReason: any; // ?
reportReasons: string[];
saved: boolean;
score: number;
sendReplies: boolean;
stickied: boolean;
subreddit: string; // Subreddit
subredditId: string;
subredditNamePrefixed: string;
subredditType: SubredditType;
ups: number;
userReports: string[];
}

export default class Voteable extends Content implements VoteableData {
approvedAtUtc: number | null;
approvedBy: string | null;
archived: boolean;
author: string;
authorFlairBackgroundColor: string | null;
authorFlairCssClass: string | null;
authorFlairRichtext: RichTextFlair[];
authorFlairTemplateId: string | null;
authorFlairText: string | null;
authorFlairTextColor: string | null;
authorFlairType: "text" | "richtext";
authorFullname: string;
authorPatreonFlair: boolean;
bannedAtUtc: number | null;
bannedBy: string | null;
canGild: boolean;
canModPost: boolean;
distinguished: "admin" | "moderator" | null;
downs: number;
edited: number | boolean;
gilded: number;
gildings: Gildings;
likes: boolean | null;
modNote: string;
modReasonBy: string;
modReasonTitle: string;
modReports: string[];
noFollow: boolean;
numReports: number;
permalink: string;
removalReason: any;
reportReasons: string[];
saved: boolean;
score: number;
sendReplies: boolean;
stickied: boolean;
subreddit: string;
subredditId: string;
subredditNamePrefixed: string;
subredditType: SubredditType;
ups: number;
userReports: string[];

protected controls: VoteableControls;

constructor(controls: VoteableControls, data: VoteableData) {
super(data);
this.controls = controls;

this.approvedAtUtc = data.approvedAtUtc;
this.approvedBy = data.approvedBy;
this.archived = data.archived;
this.author = data.author;
this.authorFlairBackgroundColor = data.authorFlairBackgroundColor;
this.authorFlairCssClass = data.authorFlairCssClass;
this.authorFlairRichtext = data.authorFlairRichtext;
this.authorFlairTemplateId = data.authorFlairTemplateId;
this.authorFlairText = data.authorFlairText;
this.authorFlairTextColor = data.authorFlairTextColor;
this.authorFlairType = data.authorFlairType;
this.authorFullname = data.authorFullname;
this.authorPatreonFlair = data.authorPatreonFlair;
this.bannedAtUtc = data.bannedAtUtc;
this.bannedBy = data.bannedBy;
this.canGild = data.canGild;
this.canModPost = data.canModPost;
this.distinguished = data.distinguished;
this.downs = data.downs;
this.edited = data.edited;
this.gilded = data.gilded;
this.gildings = data.gildings;
this.likes = data.likes;
this.modNote = data.modNote;
this.modReasonBy = data.modReasonBy;
this.modReasonTitle = data.modReasonTitle;
this.modReports = data.modReports;
this.noFollow = data.noFollow;
this.numReports = data.numReports;
this.permalink = data.permalink;
this.removalReason = data.removalReason;
this.reportReasons = data.reportReasons;
this.saved = data.saved;
this.score = data.score;
this.sendReplies = data.sendReplies;
this.stickied = data.stickied;
this.subreddit = data.subreddit;
this.subredditId = data.subredditId;
this.subredditNamePrefixed = data.subredditNamePrefixed;
this.subredditType = data.subredditType;
this.ups = data.ups;
this.userReports = data.userReports;
}

/**
* Cast an upvote.
*
* @returns A promise that resolves when the vote has been cast.
*/
async upvote(): Promise<void> {
return this.controls.upvote(this.id);
}

/**
* Remove your vote.
*
* @returns A promise that resolves when the vote has been removed.
*/
async unvote(): Promise<void> {
return this.controls.unvote(this.id);
}

/**
* Cast a downvote.
*
* @returns A promise that resolves when the vote has been cast.
*/
async downvote(): Promise<void> {
return this.controls.downvote(this.id);
}
}

0 comments on commit 2da716c

Please sign in to comment.