git » sdk » commit 7273f76

Update chat if last message changed

author Stephen Paul Weber
2026-08-31 20:42:16 UTC
committer Stephen Paul Weber
2026-09-01 15:04:15 UTC
parent 6e90bd75a143cc3c51d6a9881f59a3463026820e

Update chat if last message changed

borogove/Client.hx +5 -3
test/TestClient.hx +23 -0

diff --git a/borogove/Client.hx b/borogove/Client.hx
index 43c584e..9beb5bf 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -511,7 +511,8 @@ class Client extends EventEmitter {
 				if (chat != null) {
 					final updateChat = (chatMessage: ChatMessage) -> {
 						final eventType = chatMessage.versions.length > 1 ? CorrectionEvent : DeliveryEvent;
-						((chat.lastMessage == null || eventType == DeliveryEvent || chatMessage.canReplace(chat.lastMessage)) ?
+						final canReplaceLast = chat.lastMessage == null || chatMessage.canReplace(chat.lastMessage);
+						((eventType == DeliveryEvent || canReplaceLast) ?
 							chat.setLastMessage(chatMessage) :
 							Promise.resolve(null)
 						).then(_ -> {
@@ -520,8 +521,9 @@ class Client extends EventEmitter {
 							if (eventType == DeliveryEvent) {
 								chat.setUnreadCount(chatMessage.isIncoming() ? chat.unreadCount() + 1 : 0);
 								chatActivity(chat);
-							} else if (newChat != null) {
-								this.trigger("chats/update", [newChat]);
+							} else if (canReplaceLast || newChat != null) {
+								// chat and newChat are the same when both present
+								this.trigger("chats/update", [chat]);
 							}
 						});
 					};
diff --git a/test/TestClient.hx b/test/TestClient.hx
index 882d50a..a1e323c 100644
--- a/test/TestClient.hx
+++ b/test/TestClient.hx
@@ -638,6 +638,29 @@ class TestClient extends utest.Test {
 		client.getDirectChat("friend@example.com");
 	}
 
+	public function testChatsUpdateOnCorrection(async: Async) {
+		final persistence = new Dummy();
+		final client = new Client("test@example.com", persistence);
+		client.getDirectChat("friend@example.com");
+
+		final originalStanza = new Stanza("message", { xmlns: "jabber:client", from: "friend@example.com", id: "msg1" }).textTag("body", "hello");
+		client.stream.onStanza(originalStanza);
+
+		haxe.Timer.delay(() -> {
+			client.addChatsUpdatedListener(chats -> {
+				final c = chats.find(x -> x.chatId == "friend@example.com");
+				Assert.notNull(c?.lastMessage);
+				Assert.equals("corrected", c.lastMessage.text);
+				async.done();
+			});
+
+			final correctionStanza = new Stanza("message", { xmlns: "jabber:client", from: "friend@example.com", id: "msg2" })
+					.textTag("body", "corrected")
+					.tag("replace", { id: "msg1", xmlns: "urn:xmpp:message-correct:0" }).up();
+			client.stream.onStanza(correctionStanza);
+		}, 1);
+	}
+
 	public function testPresenceSubscription(async: Async) {
 		final persistence = new Dummy();
 		final client = new Client("test@example.com", persistence);