Skip to content

Commit

Permalink
Basic topic
Browse files Browse the repository at this point in the history
  • Loading branch information
webdev03 committed Oct 9, 2023
1 parent 8ef936d commit 42f4f9f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
6 changes: 2 additions & 4 deletions src/classes/forums/Forum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ class Forum {
child.querySelector(".item span")!.innerText.split(" ")[0]
);
const isSticky = child.classList.contains("sticky");
const topic = new Topic({
id: Number(id),
const topic = new Topic(this.session, Number(id), {
title: title,
replyCount: replyCount,
sticky: isSticky,
session: this.session
sticky: isSticky
});
topics.push(topic);
});
Expand Down
68 changes: 50 additions & 18 deletions src/classes/forums/Topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,59 @@ import { streamToString } from "../../utils";
class Topic {
id: number;
session: Session;
sticky?: boolean;
title?: string;
replyCount?: number;
constructor({
id,
session,
sticky,
title,
replyCount
}: {
id: number;
session: Session;
data?: {
sticky?: boolean;
title?: string;
replyCount?: number;
}) {
title: string;
replyCount: number;
};
constructor(
session: Session,
id: number,
data?: {
sticky: boolean;
title: string;
replyCount: number;
}
) {
this.id = id;
this.session = session;
this.sticky = sticky;
this.title = title;
this.replyCount = replyCount;
if (data) this.data = data;
}

async setData() {
const request = await fetch(
`https://scratch.mit.edu/discuss/m/topic/${this.id}`
);
if (!request.ok)
throw Error(`Request failed with status ${request.status}`);
const dom = parse(await request.text());
const pageCount =
Number(dom.querySelector(".pagination > li:last-child span")?.text) || 1;
let replyCount = 0;
if (pageCount === 1)
replyCount = dom.querySelectorAll("article").length - 1;
else {
const lastPageReq = await fetch(
`https://scratch.mit.edu/discuss/m/topic/${this.id}/?page=${pageCount}`
);
if (!lastPageReq.ok)
throw Error(`Request failed with status ${request.status}`);
const lastPageDom = parse(await lastPageReq.text());
replyCount =
(pageCount - 1) * 20 +
lastPageDom.querySelectorAll("article").length -
1;
}

this.data = {
title: dom
.querySelector("nav > h1")!
.childNodes.filter((node) => node.nodeType === 3)
.map((node) => node.text)
.join("")
.trim(),
replyCount
};
}

/**
Expand Down

0 comments on commit 42f4f9f

Please sign in to comment.