git » sdk » commit 1067389

Wait until caps are loaded to set list of chats

author Stephen Paul Weber
2026-06-29 15:03:57 UTC
committer Stephen Paul Weber
2026-06-29 15:03:57 UTC
parent 63b19df040ae91503143f1a0a2de936dfbb4a4f6

Wait until caps are loaded to set list of chats

borogove/Chat.hx +4 -3
borogove/Client.hx +8 -4

diff --git a/borogove/Chat.hx b/borogove/Chat.hx
index dc86495..9e88d01 100644
--- a/borogove/Chat.hx
+++ b/borogove/Chat.hx
@@ -2570,14 +2570,15 @@ class SerializedChat {
 	/**
 		Recreate a live Chat object from this serialized representation.
 	**/
-	public function toChat(client: Client, stream: GenericStream, persistence: Persistence) {
+	public function toChat(client: Client, stream: GenericStream, persistence: Persistence): Promise<Chat> {
 		final extensionsStanza = Stanza.parse(extensions);
 		var filterN = notificationsFiltered ?? false;
 		var mention = notifyMention;
 
+		final getCapsPromises = [];
 		// Init capsRepo with all caps for this chat
 		for (resource => p in presence) {
-			if (p != null) client.capsRepo.getAsync(p);
+			if (p != null) getCapsPromises.push(client.capsRepo.getAsync(p));
 		}
 
 		final chat = if (klass == "DirectChat") {
@@ -2605,6 +2606,6 @@ class SerializedChat {
 		for (threadId => subject in threads) {
 			chat.setThreadSubject(threadId, subject);
 		}
-		return chat;
+		return thenshim.PromiseTools.all(getCapsPromises).then(_ -> chat);
 	}
 }
diff --git a/borogove/Client.hx b/borogove/Client.hx
index cda2e9f..d4f9b3a 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -800,12 +800,16 @@ class Client extends EventEmitter {
 		}).then((protoChats) -> {
 			protoChats.sort((a, b) -> a.chatId == accountId() ? 1 : 0);
 			var oneProtoChat = null;
+			final chatPromises = [];
 			while ((oneProtoChat = protoChats.pop()) != null) {
-				chats.push(oneProtoChat.toChat(this, stream, persistence));
+				chatPromises.push(oneProtoChat.toChat(this, stream, persistence));
 			}
-			getDirectChat(accountId()); // Ensure self chat exists
-			getDirectChat(JID.parse(accountId()).domain); // Ensure server chat exists
-			return persistence.getChatsUnreadDetails(accountId(), chats);
+			return thenshim.PromiseTools.all(chatPromises).then(theChats -> {
+				chats = theChats;
+				getDirectChat(accountId()); // Ensure self chat exists
+				getDirectChat(JID.parse(accountId()).domain); // Ensure server chat exists
+				return persistence.getChatsUnreadDetails(accountId(), chats);
+			});
 		}).then((details) -> {
 			for (detail in details) {
 				var chat = getChat(detail.chatId);