From d2180c87179939bcc72a61c8fadd27f9b95fb4e6 Mon Sep 17 00:00:00 2001 From: HumanoidSandvichDispenser Date: Wed, 1 May 2024 23:39:35 -0700 Subject: [PATCH] add forum thread viewing and replying --- src/client.ts | 7 +++++++ src/forum/index.ts | 36 ++++++++++++++++++++++++++++++++++++ src/forum/types.ts | 23 +++++++++++++++++++++++ src/index.ts | 2 ++ 4 files changed, 68 insertions(+) create mode 100644 src/forum/index.ts create mode 100644 src/forum/types.ts diff --git a/src/client.ts b/src/client.ts index 3e69c3d..10ed816 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,5 +1,6 @@ import { AskModule } from "./ask"; import { MessagesModule } from "./messages"; +import { ForumModule } from "./forum"; import { MethodCall, TCJSONResponse, TCResponse, TCResponseRaw } from "./types"; /** @@ -11,6 +12,7 @@ export class Client { #messages: MessagesModule; #ask: AskModule; + #forum: ForumModule; /** @internal */ public _cache: { [key: string]: any; } = { }; @@ -27,12 +29,17 @@ export class Client { return this.#ask; } + public get forum(): ForumModule { + return this.#forum; + } + public constructor(auth?: string) { this.auth = auth; // init modules this.#messages = new MessagesModule(this); this.#ask = new AskModule(this); + this.#forum = new ForumModule(this); } //public login(username: string, password: string) { diff --git a/src/forum/index.ts b/src/forum/index.ts new file mode 100644 index 0000000..66094a5 --- /dev/null +++ b/src/forum/index.ts @@ -0,0 +1,36 @@ +import { Module } from "../module"; +import { TCResponse } from "../types"; +import { ViewPostsResponse } from "./types"; + +export class ForumModule extends Module { + public async viewPosts( + threadId: number, + fetchType: string, + postId = 0, + reverse = false, + includePost = true, + ) { + return await this.client._call( + ViewPostsResponse, + "forum.viewposts", + { + getSomeBackscroll: reverse, + includePost, + threadId, + postId, + type: fetchType, + } + ); + } + + public async replyThread(text: string, threadId: number) { + return await this.client._call( + TCResponse, + "forum.replythread", + { + text, + threadId, + } + ) + } +} diff --git a/src/forum/types.ts b/src/forum/types.ts new file mode 100644 index 0000000..fa311c4 --- /dev/null +++ b/src/forum/types.ts @@ -0,0 +1,23 @@ +import { TCResponse, TCResponseRaw } from "../types"; + +export class ViewPostsResponse extends TCResponse { + posts: Post[]; + numPostsBefore: number; + numPostsAfter: number; + jumpTo: number; + + public constructor(response: TCResponseRaw) { + super(response); + this.posts = response["posts"] ?? []; + this.numPostsBefore = response["numPostsBefore"] ?? 0; + this.numPostsAfter = response["numPostsAfter"] ?? 0; + this.jumpTo = response["jumpTo"] ?? 0; + } +} + +export interface Post { + readonly id: number; + readonly user: string; + readonly text: string; + readonly time: number; +} diff --git a/src/index.ts b/src/index.ts index 6daa5d5..4e2b584 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,3 +5,5 @@ export * from "./messages"; export * as messages from "./messages/types"; export * from "./ask"; export * as ask from "./ask/types"; +export * from "./forum"; +export * as forum from "./forum/types";