import util from "./util" import { Message } from "./message" var _ = util._ var $ = util.$ 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: Date; public latest: Date; 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; } 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.getTime() < mtime.getTime()) { this.latest = mtime; } return true; } 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 != null) { return x; } } } return null; } }