git » sdk » commit 194662f

Split uses of client.jid from client.accountId()

author Stephen Paul Weber
2023-11-20 18:01:14 UTC
committer Stephen Paul Weber
2023-11-20 18:10:33 UTC
parent bcc6c0a36a647d2aa929f6e708208bd376de6e0d

Split uses of client.jid from client.accountId()

xmpp/Chat.hx +2 -2
xmpp/ChatMessage.hx +2 -3
xmpp/Client.hx +17 -18
xmpp/JID.hx +4 -0
xmpp/Push.hx +1 -1

diff --git a/xmpp/Chat.hx b/xmpp/Chat.hx
index c70009f..7e8f05e 100644
--- a/xmpp/Chat.hx
+++ b/xmpp/Chat.hx
@@ -280,7 +280,7 @@ class DirectChat extends Chat {
 		client.chatActivity(this);
 		message.timestamp = message.timestamp ?? Date.format(std.Date.now());
 		message.direction = MessageSent;
-		message.from = JID.parse(client.jid);
+		message.from = client.jid;
 		message.sender = message.from.asBare();
 		message.replyTo = [message.sender];
 		message.recipients = getParticipants().map((p) -> JID.parse(p));
@@ -512,7 +512,7 @@ class Channel extends Chat {
 		client.chatActivity(this);
 		message.timestamp = message.timestamp ?? Date.format(std.Date.now());
 		message.direction = MessageSent;
-		message.from = JID.parse(client.jid);
+		message.from = client.jid;
 		message.sender = getFullJid();
 		message.replyTo = [message.sender];
 		message.to = JID.parse(chatId);
diff --git a/xmpp/ChatMessage.hx b/xmpp/ChatMessage.hx
index 3405aaa..eb7b777 100644
--- a/xmpp/ChatMessage.hx
+++ b/xmpp/ChatMessage.hx
@@ -60,7 +60,7 @@ class ChatMessage {
 
 	public function new() { }
 
-	public static function fromStanza(stanza:Stanza, localJidStr:String):Null<ChatMessage> {
+	public static function fromStanza(stanza:Stanza, localJid:JID):Null<ChatMessage> {
 		if (stanza.attr.get("type") == "error") return null;
 
 		var msg = new ChatMessage();
@@ -74,7 +74,6 @@ class ChatMessage {
 		final from = stanza.attr.get("from");
 		msg.from = from == null ? null : JID.parse(from);
 		msg.sender = stanza.attr.get("type") == "groupchat" ? msg.from : msg.from?.asBare();
-		final localJid = JID.parse(localJidStr);
 		final localJidBare = localJid.asBare();
 		final domain = localJid.domain;
 		final to = stanza.attr.get("to");
@@ -85,7 +84,7 @@ class ChatMessage {
 			if (carbon == null) carbon = stanza.getChild("sent", "urn:xmpp:carbons:2");
 			if (carbon != null) {
 				var fwd = carbon.getChild("forwarded", "urn:xmpp:forward:0");
-				if(fwd != null) return fromStanza(fwd.getFirstChild(), localJidStr);
+				if(fwd != null) return fromStanza(fwd.getFirstChild(), localJid);
 			}
 		}
 
diff --git a/xmpp/Client.hx b/xmpp/Client.hx
index 4ef69ea..4b93f81 100644
--- a/xmpp/Client.hx
+++ b/xmpp/Client.hx
@@ -26,7 +26,7 @@ using Lambda;
 class Client extends xmpp.EventEmitter {
 	private var stream:GenericStream;
 	private var chatMessageHandlers: Array<(ChatMessage)->Void> = [];
-	public var jid(default,null):String;
+	public var jid(default,null):JID;
 	private var chats: Array<Chat> = [];
 	private var persistence: Persistence;
 	private final caps = new Caps(
@@ -50,8 +50,8 @@ class Client extends xmpp.EventEmitter {
 
 	public function new(jid: String, persistence: Persistence) {
 		super();
-		this.jid = jid;
-		this._displayName = JID.parse(jid).node;
+		this.jid = JID.parse(jid);
+		this._displayName = this.jid.node;
 		this.persistence = persistence;
 		stream = new Stream();
 		stream.on("status/online", this.onConnected);
@@ -136,7 +136,7 @@ class Client extends xmpp.EventEmitter {
 				}
 			}
 
-			var chatMessage = ChatMessage.fromStanza(stanza, jid);
+			var chatMessage = ChatMessage.fromStanza(stanza, this.jid);
 			if (chatMessage != null) {
 				var chat = getChat(chatMessage.chatId());
 				if (chat == null && stanza.attr.get("type") != "groupchat") chat = getDirectChat(chatMessage.chatId());
@@ -357,7 +357,7 @@ class Client extends xmpp.EventEmitter {
 	}
 
 	public function accountId() {
-		return JID.parse(jid).asBare().asString();
+		return jid.asBare().asString();
 	}
 
 	public function displayName() {
@@ -371,7 +371,7 @@ class Client extends xmpp.EventEmitter {
 	}
 
 	public function start() {
-		persistence.getChats(jid, (protoChats) -> {
+		persistence.getChats(accountId(), (protoChats) -> {
 			for (protoChat in protoChats) {
 				chats.push(protoChat.toChat(this, stream, persistence));
 			}
@@ -387,15 +387,14 @@ class Client extends xmpp.EventEmitter {
 				this.trigger("chats/update", chats);
 
 				persistence.getStreamManagement(accountId(), (smId, smOut, smIn, smOutQ) -> {
-					persistence.getLogin(jid, (login) -> {
-						var ajid = jid;
-						if (login.clientId != null) ajid = JID.parse(jid).asBare().asString() + "/" + login.clientId;
+					persistence.getLogin(accountId(), (login) -> {
+						if (login.clientId != null) jid = jid.withResource(login.clientId);
 						if (login.token == null) {
-							stream.on("auth/password-needed", (data)->this.trigger("auth/password-needed", { jid: this.jid }));
+							stream.on("auth/password-needed", (data)->this.trigger("auth/password-needed", { accountId: accountId() }));
 						} else {
 							stream.on("auth/password-needed", (data)->this.stream.trigger("auth/password", { password: login.token }));
 						}
-						stream.connect(ajid, smId == null || smId == "" ? null : { id: smId, outbound: smOut, inbound: smIn, outbound_q: smOutQ });
+						stream.connect(jid.asString(), smId == null || smId == "" ? null : { id: smId, outbound: smOut, inbound: smIn, outbound_q: smOutQ });
 					});
 				});
 			});
@@ -408,8 +407,8 @@ class Client extends xmpp.EventEmitter {
 
 	private function onConnected(data) { // Fired on connect or reconnect
 		if (data != null && data.jid != null) {
-			final jidp = JID.parse(data.jid);
-			if (!jidp.isBare()) persistence.storeLogin(jidp.asBare().asString(), jidp.resource, null);
+			jid = JID.parse(data.jid);
+			if (!jid.isBare()) persistence.storeLogin(jid.asBare().asString(), jid.resource, displayName(), null);
 		}
 
 		if (data.resumed) return EventHandled;
@@ -490,7 +489,7 @@ class Client extends xmpp.EventEmitter {
 			}
 		}
 		final chat = new DirectChat(this, this.stream, this.persistence, chatId);
-		persistence.storeChat(jid, chat);
+		persistence.storeChat(accountId(), chat);
 		chats.unshift(chat);
 		if (triggerIfNew) this.trigger("chats/update", [chat]);
 		return chat;
@@ -610,7 +609,7 @@ class Client extends xmpp.EventEmitter {
 	public function enablePush(push_service: String, vapid_private_key: js.html.CryptoKey, endpoint: String, p256dh: BytesData, auth: BytesData) {
 		js.Browser.window.crypto.subtle.exportKey("pkcs8", vapid_private_key).then((vapid_private_pkcs8) -> {
 			sendQuery(new Push2Enable(
-				jid,
+				jid.asBare().asString(),
 				push_service,
 				endpoint,
 				Bytes.ofData(p256dh),
@@ -624,7 +623,7 @@ class Client extends xmpp.EventEmitter {
 	#end
 
 	public function getIceServers(callback: (Array<IceServer>)->Void) {
-		final extDiscoGet = new ExtDiscoGet(JID.parse(this.jid).domain);
+		final extDiscoGet = new ExtDiscoGet(jid.domain);
 		extDiscoGet.onFinished(() -> {
 			final servers = [];
 			for (service in extDiscoGet.getResult()) {
@@ -652,7 +651,7 @@ class Client extends xmpp.EventEmitter {
 				var chat = getDirectChat(item.jid, false);
 				chat.setTrusted(item.subscription == "both" || item.subscription == "from");
 				if (item.fn != null && item.fn != "") chat.setDisplayName(item.fn);
-				persistence.storeChat(jid, chat);
+				persistence.storeChat(accountId(), chat);
 			}
 			this.trigger("chats/update", chats);
 		});
@@ -738,7 +737,7 @@ class Client extends xmpp.EventEmitter {
 		sync.setNewestPageFirst(false);
 		sync.onMessages((messageList) -> {
 			for (message in messageList.messages) {
-				persistence.storeMessage(jid, message);
+				persistence.storeMessage(accountId(), message);
 			}
 			if (sync.hasMore()) {
 				sync.fetchNext();
diff --git a/xmpp/JID.hx b/xmpp/JID.hx
index 511ed46..85d3146 100644
--- a/xmpp/JID.hx
+++ b/xmpp/JID.hx
@@ -55,6 +55,10 @@ class JID {
 		return new JID(this.node, this.domain);
 	}
 
+	public function withResource(resource: String): JID {
+		return new JID(this.node, this.domain, resource);
+	}
+
 	public function isValid():Bool {
 		return domain.indexOf(".") >= 0;
 	}
diff --git a/xmpp/Push.hx b/xmpp/Push.hx
index 4d002a7..7511e36 100644
--- a/xmpp/Push.hx
+++ b/xmpp/Push.hx
@@ -20,7 +20,7 @@ function receive(data: String, persistence: Persistence) {
 	}
 	if (stanza.attr.get("to") == null) return null;
 	// Assume incoming message
-	final message = ChatMessage.fromStanza(stanza, JID.parse(stanza.attr.get("to")).asBare().asString());
+	final message = ChatMessage.fromStanza(stanza, JID.parse(stanza.attr.get("to")).asBare());
 	if (message != null) {
 		return Notification.fromChatMessage(message);
 	} else {