frontend/ts/adapter-element.ts (raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
import util from "./util" import { Message, Author } from "./message" import { MessageThread } from "./thread" import { AdapterState } from "./adapter" import { BatchTimer } from "./batch-timer" export class AdapterElement extends HTMLElement { static observedAttributes = [ "data-latest", "data-view", "data-viewing" ] private _latest: string = "" ; private _view: string = ""; private _name: string = "" private _viewing: string = ""; private _convoyBatchTimer = new BatchTimer((ids: string[])=>{ let url = `/api/adapters/${this._name}/fetch?entity_type=convoy`; for (let id of ids) { url += `&entity_id=${id}`; } util.authorizedFetch("GET", url, null) }); // TODO: use visibility of the thread to organize into DMs and public threads private _threads: MessageThread[] = []; private _orphans: Message[] = []; constructor() { super(); } connectedCallback() { const name = this.getAttribute("data-name"); this._name = name ?? ""; this._view = ""; this.buildThreads(); this.setAttribute("data-view", "index"); } attributeChangedCallback() { // set the viewing subject if it's changed const viewing = this.getAttribute("data-viewing"); if (this._viewing != viewing && viewing != null) { console.log(`${this._name}.attributeChangedCallback: resetting viewing subject`); this._viewing = viewing; // if the viewing subject changed (not to nothing), unset the view // this will force it to refresh if (this._viewing) { console.log(`${this._name}.attributeChangedCallback: forcing view update`); this._view = ""; } } // initialize the view if it's changed const view = this.getAttribute("data-view"); if (this._view != view ?? "index") { this._view = view ?? "index"; console.log(`${this._name}.attributeChangedCallback: setting view: ${this._view}`); switch (this._view) { case "index": this.setIdxView(); this.populateIdxView(); break; case "thread": this.setThreadView(); this.populateThreadView(); break; case "profile": this.setProfileView(); this.populateProfileView(); break; } } // if latest changed, check if it's a message const latest = this.getAttribute("data-latest"); if (latest ?? "" != this._latest) { this._latest = latest ?? ""; let datastore = AdapterState._instance.data.get(this._name); if (!datastore) { //util.errMsg(this._name + " has no datastore!"); return; } const latestMsg = datastore.messages.get(this._latest); if (latestMsg) { console.log('latest was a message; place it'); const rootId = this.placeMsg(this._latest); // if rootId is null, this is an orphan and we don't need to actually do any updates yet if (rootId) { switch (this._view) { case "index": console.log(`message was placed in thread ${rootId}, update view`) this.updateIdxView(this._latest, rootId); break; case "thread": // if the the message is part of this thread, update it case "profile": // if the message is from this user, show it in their profile break; } } } else { const latestAuthor = datastore.profileCache.get(this._latest); if (latestAuthor) { switch (this._view) { case "index": console.log (`author was updated: ${this._latest}, update their threads`) const threadsByThisAuthor = this._threads.filter(t=>t.root.data.author == this._latest); for (let t of threadsByThisAuthor) { let tse = this.querySelector(`underbbs-thread-summary[data-msg='${t.root.data.id}']`) if (tse) { console.log(`author has a thread in the dom, update it: ${t.root.data.id}`) tse.setAttribute("data-author", this._latest); } } case "thread": case "profile": break; } } } // so, try to insert it into the threads // then, switch on view // if index, iterate through the topics and find the one to indicate new activity, // if thread, if any relatives are in this thread, insert message appropriately // if profile, if latest is this profile, update it } } setIdxView() { this.innerHTML = "<ul id='dm_list'></ul><ul id='public_list'></ul>" } setThreadView() { let html = `<a href="#${this._name}">← return to index</a>`; html += "<ul id='msg_list'></ul>"; this.innerHTML = html; } setProfileView() { let profile_bar = util.$("profile_bar"); if (profile_bar) { // clear any previous data } else { // insert the profileSidebar into the dom } } populateIdxView() { // skip dm list for now // public/unified list const pl = util.$("public_list"); if (pl) { let html = ""; for (const t of this._threads.sort((a: MessageThread, b: MessageThread) => b.latest - a.latest)) { 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}" data-latest="${t.latest}"></underbbs-thread-summary></li>`; } pl.innerHTML = html; } } updateIdxView(latest: string, rootId: string) { const existingThread = document.querySelector(`underbbs-thread-summary[data-msg='${rootId}']`); const thread = this._threads.find(t=>t.root.data.id == rootId); console.log(`looking for thread ${rootId}`) console.log(`- DOM object: ${existingThread}`); console.log(`- in memory: ${thread}`); if (existingThread && thread) { console.log(`updating thread: ${thread.root.data.id} // ${thread.messageCount} NEW`) existingThread.setAttribute("data-latest", `${thread.latest}`); existingThread.setAttribute("data-len", `${thread.messageCount}`); existingThread.setAttribute("data-new", "true"); } else { // unified/public list for now const pl = util.$("public_list"); if (pl && thread) { const li = document.createElement("li"); li.innerHTML = `<underbbs-thread-summary data-len="1" data-adapter="${thread.root.data.adapter}" data-msg="${thread.root.data.id}" data-latest="${thread.latest}" data-created="${thread.created}"></underbbs-thread-summary>`; let nextThread: Element | null = null; for (let i = 0; i < pl.children.length; i++) { const c = pl.children.item(i); const latest = c?.children.item(0)?.getAttribute("data-latest") if (latest && parseInt(latest) < thread.latest) { nextThread = c; break; } } if (nextThread) { nextThread.insertAdjacentElement('beforebegin', li) return } pl.append(li); } } } populateThreadView() { } populateProfileView() { } buildThreads() { const datastore = AdapterState._instance.data.get(this._name); if (!datastore) { util.errMsg(this._name + " has no datastore!"); return; } // 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{ 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.size); } placeMsg(k: string): string | null { const datastore = AdapterState._instance.data.get(this._name); if (!datastore) { util.errMsg(this._name + " has no datastore!"); return null; } const msg = datastore.messages.get(k); if (!msg) { util.errMsg(`message [${this._name}:${k}] doesn't exist`); return null; } for (let t of this._threads) { // avoid processing nodes again on subsequent passes if (!msg || t.findNode(t.root, msg.id)) { return null; } if (msg.replyTo) { let x = t.addReply(msg.replyTo, msg); if (x) { // after adding, we try to adopt some orphans const orphanChildren = this._orphans.filter(m=>m.replyTo == k); for (let o of orphanChildren) { let adopted = this.placeMsg(o.id); if (adopted) { this._orphans.splice(this._orphans.indexOf(o), 1); } } return t.root.data.id; } } } // 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)); // after adding, we try to adopt some orphans const orphanChildren = this._orphans.filter(m=>m.replyTo == k); for (let o of orphanChildren) { let adopted = this.placeMsg(o.id); if (adopted) { this._orphans.splice(this._orphans.indexOf(o), 1); } } return msg.id; } // then, we should check if its parent is an orphan const orphanedParent = this._orphans.find(o=>o.id == msg.replyTo); if (orphanedParent) { // then, try to place them both if (this.placeMsg(orphanedParent.id)) { this._orphans.splice(this._orphans.indexOf(orphanedParent), 1); return this.placeMsg(k); } } // 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); if (msg.replyTo) { this._convoyBatchTimer.queue(k, 2000); } } return null; } } |