feat(inbox): automatically mark inbox items read when reading or viewing conversation
This commit is contained in:
parent
dcccc67d3e
commit
0de9c40337
6 changed files with 145 additions and 4 deletions
|
|
@ -740,6 +740,7 @@ public class ChatClientConnection : IDisposable
|
||||||
|
|
||||||
public Task JoinChannelAsync(string channelId)
|
public Task JoinChannelAsync(string channelId)
|
||||||
{
|
{
|
||||||
|
_state.MarkChannelInboxItemsReadLocally(channelId);
|
||||||
var envelope = NetworkEnvelope.Create(MessageTypes.JoinChannel, new JoinChannelPacket { ChannelId = channelId });
|
var envelope = NetworkEnvelope.Create(MessageTypes.JoinChannel, new JoinChannelPacket { ChannelId = channelId });
|
||||||
return SendEnvelopeAsync(envelope);
|
return SendEnvelopeAsync(envelope);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,19 @@ public class ConsoleUi
|
||||||
// Check Screen Swapping Function Keys
|
// Check Screen Swapping Function Keys
|
||||||
if (key.Key == ConsoleKey.F1)
|
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;
|
_state.NeedsRedraw = true;
|
||||||
}
|
}
|
||||||
else if (key.Key == ConsoleKey.F2)
|
else if (key.Key == ConsoleKey.F2)
|
||||||
|
|
|
||||||
|
|
@ -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<InboxItem> GetInboxItems()
|
public List<InboxItem> GetInboxItems()
|
||||||
{
|
{
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
|
|
|
||||||
|
|
@ -1218,6 +1218,11 @@
|
||||||
joinChannel(deepChannel);
|
joinChannel(deepChannel);
|
||||||
switchTab('chat');
|
switchTab('chat');
|
||||||
window.history.replaceState({}, document.title, window.location.pathname);
|
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 {
|
} catch {
|
||||||
// Ignore URL parse issues
|
// Ignore URL parse issues
|
||||||
|
|
@ -1252,6 +1257,10 @@
|
||||||
|
|
||||||
if (state.activeChannelId === histChId) {
|
if (state.activeChannelId === histChId) {
|
||||||
renderMessages(!isPrepend);
|
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;
|
break;
|
||||||
|
|
@ -1265,6 +1274,10 @@
|
||||||
state.messages[chId].push(msg);
|
state.messages[chId].push(msg);
|
||||||
if (state.activeChannelId === chId) {
|
if (state.activeChannelId === chId) {
|
||||||
appendMessage(msg);
|
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;
|
break;
|
||||||
|
|
@ -1326,16 +1339,24 @@
|
||||||
if (rawItem) {
|
if (rawItem) {
|
||||||
const item = await decryptInboxItemIfNeeded(rawItem);
|
const item = await decryptInboxItemIfNeeded(rawItem);
|
||||||
const itemId = item.id || item.Id;
|
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.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();
|
updateInboxBadge();
|
||||||
renderInbox();
|
renderInbox();
|
||||||
|
|
||||||
const itemChId = item.channelId || item.ChannelId;
|
|
||||||
const itemCategory = (item.category || item.Category || 'mention').toLowerCase();
|
const itemCategory = (item.category || item.Category || 'mention').toLowerCase();
|
||||||
const itemSender = item.senderUsername || item.SenderUsername || 'System';
|
const itemSender = item.senderUsername || item.SenderUsername || 'System';
|
||||||
const itemChName = item.channelName || item.ChannelName || 'chat';
|
const itemChName = item.channelName || item.ChannelName || 'chat';
|
||||||
const isViewingSameChannel = state.activeChannelId === itemChId && !document.hidden;
|
|
||||||
|
|
||||||
if (!isViewingSameChannel) {
|
if (!isViewingSameChannel) {
|
||||||
showToast(`🔔 [${itemCategory.toUpperCase()}] @${itemSender} in ${itemChName}`);
|
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) {
|
function toggleInboxPreference(prefKey) {
|
||||||
const prefs = state.inboxPreferences;
|
const prefs = state.inboxPreferences;
|
||||||
if (prefKey in prefs) {
|
if (prefKey in prefs) {
|
||||||
|
|
@ -1720,6 +1763,8 @@
|
||||||
renderInbox();
|
renderInbox();
|
||||||
} else if (tabName === 'chat') {
|
} else if (tabName === 'chat') {
|
||||||
renderMessages();
|
renderMessages();
|
||||||
|
if (el.messageInput) el.messageInput.focus();
|
||||||
|
markActiveChannelInboxRead();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2028,6 +2073,7 @@
|
||||||
if (!channelId) return;
|
if (!channelId) return;
|
||||||
state.activeChannelId = channelId;
|
state.activeChannelId = channelId;
|
||||||
sendEnvelope('JOIN_CHANNEL', { channelId });
|
sendEnvelope('JOIN_CHANNEL', { channelId });
|
||||||
|
markActiveChannelInboxRead();
|
||||||
renderChannels();
|
renderChannels();
|
||||||
updateActiveChannelHeader();
|
updateActiveChannelHeader();
|
||||||
renderMessages();
|
renderMessages();
|
||||||
|
|
@ -2777,6 +2823,28 @@
|
||||||
if (el.testWebPushBtn) {
|
if (el.testWebPushBtn) {
|
||||||
el.testWebPushBtn.addEventListener('click', sendTestWebPush);
|
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) {
|
if (el.e2eePinForm) {
|
||||||
el.e2eePinForm.addEventListener('submit', async (e) => {
|
el.e2eePinForm.addEventListener('submit', async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
|
||||||
|
|
@ -1002,6 +1002,42 @@ public class EndToEndChatTests : IAsyncLifetime
|
||||||
bobItems = stateBob.GetInboxItems();
|
bobItems = stateBob.GetInboxItems();
|
||||||
Assert.Contains(bobItems, x => x.Category == InboxCategories.FollowedChannel && x.ContentPreview.Contains("Regular update"));
|
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
|
// 5. Bob marks all read and clears read items
|
||||||
await connBob.SendInboxActionAsync("mark_all_read");
|
await connBob.SendInboxActionAsync("mark_all_read");
|
||||||
for (int i = 0; i < 20; i++)
|
for (int i = 0; i < 20; i++)
|
||||||
|
|
|
||||||
|
|
@ -153,6 +153,7 @@ public class WebClientStaticTests : IAsyncLifetime
|
||||||
Assert.Contains("subscribeWebPush", js);
|
Assert.Contains("subscribeWebPush", js);
|
||||||
Assert.Contains("updateDesktopNotifBanner", js);
|
Assert.Contains("updateDesktopNotifBanner", js);
|
||||||
Assert.Contains("initServiceWorkerAndPush", js);
|
Assert.Contains("initServiceWorkerAndPush", js);
|
||||||
|
Assert.Contains("markActiveChannelInboxRead", js);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue