underbbs/frontend/ts/thread.ts

69 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

import util from "./util"
import { Message } from "./message"
export class MessageNode {
public parent: MessageNode | null = null;
public children: MessageNode[] = [];
public data: Message;
constructor(msg: Message, parent: MessageNode | null = null) {
this.data = msg;
this.parent = parent;
}
findRoot(): MessageNode {
let self: MessageNode | null = this;
while(self.parent) {
self = self.parent;
}
return self;
}
}
export class MessageThread {
public root: MessageNode;
public messageCount: number;
public visibility: string;
public created: number;
public latest: number;
constructor(first: Message) {
this.root = new MessageNode(first);
this.messageCount = 1;
this.visibility = first.visibility;
this.created = first.created;
this.latest = first.edited ? first.edited : first.created;
}
2024-07-05 05:23:56 +00:00
addReply(parentID: string, reply: Message): boolean {
let node = this.findNode(this.root, parentID);
if (node) {
node.children.push(new MessageNode(reply, node));
this.messageCount++;
const mtime = reply.edited ? reply.edited : reply.created;
if (this.latest < mtime) {
this.latest = mtime;
}
2024-07-05 05:23:56 +00:00
return true;
}
2024-07-05 05:23:56 +00:00
return false;
}
findNode(node: MessageNode, id: string): MessageNode | null {
if (node.data.id == id) {
return node;
} else {
for (let n of node.children) {
const x = this.findNode(n, id);
if (x) {
return x;
}
}
}
return null;
}
}