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.
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<audio id="mm-chat-sound" src="{{ asset('assets/panel/audio/sound.mp3') }}" preload="auto" playsinline hidden></audio>
|
||||
<script>
|
||||
window.MM_CHAT = {
|
||||
conversationsUrl: @json(url('/panel/chat/conversations')),
|
||||
|
||||
Reference in New Issue
Block a user