persistence
All checks were successful
/ upload (release) Successful in 2m9s

This commit is contained in:
pavel 2026-02-07 19:36:34 +01:00
commit 55de4a1fc0
11 changed files with 2105 additions and 141 deletions

View file

@ -113,6 +113,11 @@
box-shadow: 0 0 5px var(--online-green);
}
.contact.offline .status-dot {
background: #666;
box-shadow: none;
}
.contact .username {
flex: 1;
font-weight: bold;
@ -338,7 +343,8 @@
const messageInput = document.getElementById("message");
// State
let onlineUsers = [];
let allUsers = [];
let onlineUsers = new Set();
let selectedUser = null;
let messageHistory = {}; // { username: [messages...] }
let unreadCounts = {}; // { username: count }
@ -466,20 +472,32 @@
function handleMessage(msg) {
switch (msg.type) {
case "identity":
currentUsername = msg.username;
console.log("Identity confirmed:", currentUsername);
// Re-filter contacts now that we know our username
allUsers = allUsers.filter(u => u !== currentUsername);
renderContacts();
break;
case "user_list":
onlineUsers = msg.users.filter(u => u !== currentUsername);
allUsers = msg.users;
onlineUsers = new Set(msg.online);
renderContacts();
break;
case "user_joined":
if (msg.username !== currentUsername && !onlineUsers.includes(msg.username)) {
onlineUsers.push(msg.username);
if (msg.username !== currentUsername) {
if (!allUsers.includes(msg.username)) {
allUsers.push(msg.username);
}
onlineUsers.add(msg.username);
renderContacts();
}
break;
case "user_left":
onlineUsers = onlineUsers.filter(u => u !== msg.username);
onlineUsers.delete(msg.username);
renderContacts();
if (selectedUser === msg.username) {
addSystemMessage(`${msg.username} has disconnected.`);
@ -491,13 +509,6 @@
break;
case "system":
// Extract username from welcome message
if (msg.content.startsWith("Welcome, ")) {
currentUsername = msg.content.replace("Welcome, ", "").replace("!", "");
// Re-filter contacts now that we know our username
onlineUsers = onlineUsers.filter(u => u !== currentUsername);
renderContacts();
}
addSystemMessage(msg.content);
break;
}
@ -531,9 +542,12 @@
function renderContacts() {
contactsList.innerHTML = "";
onlineUsers.forEach(username => {
allUsers.forEach(username => {
const div = document.createElement("div");
div.className = "contact" + (selectedUser === username ? " selected" : "");
const isOnline = onlineUsers.has(username);
div.className = "contact" +
(selectedUser === username ? " selected" : "") +
(isOnline ? "" : " offline");
div.onclick = () => selectUser(username);
const statusDot = document.createElement("span");
@ -558,7 +572,7 @@
});
}
function selectUser(username) {
async function selectUser(username) {
selectedUser = username;
unreadCounts[username] = 0; // Clear unread
chatHeader.textContent = `Chat with ${username}`;
@ -566,6 +580,27 @@
chatDiv.classList.add("active");
controls.classList.add("active");
renderContacts();
// Fetch history from DB
try {
const token = localStorage.getItem("access_token");
const response = await fetch(`/api/history?to=${encodeURIComponent(username)}`, {
headers: {
"Authorization": `Bearer ${token}`
}
});
if (response.ok) {
const history = await response.json();
messageHistory[username] = history.map(msg => ({
from: msg.from_user,
content: msg.content,
isSent: msg.from_user === currentUsername
}));
}
} catch (e) {
console.error("Failed to fetch history:", e);
}
renderChat();
messageInput.focus();
}