Skip to content

Commit

Permalink
feat: allow locking and unlocking comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Mar 5, 2022
1 parent 774866d commit 766726b
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/reddit/comment/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type { RedditObject } from "../types";
import { makeDebug } from "../../helper/debug";
import { Comment } from "../comment/object";
import { fakeMoreListing } from "../listing/util";
import { LockableControls } from "../lockable/controls";
import { assertKind, fromRedditData } from "../util";
import { VoteableControls } from "../voteable/controls";
import { CommentListing } from "./listing/listing";

const debug = makeDebug("controls:comment");
Expand All @@ -26,7 +26,7 @@ export type DistinguishStates = "none" | "mod" | "sticky";
*
* @category Controls
*/
export class CommentControls extends VoteableControls {
export class CommentControls extends LockableControls {
/** @internal */
constructor(client: Client) {
super(client, "t1");
Expand Down
8 changes: 4 additions & 4 deletions src/reddit/comment/object.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Maybe } from "../../helper/types";
import type { CommentControls, DistinguishStates } from "../comment/controls";
import type { Listing } from "../listing/listing";
import type { VoteableData } from "../voteable/object";
import type { LockableData } from "../lockable/object";

import { Voteable } from "../voteable/object";
import { Lockable } from "../lockable/object";

/** The attributes specific to Comment objects. */
export interface CommentData extends VoteableData {
export interface CommentData extends LockableData {
/** The plaintext body of the comment. */
body: string;

Expand Down Expand Up @@ -61,7 +61,7 @@ export interface CommentData extends VoteableData {
}

/** A single comment. */
export class Comment extends Voteable implements CommentData {
export class Comment extends Lockable implements CommentData {
bodyHtml: string;
body: string;
collapsed: boolean;
Expand Down
3 changes: 3 additions & 0 deletions src/reddit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export { Comment } from "./comment/object";
export type { ContentData } from "./content";
export { Content } from "./content";
export { Listing } from "./listing/listing";
export { LockableControls } from "./lockable/controls";
export type { LockableData } from "./lockable/object";
export { Lockable } from "./lockable/object";
export { PostControls } from "./post/controls";
export type { PostData } from "./post/object";
export { Post } from "./post/object";
Expand Down
26 changes: 26 additions & 0 deletions src/reddit/lockable/controls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { VoteableControls } from "../voteable/controls";

/** The base controls for all content that can be locked. */
export abstract class LockableControls extends VoteableControls {
/**
* Lock an item, preventing non-moderators from being able to post replies.
*
* @param id The ID of the item to lock.
*
* @returns A promise that resolves when the item has been locked.
*/
async lock(id: string): Promise<void> {
await this.gateway.post("api/lock", { id: this.namespace(id) });
}

/**
* Unlock an item, allowing non-moderators to post replies.
*
* @param id The ID of the item to unlock.
*
* @returns A promise that resolves when the item has been unlocked.
*/
async unlock(id: string): Promise<void> {
await this.gateway.post("api/unlock", { id: this.namespace(id) });
}
}
43 changes: 43 additions & 0 deletions src/reddit/lockable/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { VoteableData } from "../voteable/object";
import type { LockableControls } from "./controls";

import { Voteable } from "../voteable/object";

/** The base for all content that can be locked. */
export interface LockableData extends VoteableData {
/** Whether or not this item is locked. */
locked: boolean;
}

/** The base for all content that can be locked. */
export abstract class Lockable extends Voteable implements LockableData {
locked: boolean;

protected controls: LockableControls;

/** @internal */
constructor(controls: LockableControls, data: LockableData) {
super(controls, data);
this.controls = controls;

this.locked = data.locked;
}

/**
* Lock this item, preventing non-moderators from being able to post replies.
*
* @returns A promise that resolves when this item has been locked.
*/
async lock(): Promise<void> {
await this.controls.lock(this.id);
}

/**
* Unlock this item, allowing non-moderators to post replies.
*
* @returns A promise that resolves when this item has been unlocked.
*/
async unlock(): Promise<void> {
await this.controls.unlock(this.id);
}
}
26 changes: 2 additions & 24 deletions src/reddit/post/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import type {
import { assertKind, fromRedditData } from "..//util";
import { CommentListing } from "../comment/listing/listing";
import { fakeListingAfter } from "../listing/util";
import { LockableControls } from "../lockable/controls";
import { PostListing } from "../post/listing";
import { Post } from "../post/object";
import { VoteableControls } from "../voteable/controls";

function isRemoved(dat: Data) {
if (dat.removed != undefined) return dat.removed as boolean;
Expand All @@ -30,7 +30,7 @@ type SplitRawPost = [ListingObject, ListingObject];
*
* @category Controls
*/
export class PostControls extends VoteableControls {
export class PostControls extends LockableControls {
/** @internal */
constructor(client: Client) {
super(client, "t3");
Expand Down Expand Up @@ -212,28 +212,6 @@ export class PostControls extends VoteableControls {
await this.gateway.post("api/unhide", { id: this.namespace(id) });
}

/**
* Lock a post, preventing non-moderators from being able to post comments.
*
* @param id The ID of the post to lock.
*
* @returns A promise that resolves when the post has been locked.
*/
async lock(id: string): Promise<void> {
await this.gateway.post("api/lock", { id: this.namespace(id) });
}

/**
* Unlock a post, allowing non-moderators from being able to post comments.
*
* @param id The ID of the post to unlock.
*
* @returns A promise that resolves when the post has been unlocked.
*/
async unlock(id: string): Promise<void> {
await this.gateway.post("api/unlock", { id: this.namespace(id) });
}

/**
* Mark a post as NSFW.
*
Expand Down
31 changes: 4 additions & 27 deletions src/reddit/post/object.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Maybe } from "../../helper/types";
import type { Comment } from "../comment/object";
import type { Listing } from "../listing/listing";
import type { LockableData } from "../lockable/object";
import type { PostControls } from "../post/controls";
import type { LinkPostOptions } from "../subreddit/controls";
import type { VoteableData } from "../voteable/object";

import { Voteable } from "../voteable/object";
import { Lockable } from "../lockable/object";

// TODO: Fully document Media
// This looks like it can be split into two interfaces?
Expand Down Expand Up @@ -74,7 +74,7 @@ import { Voteable } from "../voteable/object";
// }

/** The attributes specific to Post objects. */
export interface PostData extends VoteableData {
export interface PostData extends LockableData {
/**
* The text body of the post.
*
Expand Down Expand Up @@ -142,9 +142,6 @@ export interface PostData extends VoteableData {
// linkFlairTextColor: "dark" | "light";
// linkFlairType: "text" | "richtext";

/** Whether or not this post is locked. */
locked: boolean;

// TODO: Document or remove PostData.media*
// media: Maybe<Media>; // seems to always be undefined
// mediaEmbed: MediaEmbed; // seems to always be {}
Expand Down Expand Up @@ -238,7 +235,7 @@ export interface PostData extends VoteableData {
}

/** A single post. */
export class Post extends Voteable implements PostData {
export class Post extends Lockable implements PostData {
body: string;
bodyHtml: Maybe<string>;
// clicked: boolean;
Expand All @@ -262,7 +259,6 @@ export class Post extends Voteable implements PostData {
// linkFlairText: Maybe<string>;
// linkFlairTextColor: "dark" | "light";
// linkFlairType: "text" | "richtext";
locked: boolean;
// media: Maybe<Media>;
// mediaEmbed: MediaEmbed;
// mediaOnly: boolean;
Expand Down Expand Up @@ -323,7 +319,6 @@ export class Post extends Voteable implements PostData {
// this.linkFlairText = data.linkFlairText;
// this.linkFlairTextColor = data.linkFlairTextColor;
// this.linkFlairType = data.linkFlairType;
this.locked = data.locked;
// this.media = data.media;
// this.mediaEmbed = data.mediaEmbed;
// this.mediaOnly = data.mediaOnly;
Expand Down Expand Up @@ -443,24 +438,6 @@ export class Post extends Voteable implements PostData {
return this.controls.unhide(this.id);
}

/**
* Lock this post, preventing non-moderators from being able to post comments.
*
* @returns A promise that resolves when the post has been locked.
*/
async lock(): Promise<void> {
return this.controls.lock(this.id);
}

/**
* Unlock this post, allowing non-moderators from being able to post comments.
*
* @returns A promise that resolves when the post has been unlocked.
*/
async unlock(): Promise<void> {
return this.controls.unlock(this.id);
}

/**
* Mark this post as NSFW.
*
Expand Down

0 comments on commit 766726b

Please sign in to comment.