From c6b9a84def02c4b9b6059ff7aba8eb785311e679 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sun, 28 Apr 2024 15:09:52 +0200 Subject: [PATCH] Reset retry delay to 1 second when a connection has succeeded Signed-off-by: Emmy D'Anello --- chat/static/chat.js | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/chat/static/chat.js b/chat/static/chat.js index 7a0ee09..b1b7997 100644 --- a/chat/static/chat.js +++ b/chat/static/chat.js @@ -150,6 +150,22 @@ function receiveFetchedMessages(data) { redrawMessages() } +function startPrivateChat(data) { + let channel = data['channel'] + if (!channel) { + console.error('Private chat not found:', data) + return + } + + if (!channels[channel['id']]) { + channels[channel['id']] = channel + messages[channel['id']] = new Map() + setChannels(Object.values(channels)) + } + + selectChannel(channel['id']) +} + function redrawMessages() { let messageList = document.getElementById('message-list') messageList.innerHTML = '' @@ -181,6 +197,26 @@ function redrawMessages() { authorSpan.innerText = message['author'] authorDiv.appendChild(authorSpan) + authorSpan.addEventListener('contextmenu', (menu_event) => { + menu_event.preventDefault() + const popover = bootstrap.Popover.getOrCreateInstance(authorSpan, { + 'title': message['author'], + 'content': `Envoyer un message privé`, + 'html': true, + 'placement': "bottom", + }) + popover.show() + + document.getElementById('send-private-message-link-' + message['id']).addEventListener('click', event => { + event.preventDefault() + popover.hide() + socket.send(JSON.stringify({ + 'type': 'start_private_chat', + 'user_id': message['author_id'], + })) + }) + }) + let dateSpan = document.createElement('span') dateSpan.classList.add('text-muted', 'float-end') dateSpan.innerText = new Date(message['timestamp']).toLocaleString() @@ -234,6 +270,9 @@ document.addEventListener('DOMContentLoaded', () => { case 'fetch_messages': receiveFetchedMessages(data) break + case 'start_private_chat': + startPrivateChat(data) + break default: console.log(data) console.error('Unknown message type:', data['type']) @@ -246,6 +285,7 @@ document.addEventListener('DOMContentLoaded', () => { socket = new WebSocket( (document.location.protocol === 'https:' ? 'wss' : 'ws') + '://' + window.location.host + '/ws/chat/' ) + let socketOpen = false // Listen on websockets and process messages from the server socket.addEventListener('message', e => { @@ -258,10 +298,11 @@ document.addEventListener('DOMContentLoaded', () => { // Manage errors socket.addEventListener('close', e => { console.error('Chat socket closed unexpectedly, restarting…') - setTimeout(() => setupSocket(2 * nextDelay), nextDelay) + setTimeout(() => setupSocket(socketOpen ? 1000 : 2 * nextDelay), nextDelay) }) socket.addEventListener('open', e => { + socketOpen = true socket.send(JSON.stringify({ 'type': 'fetch_channels', }))