autoloadmessages

main
jerl 2023-05-15 18:05:47 -07:00
parent b2b15231df
commit 63252a0a86
2 changed files with 35 additions and 30 deletions

View File

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

View File

@ -1,6 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script src="/openpgp.min.js"></script>
<script src="/socket.io.min.v4.6.1.js"></script>
<script src="/chat.js"></script>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
@ -61,20 +64,18 @@
</div>
<div id="threads" class="column hidden">
<h1>vybe</h1>
<h1>threads</h1>
<h3>threads</h3>
<div id="threadlist">loading...</div>
<h3>create thread</h3>
<form id="createthread">
<label for="newthreadname">thread name</label>
<input type="text" id="newthreadname" />
<button id="submitthread" type="submit">create</button>
</form>
<h3>choose existing thread</h3>
<div id="threadlist">loading...</div>
</div>
<div id="chat" class="column hidden">
<h3 class="thread">
current thread: <strong id="threadname">meow</strong>
<button id="change">change thread</button>
</h3>
<h3>messages will appear below as they are sent</h3>
<button id="loadmore">load more messages</button>
@ -84,8 +85,5 @@
<button type="submit" class="hidden" id="sendmsg"></button>
</form>
</div>
<script src="/openpgp.min.js"></script>
<script src="/chat.js"></script>
<script src="/socket.io.min.v4.6.1.js"></script>
</body>
</html>