|
|
|
@ -1,16 +1,45 @@
|
|
|
|
|
const chat_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
|
|
|
|
|
const protocol = window.location.protocol === "https:" ? "https" : "http";
|
|
|
|
|
const domain = "192.168.1.109:8000";
|
|
|
|
|
const domain = "osina.ositcom.com";
|
|
|
|
|
let osichatSocket;
|
|
|
|
|
let isOpen = false;
|
|
|
|
|
let chatLoaded = false;
|
|
|
|
|
let newMessageCount = 0;
|
|
|
|
|
let atBottom = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function scrollBottom() {
|
|
|
|
|
const conversationContainer = document.getElementById('conversation');
|
|
|
|
|
|
|
|
|
|
if (conversationContainer) {
|
|
|
|
|
conversationContainer.scrollTo({
|
|
|
|
|
top: conversationContainer.scrollHeight,
|
|
|
|
|
behavior: 'smooth'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isScrolledToBottom(element) {
|
|
|
|
|
return element && (element.scrollHeight - element.scrollTop <= element.clientHeight + 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function setupScrollEventListener() {
|
|
|
|
|
const conversationContainer = document.getElementById('conversation');
|
|
|
|
|
|
|
|
|
|
if (conversationContainer) {
|
|
|
|
|
conversationContainer.scrollTop = conversationContainer.scrollHeight;
|
|
|
|
|
conversationContainer.addEventListener('scroll', () => {
|
|
|
|
|
if (isScrolledToBottom(conversationContainer)) {
|
|
|
|
|
if (!atBottom) {
|
|
|
|
|
hideNewMessageNotification();
|
|
|
|
|
atBottom = true; // Update flag when user scrolls to the bottom
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
atBottom = false; // Update flag if user is not at the bottom
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -20,7 +49,6 @@ function showNewMessageNotification(count) {
|
|
|
|
|
if (newMessagesPopMessage && newMessagesCounter) {
|
|
|
|
|
newMessagesCounter.textContent = count;
|
|
|
|
|
newMessagesPopMessage.classList.remove('hidden');
|
|
|
|
|
console.log('popup displayed')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -28,17 +56,39 @@ function hideNewMessageNotification() {
|
|
|
|
|
const newMessagesPopMessage = document.getElementById('newMessagesPopMessage');
|
|
|
|
|
if (newMessagesPopMessage) {
|
|
|
|
|
newMessagesPopMessage.classList.add('hidden');
|
|
|
|
|
newMessageCount = 0;
|
|
|
|
|
const newMessagesCounter = document.getElementById('newMessagesCounter');
|
|
|
|
|
if (newMessagesCounter) {
|
|
|
|
|
newMessagesCounter.textContent = newMessageCount;
|
|
|
|
|
}
|
|
|
|
|
scrollBottom()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function newMessageNotification() {
|
|
|
|
|
const notificationSound = document.getElementById('notification-sound');
|
|
|
|
|
notificationSound.play();
|
|
|
|
|
|
|
|
|
|
const conversationContainer = document.getElementById('conversation');
|
|
|
|
|
|
|
|
|
|
if (!isScrolledToBottom(conversationContainer)) {
|
|
|
|
|
let count = newMessageCount + 1;
|
|
|
|
|
newMessageCount = count;
|
|
|
|
|
showNewMessageNotification(count);
|
|
|
|
|
} else {
|
|
|
|
|
scrollBottom();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// FUNCTION TO FETCH THE SESSION ID
|
|
|
|
|
async function fetchSessionID() {
|
|
|
|
|
let session_id = 'Unknown';
|
|
|
|
|
while (session_id === 'Unknown') {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch('http://192.168.1.109:3000/get-client-session/');
|
|
|
|
|
const response = await fetch('https://ositcom.com/get-client-session/');
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
if (data.session_id) {
|
|
|
|
|
session_id = data.session_id;
|
|
|
|
@ -106,6 +156,7 @@ function handleLoadChatEvent(data, osichatSocket) {
|
|
|
|
|
chatDiv.innerHTML = data.html;
|
|
|
|
|
scrollBottom();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isOpen) { // If chat widget isOpen (declared in chat-toggle.js) mark all messages as read by guest else just return number of unread messages
|
|
|
|
|
osichatSocket.send(JSON.stringify({ 'event_type': 'update_read_messages', 'chat_state': 'open' }));
|
|
|
|
|
} else {
|
|
|
|
@ -196,16 +247,16 @@ async function initializeChatWebSocket() {
|
|
|
|
|
osichatSocket.send(JSON.stringify({ 'event_type': 'update_read_messages', 'chat_state': 'closed' }));
|
|
|
|
|
}
|
|
|
|
|
messagesDiv.insertAdjacentHTML('beforeend', data.html);
|
|
|
|
|
|
|
|
|
|
const newMessagesPopMessage = document.getElementById('newMessagesPopMessage');
|
|
|
|
|
newMessagesPopMessage.addEventListener('click', hideNewMessageNotification);
|
|
|
|
|
|
|
|
|
|
if (data.user) { // If it is sent by an Osina user play a notification sound for the guest
|
|
|
|
|
const notificationSound = document.getElementById('notification-sound');
|
|
|
|
|
notificationSound.play();
|
|
|
|
|
if (typingDiv) {
|
|
|
|
|
typingDiv.remove();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let count = newMessageCount + 1;
|
|
|
|
|
newMessageCount = count;
|
|
|
|
|
showNewMessageNotification(count);
|
|
|
|
|
newMessageNotification();
|
|
|
|
|
setupScrollEventListener();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'uploaded_file':
|
|
|
|
@ -217,15 +268,19 @@ async function initializeChatWebSocket() {
|
|
|
|
|
const uploadingDiv = document.getElementById(`uploading-${data.file_name}`);
|
|
|
|
|
if (uploadingDiv) {
|
|
|
|
|
uploadingDiv.outerHTML = data.html;
|
|
|
|
|
scrollBottom();
|
|
|
|
|
if (!data.user) {
|
|
|
|
|
scrollBottom();
|
|
|
|
|
} else {
|
|
|
|
|
newMessageNotification();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
document.getElementById('messages').insertAdjacentHTML('beforeend', data.html);
|
|
|
|
|
scrollBottom();
|
|
|
|
|
}
|
|
|
|
|
if (data.user) { // If it is sent by an Osina user play a notification sound for the guest
|
|
|
|
|
const notificationSound = document.getElementById('notification-sound');
|
|
|
|
|
notificationSound.play();
|
|
|
|
|
if (!data.user) {
|
|
|
|
|
scrollBottom();
|
|
|
|
|
} else {
|
|
|
|
|
newMessageNotification();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'update_read_messages':
|
|
|
|
|