Skip to content

Commit

Permalink
feat: add controls for interacting with voteable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Mar 12, 2021
1 parent 74637d8 commit 8c08cbc
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions src/controls/voteable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import type Client from "../client";

/** The ways to distinguish an item. */
export type DistinguishStates = "clear" | "mod" | "sticky";
/** The vote types. 1 = upvote, 0 = no vote, -1 = downvote. */
export type Vote = 1 | 0 | -1;

/**
* This is an internal class that you likely shouldn't interact with directly.
*
* @internal
*/
export default class VoteableControls {
protected client: Client;
protected type: string;

constructor(client: Client, type: string) {
this.client = client;
this.type = type;
}

/**
* Set whether or not inbox replies are enabled for this object.
*
* @param id The id of the object.
* @param enabled Whether or not replies should be enabled.
*
* @returns A promise that resolves when the change has been made.
*/
protected async inboxReplies(id: string, enabled: boolean): Promise<void> {
const state = enabled;
return this.client.post("api/sendreplies", { id: this.name(id), state });
}

/**
* Enable inbox replies for this object.
*
* @param id The id of the object.
*
* @returns A promise that resolves when replies have been enabled.
*/
async enableInboxReplies(id: string): Promise<void> {
return this.inboxReplies(id, true);
}

/**
* Disable inbox replies for this object.
*
* @param id The id of the object.
*
* @returns A promise that resolves when replies have been disabled.
*/
async disableInboxReplies(id: string): Promise<void> {
return this.inboxReplies(id, false);
}

// TODO: Implement these as well.
// async delete(id: string): Promise<void> {}
// async distinguish(id: string, state: DistinguishStates): Promise<void> {}
// async edit(updatedText: string): Promise<void> {}
// async expandReplies(options?: { limit?: number; depth?: number }): Promise<T> {}
// async gild(): Promise<void> {}
// async save(): Promise<void> {}
// async undistinguish(): Promise<void> {}
// async unsave(): Promise<void> {}

/**
* Cast a vote.
*
* @param id The ID of the object to vote on.
* @param vote The vote to cast.
*
* @returns A promise that resolves when the vote has been cast.
*/
protected async vote(id: string, vote: Vote): Promise<void> {
return this.client.post("api/vote", { id: this.name(id), dir: vote });
}

/**
* Cast an upvote.
*
* @param id The ID of the object to upvote.
*
* @returns A promise that resolves when the vote has been cast.
*/
async upvote(id: string): Promise<void> {
return this.vote(id, 1);
}

/**
* Remove your vote.
*
* @param id The ID of the object to unvote.
*
* @returns A promise that resolves when the vote has been removed.
*/
async unvote(id: string): Promise<void> {
return this.vote(id, 0);
}

/**
* Cast a downvote.
*
* @param id The ID of the object to downvote.
*
* @returns A promise that resolves when the vote has been cast.
*/
async downvote(id: string): Promise<void> {
return this.vote(id, -1);
}

/**
* Convert an id into a full name.
*
* @param id The ID of the object.
*
* @returns The full name of the object.
*/
protected name(id: string): string {
return `${this.type}_${id}`;
}
}

0 comments on commit 8c08cbc

Please sign in to comment.