Skip to content

Commit

Permalink
feat: add a function to distinguish a comment
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Mar 13, 2021
1 parent d981579 commit e4c681f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/controls/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import Comment from "../objects/comment";
import CommentListing from "../listings/comment";
import VoteableControls from "./voteable";

/**
* The ways to distinguish a comment.
*
* Note that due to the way Reddit works, a comment cannot be sticky without
* also being mod-distinguished. For that reason setting `sticky` will also set
* `mod`.
*/
export type DistinguishStates = "none" | "mod" | "sticky";

/**
* Various methods to allow you to interact with comments.
*
Expand Down Expand Up @@ -46,6 +55,22 @@ export default class CommentControls extends VoteableControls {
return this.fromRaw(raw);
}

/**
* Distinguish a comment.
*
* @param id The ID of the comment to distinguish.
* @param state How the comment should be distinguished.
*
* @returns A promise that resolves when the comment has been distinguished.
*/
async distinguish(id: string, state: DistinguishStates): Promise<void> {
const how = state === "none" ? "no" : "yes";
const sticky = state === "sticky";

const body = { how, sticky, id: this.name(id) };
return this.client.post("api/distinguish", body);
}

/** @internal */
fromRaw(raw: RedditObject): Comment {
if (raw.kind != this.type) {
Expand Down
12 changes: 12 additions & 0 deletions src/objects/comment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DistinguishStates } from "../controls/comment";
import type { VoteableData } from "./voteable";
import CommentControls from "../controls/comment";
import Listing from "../listings/listing";
Expand Down Expand Up @@ -71,4 +72,15 @@ export default class Comment extends Voteable implements CommentData {
async refetch(): Promise<Comment> {
return this.controls.fetch(this.id);
}

/**
* Distinguish this comment.
*
* @param state How this comment should be distinguished.
*
* @returns A promise that resolves when this comment has been distinguished.
*/
async distinguish(state: DistinguishStates): Promise<void> {
return this.controls.distinguish(this.id, state);
}
}

0 comments on commit e4c681f

Please sign in to comment.