git » sdk » commit fcde20e

Set presence immediately without waiting for caps

author Eric Roberts
2026-10-09 16:00:38 UTC
committer Eric Roberts
2026-10-13 14:32:03 UTC
parent 494510b428505e00ff4e014668b78ee8e82ac499

Set presence immediately without waiting for caps

We don't need the caps to set presence and this led to a weird scenario
where I was waiting on caps for someone else before I myself could join
the channel. We don't need any info from the caps to set presence so
let's do it immediately and then update the chat when we get caps later.

borogove/Client.hx +9 -13
test/TestClient.hx +15 -0

diff --git a/borogove/Client.hx b/borogove/Client.hx
index 8197138..4f980b9 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -120,7 +120,7 @@ class Client extends EventEmitter {
 	@:allow(borogove)
 	private var sortId: String = "a ";
 	private var rosterVer: Null<String> = null;
-	private final pendingCaps: Map<String, Array<(Null<Caps>)->Chat>> = [];
+	private final pendingCaps: Map<String, Array<Chat>> = [];
 	private final brokenAvatars: Map<String, JID> = [];
 #if !NO_OMEMO
 	@:allow(borogove)
@@ -372,11 +372,10 @@ class Client extends EventEmitter {
 					return EventUnhandled;
 				}
 
-				if (presence.ver == null) {
-					chat.setPresence(JID.parse(stanza.attr.get("from")).resource, stanza, false);
-				} else {
+				chat.setPresence(from.resource, stanza, false);
+				if (presence.ver != null) {
 					final handleCaps = (caps) -> {
-						chat.setPresence(JID.parse(stanza.attr.get("from")).resource, stanza, false);
+						this.trigger("chats/update", [chat]);
 						return chat;
 					};
 
@@ -385,25 +384,22 @@ class Client extends EventEmitter {
 							final ver = presence.ver;
 							final pending = pendingCaps.get(ver);
 							if (pending == null) {
-								pendingCaps.set(ver, [handleCaps]);
+								pendingCaps.set(ver, [chat]);
 								final discoGet = new DiscoInfoGet(stanza.attr.get("from"), presence.capsNode + "#" + ver);
 								discoGet.onFinished(() -> {
-									final chatsToUpdate: Map<String, Chat> = [];
-									final handlers = pendingCaps.get(ver) ?? [];
+									final chatsToUpdate = pendingCaps.get(ver) ?? [];
 									pendingCaps.remove(ver);
 									if (discoGet.getResult() != null) {
 										final cachedCaps = capsRepo.add(discoGet.getResult());
-										for (handler in handlers) {
-											final c = handler(cachedCaps);
-										}
+										this.trigger("chats/update", chatsToUpdate);
 									}
 								});
 								sendQuery(discoGet);
 							} else {
-								pending.push(handleCaps);
+								pending.push(chat);
 							}
 						} else {
-							handleCaps(caps);
+							this.trigger("chats/update", [chat]);
 						}
 					});
 				}
diff --git a/test/TestClient.hx b/test/TestClient.hx
index 457711d..31cd832 100644
--- a/test/TestClient.hx
+++ b/test/TestClient.hx
@@ -29,6 +29,21 @@ using Lambda;
 
 @:access(borogove)
 class TestClient extends utest.Test {
+	public function testPresenceAppliedBeforeCapsDiscovery() {
+		final client = new Client("test@example.com", new Dummy());
+		final channel = new borogove.Chat.Channel(client, client.stream, client.persistence, "room@example.com");
+		client.chats.push(channel);
+		final caps = new borogove.Caps("test-client", [], ["urn:xmpp:jingle:apps:rtp:audio"], []);
+		final node = caps.node + "#" + caps.ver();
+		final presence = new Stanza("presence", { xmlns: "jabber:client", from: "room@example.com/me" })
+			.tag("x", { xmlns: "http://jabber.org/protocol/muc#user" }).tag("status", { code: "110" }).up().up()
+			.tag("c", { xmlns: "http://jabber.org/protocol/caps", hash: "sha-1", node: caps.node, ver: caps.ver() }).up();
+
+		Assert.isFalse(channel.isJoined);
+		client.stream.onStanza(presence);
+		Assert.isTrue(channel.isJoined);
+	}
+
 	public function testSetStatus(async: Async) {
 		final persistence = new Dummy();
 		final client = new Client("test@example.com", persistence);