feat(inbox): automatically mark inbox items read when reading or viewing conversation

This commit is contained in:
tyuXX 2026-10-31 16:28:58 +03:00
parent dcccc67d3e
commit 0de9c40337
6 changed files with 145 additions and 4 deletions

View file

@ -1218,6 +1218,11 @@
joinChannel(deepChannel);
switchTab('chat');
window.history.replaceState({}, document.title, window.location.pathname);
} else {
const isChatTabActive = el.tabViews && Array.from(el.tabViews).some(v => v.id === 'view-chat' && v.classList.contains('active'));
if (isChatTabActive && !document.hidden) {
markActiveChannelInboxRead();
}
}
} catch {
// Ignore URL parse issues
@ -1252,6 +1257,10 @@
if (state.activeChannelId === histChId) {
renderMessages(!isPrepend);
const isChatTabActive = el.tabViews && Array.from(el.tabViews).some(v => v.id === 'view-chat' && v.classList.contains('active'));
if (isChatTabActive && !document.hidden) {
markActiveChannelInboxRead();
}
}
}
break;
@ -1265,6 +1274,10 @@
state.messages[chId].push(msg);
if (state.activeChannelId === chId) {
appendMessage(msg);
const isChatTabActive = el.tabViews && Array.from(el.tabViews).some(v => v.id === 'view-chat' && v.classList.contains('active'));
if (isChatTabActive && !document.hidden) {
markActiveChannelInboxRead();
}
}
}
break;
@ -1326,16 +1339,24 @@
if (rawItem) {
const item = await decryptInboxItemIfNeeded(rawItem);
const itemId = item.id || item.Id;
const itemChId = item.channelId || item.ChannelId;
const isChatTabActive = el.tabViews && Array.from(el.tabViews).some(v => v.id === 'view-chat' && v.classList.contains('active'));
const isViewingSameChannel = state.activeChannelId === itemChId && isChatTabActive && !document.hidden;
if (isViewingSameChannel) {
item.isRead = true;
item.IsRead = true;
sendInboxAction('mark_read', itemId, itemChId);
}
state.inboxItems = [item, ...state.inboxItems.filter(x => (x.id || x.Id) !== itemId)];
state.unreadInboxCount = payload.unreadCount ?? payload.UnreadCount ?? state.inboxItems.filter(i => !(i.isRead ?? i.IsRead)).length;
state.unreadInboxCount = state.inboxItems.filter(i => !(i.isRead ?? i.IsRead)).length;
updateInboxBadge();
renderInbox();
const itemChId = item.channelId || item.ChannelId;
const itemCategory = (item.category || item.Category || 'mention').toLowerCase();
const itemSender = item.senderUsername || item.SenderUsername || 'System';
const itemChName = item.channelName || item.ChannelName || 'chat';
const isViewingSameChannel = state.activeChannelId === itemChId && !document.hidden;
if (!isViewingSameChannel) {
showToast(`🔔 [${itemCategory.toUpperCase()}] @${itemSender} in ${itemChName}`);
@ -1444,6 +1465,28 @@
});
}
function markActiveChannelInboxRead() {
if (!state.activeChannelId) return;
let markedAny = false;
for (const item of state.inboxItems) {
const chId = item.channelId || item.ChannelId;
const isRead = item.isRead ?? item.IsRead ?? false;
if (chId === state.activeChannelId && !isRead) {
item.isRead = true;
item.IsRead = true;
markedAny = true;
}
}
if (markedAny) {
state.unreadInboxCount = state.inboxItems.filter(i => !(i.isRead ?? i.IsRead)).length;
updateInboxBadge();
renderInbox();
sendInboxAction('mark_read', null, state.activeChannelId);
}
}
function toggleInboxPreference(prefKey) {
const prefs = state.inboxPreferences;
if (prefKey in prefs) {
@ -1720,6 +1763,8 @@
renderInbox();
} else if (tabName === 'chat') {
renderMessages();
if (el.messageInput) el.messageInput.focus();
markActiveChannelInboxRead();
}
}
@ -2028,6 +2073,7 @@
if (!channelId) return;
state.activeChannelId = channelId;
sendEnvelope('JOIN_CHANNEL', { channelId });
markActiveChannelInboxRead();
renderChannels();
updateActiveChannelHeader();
renderMessages();
@ -2777,6 +2823,28 @@
if (el.testWebPushBtn) {
el.testWebPushBtn.addEventListener('click', sendTestWebPush);
}
// Mark active conversation inbox items read when focusing or interacting
window.addEventListener('focus', () => {
const isChatTabActive = el.tabViews && Array.from(el.tabViews).some(v => v.id === 'view-chat' && v.classList.contains('active'));
if (isChatTabActive && !document.hidden) {
markActiveChannelInboxRead();
}
});
document.addEventListener('visibilitychange', () => {
if (!document.hidden) {
const isChatTabActive = el.tabViews && Array.from(el.tabViews).some(v => v.id === 'view-chat' && v.classList.contains('active'));
if (isChatTabActive) {
markActiveChannelInboxRead();
}
}
});
if (el.messageInput) {
el.messageInput.addEventListener('focus', markActiveChannelInboxRead);
}
if (el.chatMessages) {
el.chatMessages.addEventListener('click', markActiveChannelInboxRead);
}
if (el.e2eePinForm) {
el.e2eePinForm.addEventListener('submit', async (e) => {
e.preventDefault();