git » sdk » commit 6f5cd19

Only do pings/joins once we're online

author Stephen Paul Weber
2023-11-30 04:28:14 UTC
committer Stephen Paul Weber
2023-12-11 15:59:52 UTC
parent 7f3238b411995abb070d4ffedb89076ff351b320

Only do pings/joins once we're online

xmpp/Chat.hx +2 -2
xmpp/Client.hx +10 -3

diff --git a/xmpp/Chat.hx b/xmpp/Chat.hx
index a7eb2df..ffadede 100644
--- a/xmpp/Chat.hx
+++ b/xmpp/Chat.hx
@@ -353,7 +353,6 @@ class Channel extends Chat {
 	public function new(client:Client, stream:GenericStream, persistence:Persistence, chatId:String, uiState = Open, extensions = null, ?disco: Caps) {
 		super(client, stream, persistence, chatId, uiState, extensions);
 		if (disco != null) this.disco = disco;
-		selfPing(disco == null);
 	}
 
 	public function selfPing(shouldRefreshDisco = true) {
@@ -380,8 +379,9 @@ class Channel extends Chat {
 					(shouldRefreshDisco ? refreshDisco : (cb)->cb())(() -> {
 						presence = {}; // About to ask for a fresh set
 						inSync = false;
+						final desiredFullJid = JID.parse(chatId).withResource(client.displayName());
 						client.sendPresence(
-							getFullJid().asString(),
+							desiredFullJid.asString(),
 							(stanza) -> {
 								stanza.tag("x", { xmlns: "http://jabber.org/protocol/muc" });
 								if (disco.features.contains("urn:xmpp:mam:2")) stanza.tag("history", { maxchars: "0" }).up();
diff --git a/xmpp/Client.hx b/xmpp/Client.hx
index a46ab8b..4ed1fdd 100644
--- a/xmpp/Client.hx
+++ b/xmpp/Client.hx
@@ -381,9 +381,7 @@ class Client extends xmpp.EventEmitter {
 		if (fn == null || fn == "" || fn == displayName()) return false;
 		_displayName = fn;
 		persistence.storeLogin(jid.asBare().asString(), stream.clientId ?? jid.resource, fn, null);
-		for (chat in getChats()) {
-			if (Std.isOfType(chat, Channel)) Std.downcast(chat, Channel)?.selfPing(false);
-		}
+		pingAllChannels();
 		// TODO: should this path publish to server too? But we use it for notifications from server right now...
 		return true;
 	}
@@ -466,6 +464,7 @@ class Client extends xmpp.EventEmitter {
 					this.trigger("chats/update", chats);
 					// Set self to online
 					sendPresence();
+					pingAllChannels();
 					this.trigger("status/online", {});
 				});
 			});
@@ -541,6 +540,7 @@ class Client extends xmpp.EventEmitter {
 		final chat = if (caps.isChannel(chatId)) {
 			final channel = new Channel(this, this.stream, this.persistence, chatId, Open, null, caps);
 			chats.unshift(channel);
+			channel.selfPing(false);
 			channel;
 		} else {
 			getDirectChat(chatId, false);
@@ -846,4 +846,11 @@ class Client extends xmpp.EventEmitter {
 		});
 		sync.fetchNext();
 	}
+
+	private function pingAllChannels() {
+		for (chat in getChats()) {
+			final channel = Std.downcast(chat, Channel);
+			channel?.selfPing(channel?.disco == null);
+		}
+	}
 }