index view kinda working

This commit is contained in:
Iris Lightshard 2024-07-04 23:23:56 -06:00
parent fd2abcbb76
commit c03fbd7950
Signed by: nilix
GPG key ID: 688407174966CAF3
4 changed files with 69 additions and 57 deletions

View file

@ -24,8 +24,9 @@ export class AdapterElement extends HTMLElement {
connectedCallback() { connectedCallback() {
const name = this.getAttribute("data-name"); const name = this.getAttribute("data-name");
this._name = name ?? ""; this._name = name ?? "";
this._view = "";
this.buildThreads(); this.buildThreads();
this.attributeChangedCallback(); this.setAttribute("data-view", "index");
} }
attributeChangedCallback() { attributeChangedCallback() {
@ -43,9 +44,10 @@ export class AdapterElement extends HTMLElement {
// initialize the view if it's changed // initialize the view if it's changed
const view = this.getAttribute("data-view"); const view = this.getAttribute("data-view");
if (this._view != view) { if (this._view != view ?? "index") {
console.log("view changed! let's go") console.log("view changed! let's go")
this._view = view ?? ""; this._view = view ?? "index";
console.log(this._view);
switch (this._view) { switch (this._view) {
case "index": case "index":
this.setIdxView(); this.setIdxView();
@ -127,11 +129,11 @@ export class AdapterElement extends HTMLElement {
// public/unified list // public/unified list
const pl = $("public_list"); const pl = $("public_list");
if (pl) { if (pl) {
console.log(JSON.stringify(this._threads)); let html = "";
for (const t of this._threads) { for (const t of this._threads) {
console.log(t.root.data.id); html +=`<li><underbbs-thread-summary data-len="${t.messageCount}" data-adapter="${t.root.data.adapter}" data-msg="${t.root.data.id}" data-created="${t.created}"></underbbs-thread-summary></li>`;
pl.append(`<li><underbbs-thread-summary data-len="${t.messageCount}" data-adapter="${t.root.data.adapter}" data-msg="${t.root.data.id}" data-latest="${t.latest}" data-created="${t.created}" data-new=""></underbbs-thread-summary></li>`);
} }
pl.innerHTML = html;
} }
} }
@ -158,31 +160,32 @@ export class AdapterElement extends HTMLElement {
} }
buildThreads() { buildThreads() {
console.log("building threads for " + this._name);
const datastore = _("datastore")[this._name]; const datastore = _("datastore")[this._name];
// make multiple passes over the store until every message is either // 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 // placed in a thread, or orphaned and waiting for its parent to be returned
do{ do{
console.log("making a pass at all the messages");
for (let k of datastore.messages.keys()) { for (let k of datastore.messages.keys()) {
this.placeMsg(k); this.placeMsg(k);
} }
} while (this._threads.reduce((sum: number, thread: MessageThread)=>{ } while (this._threads.reduce((sum: number, thread: MessageThread)=>{
return sum + thread.messageCount; return sum + thread.messageCount;
}, 0) + this._orphans.length < datastore.messages.keys().length) }, 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 { placeMsg(k: string): string | null {
const msg = _("datastore")[this._name].messages.get(k); const msg = _("datastore")[this._name].messages.get(k);
if (msg.replyTo) { for (let t of this._threads) {
for (let t of this._threads) { // avoid processing nodes again on subsequent passes
// avoid processing nodes again on subsequent passes if (t.findNode(t.root, msg.id)) {
if (t.findNode(t.root, msg.id)) { return null;
return null; }
} if (msg.replyTo) {
let x = t.addReply(msg.replyTo, msg);
let x = t.findNode(t.root, msg.replyTo);
if (x) { if (x) {
t.addReply(msg.replyTo, msg);
// after adding, we try to adopt some orphans // after adding, we try to adopt some orphans
const orphanChildren = this._orphans.filter(m=>m.replyTo == k); const orphanChildren = this._orphans.filter(m=>m.replyTo == k);
for (let o of orphanChildren) { for (let o of orphanChildren) {
@ -194,15 +197,21 @@ export class AdapterElement extends HTMLElement {
return t.root.data.id; 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;
} }

View file

@ -76,7 +76,7 @@ export class TabBarElement extends HTMLElement {
return ()=>{ return ()=>{
let x = $("mainarea_injectparent"); let x = $("mainarea_injectparent");
if (x) { if (x) {
x.innerHTML = `<underbbs-adapter id="adapter_${adapter}" data-name="${adapter}" data-view="index"></underbbs-adapter>`; x.innerHTML = `<underbbs-adapter id="adapter_${adapter}" data-name="${adapter}"></underbbs-adapter>`;
self.setAttribute("data-currentadapter", adapter); self.setAttribute("data-currentadapter", adapter);
} }
} }

View file

@ -4,7 +4,7 @@ var _ = util._
var $ = util.$ var $ = util.$
export class ThreadSummaryElement extends HTMLElement { 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 _len: number = 0;;
private _msg: Message | null = null;; private _msg: Message | null = null;;
@ -32,7 +32,7 @@ export class ThreadSummaryElement extends HTMLElement {
attributeChangedCallback() { attributeChangedCallback() {
const datastore = _("datastore")[this._adapter]; const datastore = _("datastore")[this._adapter];
const msgId = this.getAttribute("data-msg"); 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); this._msg = datastore.messages.get(msgId);
if (this._msg) { if (this._msg) {
const threadText = this.querySelector(".thread_text"); const threadText = this.querySelector(".thread_text");
@ -46,26 +46,27 @@ export class ThreadSummaryElement extends HTMLElement {
this._author = author || <Author>{ id: this._msg.author }; this._author = author || <Author>{ id: this._msg.author };
const threadAuthor = this.querySelector(".thread_author"); const threadAuthor = this.querySelector(".thread_author");
if (threadAuthor && this._author) { if (threadAuthor && this._author) {
threadAuthor.innerHTML = this._author.profilePic
? `<img src="${this._author.profilePic}" alt="${this._author.id}"/> <a id="thread_${this._adapter}_${this._msg.id}_${this._author.id}" href="#author?id=${this._author.id}">${this._author.id}</a>`
: `<a id="thread_${this._adapter}_${this._msg.id}_${this._author.id}" href="#author?id=${this._author.id}">${this._author.id}</a>`;
}
}
}
// 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 threadAuthor.innerHTML = this._author.profilePic
? `<img src="${this._author.profilePic}" alt="${this._author.id}"/> <a id="thread_${this._adapter}_${this._msg.id}_${this._author.id}" href="#author?id=${this._author.id}>${this._author.id}</a>` ? `<img src="${this._author.profilePic}" alt="${this._author.id}"/> <a id="thread_${this._adapter}_${this._msg.id}_${this._author.id}" href="#author?id=${this._author.id}>${this._author.id}</a>`
: `<a id="thread_${this._adapter}_${this._msg.id}_${this._author.id}" href="#author?id=${this._author.id}>${this._author.id}</a>`; : `<a id="thread_${this._adapter}_${this._msg.id}_${this._author.id}" href="#author?id=${author.id}>${this._author.id}</a>` ;
} }
}
}
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) {
threadAuthor.innerHTML = this._author.profilePic
? `<img src="${this._author.profilePic}" alt="${this._author.id}"/> <a id="thread_${this._adapter}_${this._msg.id}_${this._author.id}" href="#author?id=${this._author.id}>${this._author.id}</a>`
: `<a id="thread_${this._adapter}_${this._msg.id}_${this._author.id}" href="#author?id=${author.id}>${this._author.id}</a>` ;
}
}
}
} }
const l = parseInt(this.getAttribute("data-len") ?? "0"); const l = parseInt(this.getAttribute("data-len") ?? "0");
const latest = new Date(this.getAttribute("data-latest") ?? 0); const latest = new Date(this.getAttribute("data-latest") ?? 0);
@ -74,18 +75,20 @@ export class ThreadSummaryElement extends HTMLElement {
let metadataChanged = false; let metadataChanged = false;
if (l != this._len) { if (l && l != this._len) {
metadataChanged = true; metadataChanged = true;
this._len = l; this._len = l;
} }
if (latest != this._latest) { if (created && created != this._created) {
metadataChanged = true;
this._created = created;
this._latest = created;
}
if (latest && latest != this._latest) {
metadataChanged = true; metadataChanged = true;
this._latest = latest; this._latest = latest;
} }
if (created != this._created) {
metadataChanged = true;
this._created = created;
}
if (newness != this._new) { if (newness != this._new) {
metadataChanged = true; metadataChanged = true;
this._new = newness; this._new = newness;
@ -98,7 +101,6 @@ export class ThreadSummaryElement extends HTMLElement {
} }
} }
} }
viewThread(self: ThreadSummaryElement) { viewThread(self: ThreadSummaryElement) {
return () => { return () => {
const a = $(`adapter_${self._adapter}`); const a = $(`adapter_${self._adapter}`);

View file

@ -38,16 +38,18 @@ export class MessageThread {
this.latest = first.edited ? first.edited : first.created; 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); let node = this.findNode(this.root, parentID);
if (node) { if (node) {
node.children.push(new MessageNode(reply, node)); node.children.push(new MessageNode(reply, node));
this.messageCount++; this.messageCount++;
const mtime = reply.edited ? reply.edited : reply.created; const mtime = reply.edited ? reply.edited : reply.created;
if (this.latest < mtime) { if (this.latest.getTime() < mtime.getTime()) {
this.latest = mtime; this.latest = mtime;
} }
return true;
} }
return false;
} }
findNode(node: MessageNode, id: string): MessageNode | null { findNode(node: MessageNode, id: string): MessageNode | null {
@ -55,7 +57,6 @@ export class MessageThread {
return node; return node;
} else { } else {
for (let n of node.children) { for (let n of node.children) {
console.log("descending through children...")
const x = this.findNode(n, id); const x = this.findNode(n, id);
if (x != null) { if (x != null) {
return x; return x;