git » sdk » commit b1f95ea

Persist display name and reconnect MUCs if needed

author Stephen Paul Weber
2023-11-20 18:10:44 UTC
committer Stephen Paul Weber
2023-11-20 18:51:46 UTC
parent 194662faa7b61244888b90d4e669265959d917a2

Persist display name and reconnect MUCs if needed

xmpp/Chat.hx +1 -2
xmpp/Client.hx +11 -6
xmpp/Persistence.hx +2 -2
xmpp/persistence/browser.js +6 -7

diff --git a/xmpp/Chat.hx b/xmpp/Chat.hx
index 7e8f05e..1ab0088 100644
--- a/xmpp/Chat.hx
+++ b/xmpp/Chat.hx
@@ -456,8 +456,7 @@ class Channel extends Chat {
 	}
 
 	private function getFullJid() {
-		final jid = JID.parse(chatId);
-		return new JID(jid.node, jid.domain, client.displayName());
+		return JID.parse(chatId).withResource(client.displayName());
 	}
 
 	public function getParticipants() {
diff --git a/xmpp/Client.hx b/xmpp/Client.hx
index 4b93f81..467e84a 100644
--- a/xmpp/Client.hx
+++ b/xmpp/Client.hx
@@ -365,9 +365,13 @@ class Client extends xmpp.EventEmitter {
 	}
 
 	public function setDisplayName(fn: String) {
-		// TODO: persist
-		// TODO: do self ping on all channels to maybe change nick
+		if (fn == null || fn == "" || fn == displayName()) return;
 		_displayName = fn;
+		persistence.storeLogin(jid.asBare().asString(), jid.resource, fn, null);
+		for (chat in getChats()) {
+			if (Std.isOfType(chat, Channel)) Std.downcast(chat, Channel)?.selfPing(false);
+		}
+		// TODO: should this path publish to server too? But we use it for notifications from server right now...
 	}
 
 	public function start() {
@@ -387,12 +391,13 @@ class Client extends xmpp.EventEmitter {
 				this.trigger("chats/update", chats);
 
 				persistence.getStreamManagement(accountId(), (smId, smOut, smIn, smOutQ) -> {
-					persistence.getLogin(accountId(), (login) -> {
-						if (login.clientId != null) jid = jid.withResource(login.clientId);
-						if (login.token == null) {
+					persistence.getLogin(accountId(), (clientId, token, displayName) -> {
+						setDisplayName(displayName);
+						if (clientId != null) jid = jid.withResource(clientId);
+						if (token == null) {
 							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.on("auth/password-needed", (data)->this.stream.trigger("auth/password", { password: token }));
 						}
 						stream.connect(jid.asString(), smId == null || smId == "" ? null : { id: smId, outbound: smOut, inbound: smIn, outbound_q: smOutQ });
 					});
diff --git a/xmpp/Persistence.hx b/xmpp/Persistence.hx
index 50c5fb8..5115c19 100644
--- a/xmpp/Persistence.hx
+++ b/xmpp/Persistence.hx
@@ -17,8 +17,8 @@ abstract class Persistence {
 	abstract public function storeMedia(mime:String, bytes:BytesData, callback: ()->Void):Void;
 	abstract public function storeCaps(caps:Caps):Void;
 	abstract public function getCaps(ver:String, callback: (Caps)->Void):Void;
-	abstract public function storeLogin(login:String, clientId:String, token:Null<String>):Void;
-	abstract public function getLogin(login:String, callback:({ ?clientId: String, ?token: String })->Void):Void;
+	abstract public function storeLogin(login:String, clientId:String, displayName:String, token:Null<String>):Void;
+	abstract public function getLogin(login:String, callback:(clientId:String, token:Null<String>, displayName:String)->Void):Void;
 	abstract public function storeStreamManagement(accountId:String, smId:String, outboundCount:Int, inboundCount:Int, outboundQueue:Array<String>):Void;
 	abstract public function getStreamManagement(accountId:String, callback: (smId:String, outboundCount:Int, inboundCount:Int, outboundQueue:Array<String>)->Void):Void;
 }
diff --git a/xmpp/persistence/browser.js b/xmpp/persistence/browser.js
index 08fed3a..7e24598 100644
--- a/xmpp/persistence/browser.js
+++ b/xmpp/persistence/browser.js
@@ -356,10 +356,11 @@ exports.xmpp.persistence = {
 				}
 			},
 
-			storeLogin: function(login, clientId, token) {
+			storeLogin: function(login, clientId, displayName, token) {
 				const tx = db.transaction(["keyvaluepairs"], "readwrite");
 				const store = tx.objectStore("keyvaluepairs");
 				store.put(clientId, "login:clientId:" + login).onerror = console.error;
+				store.put(displayName, "fn:" + login).onerror = console.error;
 				if (token != null) store.put(token, "login:token:" + login).onerror = console.error;
 			},
 
@@ -388,14 +389,12 @@ exports.xmpp.persistence = {
 				const store = tx.objectStore("keyvaluepairs");
 				Promise.all([
 					promisifyRequest(store.get("login:clientId:" + login)),
-					promisifyRequest(store.get("login:token:" + login))
+					promisifyRequest(store.get("login:token:" + login)),
+					promisifyRequest(store.get("fn:" + login)),
 				]).then((result) => {
-					callback({
-						clientId: result[0],
-						token: result[1]
-					});
+					callback(result[0], result[1], result[2]);
 				}).catch((e) => {
-					callback({});
+					callback(null, null, null);
 				});
 			}
 		}