git » sdk » commit 1ee6926

Search from end

author Stephen Paul Weber
2025-11-12 15:53:23 UTC
committer Stephen Paul Weber
2025-11-12 15:53:23 UTC
parent 97b0b197f98d166045692f3495b1640573736e8b

Search from end

Find the most *recent* message that was outgoing, otherwise the unread
count will be messed up.

borogove/Chat.hx +11 -9
borogove/Util.hx +14 -0

diff --git a/borogove/Chat.hx b/borogove/Chat.hx
index 3ca99e5..573e4e3 100644
--- a/borogove/Chat.hx
+++ b/borogove/Chat.hx
@@ -1248,14 +1248,6 @@ class Channel extends Chat {
 					inSync = true;
 					sync = null;
 
-					// Sort by time so that eg edits go into the past
-					chatMessages.sort((x, y) -> Reflect.compare(x.timestamp, y.timestamp));
-
-					final lastFromSync = chatMessages[chatMessages.length - 1];
-					if (lastFromSync != null && (lastMessage?.timestamp == null || Reflect.compare(lastFromSync.timestamp, lastMessage?.timestamp) > 0)) {
-						setLastMessage(lastFromSync);
-						client.sortChats();
-					}
 					final serverIds: Map<String, Bool> = [];
 					final dedupedMessages = [];
 					chatMessages.reverse();
@@ -1265,7 +1257,17 @@ class Channel extends Chat {
 							serverIds[m.serverId] = true;
 						}
 					}
-					final readIndex = dedupedMessages.findIndex((m) -> m.serverId == readUpTo() || !m.isIncoming());
+
+					// Sort by time so that eg edits go into the past
+					dedupedMessages.sort((x, y) -> Reflect.compare(x.timestamp, y.timestamp));
+
+					final lastFromSync = dedupedMessages[dedupedMessages.length - 1];
+					if (lastFromSync != null && (lastMessage?.timestamp == null || Reflect.compare(lastFromSync.timestamp, lastMessage?.timestamp) > 0)) {
+						setLastMessage(lastFromSync);
+						client.sortChats();
+					}
+
+					final readIndex = dedupedMessages.findLastIndex((m) -> m.serverId == readUpTo() || !m.isIncoming());
 					if (readIndex < 0) {
 						setUnreadCount(unreadCount() + dedupedMessages.length);
 					} else {
diff --git a/borogove/Util.hx b/borogove/Util.hx
index f33949d..e276113 100644
--- a/borogove/Util.hx
+++ b/borogove/Util.hx
@@ -149,6 +149,20 @@ class Util {
 		return arr[i];
 	}
 
+	#if js inline #end static public function findLastIndex<T>(it:Array<T>, f:(item:T) -> Bool):Int {
+	#if js
+		return untyped it.findLastIndex(f);
+	#else
+		var i = it.length - 1;
+		while (i > -1) {
+			if (f(it[i]))
+				return i;
+			i--;
+		}
+		return -1;
+	#end
+	}
+
 	inline static public function writeS(o: haxe.io.Output, s: String) {
 		final b = bytesOfString(s);
 		o.writeBytes(b, 0, b.length);