git » sdk » commit a0c7909

Normalize on chat and chatId not conversation

author Stephen Paul Weber
2023-10-11 15:16:05 UTC
committer Stephen Paul Weber
2023-10-11 15:16:05 UTC
parent 37422199e72954d687a35f646d0460a67b7d07d7

Normalize on chat and chatId not conversation

xmpp/ChatMessage.hx +3 -3
xmpp/Client.hx +1 -1
xmpp/Notification.hx +1 -1
xmpp/persistence/browser.js +8 -8

diff --git a/xmpp/ChatMessage.hx b/xmpp/ChatMessage.hx
index ea24a27..511c521 100644
--- a/xmpp/ChatMessage.hx
+++ b/xmpp/ChatMessage.hx
@@ -89,12 +89,12 @@ class ChatMessage {
 		return this.timestamp = timestamp;
 	}
 
-	public function conversation():String {
-		return isIncoming() ? JID.parse(from).asBare().asString() : JID.parse(to).asBare().asString();
+	public function chatId():String {
+		return (isIncoming() ? from?.asBare()?.asString() : to?.asBare()?.asString()) ?? throw "from or to is null";
 	}
 
 	public function account():String {
-		return !isIncoming() ? JID.parse(from).asBare().asString() : JID.parse(to).asBare().asString();
+		return (!isIncoming() ? from?.asBare()?.asString() : to?.asBare()?.asString()) ?? throw "from or to is null";
 	}
 
 	public function isIncoming():Bool {
diff --git a/xmpp/Client.hx b/xmpp/Client.hx
index e4034ef..7a740d2 100644
--- a/xmpp/Client.hx
+++ b/xmpp/Client.hx
@@ -123,7 +123,7 @@ class Client extends xmpp.EventEmitter {
 
 			final chatMessage = ChatMessage.fromStanza(stanza, jid);
 			if (chatMessage != null) {
-				var chat = getDirectChat(chatMessage.conversation());
+				var chat = getDirectChat(chatMessage.chatId());
 				chatActivity(chat);
 				for (handler in chatMessageHandlers) {
 					handler(chatMessage);
diff --git a/xmpp/Notification.hx b/xmpp/Notification.hx
index e220753..1db2121 100644
--- a/xmpp/Notification.hx
+++ b/xmpp/Notification.hx
@@ -35,7 +35,7 @@ class Notification {
 			"New Message",
 			m.text,
 			m.account(),
-			m.conversation(),
+			m.chatId(),
 			m.serverId,
 			imageUri,
 			m.lang,
diff --git a/xmpp/persistence/browser.js b/xmpp/persistence/browser.js
index 869b5d1..a58c4c6 100644
--- a/xmpp/persistence/browser.js
+++ b/xmpp/persistence/browser.js
@@ -12,7 +12,7 @@ exports.xmpp.persistence = {
 				if (!db.objectStoreNames.contains("messages")) {
 					const store = upgradeDb.createObjectStore("messages", { keyPath: "serverId" });
 					store.createIndex("account", ["account", "timestamp"]);
-					store.createIndex("conversation", ["account", "conversation", "timestamp"]);
+					store.createIndex("chats", ["account", "chatId", "timestamp"]);
 				}
 				if (!db.objectStoreNames.contains("keyvaluepairs")) {
 					upgradeDb.createObjectStore("keyvaluepairs");
@@ -54,9 +54,9 @@ exports.xmpp.persistence = {
 					"prev"
 					);
 				} else {
-					cursor = store.index("conversation").openCursor(
-					IDBKeyRange.bound([account, jid, new Date(0)], [account, jid, new Date("9999-01-01")]),
-					"prev"
+					cursor = store.index("chats").openCursor(
+						IDBKeyRange.bound([account, jid, new Date(0)], [account, jid, new Date("9999-01-01")]),
+						"prev"
 					);
 				}
 				cursor.onsuccess = (event) => {
@@ -74,18 +74,18 @@ exports.xmpp.persistence = {
 				store.put({
 					...message,
 					account: account,
-					conversation: message.conversation(),
+					chatId: message.chatId(),
 					timestamp: new Date(message.timestamp),
 					direction: message.direction.toString()
 				});
 			},
 
-			getMessages: function(account, conversation, _beforeId, beforeTime, callback) {
+			getMessages: function(account, chatId, _beforeId, beforeTime, callback) {
 				const beforeDate = beforeTime ? new Date(beforeTime) : new Date("9999-01-01");
 				const tx = db.transaction(["messages"], "readonly");
 				const store = tx.objectStore("messages");
-				const cursor = store.index("conversation").openCursor(
-					IDBKeyRange.bound([account, conversation, new Date(0)], [account, conversation, beforeDate]),
+				const cursor = store.index("chats").openCursor(
+					IDBKeyRange.bound([account, chatId, new Date(0)], [account, chatId, beforeDate]),
 					"prev"
 				);
 				const result = [];