fixes, hopefully
This commit is contained in:
parent
7495e34ade
commit
cbc24e7672
13 changed files with 449 additions and 78 deletions
|
|
@ -26,7 +26,7 @@
|
|||
userBadge: document.getElementById('userBadge'),
|
||||
|
||||
// Chat View
|
||||
activeChannelName: document.getElementById('activeChannelName'),
|
||||
channelSelectDropdown: document.getElementById('channelSelectDropdown'),
|
||||
activeChannelTopic: document.getElementById('activeChannelTopic'),
|
||||
chatMessages: document.getElementById('chatMessages'),
|
||||
messageInput: document.getElementById('messageInput'),
|
||||
|
|
@ -66,7 +66,8 @@
|
|||
authInviteToken: document.getElementById('authInviteToken'),
|
||||
authSubmitBtn: document.getElementById('authSubmitBtn'),
|
||||
authError: document.getElementById('authError'),
|
||||
toastContainer: document.getElementById('toastContainer')
|
||||
toastContainer: document.getElementById('toastContainer'),
|
||||
dropOverlay: document.getElementById('dropOverlay')
|
||||
};
|
||||
|
||||
// ==========================================
|
||||
|
|
@ -229,66 +230,84 @@
|
|||
}
|
||||
|
||||
function handleNetworkEnvelope(envelope) {
|
||||
const payload = JSON.parse(envelope.payload || '{}');
|
||||
const type = (envelope.type || envelope.Type || '').toUpperCase();
|
||||
const payloadRaw = envelope.payload ?? envelope.Payload ?? '{}';
|
||||
let payload = {};
|
||||
try {
|
||||
payload = typeof payloadRaw === 'string' ? JSON.parse(payloadRaw) : payloadRaw;
|
||||
} catch (e) {
|
||||
console.error('Failed to parse envelope payload', e);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (envelope.type) {
|
||||
switch (type) {
|
||||
case 'CHANNEL_LIST_SYNC':
|
||||
state.channels = payload.channels || [];
|
||||
if (payload.activeChannelId) {
|
||||
state.activeChannelId = payload.activeChannelId;
|
||||
state.channels = payload.channels || payload.Channels || [];
|
||||
const activeCh = payload.activeChannelId || payload.ActiveChannelId;
|
||||
if (activeCh) {
|
||||
state.activeChannelId = activeCh;
|
||||
} else if (!state.activeChannelId && state.channels.length > 0) {
|
||||
const first = state.channels[0];
|
||||
state.activeChannelId = first.id || first.Id;
|
||||
}
|
||||
renderChannels();
|
||||
updateActiveChannelHeader();
|
||||
break;
|
||||
|
||||
case 'HISTORY_SYNC':
|
||||
if (payload.channelId) {
|
||||
state.messages[payload.channelId] = payload.messages || [];
|
||||
if (state.activeChannelId === payload.channelId) {
|
||||
const histChId = payload.channelId || payload.ChannelId;
|
||||
if (histChId) {
|
||||
state.messages[histChId] = payload.messages || payload.Messages || [];
|
||||
if (state.activeChannelId === histChId) {
|
||||
renderMessages();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'MESSAGE_BROADCAST':
|
||||
if (payload.message) {
|
||||
const chId = payload.message.channelId;
|
||||
const msg = payload.message || payload.Message;
|
||||
if (msg) {
|
||||
const chId = msg.channelId || msg.ChannelId;
|
||||
if (!state.messages[chId]) state.messages[chId] = [];
|
||||
state.messages[chId].push(payload.message);
|
||||
state.messages[chId].push(msg);
|
||||
if (state.activeChannelId === chId) {
|
||||
appendMessage(payload.message);
|
||||
appendMessage(msg);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'USER_LIST_SYNC':
|
||||
state.users = payload.users || [];
|
||||
state.users = payload.users || payload.Users || [];
|
||||
renderUsers();
|
||||
break;
|
||||
|
||||
case 'FILE_LIST_SYNC':
|
||||
state.files = payload.files || [];
|
||||
state.files = payload.files || payload.Files || [];
|
||||
renderFiles();
|
||||
break;
|
||||
|
||||
case 'SYSTEM_NOTICE':
|
||||
if (payload.text) {
|
||||
showToast(payload.text);
|
||||
const noticeText = payload.text || payload.Text;
|
||||
if (noticeText) {
|
||||
showToast(noticeText);
|
||||
if (state.activeChannelId) {
|
||||
appendSystemNotice(payload.text);
|
||||
appendSystemNotice(noticeText);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'COMMAND_RESULT':
|
||||
if (payload.output) {
|
||||
appendSystemNotice(payload.output);
|
||||
const cmdOut = payload.output || payload.Output;
|
||||
if (cmdOut) {
|
||||
appendSystemNotice(cmdOut);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'ERROR':
|
||||
if (payload.message) {
|
||||
showToast(`Error: ${payload.message}`);
|
||||
const errMsg = payload.message || payload.Message;
|
||||
if (errMsg) {
|
||||
showToast(`Error: ${errMsg}`);
|
||||
appendSystemNotice(`[Error] ${errMsg}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
@ -307,36 +326,61 @@
|
|||
}
|
||||
|
||||
function updateActiveChannelHeader() {
|
||||
const active = state.channels.find(c => c.id === state.activeChannelId);
|
||||
const active = state.channels.find(c => (c.id || c.Id) === state.activeChannelId);
|
||||
if (active) {
|
||||
el.activeChannelName.textContent = active.name;
|
||||
el.activeChannelTopic.textContent = active.topic ? ` - ${active.topic}` : '';
|
||||
el.messageInput.placeholder = `Message #${active.name}... (or /help)`;
|
||||
const chId = active.id || active.Id;
|
||||
const chName = active.name || active.Name || 'general';
|
||||
const chTopic = active.topic || active.Topic || '';
|
||||
if (el.channelSelectDropdown) {
|
||||
el.channelSelectDropdown.value = chId;
|
||||
}
|
||||
el.activeChannelTopic.textContent = chTopic ? ` - ${chTopic}` : '';
|
||||
el.messageInput.placeholder = `Message #${chName}... (or /help)`;
|
||||
}
|
||||
}
|
||||
|
||||
function renderChannels() {
|
||||
el.channelList.innerHTML = '';
|
||||
if (el.channelSelectDropdown) {
|
||||
el.channelSelectDropdown.innerHTML = '';
|
||||
}
|
||||
|
||||
state.channels.forEach(ch => {
|
||||
const chId = ch.id || ch.Id;
|
||||
const chName = ch.name || ch.Name;
|
||||
const chTopic = ch.topic || ch.Topic || '';
|
||||
const isActive = chId === state.activeChannelId;
|
||||
|
||||
// Channels View list item
|
||||
const item = document.createElement('div');
|
||||
item.className = 'channel-item' + (ch.id === state.activeChannelId ? ' active' : '');
|
||||
item.className = 'channel-item' + (isActive ? ' active' : '');
|
||||
item.innerHTML = `
|
||||
<div class="channel-meta">
|
||||
<span class="channel-title">#${escapeHtml(ch.name)}</span>
|
||||
<span class="channel-desc">${escapeHtml(ch.topic || '')}</span>
|
||||
<span class="channel-title">#${escapeHtml(chName)}</span>
|
||||
<span class="channel-desc">${escapeHtml(chTopic)}</span>
|
||||
</div>
|
||||
${ch.id === state.activeChannelId ? '<span class="channel-badge">ACTIVE</span>' : ''}
|
||||
${isActive ? '<span class="channel-badge">ACTIVE</span>' : ''}
|
||||
`;
|
||||
item.addEventListener('click', () => {
|
||||
joinChannel(ch.id);
|
||||
joinChannel(chId);
|
||||
switchTab('chat');
|
||||
});
|
||||
el.channelList.appendChild(item);
|
||||
|
||||
// In-chat dropdown option
|
||||
if (el.channelSelectDropdown) {
|
||||
const opt = document.createElement('option');
|
||||
opt.value = chId;
|
||||
opt.textContent = `#${chName}`;
|
||||
opt.selected = isActive;
|
||||
el.channelSelectDropdown.appendChild(opt);
|
||||
}
|
||||
});
|
||||
|
||||
// Show create button for Admin or Owner
|
||||
// Show create button for Admin, Owner, or if allowed for members
|
||||
const isPrivileged = state.user && (state.user.role === 'Owner' || state.user.role === 'Admin');
|
||||
el.openCreateChannelBtn.style.display = isPrivileged ? 'inline-flex' : 'none';
|
||||
const allowMemberChannels = state.serverInfo && state.serverInfo.allowMemberChannelCreation;
|
||||
el.openCreateChannelBtn.style.display = (isPrivileged || allowMemberChannels) ? 'inline-flex' : 'none';
|
||||
}
|
||||
|
||||
function joinChannel(channelId) {
|
||||
|
|
@ -349,11 +393,13 @@
|
|||
|
||||
function renderMessages() {
|
||||
el.chatMessages.innerHTML = '';
|
||||
const active = state.channels.find(c => (c.id || c.Id) === state.activeChannelId);
|
||||
const activeName = active ? (active.name || active.Name) : 'general';
|
||||
const msgs = state.messages[state.activeChannelId] || [];
|
||||
if (msgs.length === 0) {
|
||||
el.chatMessages.innerHTML = `
|
||||
<div class="welcome-message">
|
||||
<h3>Welcome to #${escapeHtml(el.activeChannelName.textContent)}</h3>
|
||||
<h3>Welcome to #${escapeHtml(activeName)}</h3>
|
||||
<p>No messages yet. Say hello!</p>
|
||||
</div>
|
||||
`;
|
||||
|
|
@ -362,37 +408,85 @@
|
|||
msgs.forEach(appendMessage);
|
||||
}
|
||||
|
||||
async function downloadFile(fileId, fileName) {
|
||||
if (!state.token) {
|
||||
showToast('Please login to download files');
|
||||
return;
|
||||
}
|
||||
showToast(`Downloading ${fileName}...`);
|
||||
try {
|
||||
const res = await fetch(`/api/files/${encodeURIComponent(fileId)}`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${state.token}`
|
||||
}
|
||||
});
|
||||
if (!res.ok) {
|
||||
showToast(`Failed to download ${fileName} (HTTP ${res.status})`);
|
||||
return;
|
||||
}
|
||||
const blob = await res.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = fileName;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
}, 200);
|
||||
} catch (err) {
|
||||
showToast(`Download error: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
function appendMessage(msg) {
|
||||
const welcome = el.chatMessages.querySelector('.welcome-message');
|
||||
if (welcome) welcome.remove();
|
||||
|
||||
const time = new Date(msg.createdAt).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
||||
const roleClass = (msg.senderRole || 'member').toLowerCase();
|
||||
const createdAt = msg.createdAt || msg.CreatedAt;
|
||||
const time = createdAt ? new Date(createdAt).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }) : '';
|
||||
const rawRole = msg.senderRole || msg.SenderRole || 'member';
|
||||
const roleClass = rawRole.toLowerCase();
|
||||
const sender = msg.senderUsername || msg.SenderUsername || 'User';
|
||||
const rawContent = msg.content || msg.Content || '';
|
||||
const attId = msg.attachmentId || msg.AttachmentId;
|
||||
const attName = msg.attachmentName || msg.AttachmentName;
|
||||
const attBytes = msg.attachmentSizeBytes ?? msg.AttachmentSizeBytes ?? 0;
|
||||
|
||||
const div = document.createElement('div');
|
||||
div.className = 'message-item';
|
||||
|
||||
let contentHtml = escapeHtml(msg.content);
|
||||
if (msg.attachmentId) {
|
||||
const sizeStr = formatBytes(msg.attachmentSizeBytes || 0);
|
||||
let contentHtml = escapeHtml(rawContent);
|
||||
if (attId) {
|
||||
const sizeStr = formatBytes(attBytes);
|
||||
const safeId = escapeHtml(attId);
|
||||
const safeName = escapeHtml(attName || 'file');
|
||||
contentHtml += `
|
||||
<div>
|
||||
<a class="attachment-card" href="/api/files/${encodeURIComponent(msg.attachmentId)}?token=${encodeURIComponent(state.token)}" target="_blank" download>
|
||||
📎 ${escapeHtml(msg.attachmentName || 'file')} (${sizeStr})
|
||||
</a>
|
||||
<button type="button" class="attachment-card" data-file-id="${safeId}" data-file-name="${safeName}">
|
||||
📎 ${safeName} (${sizeStr})
|
||||
</button>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
div.innerHTML = `
|
||||
<div class="message-meta">
|
||||
<span class="message-author">@${escapeHtml(msg.senderUsername)}</span>
|
||||
<span class="role-badge role-${roleClass}">${escapeHtml(msg.senderRole || 'Member')}</span>
|
||||
<span class="message-author">@${escapeHtml(sender)}</span>
|
||||
<span class="role-badge role-${roleClass}">${escapeHtml(rawRole)}</span>
|
||||
<span class="message-time">${time}</span>
|
||||
</div>
|
||||
<div class="message-content">${contentHtml}</div>
|
||||
`;
|
||||
|
||||
div.querySelectorAll('.attachment-card').forEach(btn => {
|
||||
btn.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
downloadFile(btn.dataset.fileId, btn.dataset.fileName);
|
||||
});
|
||||
});
|
||||
|
||||
el.chatMessages.appendChild(div);
|
||||
el.chatMessages.scrollTop = el.chatMessages.scrollHeight;
|
||||
}
|
||||
|
|
@ -407,19 +501,22 @@
|
|||
|
||||
function renderUsers() {
|
||||
el.userList.innerHTML = '';
|
||||
const onlineCount = state.users.filter(u => u.isOnline).length;
|
||||
const onlineCount = state.users.filter(u => u.isOnline ?? u.IsOnline).length;
|
||||
el.userCountBadge.textContent = `${onlineCount} Online / ${state.users.length} Total`;
|
||||
|
||||
state.users.forEach(u => {
|
||||
const item = document.createElement('div');
|
||||
item.className = 'user-item';
|
||||
const roleClass = (u.role || 'member').toLowerCase();
|
||||
const username = u.username || u.Username || 'User';
|
||||
const role = u.role || u.Role || 'Member';
|
||||
const roleClass = role.toLowerCase();
|
||||
const isOnline = u.isOnline ?? u.IsOnline ?? false;
|
||||
item.innerHTML = `
|
||||
<div class="user-meta">
|
||||
<span class="user-dot ${u.isOnline ? 'online' : ''}"></span>
|
||||
<span style="font-weight:600;">@${escapeHtml(u.username)}</span>
|
||||
<span class="user-dot ${isOnline ? 'online' : ''}"></span>
|
||||
<span style="font-weight:600;">@${escapeHtml(username)}</span>
|
||||
</div>
|
||||
<span class="role-badge role-${roleClass}">${escapeHtml(u.role)}</span>
|
||||
<span class="role-badge role-${roleClass}">${escapeHtml(role)}</span>
|
||||
`;
|
||||
el.userList.appendChild(item);
|
||||
});
|
||||
|
|
@ -435,16 +532,28 @@
|
|||
state.files.forEach(f => {
|
||||
const item = document.createElement('div');
|
||||
item.className = 'file-item';
|
||||
const sizeStr = formatBytes(f.fileSizeBytes || 0);
|
||||
const dateStr = new Date(f.createdAt).toLocaleDateString();
|
||||
const fileId = f.id || f.Id;
|
||||
const fileName = f.originalFileName || f.OriginalFileName || 'file';
|
||||
const fileBytes = f.fileSizeBytes ?? f.FileSizeBytes ?? 0;
|
||||
const uploader = f.uploaderUsername || f.UploaderUsername || 'User';
|
||||
const createdAt = f.createdAt || f.CreatedAt;
|
||||
const sizeStr = formatBytes(fileBytes);
|
||||
const dateStr = createdAt ? new Date(createdAt).toLocaleDateString() : '';
|
||||
|
||||
item.innerHTML = `
|
||||
<div class="file-meta">
|
||||
<div style="font-weight:600;">📎 ${escapeHtml(f.originalFileName)}</div>
|
||||
<div style="font-size:0.8rem; color:var(--text-muted);">${sizeStr} • shared by @${escapeHtml(f.uploaderUsername)} on ${dateStr}</div>
|
||||
<div style="font-weight:600;">📎 ${escapeHtml(fileName)}</div>
|
||||
<div style="font-size:0.8rem; color:var(--text-muted);">${sizeStr} • shared by @${escapeHtml(uploader)} on ${dateStr}</div>
|
||||
</div>
|
||||
<a class="btn btn-primary btn-sm" href="/api/files/${encodeURIComponent(f.id)}?token=${encodeURIComponent(state.token)}" download>Download</a>
|
||||
<button type="button" class="btn btn-primary btn-sm btn-download" data-file-id="${escapeHtml(fileId)}" data-file-name="${escapeHtml(fileName)}">Download</button>
|
||||
`;
|
||||
const dlBtn = item.querySelector('.btn-download');
|
||||
if (dlBtn) {
|
||||
dlBtn.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
downloadFile(dlBtn.dataset.fileId, dlBtn.dataset.fileName);
|
||||
});
|
||||
}
|
||||
el.filesList.appendChild(item);
|
||||
});
|
||||
}
|
||||
|
|
@ -649,6 +758,56 @@
|
|||
createChannel(el.newChannelName.value.trim(), el.newChannelTopic.value.trim());
|
||||
});
|
||||
|
||||
// In-chat Channel Selector Dropdown
|
||||
if (el.channelSelectDropdown) {
|
||||
el.channelSelectDropdown.addEventListener('change', (e) => {
|
||||
if (e.target.value) {
|
||||
joinChannel(e.target.value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Global Drag & Drop handling (Prevents Firefox "Security Error: Content may not load or link to file:///")
|
||||
let dragCounter = 0;
|
||||
|
||||
window.addEventListener('dragenter', (e) => {
|
||||
e.preventDefault();
|
||||
dragCounter++;
|
||||
if (el.dropOverlay) {
|
||||
el.dropOverlay.classList.add('visible');
|
||||
}
|
||||
}, false);
|
||||
|
||||
window.addEventListener('dragover', (e) => {
|
||||
e.preventDefault();
|
||||
if (e.dataTransfer) {
|
||||
e.dataTransfer.dropEffect = 'copy';
|
||||
}
|
||||
}, false);
|
||||
|
||||
window.addEventListener('dragleave', (e) => {
|
||||
e.preventDefault();
|
||||
dragCounter--;
|
||||
if (dragCounter <= 0) {
|
||||
dragCounter = 0;
|
||||
if (el.dropOverlay) {
|
||||
el.dropOverlay.classList.remove('visible');
|
||||
}
|
||||
}
|
||||
}, false);
|
||||
|
||||
window.addEventListener('drop', (e) => {
|
||||
e.preventDefault();
|
||||
dragCounter = 0;
|
||||
if (el.dropOverlay) {
|
||||
el.dropOverlay.classList.remove('visible');
|
||||
}
|
||||
|
||||
if (e.dataTransfer && e.dataTransfer.files && e.dataTransfer.files.length > 0) {
|
||||
uploadFile(e.dataTransfer.files[0]);
|
||||
}
|
||||
}, false);
|
||||
|
||||
// Logout
|
||||
el.logoutBtn.addEventListener('click', logout);
|
||||
}
|
||||
|
|
@ -672,5 +831,9 @@
|
|||
}
|
||||
|
||||
// Start app
|
||||
window.addEventListener('DOMContentLoaded', init);
|
||||
if (document.readyState === 'loading') {
|
||||
window.addEventListener('DOMContentLoaded', init);
|
||||
} else {
|
||||
init();
|
||||
}
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue