add forum thread viewing and replying

master
John Montagu, the 4th Earl of Sandvich 2024-05-01 23:39:35 -07:00
parent c46a573596
commit d2180c8717
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
4 changed files with 68 additions and 0 deletions

View File

@ -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) {

36
src/forum/index.ts 100644
View File

@ -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,
}
)
}
}

23
src/forum/types.ts 100644
View File

@ -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;
}

View File

@ -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";