diff --git a/ts/adapter-element.ts b/ts/adapter-element.ts index 0349a92..8a78900 100644 --- a/ts/adapter-element.ts +++ b/ts/adapter-element.ts @@ -24,8 +24,9 @@ export class AdapterElement extends HTMLElement { connectedCallback() { const name = this.getAttribute("data-name"); this._name = name ?? ""; + this._view = ""; this.buildThreads(); - this.attributeChangedCallback(); + this.setAttribute("data-view", "index"); } attributeChangedCallback() { @@ -43,9 +44,10 @@ export class AdapterElement extends HTMLElement { // initialize the view if it's changed const view = this.getAttribute("data-view"); - if (this._view != view) { + if (this._view != view ?? "index") { console.log("view changed! let's go") - this._view = view ?? ""; + this._view = view ?? "index"; + console.log(this._view); switch (this._view) { case "index": this.setIdxView(); @@ -127,11 +129,11 @@ export class AdapterElement extends HTMLElement { // public/unified list const pl = $("public_list"); if (pl) { - console.log(JSON.stringify(this._threads)); + let html = ""; for (const t of this._threads) { - console.log(t.root.data.id); - pl.append(`
  • `); + html +=`
  • `; } + pl.innerHTML = html; } } @@ -158,31 +160,32 @@ export class AdapterElement extends HTMLElement { } buildThreads() { + console.log("building threads for " + this._name); const datastore = _("datastore")[this._name]; // make multiple passes over the store until every message is either // placed in a thread, or orphaned and waiting for its parent to be returned do{ + console.log("making a pass at all the messages"); for (let k of datastore.messages.keys()) { this.placeMsg(k); } } while (this._threads.reduce((sum: number, thread: MessageThread)=>{ return sum + thread.messageCount; }, 0) + this._orphans.length < datastore.messages.keys().length) + console.log("all messages have been placed"); + console.log(JSON.stringify(this._threads)); } placeMsg(k: string): string | null { const msg = _("datastore")[this._name].messages.get(k); - if (msg.replyTo) { - for (let t of this._threads) { - // avoid processing nodes again on subsequent passes - if (t.findNode(t.root, msg.id)) { - return null; - } - - let x = t.findNode(t.root, msg.replyTo); + for (let t of this._threads) { + // avoid processing nodes again on subsequent passes + if (t.findNode(t.root, msg.id)) { + return null; + } + if (msg.replyTo) { + let x = t.addReply(msg.replyTo, msg); if (x) { - t.addReply(msg.replyTo, msg); - // after adding, we try to adopt some orphans const orphanChildren = this._orphans.filter(m=>m.replyTo == k); for (let o of orphanChildren) { @@ -192,17 +195,23 @@ export class AdapterElement extends HTMLElement { } } return t.root.data.id; - } - } - if (this._orphans.filter(o=>o.id == msg.id).length == 0) { - this._orphans.push(msg); - // TODO: request the parent's data - } - return null; - } else { - this._threads.push(new MessageThread(msg)); - return k; + } + } } + // if we made it this far, this message doesn't go in any existing thread + + // if it doesn't have a parent, we can make a new thread with it + if (!msg.replyTo) { + this._threads.push(new MessageThread(msg)); + return msg.id; + } + + // otherwise we can orphan it and try to fill it in later + if (this._orphans.filter(o=>o.id == msg.id).length == 0) { + this._orphans.push(msg); + // TODO: request the parent's data + } + return null; } diff --git a/ts/tabbar-element.ts b/ts/tabbar-element.ts index 81f3c37..c921d29 100644 --- a/ts/tabbar-element.ts +++ b/ts/tabbar-element.ts @@ -76,7 +76,7 @@ export class TabBarElement extends HTMLElement { return ()=>{ let x = $("mainarea_injectparent"); if (x) { - x.innerHTML = ``; + x.innerHTML = ``; self.setAttribute("data-currentadapter", adapter); } } diff --git a/ts/thread-summary-element.ts b/ts/thread-summary-element.ts index 196ed90..4f75860 100644 --- a/ts/thread-summary-element.ts +++ b/ts/thread-summary-element.ts @@ -4,7 +4,7 @@ var _ = util._ var $ = util.$ export class ThreadSummaryElement extends HTMLElement { - static observedAttributes = [ "data-len", "data-msg", "data-author", "data-latest", "data-created", "data-new" ]; + static observedAttributes = [ "data-len", "data-author", "data-latest", "data-new" ]; private _len: number = 0;; private _msg: Message | null = null;; @@ -32,7 +32,7 @@ export class ThreadSummaryElement extends HTMLElement { attributeChangedCallback() { const datastore = _("datastore")[this._adapter]; const msgId = this.getAttribute("data-msg"); - if (msgId && datastore && ((this._msg && this._msg.id != msgId) || !this._msg)) { + if (msgId && datastore && !this._msg) { this._msg = datastore.messages.get(msgId); if (this._msg) { const threadText = this.querySelector(".thread_text"); @@ -46,26 +46,27 @@ export class ThreadSummaryElement extends HTMLElement { this._author = author || { id: this._msg.author }; const threadAuthor = this.querySelector(".thread_author"); if (threadAuthor && this._author) { + threadAuthor.innerHTML = this._author.profilePic + ? `${this._author.id} ${this._author.id}` + : `${this._author.id}`; + } + } + } + + // update author if it's passed in the attribute + const authorId = this.getAttribute("data-author"); + if (authorId) { + let author = datastore?.profileCache?.get(this._msg?.author); + if (author) { + this._author = author; + const threadAuthor = this.querySelector(".thread_author"); + if (threadAuthor && this._author && this._msg) { threadAuthor.innerHTML = this._author.profilePic ? `${this._author.id} ${this._new ? "!" : ""}[${this._len}] created: ${this._created}, updated: ${this._latest}`; } } - } - + } viewThread(self: ThreadSummaryElement) { return () => { const a = $(`adapter_${self._adapter}`); diff --git a/ts/thread.ts b/ts/thread.ts index 968c935..143f88b 100644 --- a/ts/thread.ts +++ b/ts/thread.ts @@ -38,16 +38,18 @@ export class MessageThread { this.latest = first.edited ? first.edited : first.created; } - addReply(parentID: string, reply: Message) { + 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) { + if (this.latest.getTime() < mtime.getTime()) { this.latest = mtime; } + return true; } + return false; } findNode(node: MessageNode, id: string): MessageNode | null { @@ -55,7 +57,6 @@ export class MessageThread { return node; } else { for (let n of node.children) { - console.log("descending through children...") const x = this.findNode(n, id); if (x != null) { return x;