git » sdk » commit 8f4f9b2

Don't sync while syncing

author Stephen Paul Weber
2026-08-24 18:20:59 UTC
committer Stephen Paul Weber
2026-08-24 18:20:59 UTC
parent a1744e2f5c6c364802368bcd4cb8d16a5e015e85

Don't sync while syncing

Otherwise our generation of sortIds may be totally off.

borogove/Chat.hx +14 -0
test/TestChat.hx +5 -0

diff --git a/borogove/Chat.hx b/borogove/Chat.hx
index 53dbff9..8357c7a 100644
--- a/borogove/Chat.hx
+++ b/borogove/Chat.hx
@@ -217,6 +217,12 @@ abstract class Chat extends EventEmitter {
 	abstract public function getMessagesAround(around: ChatMessage):Promise<Array<ChatMessage>>;
 
 	private function fetchFromSync(sync: MessageSync): Promise<Array<ChatMessage>> {
+		// If we are already syncing, don't do this
+		// it would cause ordering overlap issues
+		if (inMessageSync()) {
+			return Promise.resolve([]);
+		}
+
 		return new thenshim.Promise((resolve, reject) -> {
 			sync.onMessages((messageList) -> {
 				final chatMessages = [];
@@ -760,6 +766,10 @@ abstract class Chat extends EventEmitter {
 		return !client.inSync;
 	}
 
+	private function inMessageSync() {
+		return !client.inSync;
+	}
+
 	/**
 		Can audio calls be started in this Chat?
 	**/
@@ -2021,6 +2031,10 @@ class Channel extends Chat {
 		return sync != null || !livePresence();
 	}
 
+	override private function inMessageSync() {
+		return sync != null;
+	}
+
 	override private function setLastMessage(message:Null<ChatMessage>) {
 		return super.setLastMessage(message).then(_ -> {
 			if (message != null && message.type == MessageChannel && (sortId == null || sortId < message.sortId)) sortId = message.sortId;
diff --git a/test/TestChat.hx b/test/TestChat.hx
index f202710..e7758a0 100644
--- a/test/TestChat.hx
+++ b/test/TestChat.hx
@@ -58,6 +58,7 @@ class TestChat extends utest.Test {
 	public function testGetMessagesBeforeNull(async: Async) {
 		final persistence = new Dummy();
 		final client = new Client("test@example.com", persistence);
+		client.inSync = true;
 		final chat = client.getDirectChat("friend@example.com");
 
 		client.stream.on("sendStanza", (stanza: Stanza) -> {
@@ -80,6 +81,7 @@ class TestChat extends utest.Test {
 	public function testGetMessagesBefore(async: Async) {
 		final persistence = new Dummy();
 		final client = new Client("test@example.com", persistence);
+		client.inSync = true;
 		final chat = client.getDirectChat("friend@example.com");
 		final builder = new ChatMessageBuilder();
 		builder.serverId = "msg123";
@@ -110,6 +112,7 @@ class TestChat extends utest.Test {
 	public function testGetMessagesAfterNull(async: Async) {
 		final persistence = new Dummy();
 		final client = new Client("test@example.com", persistence);
+		client.inSync = true;
 		final chat = client.getDirectChat("friend@example.com");
 
 		client.stream.on("sendStanza", (stanza: Stanza) -> {
@@ -129,6 +132,7 @@ class TestChat extends utest.Test {
 	public function testGetMessagesAfter(async: Async) {
 		final persistence = new Dummy();
 		final client = new Client("test@example.com", persistence);
+		client.inSync = true;
 		final chat = client.getDirectChat("friend@example.com");
 		final builder = new ChatMessageBuilder();
 		builder.serverId = "msg456";
@@ -159,6 +163,7 @@ class TestChat extends utest.Test {
 	public function testGetMessagesBeforeNullChannel(async: Async) {
 		final persistence = new Dummy();
 		final client = new Client("test@example.com", persistence);
+		client.inSync = true;
 		final chat = new borogove.Chat.Channel(client, client.stream, persistence, "channel@example.com");
 
 		client.stream.on("sendStanza", (stanza: Stanza) -> {