// static/script.js
(function() {
'use strict';
// =============================================
// State
// =============================================
const State = {
token: localStorage.getItem('token'),
user: null,
ws: null,
wsState: 'disconnected',
messages: new Map(),
messageOrder: [],
cursor: null,
hasMore: true,
replyTo: null,
pendingFile: null,
typingUsers: new Map(),
onlineUsers: new Map(),
isAtBottom: true,
theme: localStorage.getItem('theme') || 'light',
customColors: {
self: localStorage.getItem('custom-self') || '#2563EB',
other: localStorage.getItem('custom-other') || '#F3F4F6',
text: localStorage.getItem('custom-text') || '#111827'
}
};
// =============================================
// DOM Cache
// =============================================
const DOM = {
authScreen: document.getElementById('auth-screen'),
chatScreen: document.getElementById('chat-screen'),
loginForm: document.getElementById('login-form'),
signupForm: document.getElementById('signup-form'),
authTabs: document.querySelectorAll('.auth-tab'),
authError: document.getElementById('auth-error'),
authErrorText: document.getElementById('auth-error-text'),
authLoading: document.getElementById('auth-loading'),
headerDisplayName: document.getElementById('header-display-name'),
headerAvatar: document.getElementById('header-avatar'),
headerAvatarPlaceholder: document.getElementById('header-avatar-placeholder'),
headerStatus: document.getElementById('header-status'),
onlineBadge: document.getElementById('online-badge'),
connectionStatus: document.getElementById('connection-status'),
connectionText: document.getElementById('connection-text'),
messagesList: document.getElementById('messages-list'),
messagesScroll: document.getElementById('messages-scroll'),
emptyState: document.getElementById('empty-state'),
loadOlderBtn: document.getElementById('load-older-btn'),
scrollBottomBtn: document.getElementById('scroll-bottom-btn'),
typingIndicator: document.getElementById('typing-indicator'),
typingText: document.getElementById('typing-text'),
replyPreview: document.getElementById('reply-preview'),
replyUser: document.getElementById('reply-user'),
replyText: document.getElementById('reply-text'),
composeForm: document.getElementById('compose-form'),
messageInput: document.getElementById('message-input'),
sendBtn: document.getElementById('send-btn'),
attachBtn: document.getElementById('attach-btn'),
emojiBtn: document.getElementById('emoji-btn'),
fileInput: document.getElementById('file-input'),
uploadPreview: document.getElementById('upload-preview'),
uploadFilename: document.getElementById('upload-filename'),
cancelUpload: document.getElementById('cancel-upload'),
uploadProgress: document.getElementById('upload-progress'),
progressFill: document.getElementById('progress-fill'),
progressText: document.getElementById('progress-text'),
settingsModal: document.getElementById('settings-modal'),
onlineModal: document.getElementById('online-modal'),
emojiPicker: document.getElementById('emoji-picker'),
emojiGrid: document.getElementById('emoji-grid'),
settingsDisplayName: document.getElementById('settings-display-name'),
settingsUsername: document.getElementById('settings-username'),
settingsAvatarPreview: document.getElementById('settings-avatar-preview'),
settingsAvatarImg: document.getElementById('settings-avatar-img'),
avatarInput: document.getElementById('avatar-input'),
avatarUploadBtn: document.getElementById('avatar-upload-btn'),
themeOptions: document.querySelectorAll('.theme-option'),
customThemeSettings: document.getElementById('custom-theme-settings'),
customSelfColor: document.getElementById('custom-self-color'),
customOtherColor: document.getElementById('custom-other-color'),
customTextColor: document.getElementById('custom-text-color'),
selfColorHex: document.getElementById('self-color-hex'),
otherColorHex: document.getElementById('other-color-hex'),
textColorHex: document.getElementById('text-color-hex'),
onlineBtn: document.getElementById('online-btn'),
onlineUsersList: document.getElementById('online-users-list'),
onlineTotal: document.getElementById('online-total'),
toastContainer: document.getElementById('toast-container')
};
const API = window.location.origin;
// =============================================
// File URL helper - uses query param for token
// so img src, video src, window.open all work
// =============================================
function fileUrl(filePath) {
return `${API}/api/download/${encodeURIComponent(filePath)}?t=${encodeURIComponent(State.token)}`;
}
// =============================================
// Theme
// =============================================
function applyTheme(theme) {
document.documentElement.setAttribute('data-theme', theme);
State.theme = theme;
localStorage.setItem('theme', theme);
if (theme === 'custom') {
const selfColor = State.customColors.self;
const otherColor = State.customColors.other;
const textColor = State.customColors.text;
document.documentElement.style.setProperty('--bubble-self', selfColor);
document.documentElement.style.setProperty('--bubble-other', otherColor);
document.documentElement.style.setProperty('--bubble-other-text', textColor);
const isLight = (c) => {
const r = parseInt(c.slice(1,3),16), g = parseInt(c.slice(3,5),16), b = parseInt(c.slice(5,7),16);
return (0.299*r + 0.587*g + 0.114*b) > 150;
};
document.documentElement.style.setProperty('--bubble-self-text', isLight(selfColor) ? '#111827' : '#FFFFFF');
} else {
document.documentElement.style.removeProperty('--bubble-self');
document.documentElement.style.removeProperty('--bubble-other');
document.documentElement.style.removeProperty('--bubble-other-text');
document.documentElement.style.removeProperty('--bubble-self-text');
}
}
applyTheme(State.theme);
// =============================================
// Timestamp updater - refreshes every 30s
// =============================================
function startTimestampUpdater() {
setInterval(() => {
document.querySelectorAll('.message-time[data-ts]').forEach(el => {
el.textContent = formatTime(parseInt(el.dataset.ts));
});
}, 30000);
}
// =============================================
// Toast Notifications
// =============================================
function showToast(message, type = 'info') {
const toast = document.createElement('div');
toast.className = `toast toast-${type}`;
const icons = { success: 'check-circle', error: 'exclamation-circle', warning: 'exclamation-triangle', info: 'info-circle' };
toast.innerHTML = `${message}`;
DOM.toastContainer.appendChild(toast);
setTimeout(() => toast.remove(), 4000);
}
// =============================================
// Authentication
// =============================================
DOM.authTabs.forEach(tab => {
tab.addEventListener('click', () => {
const target = tab.dataset.tab;
DOM.authTabs.forEach(t => t.classList.remove('active'));
tab.classList.add('active');
DOM.loginForm.classList.toggle('active', target === 'login');
DOM.signupForm.classList.toggle('active', target === 'signup');
DOM.authError.style.display = 'none';
});
});
DOM.loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
const username = document.getElementById('login-username').value.trim();
const password = document.getElementById('login-password').value;
await login(username, password);
});
DOM.signupForm.addEventListener('submit', async (e) => {
e.preventDefault();
const username = document.getElementById('signup-username').value.trim();
const display = document.getElementById('signup-display').value.trim() || username;
const password = document.getElementById('signup-password').value;
await signup(username, password, display);
});
async function login(username, password) {
DOM.authLoading.classList.remove('hidden');
hideAuthError();
try {
const res = await fetch(`${API}/api/auth/login?username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`, { method: 'POST' });
const data = await res.json();
if (!res.ok) throw new Error(data.detail || 'Login failed');
handleAuthSuccess(data);
} catch (err) {
showAuthError(err.message);
} finally {
DOM.authLoading.classList.add('hidden');
}
}
async function signup(username, password, display) {
DOM.authLoading.classList.remove('hidden');
hideAuthError();
try {
const res = await fetch(`${API}/api/auth/signup?username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}&display_name=${encodeURIComponent(display)}`, { method: 'POST' });
const data = await res.json();
if (!res.ok) throw new Error(data.detail || 'Signup failed');
handleAuthSuccess(data);
} catch (err) {
showAuthError(err.message);
} finally {
DOM.authLoading.classList.add('hidden');
}
}
function handleAuthSuccess(data) {
State.token = data.token;
State.user = data.user;
localStorage.setItem('token', State.token);
showChat();
connectWebSocket();
}
function showAuthError(msg) {
DOM.authErrorText.textContent = msg;
DOM.authError.style.display = 'flex';
}
function hideAuthError() {
DOM.authError.style.display = 'none';
}
// =============================================
// WebSocket
// =============================================
function connectWebSocket() {
if (!State.token) return;
if (State.ws && State.ws.readyState === WebSocket.OPEN) return;
State.wsState = 'connecting';
updateConnectionUI();
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
const url = `${proto}//${location.host}/ws?token=${encodeURIComponent(State.token)}`;
State.ws = new WebSocket(url);
State.ws.onopen = () => {
State.wsState = 'connected';
updateConnectionUI();
loadMessages();
requestOnlineUsers();
};
State.ws.onmessage = (event) => {
try {
handleWSMessage(JSON.parse(event.data));
} catch (e) {
console.error('WS parse error', e);
}
};
State.ws.onclose = () => {
if (State.wsState === 'connected') {
State.wsState = 'reconnecting';
updateConnectionUI();
setTimeout(connectWebSocket, 3000);
} else {
State.wsState = 'disconnected';
updateConnectionUI();
}
};
State.ws.onerror = () => {
State.wsState = 'disconnected';
updateConnectionUI();
};
}
function updateConnectionUI() {
const status = DOM.connectionStatus;
status.className = 'connection-status';
switch (State.wsState) {
case 'connected':
status.classList.add('connected');
DOM.connectionText.textContent = 'Connected';
break;
case 'reconnecting':
status.classList.add('reconnecting');
DOM.connectionText.textContent = 'Reconnecting...';
break;
default:
status.classList.add('disconnected');
DOM.connectionText.textContent = 'Disconnected';
}
}
function handleWSMessage(msg) {
switch (msg.type) {
case 'connection_established':
State.user = {
id: msg.user_id,
username: msg.username,
display_name: msg.display_name,
avatar_path: msg.avatar_path
};
State.onlineUsers.clear();
msg.online_users.forEach(u => State.onlineUsers.set(u.id, u));
updateHeader();
updateOnlineBadge();
break;
case 'new_message': {
const m = msg.message;
// Deduplicate
if (State.messages.has(m.id)) {
// Update status if we already have it (e.g. optimistic)
const existing = State.messages.get(m.id);
existing.status = m.status;
updateMessageBubble(m.id);
return;
}
State.messages.set(m.id, m);
// Insert in correct sorted position
insertMessageIdSorted(m.id, m.timestamp_ms);
renderMessage(m, true);
if (State.isAtBottom) scrollToBottom();
if (document.visibilityState === 'visible') sendMarkRead(m.id);
if (document.visibilityState !== 'visible' && Notification.permission === 'granted') {
new Notification(m.sender_display_name || m.sender_username, {
body: m.content || 'Sent a file',
icon: '/static/icon.png'
});
}
break;
}
case 'messages_loaded':
// Sort loaded messages by id ascending before inserting
const sorted = [...msg.messages].sort((a, b) => a.id - b.id);
sorted.forEach(m => {
if (!State.messages.has(m.id)) {
State.messages.set(m.id, m);
insertMessageIdSorted(m.id, m.timestamp_ms);
}
});
State.hasMore = msg.has_more;
if (msg.next_cursor) State.cursor = msg.next_cursor;
renderAllMessages();
DOM.loadOlderBtn.classList.toggle('hidden', !msg.has_more);
break;
case 'user_status':
if (msg.status === 'online') {
State.onlineUsers.set(msg.user_id, {
id: msg.user_id,
username: msg.username,
display_name: msg.display_name,
avatar_path: msg.avatar_path
});
} else {
State.onlineUsers.delete(msg.user_id);
}
updateOnlineBadge();
break;
case 'typing_indicator':
if (msg.is_typing) {
State.typingUsers.set(msg.user_id, msg);
} else {
State.typingUsers.delete(msg.user_id);
}
updateTypingIndicator();
break;
case 'message_edited':
if (State.messages.has(msg.message_id)) {
const m = State.messages.get(msg.message_id);
m.content = msg.content;
m.is_edited = true;
updateMessageBubble(msg.message_id);
}
break;
case 'message_deleted':
if (State.messages.has(msg.message_id)) {
State.messages.get(msg.message_id).is_deleted = true;
updateMessageBubble(msg.message_id);
}
break;
case 'message_read':
if (State.messages.has(msg.message_id)) {
const m = State.messages.get(msg.message_id);
if (m.sender_id === State.user.id) {
m.status = 'read';
updateMessageBubble(msg.message_id);
}
}
break;
case 'online_users':
State.onlineUsers.clear();
msg.users.forEach(u => State.onlineUsers.set(u.id, u));
updateOnlineBadge();
if (!DOM.onlineModal.classList.contains('hidden')) renderOnlineUsers();
break;
case 'error':
showToast(msg.message, 'error');
break;
}
}
// Keep messageOrder sorted by id ascending at all times
function insertMessageIdSorted(id, ts) {
// Binary insert by id
let lo = 0, hi = State.messageOrder.length;
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (State.messageOrder[mid] < id) lo = mid + 1;
else hi = mid;
}
State.messageOrder.splice(lo, 0, id);
}
// =============================================
// Message Actions
// =============================================
function loadMessages() {
if (State.ws?.readyState === WebSocket.OPEN) {
State.ws.send(JSON.stringify({ type: 'load_messages', limit: 50 }));
}
}
function requestOnlineUsers() {
if (State.ws?.readyState === WebSocket.OPEN) {
State.ws.send(JSON.stringify({ type: 'get_online_users' }));
}
}
function sendMarkRead(messageId) {
if (State.ws?.readyState === WebSocket.OPEN) {
State.ws.send(JSON.stringify({ type: 'mark_read', up_to_message_id: messageId }));
}
}
// =============================================
// Rendering
// =============================================
// Build avatar HTML for a message sender
function buildSenderAvatarHTML(msg) {
if (msg.sender_avatar_path) {
const url = fileUrl(msg.sender_avatar_path);
return ``;
}
const initials = (msg.sender_display_name || msg.sender_username || '?').charAt(0).toUpperCase();
return `
${initials}
`;
}
function renderMessage(msg, append = false) {
if (DOM.messagesList.querySelector(`[data-id="${msg.id}"]`)) return;
const wrapper = createMessageElement(msg);
if (append) {
DOM.messagesList.appendChild(wrapper);
} else {
// Find correct position in DOM by id order
const ids = State.messageOrder;
const idx = ids.indexOf(msg.id);
const nextId = ids[idx + 1];
if (nextId) {
const nextEl = DOM.messagesList.querySelector(`[data-id="${nextId}"]`);
if (nextEl) {
DOM.messagesList.insertBefore(wrapper, nextEl);
return;
}
}
DOM.messagesList.appendChild(wrapper);
}
}
function createMessageElement(msg) {
const isSelf = msg.sender_id === State.user?.id;
const wrapper = document.createElement('div');
wrapper.className = `message-wrapper ${isSelf ? 'self' : 'other'}`;
wrapper.dataset.id = msg.id;
wrapper.innerHTML = buildMessageHTML(msg);
return wrapper;
}
function buildMessageHTML(msg) {
if (msg.is_deleted) {
return '
This message was deleted
';
}
const isSelf = msg.sender_id === State.user?.id;
const displayName = escapeHtml(msg.sender_display_name || msg.sender_username || 'Unknown');
let html = '';
// Sender info row (show for others, and also for self on first load so you know it's you)
if (!isSelf) {
html += `
`;
html += buildSenderAvatarHTML(msg);
html += `${displayName}`;
html += `
`;
// Message text (only if non-empty)
if (msg.content) {
html += `
${escapeHtml(msg.content)}
`;
}
// Attachment
if (msg.file_path) {
html += buildAttachmentHTML(msg);
}
// Meta row: time + edited + status
html += `
`;
html += `${formatTime(msg.timestamp_ms)}`;
if (msg.is_edited) html += ` edited`;
if (isSelf) {
const isRead = msg.status === 'read';
html += ``;
html += isRead ? '' : '';
html += ``;
}
html += `
`;
// Action buttons
html += `
`;
html += ``;
if (isSelf) {
html += ``;
html += ``;
}
html += `
`;
html += `
`; // close message-bubble
// Self sender info on right side
if (isSelf) {
html += `
`;
html += `${displayName}`;
html += buildSenderAvatarHTML(msg);
html += `
`;
}
return html;
}
function buildAttachmentHTML(msg) {
const url = fileUrl(msg.file_path);
const ft = (msg.file_type || '').toLowerCase();
const fname = escapeHtml(msg.file_name || 'File');
if (ft.startsWith('image/')) {
return `