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-28 11:50:04 +00:00
|
|
|
const channel_categories = ['general', 'tournament', 'team', 'private']
|
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) {
|
2024-04-27 18:37:23 +00:00
|
|
|
Notification.requestPermission().then((status) => {
|
|
|
|
if (status === 'granted')
|
|
|
|
new Notification(title, {'body': body, 'icon': "/static/tfjm-192.png"})
|
|
|
|
})
|
2024-04-27 06:57:01 +00:00
|
|
|
}
|
|
|
|
|
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-28 21:56:41 +00:00
|
|
|
localStorage.setItem('chat.last-channel-id', channel_id)
|
2024-04-27 14:16:57 +00:00
|
|
|
|
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-28 11:50:04 +00:00
|
|
|
let categoryLists = {}
|
|
|
|
for (let category of channel_categories) {
|
|
|
|
categoryLists[category] = document.getElementById(`nav-${category}-channels-tab`)
|
|
|
|
categoryLists[category].innerHTML = ''
|
|
|
|
categoryLists[category].parentElement.classList.add('d-none')
|
|
|
|
}
|
2024-04-27 12:50:25 +00:00
|
|
|
|
2024-04-28 21:25:15 +00:00
|
|
|
for (let channel of new_channels)
|
|
|
|
addChannel(channel, categoryLists)
|
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])) {
|
2024-04-28 21:56:41 +00:00
|
|
|
let last_channel_id = parseInt(localStorage.getItem('chat.last-channel-id'))
|
|
|
|
if (last_channel_id && channels[last_channel_id])
|
|
|
|
selectChannel(last_channel_id)
|
2024-04-27 14:16:57 +00:00
|
|
|
else
|
|
|
|
selectChannel(Object.keys(channels)[0])
|
|
|
|
}
|
2024-04-27 10:08:10 +00:00
|
|
|
}
|
|
|
|
|
2024-04-28 21:25:15 +00:00
|
|
|
async function addChannel(channel, categoryLists) {
|
|
|
|
channels[channel['id']] = channel
|
|
|
|
if (!messages[channel['id']])
|
|
|
|
messages[channel['id']] = new Map()
|
|
|
|
|
|
|
|
let categoryList = categoryLists[channel['category']]
|
|
|
|
categoryList.parentElement.classList.remove('d-none')
|
|
|
|
|
|
|
|
let navItem = document.createElement('li')
|
2024-04-28 21:54:00 +00:00
|
|
|
navItem.classList.add('list-group-item', 'tab-channel')
|
2024-04-28 21:25:15 +00:00
|
|
|
navItem.id = `tab-channel-${channel['id']}`
|
|
|
|
navItem.setAttribute('data-bs-dismiss', 'offcanvas')
|
|
|
|
navItem.onclick = () => selectChannel(channel['id'])
|
|
|
|
categoryList.appendChild(navItem)
|
|
|
|
|
|
|
|
let channelButton = document.createElement('button')
|
|
|
|
channelButton.classList.add('nav-link')
|
|
|
|
channelButton.type = 'button'
|
|
|
|
channelButton.innerText = channel['name']
|
|
|
|
navItem.appendChild(channelButton)
|
|
|
|
|
|
|
|
let unreadBadge = document.createElement('span')
|
|
|
|
unreadBadge.classList.add('badge', 'rounded-pill', 'text-bg-light', 'ms-2')
|
|
|
|
unreadBadge.id = `unread-messages-${channel['id']}`
|
|
|
|
unreadBadge.innerText = channel.unread_messages || 0
|
|
|
|
if (!channel.unread_messages)
|
|
|
|
unreadBadge.classList.add('d-none')
|
|
|
|
channelButton.appendChild(unreadBadge)
|
|
|
|
|
2024-04-28 21:54:00 +00:00
|
|
|
if (document.getElementById('sort-by-unread-switch').checked)
|
|
|
|
navItem.style.order = `${-channel.unread_messages}`
|
|
|
|
|
2024-04-28 21:25:15 +00:00
|
|
|
fetchMessages(channel['id'])
|
|
|
|
}
|
|
|
|
|
2024-04-27 10:59:50 +00:00
|
|
|
function receiveMessage(message) {
|
2024-04-27 18:23:55 +00:00
|
|
|
let scrollableContent = document.getElementById('chat-messages')
|
|
|
|
let isScrolledToBottom = scrollableContent.scrollHeight - scrollableContent.clientHeight <= scrollableContent.scrollTop + 1
|
|
|
|
|
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 18:23:55 +00:00
|
|
|
|
|
|
|
// Scroll to bottom if the user was already at the bottom
|
|
|
|
if (isScrolledToBottom)
|
|
|
|
scrollableContent.scrollTop = scrollableContent.scrollHeight - scrollableContent.clientHeight
|
|
|
|
|
|
|
|
if (message['content'].includes("@everyone"))
|
|
|
|
showNotification(channels[message['channel_id']]['name'], `${message['author']} : ${message['content']}`)
|
2024-04-27 11:27:27 +00:00
|
|
|
}
|
|
|
|
|
2024-04-28 14:56:30 +00:00
|
|
|
function editMessage(data) {
|
|
|
|
messages[data['channel_id']].get(data['id'])['content'] = data['content']
|
|
|
|
redrawMessages()
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteMessage(data) {
|
|
|
|
messages[data['channel_id']].delete(data['id'])
|
|
|
|
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-28 21:25:15 +00:00
|
|
|
function markMessageAsRead(data) {
|
|
|
|
for (let message of data['messages']) {
|
|
|
|
let stored_message = messages[message['channel_id']].get(message['id'])
|
|
|
|
if (stored_message)
|
|
|
|
stored_message['read'] = true
|
|
|
|
}
|
|
|
|
redrawMessages()
|
|
|
|
updateUnreadBadges(data['unread_messages'])
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateUnreadBadges(unreadMessages) {
|
2024-04-28 21:54:00 +00:00
|
|
|
const sortByUnread = document.getElementById('sort-by-unread-switch').checked
|
|
|
|
|
2024-04-28 21:25:15 +00:00
|
|
|
for (let channel of Object.values(channels)) {
|
|
|
|
let unreadMessagesChannel = unreadMessages[channel['id']] || 0
|
|
|
|
console.log(channel, unreadMessagesChannel)
|
|
|
|
channel.unread_messages = unreadMessagesChannel
|
|
|
|
|
|
|
|
let unreadBadge = document.getElementById(`unread-messages-${channel['id']}`)
|
|
|
|
unreadBadge.innerText = unreadMessagesChannel
|
|
|
|
if (unreadMessagesChannel)
|
|
|
|
unreadBadge.classList.remove('d-none')
|
|
|
|
else
|
|
|
|
unreadBadge.classList.add('d-none')
|
2024-04-28 21:54:00 +00:00
|
|
|
|
|
|
|
if (sortByUnread)
|
|
|
|
document.getElementById(`tab-channel-${channel['id']}`).style.order = `${-unreadMessagesChannel}`
|
2024-04-28 21:25:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-28 13:09:52 +00:00
|
|
|
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'])
|
|
|
|
}
|
|
|
|
|
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')
|
2024-04-28 21:25:15 +00:00
|
|
|
messageContentDiv.classList.add('message')
|
|
|
|
messageContentDiv.setAttribute('data-message-id', message['id'])
|
2024-04-27 11:27:27 +00:00
|
|
|
lastContentDiv.appendChild(messageContentDiv)
|
2024-04-28 14:28:39 +00:00
|
|
|
let messageContentSpan = document.createElement('span')
|
|
|
|
messageContentSpan.innerText = message['content']
|
|
|
|
messageContentDiv.appendChild(messageContentSpan)
|
|
|
|
|
2024-04-28 18:25:00 +00:00
|
|
|
registerMessageContextMenu(message, messageContentDiv, messageContentSpan)
|
2024-04-27 11:27:27 +00:00
|
|
|
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)
|
|
|
|
|
2024-04-28 18:25:00 +00:00
|
|
|
registerSendPrivateMessageContextMenu(message, authorDiv, authorSpan)
|
2024-04-28 13:09:52 +00:00
|
|
|
|
2024-04-27 11:27:27 +00:00
|
|
|
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')
|
2024-04-28 21:25:15 +00:00
|
|
|
messageContentDiv.classList.add('message')
|
|
|
|
messageContentDiv.setAttribute('data-message-id', message['id'])
|
2024-04-27 11:27:27 +00:00
|
|
|
contentDiv.appendChild(messageContentDiv)
|
2024-04-28 14:28:39 +00:00
|
|
|
let messageContentSpan = document.createElement('span')
|
|
|
|
messageContentSpan.innerText = message['content']
|
|
|
|
messageContentDiv.appendChild(messageContentSpan)
|
|
|
|
|
2024-04-28 18:25:00 +00:00
|
|
|
registerMessageContextMenu(message, messageContentDiv, messageContentSpan)
|
2024-04-27 11:27:27 +00:00
|
|
|
|
|
|
|
lastMessage = message
|
|
|
|
lastContentDiv = contentDiv
|
|
|
|
}
|
2024-04-27 12:12:08 +00:00
|
|
|
|
|
|
|
let fetchMoreButton = document.getElementById('fetch-previous-messages')
|
2024-04-28 09:57:25 +00:00
|
|
|
if (!messages[selected_channel_id].size || messages[selected_channel_id].size % MAX_MESSAGES !== 0)
|
2024-04-27 12:12:08 +00:00
|
|
|
fetchMoreButton.classList.add('d-none')
|
|
|
|
else
|
|
|
|
fetchMoreButton.classList.remove('d-none')
|
2024-04-28 21:25:15 +00:00
|
|
|
|
|
|
|
messageList.dispatchEvent(new CustomEvent('updatemessages'))
|
2024-04-27 10:59:50 +00:00
|
|
|
}
|
|
|
|
|
2024-04-28 14:36:54 +00:00
|
|
|
function removeAllPopovers() {
|
|
|
|
for (let popover of document.querySelectorAll('*[aria-describedby*="popover"]')) {
|
|
|
|
let instance = bootstrap.Popover.getInstance(popover)
|
|
|
|
if (instance)
|
|
|
|
instance.dispose()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-28 18:25:00 +00:00
|
|
|
function registerSendPrivateMessageContextMenu(message, div, span) {
|
|
|
|
div.addEventListener('contextmenu', (menu_event) => {
|
2024-04-28 14:28:39 +00:00
|
|
|
menu_event.preventDefault()
|
2024-04-28 14:36:54 +00:00
|
|
|
removeAllPopovers()
|
2024-04-28 18:25:00 +00:00
|
|
|
const popover = bootstrap.Popover.getOrCreateInstance(span, {
|
2024-04-28 14:28:39 +00:00
|
|
|
'title': message['author'],
|
|
|
|
'content': `<a id="send-private-message-link-${message['id']}" class="nav-link" href="#" tabindex="0">Envoyer un message privé</a>`,
|
|
|
|
'html': true,
|
|
|
|
})
|
|
|
|
popover.show()
|
|
|
|
|
|
|
|
document.getElementById('send-private-message-link-' + message['id']).addEventListener('click', event => {
|
|
|
|
event.preventDefault()
|
2024-04-28 14:36:54 +00:00
|
|
|
popover.dispose()
|
2024-04-28 14:28:39 +00:00
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'type': 'start_private_chat',
|
|
|
|
'user_id': message['author_id'],
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-28 18:25:00 +00:00
|
|
|
function registerMessageContextMenu(message, div, span) {
|
|
|
|
div.addEventListener('contextmenu', (menu_event) => {
|
2024-04-28 14:28:39 +00:00
|
|
|
menu_event.preventDefault()
|
2024-04-28 14:36:54 +00:00
|
|
|
removeAllPopovers()
|
2024-04-28 14:28:39 +00:00
|
|
|
let content = `<a id="send-private-message-link-msg-${message['id']}" class="nav-link" href="#" tabindex="0">Envoyer un message privé</a>`
|
2024-04-28 14:56:30 +00:00
|
|
|
|
|
|
|
let has_right_to_edit = message['author_id'] === USER_ID || IS_ADMIN
|
|
|
|
if (has_right_to_edit) {
|
2024-04-28 14:36:54 +00:00
|
|
|
content += `<hr class="my-1">`
|
2024-04-28 14:56:30 +00:00
|
|
|
content += `<a id="edit-message-${message['id']}" class="nav-link" href="#" tabindex="0">Modifier</a>`
|
|
|
|
content += `<a id="delete-message-${message['id']}" class="nav-link" href="#" tabindex="0">Supprimer</a>`
|
2024-04-28 14:36:54 +00:00
|
|
|
}
|
2024-04-28 14:56:30 +00:00
|
|
|
|
2024-04-28 18:25:00 +00:00
|
|
|
const popover = bootstrap.Popover.getOrCreateInstance(span, {
|
2024-04-28 14:28:39 +00:00
|
|
|
'content': content,
|
|
|
|
'html': true,
|
|
|
|
'placement': 'bottom',
|
|
|
|
})
|
|
|
|
popover.show()
|
|
|
|
|
|
|
|
document.getElementById('send-private-message-link-msg-' + message['id']).addEventListener('click', event => {
|
|
|
|
event.preventDefault()
|
2024-04-28 14:36:54 +00:00
|
|
|
popover.dispose()
|
2024-04-28 14:28:39 +00:00
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'type': 'start_private_chat',
|
|
|
|
'user_id': message['author_id'],
|
|
|
|
}))
|
|
|
|
})
|
2024-04-28 14:56:30 +00:00
|
|
|
|
|
|
|
if (has_right_to_edit) {
|
|
|
|
document.getElementById('edit-message-' + message['id']).addEventListener('click', event => {
|
|
|
|
event.preventDefault()
|
|
|
|
popover.dispose()
|
|
|
|
let new_message = prompt("Modifier le message", message['content'])
|
|
|
|
if (new_message) {
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'type': 'edit_message',
|
|
|
|
'message_id': message['id'],
|
|
|
|
'content': new_message,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
document.getElementById('delete-message-' + message['id']).addEventListener('click', event => {
|
|
|
|
event.preventDefault()
|
|
|
|
popover.dispose()
|
2024-04-28 18:25:00 +00:00
|
|
|
if (confirm(`Supprimer le message ?\n${message['content']}`)) {
|
2024-04-28 14:56:30 +00:00
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'type': 'delete_message',
|
|
|
|
'message_id': message['id'],
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-04-28 14:28:39 +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')
|
2024-04-28 21:56:41 +00:00
|
|
|
window.history.replaceState({}, null, `?fullscreen=1`)
|
2024-04-27 14:16:57 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
chatContainer.removeAttribute('data-fullscreen')
|
|
|
|
chatContainer.classList.remove('position-absolute', 'top-0', 'start-0', 'vh-100', 'z-3')
|
2024-04-28 21:56:41 +00:00
|
|
|
window.history.replaceState({}, null, `?fullscreen=0`)
|
2024-04-27 14:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 06:57:01 +00:00
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
2024-04-28 14:36:54 +00:00
|
|
|
document.addEventListener('click', removeAllPopovers)
|
2024-04-28 14:28:39 +00:00
|
|
|
|
2024-04-28 21:54:00 +00:00
|
|
|
document.getElementById('sort-by-unread-switch').addEventListener('change', event => {
|
|
|
|
const sortByUnread = event.target.checked
|
|
|
|
for (let channel of Object.values(channels)) {
|
|
|
|
let item = document.getElementById(`tab-channel-${channel['id']}`)
|
|
|
|
if (sortByUnread)
|
|
|
|
item.style.order = `${-channel.unread_messages}`
|
|
|
|
else
|
|
|
|
item.style.removeProperty('order')
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.setItem('chat.sort-by-unread', sortByUnread)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (localStorage.getItem('chat.sort-by-unread') === 'true') {
|
|
|
|
document.getElementById('sort-by-unread-switch').checked = true
|
|
|
|
document.getElementById('sort-by-unread-switch').dispatchEvent(new Event('change'))
|
|
|
|
}
|
|
|
|
|
2024-04-27 06:57:01 +00:00
|
|
|
/**
|
|
|
|
* 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-28 14:56:30 +00:00
|
|
|
case 'edit_message':
|
|
|
|
editMessage(data)
|
|
|
|
break
|
|
|
|
case 'delete_message':
|
|
|
|
deleteMessage(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-28 21:25:15 +00:00
|
|
|
case 'mark_read':
|
|
|
|
markMessageAsRead(data)
|
|
|
|
break
|
2024-04-28 13:09:52 +00:00
|
|
|
case 'start_private_chat':
|
|
|
|
startPrivateChat(data)
|
|
|
|
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/'
|
|
|
|
)
|
2024-04-28 13:09:52 +00:00
|
|
|
let socketOpen = false
|
2024-04-27 06:57:01 +00:00
|
|
|
|
|
|
|
// 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…')
|
2024-04-28 13:09:52 +00:00
|
|
|
setTimeout(() => setupSocket(socketOpen ? 1000 : 2 * nextDelay), nextDelay)
|
2024-04-27 06:57:01 +00:00
|
|
|
})
|
2024-04-27 07:53:55 +00:00
|
|
|
|
|
|
|
socket.addEventListener('open', e => {
|
2024-04-28 13:09:52 +00:00
|
|
|
socketOpen = true
|
2024-04-27 07:53:55 +00:00
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'type': 'fetch_channels',
|
|
|
|
}))
|
|
|
|
})
|
2024-04-27 06:57:01 +00:00
|
|
|
}
|
|
|
|
|
2024-04-28 10:55:25 +00:00
|
|
|
function setupSwipeOffscreen() {
|
|
|
|
const offcanvas = new bootstrap.Offcanvas(document.getElementById('channelSelector'))
|
2024-04-27 17:11:03 +00:00
|
|
|
|
2024-04-28 10:55:25 +00:00
|
|
|
let lastX = null
|
|
|
|
document.addEventListener('touchstart', (event) => {
|
|
|
|
if (event.touches.length === 1)
|
|
|
|
lastX = event.touches[0].clientX
|
|
|
|
})
|
|
|
|
document.addEventListener('touchmove', (event) => {
|
|
|
|
if (event.touches.length === 1 && lastX !== null) {
|
|
|
|
const diff = event.touches[0].clientX - lastX
|
|
|
|
if (diff > window.innerWidth / 10 && lastX < window.innerWidth / 4) {
|
|
|
|
offcanvas.show()
|
|
|
|
lastX = null
|
2024-04-27 17:11:03 +00:00
|
|
|
}
|
2024-04-28 10:55:25 +00:00
|
|
|
else if (diff < -window.innerWidth / 10) {
|
|
|
|
offcanvas.hide()
|
|
|
|
lastX = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
document.addEventListener('touchend', () => {
|
|
|
|
lastX = null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-28 21:25:15 +00:00
|
|
|
function setupReadTracker() {
|
|
|
|
const scrollableContent = document.getElementById('chat-messages')
|
|
|
|
const messagesList = document.getElementById('message-list')
|
|
|
|
let markReadBuffer = []
|
|
|
|
let markReadTimeout = null
|
|
|
|
|
|
|
|
scrollableContent.addEventListener('scroll', () => {
|
|
|
|
if (scrollableContent.clientHeight - scrollableContent.scrollTop === scrollableContent.scrollHeight
|
|
|
|
&& !document.getElementById('fetch-previous-messages').classList.contains('d-none')) {
|
|
|
|
// If the user is at the top of the chat, fetch previous messages
|
|
|
|
fetchPreviousMessages()}
|
|
|
|
|
|
|
|
markVisibleMessagesAsRead()
|
|
|
|
})
|
|
|
|
|
|
|
|
messagesList.addEventListener('updatemessages', () => markVisibleMessagesAsRead())
|
|
|
|
|
|
|
|
function markVisibleMessagesAsRead() {
|
|
|
|
let viewport = scrollableContent.getBoundingClientRect()
|
|
|
|
|
|
|
|
for (let item of messagesList.querySelectorAll('.message')) {
|
|
|
|
let message = messages[selected_channel_id].get(parseInt(item.getAttribute('data-message-id')))
|
|
|
|
if (!message.read) {
|
|
|
|
let rect = item.getBoundingClientRect()
|
|
|
|
if (rect.top >= viewport.top && rect.bottom <= viewport.bottom) {
|
|
|
|
message.read = true
|
|
|
|
markReadBuffer.push(message['id'])
|
|
|
|
if (markReadTimeout)
|
|
|
|
clearTimeout(markReadTimeout)
|
|
|
|
markReadTimeout = setTimeout(() => {
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
'type': 'mark_read',
|
|
|
|
'message_ids': markReadBuffer,
|
|
|
|
}))
|
|
|
|
markReadBuffer = []
|
|
|
|
markReadTimeout = null
|
|
|
|
}, 3000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
markVisibleMessagesAsRead()
|
|
|
|
}
|
|
|
|
|
2024-04-28 10:55:25 +00:00
|
|
|
function setupPWAPrompt() {
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
setupSocket()
|
|
|
|
setupSwipeOffscreen()
|
2024-04-28 21:25:15 +00:00
|
|
|
setupReadTracker()
|
2024-04-28 10:55:25 +00:00
|
|
|
setupPWAPrompt()
|
2024-04-27 06:57:01 +00:00
|
|
|
})
|