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: Iris Lightshard
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() {
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(`<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>`);
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.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) {
@ -194,15 +197,21 @@ 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;
}

View file

@ -76,7 +76,7 @@ export class TabBarElement extends HTMLElement {
return ()=>{
let x = $("mainarea_injectparent");
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);
}
}

View file

@ -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 || <Author>{ id: this._msg.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=${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
? `<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 latest = new Date(this.getAttribute("data-latest") ?? 0);
@ -74,18 +75,20 @@ export class ThreadSummaryElement extends HTMLElement {
let metadataChanged = false;
if (l != this._len) {
if (l && l != this._len) {
metadataChanged = true;
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;
this._latest = latest;
}
if (created != this._created) {
metadataChanged = true;
this._created = created;
}
if (newness != this._new) {
metadataChanged = true;
this._new = newness;
@ -98,7 +101,6 @@ export class ThreadSummaryElement extends HTMLElement {
}
}
}
viewThread(self: ThreadSummaryElement) {
return () => {
const a = $(`adapter_${self._adapter}`);

View file

@ -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;