From 0de9c40337ca7dc2abdad2a15fac4dec0d1c4e50 Mon Sep 17 00:00:00 2001 From: tyuXX Date: Sat, 31 Oct 2026 16:28:58 +0300 Subject: [PATCH] feat(inbox): automatically mark inbox items read when reading or viewing conversation --- .../Network/ChatClientConnection.cs | 1 + src/GsDChat.Client/Rendering/ConsoleUi.cs | 14 +++- src/GsDChat.Client/State/ClientState.cs | 23 ++++++ src/GsDChat.Server/wwwroot/app.js | 74 ++++++++++++++++++- tests/GsDChat.Tests/EndToEndChatTests.cs | 36 +++++++++ tests/GsDChat.Tests/WebClientStaticTests.cs | 1 + 6 files changed, 145 insertions(+), 4 deletions(-) diff --git a/src/GsDChat.Client/Network/ChatClientConnection.cs b/src/GsDChat.Client/Network/ChatClientConnection.cs index 29ccdae..c86a94e 100644 --- a/src/GsDChat.Client/Network/ChatClientConnection.cs +++ b/src/GsDChat.Client/Network/ChatClientConnection.cs @@ -740,6 +740,7 @@ public class ChatClientConnection : IDisposable public Task JoinChannelAsync(string channelId) { + _state.MarkChannelInboxItemsReadLocally(channelId); var envelope = NetworkEnvelope.Create(MessageTypes.JoinChannel, new JoinChannelPacket { ChannelId = channelId }); return SendEnvelopeAsync(envelope); } diff --git a/src/GsDChat.Client/Rendering/ConsoleUi.cs b/src/GsDChat.Client/Rendering/ConsoleUi.cs index db676d8..1c21a7e 100644 --- a/src/GsDChat.Client/Rendering/ConsoleUi.cs +++ b/src/GsDChat.Client/Rendering/ConsoleUi.cs @@ -100,7 +100,19 @@ public class ConsoleUi // Check Screen Swapping Function Keys if (key.Key == ConsoleKey.F1) { - if (_state.ActiveScreenId != "F1_Chat") { _state.ActiveScreenId = "F1_Chat"; Console.Clear(); } + if (_state.ActiveScreenId != "F1_Chat") + { + _state.ActiveScreenId = "F1_Chat"; + Console.Clear(); + if (!string.IsNullOrEmpty(_state.ActiveChannelId)) + { + int changed = _state.MarkChannelInboxItemsReadLocally(_state.ActiveChannelId); + if (changed > 0) + { + _ = _connection.SendInboxActionAsync("mark_read", channelId: _state.ActiveChannelId); + } + } + } _state.NeedsRedraw = true; } else if (key.Key == ConsoleKey.F2) diff --git a/src/GsDChat.Client/State/ClientState.cs b/src/GsDChat.Client/State/ClientState.cs index 8e260fc..579b0de 100644 --- a/src/GsDChat.Client/State/ClientState.cs +++ b/src/GsDChat.Client/State/ClientState.cs @@ -477,6 +477,29 @@ public class ClientState } } + public int MarkChannelInboxItemsReadLocally(string channelId) + { + if (string.IsNullOrEmpty(channelId)) return 0; + lock (_lock) + { + int changed = 0; + foreach (var item in InboxItems) + { + if (string.Equals(item.ChannelId, channelId, StringComparison.OrdinalIgnoreCase) && !item.IsRead) + { + item.IsRead = true; + changed++; + } + } + if (changed > 0) + { + UnreadInboxCount = Math.Max(0, UnreadInboxCount - changed); + NeedsRedraw = true; + } + return changed; + } + } + public List GetInboxItems() { lock (_lock) diff --git a/src/GsDChat.Server/wwwroot/app.js b/src/GsDChat.Server/wwwroot/app.js index 24bdbef..42ab1e0 100644 --- a/src/GsDChat.Server/wwwroot/app.js +++ b/src/GsDChat.Server/wwwroot/app.js @@ -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(); diff --git a/tests/GsDChat.Tests/EndToEndChatTests.cs b/tests/GsDChat.Tests/EndToEndChatTests.cs index 508e821..0a14ce7 100644 --- a/tests/GsDChat.Tests/EndToEndChatTests.cs +++ b/tests/GsDChat.Tests/EndToEndChatTests.cs @@ -1002,6 +1002,42 @@ public class EndToEndChatTests : IAsyncLifetime bobItems = stateBob.GetInboxItems(); Assert.Contains(bobItems, x => x.Category == InboxCategories.FollowedChannel && x.ContentPreview.Contains("Regular update")); + // 4b. Bob looks at / joins #general -> all inbox items for #general are auto-marked read! + await connBob.JoinChannelAsync(generalId); + for (int i = 0; i < 20; i++) + { + if (stateBob.UnreadInboxCount == 0) break; + await Task.Delay(100); + } + Assert.Equal(0, stateBob.UnreadInboxCount); + bobItems = stateBob.GetInboxItems(); + Assert.All(bobItems, item => Assert.True(item.IsRead)); + + // 4c. Alice sends DM to Bob while Bob is in #general -> Bob receives unread DM inbox item + await connAlice.StartDmAsync(stateBob.CurrentUsername, stateBob.CurrentUserId); + for (int i = 0; i < 20; i++) + { + if (stateAlice.DirectMessages.Count > 0 && stateBob.DirectMessages.Count > 0) break; + await Task.Delay(100); + } + string dmId = stateBob.DirectMessages[0].Id; + await connAlice.SendChatMessageAsync(dmId, "Hey Bob, private message here!"); + for (int i = 0; i < 20; i++) + { + if (stateBob.UnreadInboxCount >= 1) break; + await Task.Delay(100); + } + Assert.Equal(1, stateBob.UnreadInboxCount); + + // When Bob opens/looks at the DM -> DM inbox item is marked read! + await connBob.JoinChannelAsync(dmId); + for (int i = 0; i < 20; i++) + { + if (stateBob.UnreadInboxCount == 0) break; + await Task.Delay(100); + } + Assert.Equal(0, stateBob.UnreadInboxCount); + // 5. Bob marks all read and clears read items await connBob.SendInboxActionAsync("mark_all_read"); for (int i = 0; i < 20; i++) diff --git a/tests/GsDChat.Tests/WebClientStaticTests.cs b/tests/GsDChat.Tests/WebClientStaticTests.cs index 0be95e7..246b6cb 100644 --- a/tests/GsDChat.Tests/WebClientStaticTests.cs +++ b/tests/GsDChat.Tests/WebClientStaticTests.cs @@ -153,6 +153,7 @@ public class WebClientStaticTests : IAsyncLifetime Assert.Contains("subscribeWebPush", js); Assert.Contains("updateDesktopNotifBanner", js); Assert.Contains("initServiceWorkerAndPush", js); + Assert.Contains("markActiveChannelInboxRead", js); } [Fact]