Skip to content

Commit

Permalink
Better unread counts for MUC
Browse files Browse the repository at this point in the history
Use the stored version of messages for computing most recent and unread
counts, especially this means that edits are resolved so removing the
duplicate items will make sure we don't count an edit as a second unread
message.
  • Loading branch information
singpolyma committed Nov 19, 2024
1 parent 73fb4ce commit f0492f0
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions snikket/Chat.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1009,16 +1009,19 @@ class Channel extends Chat {
promises.push(new thenshim.Promise((resolve, reject) -> {
client.storeMessage(message, resolve);
}));
if (message.chatId() == chatId) chatMessages.push(message);
case ReactionUpdateStanza(update):
promises.push(new thenshim.Promise((resolve, reject) -> {
persistence.storeReaction(client.accountId(), update, resolve);
persistence.storeReaction(client.accountId(), update, (_) -> resolve(null));
}));
default:
// ignore
}
}
thenshim.PromiseTools.all(promises).then((_) -> {
thenshim.PromiseTools.all(promises).then((stored) -> {
for (message in stored) {
if (message != null && message.chatId() == chatId) chatMessages.push(message);
if (chatMessages.length > 1000) chatMessages.shift(); // Save some RAM
}
if (sync.hasMore()) {
sync.fetchNext();
} else {
Expand All @@ -1029,11 +1032,20 @@ class Channel extends Chat {
setLastMessage(lastFromSync);
client.sortChats();
}
final readIndex = chatMessages.findIndex((m) -> m.serverId == readUpTo());
final serverIds: Map<String, Bool> = [];
final dedupedMessages = [];
chatMessages.reverse();
for (m in chatMessages) {
if (!(serverIds[m.serverId] ?? false)) {
dedupedMessages.unshift(m);
serverIds[m.serverId] = true;
}
}
final readIndex = dedupedMessages.findIndex((m) -> m.serverId == readUpTo());
if (readIndex < 0) {
setUnreadCount(unreadCount() + chatMessages.length);
setUnreadCount(unreadCount() + dedupedMessages.length);
} else {
setUnreadCount(chatMessages.length - readIndex - 1);
setUnreadCount(dedupedMessages.length - readIndex - 1);
}
client.trigger("chats/update", [this]);
}
Expand Down

0 comments on commit f0492f0

Please sign in to comment.