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)
|
||||
{
|
||||
_state.MarkChannelInboxItemsReadLocally(channelId);
|
||||
var envelope = NetworkEnvelope.Create(MessageTypes.JoinChannel, new JoinChannelPacket { ChannelId = channelId });
|
||||
return SendEnvelopeAsync(envelope);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
{
|
||||
lock (_lock)
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue