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:
1 parent
f4f781b835
commit
c78f9bc944
2 files changed
+118
-34
No files matched your search
+117
-34
@@ -19,9 +19,23 @@
|
|||||||
echo: null,
|
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();
|
const soundedIds = new Set();
|
||||||
let inboxReady = false;
|
let inboxReady = false;
|
||||||
|
let soundReady = false;
|
||||||
|
let soundPending = false;
|
||||||
|
|
||||||
function headers(json) {
|
function headers(json) {
|
||||||
const next = {
|
const next = {
|
||||||
@@ -130,22 +144,77 @@
|
|||||||
return !otherId || message.user?.id !== otherId;
|
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) {
|
function playIncomingSound(message) {
|
||||||
if (!message || !message.id || soundedIds.has(message.id)) {
|
const key = messageKey(message);
|
||||||
|
if (!key || soundedIds.has(key)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
soundedIds.add(key);
|
||||||
const conversation = state.conversations.find(
|
playNotifySound();
|
||||||
(item) => item.id === message.conversation_id
|
|
||||||
);
|
|
||||||
|
|
||||||
if (conversation && isOutgoing(message, conversation)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
soundedIds.add(message.id);
|
|
||||||
notifySound.currentTime = 0;
|
|
||||||
notifySound.play().catch(() => {});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderMessages() {
|
function renderMessages() {
|
||||||
@@ -185,19 +254,26 @@
|
|||||||
|
|
||||||
if (inboxReady) {
|
if (inboxReady) {
|
||||||
state.conversations.forEach((conversation) => {
|
state.conversations.forEach((conversation) => {
|
||||||
const last = conversation.last_message;
|
|
||||||
if (!last) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const prev = previous.find((item) => item.id === conversation.id);
|
const prev = previous.find((item) => item.id === conversation.id);
|
||||||
const prevLastId = prev && prev.last_message && prev.last_message.id;
|
const last = conversation.last_message;
|
||||||
if (prevLastId === last.id) {
|
const lastChanged = Boolean(last) && messageKey(last) !== messageKey(prev && prev.last_message);
|
||||||
return;
|
const unreadUp =
|
||||||
|
(conversation.unread_count || 0) > ((prev && prev.unread_count) || 0);
|
||||||
|
|
||||||
|
if (lastChanged) {
|
||||||
|
playIncomingSound(last);
|
||||||
|
} else if (unreadUp) {
|
||||||
|
playNotifySound();
|
||||||
}
|
}
|
||||||
playIncomingSound(last);
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
inboxReady = true;
|
inboxReady = true;
|
||||||
|
state.conversations.forEach((conversation) => {
|
||||||
|
const key = messageKey(conversation.last_message);
|
||||||
|
if (key) {
|
||||||
|
soundedIds.add(key);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
renderInbox();
|
renderInbox();
|
||||||
@@ -229,6 +305,13 @@
|
|||||||
state.messages = items.concat(state.messages);
|
state.messages = items.concat(state.messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
items.forEach((message) => {
|
||||||
|
const key = messageKey(message);
|
||||||
|
if (key) {
|
||||||
|
soundedIds.add(key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
renderMessages();
|
renderMessages();
|
||||||
} finally {
|
} finally {
|
||||||
state.loadingMessages = false;
|
state.loadingMessages = false;
|
||||||
@@ -296,7 +379,10 @@
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
const message = payload.data;
|
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);
|
state.messages.push(message);
|
||||||
renderMessages();
|
renderMessages();
|
||||||
}
|
}
|
||||||
@@ -308,7 +394,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function upsertIncoming(message) {
|
function upsertIncoming(payload) {
|
||||||
|
const message = normalizeIncoming(payload);
|
||||||
if (!message || !message.conversation_id) {
|
if (!message || !message.conversation_id) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -316,7 +403,7 @@
|
|||||||
playIncomingSound(message);
|
playIncomingSound(message);
|
||||||
|
|
||||||
if (state.activeId === message.conversation_id) {
|
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);
|
state.messages.push(message);
|
||||||
renderMessages();
|
renderMessages();
|
||||||
}
|
}
|
||||||
@@ -429,11 +516,11 @@
|
|||||||
{ headers: headers() }
|
{ headers: headers() }
|
||||||
);
|
);
|
||||||
const items = (payload.data || []).slice().reverse();
|
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;
|
let added = false;
|
||||||
|
|
||||||
items.forEach((message) => {
|
items.forEach((message) => {
|
||||||
if (!known.has(message.id)) {
|
if (!known.has(String(message.id))) {
|
||||||
state.messages.push(message);
|
state.messages.push(message);
|
||||||
added = true;
|
added = true;
|
||||||
playIncomingSound(message);
|
playIncomingSound(message);
|
||||||
@@ -467,13 +554,9 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
document.addEventListener(
|
["pointerdown", "keydown", "touchstart", "click"].forEach((eventName) => {
|
||||||
"pointerdown",
|
document.addEventListener(eventName, unlockSound, true);
|
||||||
() => {
|
});
|
||||||
notifySound.load();
|
|
||||||
},
|
|
||||||
{ once: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
loadInbox();
|
loadInbox();
|
||||||
bootEcho();
|
bootEcho();
|
||||||
|
|||||||
@@ -41,6 +41,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<audio id="mm-chat-sound" src="{{ asset('assets/panel/audio/sound.mp3') }}" preload="auto" playsinline hidden></audio>
|
||||||
<script>
|
<script>
|
||||||
window.MM_CHAT = {
|
window.MM_CHAT = {
|
||||||
conversationsUrl: @json(url('/panel/chat/conversations')),
|
conversationsUrl: @json(url('/panel/chat/conversations')),
|
||||||
|
|||||||
Reference in new issue
Block a user