From 63252a0a86412bcb50e1555a46bf21fb4cf10258 Mon Sep 17 00:00:00 2001 From: jerl Date: Mon, 15 May 2023 18:05:47 -0700 Subject: [PATCH] autoloadmessages --- client/chat.js | 53 +++++++++++++++++++++++++++-------------------- client/index.html | 12 +++++------ 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/client/chat.js b/client/chat.js index ee0c417..17f534a 100644 --- a/client/chat.js +++ b/client/chat.js @@ -19,15 +19,6 @@ async function auth() { window.socket.emit("authenticate", { name: window.name, message: sig }); } -function chooseThread(thread) { - window.currentThreadId = thread.id; - window.earliestMessage = null; - document.getElementById("loadmore").classList.remove("hidden"); - document.getElementById("chat").classList.remove("hidden"); - document.getElementById("messages").innerHTML = ""; - document.getElementById("threadname").innerHTML = thread.name; -} - async function loadKeys(keys) { const priv = await openpgp.readKey({ armoredKey: keys.privateKey }); const pub = await openpgp.readKey({ armoredKey: keys.publicKey }); @@ -35,6 +26,35 @@ async function loadKeys(keys) { await auth(); } +function chooseThread(thread) { + window.currentThreadId = thread.id; + window.earliestMessage = null; + document.getElementById("loadmore").classList.remove("hidden"); + document.getElementById("messages").innerHTML = ""; + document.getElementById("threadname").innerHTML = thread.name; +} + +function loadMessages() { + window.socket.emit( + "get_history", + { before: window.earliestMessage, thread: window.currentThreadId } + ); +} + +function addThread(thread) { + const el = document.createElement("div"); + el.classList.add("thread"); + el.innerHTML = thread.name; + const btn = document.createElement("button"); + btn.innerHTML = "choose"; + btn.onclick = () => { + chooseThread(thread); + loadMessages(); + }; + el.appendChild(btn); + document.getElementById("threadlist").appendChild(el); +} + window.onload = () => { window.currentThreadId = 1; window.socket = io(); @@ -83,16 +103,6 @@ window.onload = () => { else return emitter.call(window.socket, type); }; }); - function addThread(thread) { - const el = document.createElement("div"); - el.classList.add("thread"); - el.innerHTML = thread.name; - const btn = document.createElement("button"); - btn.innerHTML = "choose"; - btn.onclick = () => chooseThread(thread); - el.appendChild(btn); - document.getElementById("threadlist").appendChild(el); - } window.socket.on("list_threads", (msg) => { document.getElementById("threadlist").innerHTML = ""; for (let thread of msg.threads) @@ -137,10 +147,7 @@ window.onload = () => { document.getElementById("messages").appendChild(el); }; document.getElementById("loadmore").onclick = e => { - window.socket.emit( - "get_history", - { before: window.earliestMessage, thread: window.currentThreadId } - ); + loadMessages(); }; document.getElementById("createthread").onsubmit = e => { e.preventDefault(); diff --git a/client/index.html b/client/index.html index 7e56d6f..c374def 100644 --- a/client/index.html +++ b/client/index.html @@ -1,6 +1,9 @@ + + + @@ -61,20 +64,18 @@ - - -