git » sdk » commit f0492f0

Better unread counts for MUC

author Stephen Paul Weber
2024-11-19 20:04:24 UTC
committer Stephen Paul Weber
2024-11-19 20:04:24 UTC
parent 73fb4ce9a2dfa39199ed39a490b93ce654e4f5d4

Better unread counts for MUC

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.

snikket/Chat.hx +18 -6

diff --git a/snikket/Chat.hx b/snikket/Chat.hx
index c40e39f..2763557 100644
--- a/snikket/Chat.hx
+++ b/snikket/Chat.hx
@@ -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 {
@@ -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]);
 				}