From c78f9bc944e5093c7f0da2dbe952d402ed38f0a6 Mon Sep 17 00:00:00 2001 From: Mekan1206 Date: Sun, 16 Aug 2026 19:35:51 +0500 Subject: [PATCH] Enhance audio notification system in chat.js: refactor sound handling for incoming messages, ensuring proper audio element management and improved playback logic. Add audio element in chat.blade.php for seamless integration. --- public/assets/panel/js/chat.js | 151 +++++++++++++++++++++------- resources/views/nova/chat.blade.php | 1 + 2 files changed, 118 insertions(+), 34 deletions(-) diff --git a/public/assets/panel/js/chat.js b/public/assets/panel/js/chat.js index 3873012..bbe85e0 100644 --- a/public/assets/panel/js/chat.js +++ b/public/assets/panel/js/chat.js @@ -19,9 +19,23 @@ echo: null, }; - const notifySound = new Audio("/assets/panel/audio/sound.mp3"); + const notifySound = + document.getElementById("mm-chat-sound") || new Audio("/assets/panel/audio/sound.mp3"); + notifySound.preload = "auto"; + notifySound.playsInline = true; + notifySound.controls = false; + notifySound.muted = false; + notifySound.volume = 1; + notifySound.setAttribute("playsinline", "true"); + if (!notifySound.isConnected) { + notifySound.style.cssText = "position:absolute;width:0;height:0;opacity:0;pointer-events:none"; + document.body.appendChild(notifySound); + } + const soundedIds = new Set(); let inboxReady = false; + let soundReady = false; + let soundPending = false; function headers(json) { const next = { @@ -130,22 +144,77 @@ return !otherId || message.user?.id !== otherId; } + function messageKey(message) { + return message && message.id != null ? String(message.id) : ""; + } + + function normalizeIncoming(payload) { + if (!payload || typeof payload !== "object") { + return payload; + } + if (payload.conversation_id && payload.id) { + return payload; + } + if (payload.message && payload.message.id) { + return payload.message; + } + if (payload.data && payload.data.id) { + return payload.data; + } + return payload; + } + + function unlockSound() { + if (soundReady && !soundPending) { + return; + } + notifySound.muted = true; + const play = notifySound.play(); + if (!play || typeof play.then !== "function") { + notifySound.muted = false; + soundReady = true; + return; + } + play + .then(() => { + notifySound.pause(); + notifySound.currentTime = 0; + notifySound.muted = false; + soundReady = true; + if (soundPending) { + soundPending = false; + playNotifySound(); + } + }) + .catch(() => { + notifySound.muted = false; + }); + } + + function playNotifySound() { + if (!soundReady) { + soundPending = true; + return; + } + try { + notifySound.pause(); + notifySound.currentTime = 0; + } catch (error) { + // Ignore until metadata is ready. + } + notifySound.play().catch(() => { + soundReady = false; + soundPending = true; + }); + } + function playIncomingSound(message) { - if (!message || !message.id || soundedIds.has(message.id)) { + const key = messageKey(message); + if (!key || soundedIds.has(key)) { return; } - - const conversation = state.conversations.find( - (item) => item.id === message.conversation_id - ); - - if (conversation && isOutgoing(message, conversation)) { - return; - } - - soundedIds.add(message.id); - notifySound.currentTime = 0; - notifySound.play().catch(() => {}); + soundedIds.add(key); + playNotifySound(); } function renderMessages() { @@ -185,19 +254,26 @@ if (inboxReady) { state.conversations.forEach((conversation) => { - const last = conversation.last_message; - if (!last) { - return; - } const prev = previous.find((item) => item.id === conversation.id); - const prevLastId = prev && prev.last_message && prev.last_message.id; - if (prevLastId === last.id) { - return; + const last = conversation.last_message; + const lastChanged = Boolean(last) && messageKey(last) !== messageKey(prev && prev.last_message); + const unreadUp = + (conversation.unread_count || 0) > ((prev && prev.unread_count) || 0); + + if (lastChanged) { + playIncomingSound(last); + } else if (unreadUp) { + playNotifySound(); } - playIncomingSound(last); }); } else { inboxReady = true; + state.conversations.forEach((conversation) => { + const key = messageKey(conversation.last_message); + if (key) { + soundedIds.add(key); + } + }); } renderInbox(); @@ -229,6 +305,13 @@ state.messages = items.concat(state.messages); } + items.forEach((message) => { + const key = messageKey(message); + if (key) { + soundedIds.add(key); + } + }); + renderMessages(); } finally { state.loadingMessages = false; @@ -296,7 +379,10 @@ } ); const message = payload.data; - if (message && !state.messages.some((item) => item.id === message.id)) { + if (message && message.id) { + soundedIds.add(messageKey(message)); + } + if (message && !state.messages.some((item) => String(item.id) === String(message.id))) { state.messages.push(message); renderMessages(); } @@ -308,7 +394,8 @@ } } - function upsertIncoming(message) { + function upsertIncoming(payload) { + const message = normalizeIncoming(payload); if (!message || !message.conversation_id) { return; } @@ -316,7 +403,7 @@ playIncomingSound(message); if (state.activeId === message.conversation_id) { - if (!state.messages.some((item) => item.id === message.id)) { + if (!state.messages.some((item) => String(item.id) === String(message.id))) { state.messages.push(message); renderMessages(); } @@ -429,11 +516,11 @@ { headers: headers() } ); const items = (payload.data || []).slice().reverse(); - const known = new Set(state.messages.map((item) => item.id)); + const known = new Set(state.messages.map((item) => String(item.id))); let added = false; items.forEach((message) => { - if (!known.has(message.id)) { + if (!known.has(String(message.id))) { state.messages.push(message); added = true; playIncomingSound(message); @@ -467,13 +554,9 @@ } }); - document.addEventListener( - "pointerdown", - () => { - notifySound.load(); - }, - { once: true } - ); + ["pointerdown", "keydown", "touchstart", "click"].forEach((eventName) => { + document.addEventListener(eventName, unlockSound, true); + }); loadInbox(); bootEcho(); diff --git a/resources/views/nova/chat.blade.php b/resources/views/nova/chat.blade.php index 317cca0..d2345a6 100644 --- a/resources/views/nova/chat.blade.php +++ b/resources/views/nova/chat.blade.php @@ -41,6 +41,7 @@ +