git » sdk » commit 08eb471

Set last message, then notify handlers, then update unreads, then notify chat updated

author Stephen Paul Weber
2026-05-03 00:25:45 UTC
committer Stephen Paul Weber
2026-05-03 00:25:45 UTC
parent f48ae29b3c89939c442ab2e2b7743f05917baf29

Set last message, then notify handlers, then update unreads, then notify chat updated

borogove/Client.hx +9 -4

diff --git a/borogove/Client.hx b/borogove/Client.hx
index 6cb3a03..c000d72 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -489,11 +489,16 @@ class Client extends EventEmitter {
 				}
 				final chat = getChat(chatMessage.chatId());
 				if (chat != null) {
-					final updateChat = (chatMessage) -> {
-						notifyMessageHandlers(chatMessage, chatMessage.versions.length > 1 ? CorrectionEvent : DeliveryEvent);
-						if (chatMessage.versions.length < 1 || chat.lastMessageId() == chatMessage.serverId || chat.lastMessageId() == chatMessage.localId) {
+					final updateChat = (chatMessage: ChatMessage) -> {
+						final eventType = chatMessage.versions.length > 1 ? CorrectionEvent : DeliveryEvent;
+						if (chat.lastMessage == null || eventType == DeliveryEvent || chatMessage.canReplace(chat.lastMessage)) {
 							chat.setLastMessage(chatMessage);
-							if (chatMessage.versions.length < 1) chat.setUnreadCount(chatMessage.isIncoming() ? chat.unreadCount() + 1 : 0);
+						}
+
+						notifyMessageHandlers(chatMessage, eventType);
+
+						if (eventType == DeliveryEvent) {
+							chat.setUnreadCount(chatMessage.isIncoming() ? chat.unreadCount() + 1 : 0);
 							chatActivity(chat);
 						} else if (newChat != null) {
 							this.trigger("chats/update", [newChat]);