2024-04-27 06:57:01 +00:00
|
|
|
(async () => {
|
|
|
|
// check notification permission
|
|
|
|
// This is useful to alert people that they should do something
|
|
|
|
await Notification.requestPermission()
|
|
|
|
})()
|
|
|
|
|
2024-04-27 12:12:08 +00:00
|
|
|
const MAX_MESSAGES = 50
|
|
|
|
|
2024-04-27 10:08:10 +00:00
|
|
|
let channels = {}
|
2024-04-27 11:27:27 +00:00
|
|
|
let messages = {}
|
2024-04-27 10:08:10 +00:00
|
|
|
let selected_channel_id = null
|
|
|
|
|
2024-04-27 06:57:01 +00:00
|
|
|
/**
|
|
|
|
* Display a new notification with the given title and the given body.
|
|
|
|
* @param title The title of the notification
|
|
|
|
* @param body The body of the notification
|
|
|
|
* @param timeout The time (in milliseconds) after that the notification automatically closes. 0 to make indefinite. Default to 5000 ms.
|
|
|
|
* @return Notification
|
|
|
|
*/
|
|
|
|
function showNotification(title, body, timeout = 5000) {
|
|
|
|
let notif = new Notification(title, {'body': body, 'icon': "/static/tfjm.svg"})
|
|
|
|
if (timeout)
|
|
|
|
setTimeout(() => notif.close(), timeout)
|
|
|
|
return notif
|
|
|
|
}
|
|
|
|
|
2024-04-27 10:08:10 +00:00
|
|
|
function selectChannel(channel_id) {
|
|
|
|
let channel = channels[channel_id]
|
|
|
|
if (!channel) {
|
|
|
|
console.error('Channel not found:', channel_id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
selected_channel_id = channel_id
|
|
|
|
|
2024-04-27 14:16:57 +00:00
|
|
|
window.history.replaceState({}, null, `#channel-${channel['id']}`)
|
|
|
|
|
2024-04-27 10:08:10 +00:00
|
|
|
let channelTitle = document.getElementById('channel-title')
|
|
|
|
channelTitle.innerText = channel['name']
|
|
|
|
|
|
|
|
let messageInput = document.getElementById('input-message')
|
|
|
|
messageInput.disabled = !channel['write_access']
|
2024-04-27 11:27:27 +00:00
|
|
|
|
|
|
|
redrawMessages()
|
2024-04-27 10:08:10 +00:00
|
|
|
}
|
|
|
|
|
2024-04-27 10:59:50 +00:00
|
|
|
function sendMessage() {
|
|
|
|
let messageInput = document.getElementById('input-message')
|
|
|
|
let message = messageInput.value
|
|
|
|
messageInput.value = ''
|
|
|
|
|
|
|
|
if (!message) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'type': 'send_message',
|
|
|
|
'channel_id': selected_channel_id,
|
|
|
|
'content': message,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2024-04-27 10:08:10 +00:00
|
|
|
function setChannels(new_channels) {
|
|
|
|
channels = {}
|
2024-04-27 12:50:25 +00:00
|
|
|
let navTab = document.getElementById('nav-channels-tab')
|
|
|
|
navTab.innerHTML = ''
|
|
|
|
|
2024-04-27 10:08:10 +00:00
|
|
|
for (let channel of new_channels) {
|
|
|
|
channels[channel['id']] = channel
|
2024-04-27 11:27:27 +00:00
|
|
|
if (!messages[channel['id']])
|
2024-04-27 12:12:08 +00:00
|
|
|
messages[channel['id']] = new Map()
|
2024-04-27 11:27:27 +00:00
|
|
|
|
2024-04-27 12:50:25 +00:00
|
|
|
let navItem = document.createElement('li')
|
|
|
|
navItem.classList.add('list-group-item')
|
|
|
|
navItem.id = `tab-channel-${channel['id']}`
|
|
|
|
navItem.setAttribute('data-bs-dismiss', 'offcanvas')
|
|
|
|
navItem.onclick = () => selectChannel(channel['id'])
|
|
|
|
navTab.appendChild(navItem)
|
|
|
|
|
|
|
|
let channelButton = document.createElement('button')
|
|
|
|
channelButton.classList.add('nav-link')
|
|
|
|
channelButton.type = 'button'
|
|
|
|
channelButton.innerText = channel['name']
|
|
|
|
navItem.appendChild(channelButton)
|
|
|
|
|
2024-04-27 12:12:08 +00:00
|
|
|
fetchMessages(channel['id'])
|
2024-04-27 10:08:10 +00:00
|
|
|
}
|
|
|
|
|
2024-04-27 14:16:57 +00:00
|
|
|
if (new_channels && (!selected_channel_id || !channels[selected_channel_id])) {
|
|
|
|
if (window.location.hash)
|
|
|
|
selectChannel(window.location.hash.substring(9))
|
|
|
|
else
|
|
|
|
selectChannel(Object.keys(channels)[0])
|
|
|
|
}
|
2024-04-27 10:08:10 +00:00
|
|
|
}
|
|
|
|
|
2024-04-27 10:59:50 +00:00
|
|
|
function receiveMessage(message) {
|
2024-04-27 12:50:25 +00:00
|
|
|
messages[message['channel_id']].set(message['id'], message)
|
2024-04-27 11:27:27 +00:00
|
|
|
redrawMessages()
|
|
|
|
}
|
|
|
|
|
2024-04-27 12:12:08 +00:00
|
|
|
function fetchMessages(channel_id, offset = 0, limit = MAX_MESSAGES) {
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'type': 'fetch_messages',
|
|
|
|
'channel_id': channel_id,
|
|
|
|
'offset': offset,
|
|
|
|
'limit': limit,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetchPreviousMessages() {
|
|
|
|
let channel_id = selected_channel_id
|
|
|
|
let offset = messages[channel_id].size
|
|
|
|
fetchMessages(channel_id, offset, MAX_MESSAGES)
|
|
|
|
}
|
|
|
|
|
|
|
|
function receiveFetchedMessages(data) {
|
2024-04-27 11:27:27 +00:00
|
|
|
let channel_id = data['channel_id']
|
|
|
|
let new_messages = data['messages']
|
2024-04-27 10:59:50 +00:00
|
|
|
|
2024-04-27 11:27:27 +00:00
|
|
|
if (!messages[channel_id])
|
2024-04-27 12:12:08 +00:00
|
|
|
messages[channel_id] = new Map()
|
2024-04-27 10:59:50 +00:00
|
|
|
|
2024-04-27 12:12:08 +00:00
|
|
|
for (let message of new_messages)
|
|
|
|
messages[channel_id].set(message['id'], message)
|
|
|
|
|
|
|
|
// Sort messages by timestamp
|
|
|
|
messages[channel_id] = new Map([...messages[channel_id].values()]
|
|
|
|
.sort((a, b) => new Date(a['timestamp']) - new Date(b['timestamp']))
|
|
|
|
.map(message => [message['id'], message]))
|
2024-04-27 11:27:27 +00:00
|
|
|
|
|
|
|
redrawMessages()
|
|
|
|
}
|
2024-04-27 10:59:50 +00:00
|
|
|
|
2024-04-27 11:27:27 +00:00
|
|
|
function redrawMessages() {
|
|
|
|
let messageList = document.getElementById('message-list')
|
|
|
|
messageList.innerHTML = ''
|
|
|
|
|
|
|
|
let lastMessage = null
|
|
|
|
let lastContentDiv = null
|
|
|
|
|
2024-04-27 12:12:08 +00:00
|
|
|
for (let message of messages[selected_channel_id].values()) {
|
2024-04-27 11:27:27 +00:00
|
|
|
if (lastMessage && lastMessage['author'] === message['author']) {
|
|
|
|
let lastTimestamp = new Date(lastMessage['timestamp'])
|
|
|
|
let newTimestamp = new Date(message['timestamp'])
|
|
|
|
if ((newTimestamp - lastTimestamp) / 1000 < 60 * 10) {
|
|
|
|
let messageContentDiv = document.createElement('div')
|
|
|
|
messageContentDiv.innerText = message['content']
|
|
|
|
lastContentDiv.appendChild(messageContentDiv)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let messageElement = document.createElement('li')
|
|
|
|
messageElement.classList.add('list-group-item')
|
|
|
|
messageList.appendChild(messageElement)
|
|
|
|
|
|
|
|
let authorDiv = document.createElement('div')
|
|
|
|
messageElement.appendChild(authorDiv)
|
|
|
|
|
|
|
|
let authorSpan = document.createElement('span')
|
|
|
|
authorSpan.classList.add('text-muted', 'fw-bold')
|
|
|
|
authorSpan.innerText = message['author']
|
|
|
|
authorDiv.appendChild(authorSpan)
|
|
|
|
|
|
|
|
let dateSpan = document.createElement('span')
|
|
|
|
dateSpan.classList.add('text-muted', 'float-end')
|
|
|
|
dateSpan.innerText = new Date(message['timestamp']).toLocaleString()
|
|
|
|
authorDiv.appendChild(dateSpan)
|
|
|
|
|
|
|
|
let contentDiv = document.createElement('div')
|
|
|
|
messageElement.appendChild(contentDiv)
|
|
|
|
|
|
|
|
let messageContentDiv = document.createElement('div')
|
|
|
|
messageContentDiv.innerText = message['content']
|
|
|
|
contentDiv.appendChild(messageContentDiv)
|
|
|
|
|
|
|
|
lastMessage = message
|
|
|
|
lastContentDiv = contentDiv
|
|
|
|
}
|
2024-04-27 12:12:08 +00:00
|
|
|
|
|
|
|
let fetchMoreButton = document.getElementById('fetch-previous-messages')
|
|
|
|
if (!messages[selected_channel_id] || messages[selected_channel_id].size % MAX_MESSAGES !== 0)
|
|
|
|
fetchMoreButton.classList.add('d-none')
|
|
|
|
else
|
|
|
|
fetchMoreButton.classList.remove('d-none')
|
2024-04-27 10:59:50 +00:00
|
|
|
}
|
|
|
|
|
2024-04-27 14:16:57 +00:00
|
|
|
function toggleFullscreen() {
|
|
|
|
let chatContainer = document.getElementById('chat-container')
|
|
|
|
if (!chatContainer.getAttribute('data-fullscreen')) {
|
|
|
|
chatContainer.setAttribute('data-fullscreen', 'true')
|
|
|
|
chatContainer.classList.add('position-absolute', 'top-0', 'start-0', 'vh-100', 'z-3')
|
|
|
|
window.history.replaceState({}, null, `?fullscreen=1#channel-${selected_channel_id}`)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
chatContainer.removeAttribute('data-fullscreen')
|
|
|
|
chatContainer.classList.remove('position-absolute', 'top-0', 'start-0', 'vh-100', 'z-3')
|
|
|
|
window.history.replaceState({}, null, `?fullscreen=0#channel-${selected_channel_id}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 06:57:01 +00:00
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
/**
|
|
|
|
* Process the received data from the server.
|
|
|
|
* @param data The received message
|
|
|
|
*/
|
|
|
|
function processMessage(data) {
|
2024-04-27 10:08:10 +00:00
|
|
|
switch (data['type']) {
|
|
|
|
case 'fetch_channels':
|
|
|
|
setChannels(data['channels'])
|
|
|
|
break
|
2024-04-27 10:59:50 +00:00
|
|
|
case 'send_message':
|
|
|
|
receiveMessage(data)
|
|
|
|
break
|
2024-04-27 11:27:27 +00:00
|
|
|
case 'fetch_messages':
|
2024-04-27 12:12:08 +00:00
|
|
|
receiveFetchedMessages(data)
|
2024-04-27 11:27:27 +00:00
|
|
|
break
|
2024-04-27 10:08:10 +00:00
|
|
|
default:
|
|
|
|
console.log(data)
|
|
|
|
console.error('Unknown message type:', data['type'])
|
|
|
|
break
|
|
|
|
}
|
2024-04-27 06:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setupSocket(nextDelay = 1000) {
|
|
|
|
// Open a global websocket
|
|
|
|
socket = new WebSocket(
|
|
|
|
(document.location.protocol === 'https:' ? 'wss' : 'ws') + '://' + window.location.host + '/ws/chat/'
|
|
|
|
)
|
|
|
|
|
|
|
|
// Listen on websockets and process messages from the server
|
|
|
|
socket.addEventListener('message', e => {
|
|
|
|
// Parse received data as JSON
|
|
|
|
const data = JSON.parse(e.data)
|
|
|
|
|
|
|
|
processMessage(data)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Manage errors
|
|
|
|
socket.addEventListener('close', e => {
|
|
|
|
console.error('Chat socket closed unexpectedly, restarting…')
|
|
|
|
setTimeout(() => setupSocket(2 * nextDelay), nextDelay)
|
|
|
|
})
|
2024-04-27 07:53:55 +00:00
|
|
|
|
|
|
|
socket.addEventListener('open', e => {
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'type': 'fetch_channels',
|
|
|
|
}))
|
|
|
|
})
|
2024-04-27 06:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setupSocket()
|
2024-04-27 17:11:03 +00:00
|
|
|
|
|
|
|
let deferredPrompt = null
|
|
|
|
|
|
|
|
window.addEventListener("beforeinstallprompt", (e) => {
|
|
|
|
e.preventDefault()
|
|
|
|
deferredPrompt = e
|
|
|
|
let btn = document.getElementById('install-app-home-screen')
|
|
|
|
let alert = document.getElementById('alert-download-chat-app')
|
|
|
|
btn.classList.remove('d-none')
|
|
|
|
alert.classList.remove('d-none')
|
|
|
|
btn.onclick = function () {
|
|
|
|
deferredPrompt.prompt()
|
|
|
|
deferredPrompt.userChoice.then((choiceResult) => {
|
|
|
|
if (choiceResult.outcome === 'accepted') {
|
|
|
|
deferredPrompt = null
|
|
|
|
btn.classList.add('d-none')
|
|
|
|
alert.classList.add('d-none')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2024-04-27 06:57:01 +00:00
|
|
|
})
|