398 lines
7.8 KiB
JavaScript
398 lines
7.8 KiB
JavaScript
// chat-notifications.js
|
|
//
|
|
// Reliable parent-level unread notification handling.
|
|
// Incoming LiveKit chat is observed directly in the host app instead
|
|
// of depending on the chat iframe to echo a notification back.
|
|
|
|
window.Kronick =
|
|
window.Kronick || {};
|
|
|
|
(() => {
|
|
const seenMessageKeys =
|
|
new Map();
|
|
|
|
const MESSAGE_DEDUPE_MS =
|
|
15000;
|
|
|
|
let unreadCount =
|
|
0;
|
|
|
|
function getChatButton() {
|
|
return document.getElementById(
|
|
'chatToolButton'
|
|
);
|
|
}
|
|
|
|
function getUnreadBadge() {
|
|
return document.getElementById(
|
|
'chatUnreadCount'
|
|
);
|
|
}
|
|
|
|
function getChatWindow() {
|
|
return document.getElementById(
|
|
'chatFrameWrap'
|
|
);
|
|
}
|
|
|
|
function getMessageKey(message) {
|
|
if (!message) {
|
|
return '';
|
|
}
|
|
|
|
if (message.id) {
|
|
return String(
|
|
message.id
|
|
);
|
|
}
|
|
|
|
return [
|
|
message.sender || '',
|
|
message.createdAt || '',
|
|
message.text || '',
|
|
message.gifUrl || '',
|
|
message.imageUrl || ''
|
|
].join('|');
|
|
}
|
|
|
|
function pruneSeenMessages() {
|
|
const cutoff =
|
|
Date.now()
|
|
- MESSAGE_DEDUPE_MS;
|
|
|
|
for (
|
|
const [key, timestamp]
|
|
of seenMessageKeys
|
|
) {
|
|
if (timestamp < cutoff) {
|
|
seenMessageKeys.delete(
|
|
key
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
function markMessageSeen(message) {
|
|
const key =
|
|
getMessageKey(
|
|
message
|
|
);
|
|
|
|
if (!key) {
|
|
return false;
|
|
}
|
|
|
|
pruneSeenMessages();
|
|
|
|
if (
|
|
seenMessageKeys.has(
|
|
key
|
|
)
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
seenMessageKeys.set(
|
|
key,
|
|
Date.now()
|
|
);
|
|
|
|
return true;
|
|
}
|
|
|
|
function chatIsVisible() {
|
|
const chatWindow =
|
|
getChatWindow();
|
|
|
|
if (!chatWindow) {
|
|
return false;
|
|
}
|
|
|
|
if (
|
|
chatWindow.classList.contains(
|
|
'window-minimized'
|
|
)
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
const style =
|
|
window.getComputedStyle(
|
|
chatWindow
|
|
);
|
|
|
|
return (
|
|
style.display !== 'none'
|
|
&& style.visibility !== 'hidden'
|
|
&& style.opacity !== '0'
|
|
);
|
|
}
|
|
|
|
function isOwnMessage(message) {
|
|
const localIdentity =
|
|
Kronick.state
|
|
?.room
|
|
?.localParticipant
|
|
?.identity
|
|
|| '';
|
|
|
|
if (!localIdentity) {
|
|
return false;
|
|
}
|
|
|
|
return (
|
|
String(
|
|
message?.sender
|
|
|| ''
|
|
)
|
|
=== String(
|
|
localIdentity
|
|
)
|
|
);
|
|
}
|
|
|
|
function renderUnreadState() {
|
|
const button =
|
|
getChatButton();
|
|
|
|
const badge =
|
|
getUnreadBadge();
|
|
|
|
if (!button || !badge) {
|
|
return;
|
|
}
|
|
|
|
const hasUnread =
|
|
unreadCount > 0;
|
|
|
|
button.classList.toggle(
|
|
'chat-alert',
|
|
hasUnread
|
|
);
|
|
|
|
badge.dataset.count =
|
|
String(
|
|
unreadCount
|
|
);
|
|
|
|
badge.textContent =
|
|
unreadCount > 99
|
|
? '99+'
|
|
: String(
|
|
unreadCount
|
|
);
|
|
|
|
badge.hidden =
|
|
!hasUnread;
|
|
|
|
button.setAttribute(
|
|
'aria-label',
|
|
hasUnread
|
|
? `Open chat, ${unreadCount} unread message${
|
|
unreadCount === 1
|
|
? ''
|
|
: 's'
|
|
}`
|
|
: 'Open chat'
|
|
);
|
|
}
|
|
|
|
function clearUnread() {
|
|
unreadCount =
|
|
0;
|
|
|
|
renderUnreadState();
|
|
}
|
|
|
|
function notifyForMessage(
|
|
message
|
|
) {
|
|
if (!message) {
|
|
return;
|
|
}
|
|
|
|
if (
|
|
isOwnMessage(
|
|
message
|
|
)
|
|
) {
|
|
return;
|
|
}
|
|
|
|
if (
|
|
!markMessageSeen(
|
|
message
|
|
)
|
|
) {
|
|
return;
|
|
}
|
|
|
|
if (
|
|
chatIsVisible()
|
|
) {
|
|
clearUnread();
|
|
return;
|
|
}
|
|
|
|
unreadCount +=
|
|
1;
|
|
|
|
renderUnreadState();
|
|
}
|
|
|
|
function installReceiveHook() {
|
|
const originalReceive =
|
|
Kronick.receiveChatMessage;
|
|
|
|
if (
|
|
typeof originalReceive
|
|
!== 'function'
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
if (
|
|
originalReceive
|
|
.__kronickUnreadWrapped
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
const wrappedReceive =
|
|
function wrappedReceiveChatMessage(
|
|
message
|
|
) {
|
|
const result =
|
|
originalReceive.apply(
|
|
this,
|
|
arguments
|
|
);
|
|
|
|
notifyForMessage(
|
|
message
|
|
);
|
|
|
|
return result;
|
|
};
|
|
|
|
wrappedReceive
|
|
.__kronickUnreadWrapped =
|
|
true;
|
|
|
|
Kronick.receiveChatMessage =
|
|
wrappedReceive;
|
|
|
|
return true;
|
|
}
|
|
|
|
function installUiHooks() {
|
|
const chatButton =
|
|
getChatButton();
|
|
|
|
const chatWindow =
|
|
getChatWindow();
|
|
|
|
chatButton?.addEventListener(
|
|
'click',
|
|
() => {
|
|
window.setTimeout(
|
|
() => {
|
|
if (
|
|
chatIsVisible()
|
|
) {
|
|
clearUnread();
|
|
}
|
|
},
|
|
0
|
|
);
|
|
}
|
|
);
|
|
|
|
if (chatWindow) {
|
|
const observer =
|
|
new MutationObserver(
|
|
() => {
|
|
if (
|
|
chatIsVisible()
|
|
) {
|
|
clearUnread();
|
|
}
|
|
}
|
|
);
|
|
|
|
observer.observe(
|
|
chatWindow,
|
|
{
|
|
attributes:
|
|
true,
|
|
|
|
attributeFilter:
|
|
[
|
|
'class',
|
|
'style',
|
|
'hidden'
|
|
]
|
|
}
|
|
);
|
|
}
|
|
|
|
window.addEventListener(
|
|
'focus',
|
|
() => {
|
|
if (
|
|
chatIsVisible()
|
|
) {
|
|
clearUnread();
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
// Keep old iframe notification calls compatible,
|
|
// but funnel them through the same deduped handler.
|
|
Kronick
|
|
.handleIncomingChatNotification =
|
|
notifyForMessage;
|
|
|
|
Kronick
|
|
.clearChatNotification =
|
|
clearUnread;
|
|
|
|
function init() {
|
|
installReceiveHook();
|
|
installUiHooks();
|
|
renderUnreadState();
|
|
|
|
if (
|
|
typeof Kronick.receiveChatMessage
|
|
!== 'function'
|
|
) {
|
|
window.setTimeout(
|
|
installReceiveHook,
|
|
0
|
|
);
|
|
|
|
window.setTimeout(
|
|
installReceiveHook,
|
|
250
|
|
);
|
|
}
|
|
}
|
|
|
|
if (
|
|
document.readyState
|
|
=== 'loading'
|
|
) {
|
|
document.addEventListener(
|
|
'DOMContentLoaded',
|
|
init,
|
|
{
|
|
once:
|
|
true
|
|
}
|
|
);
|
|
} else {
|
|
init();
|
|
}
|
|
})();
|