git » sdk » commit 50e4aa1

Create new chats from MAM

author Stephen Paul Weber
2025-03-26 18:26:18 UTC
committer Stephen Paul Weber
2025-03-26 18:26:18 UTC
parent 8aa0a5712449a8909d128b657c772bed8cc63831

Create new chats from MAM

If we see a message in MAM for a chat we don't have, create a chat for it.

snikket/Client.hx +7 -0
snikket/Message.hx +3 -0

diff --git a/snikket/Client.hx b/snikket/Client.hx
index 6312f65..0672a2e 100644
--- a/snikket/Client.hx
+++ b/snikket/Client.hx
@@ -1461,6 +1461,7 @@ class Client extends EventEmitter {
 			builder.syncPoint = true;
 			return builder;
 		});
+		final chatIds: Map<String, Bool> = [];
 		sync.onMessages((messageList) -> {
 			final promises = [];
 			final chatMessages = [];
@@ -1468,6 +1469,7 @@ class Client extends EventEmitter {
 				switch (m) {
 					case ChatMessageStanza(message):
 						chatMessages.push(message);
+						if (message.type == MessageChat) chatIds[message.chatId()] = true;
 					case ReactionUpdateStanza(update):
 						promises.push(new thenshim.Promise((resolve, reject) -> {
 							persistence.storeReaction(accountId(), update, (_) -> resolve(null));
@@ -1501,6 +1503,11 @@ class Client extends EventEmitter {
 					for (sid => stanza in sync.jmi) {
 						onMAMJMI(sid, stanza);
 					}
+					for (chatId => _ in chatIds) {
+						// If this is a message from a prevoiusly unknown direct chat, record the chat
+						final chat = getChat(chatId);
+						if (chat == null) getDirectChat(chatId);
+					}
 					if (callback != null) callback(true);
 				}
 			},
diff --git a/snikket/Message.hx b/snikket/Message.hx
index b513535..7089050 100644
--- a/snikket/Message.hx
+++ b/snikket/Message.hx
@@ -62,6 +62,9 @@ class Message {
 		msg.from = JID.parse(from);
 		final isGroupchat = stanza.attr.get("type") == "groupchat";
 		msg.type = isGroupchat ? MessageChannel : MessageChat;
+		if (msg.type == MessageChat && stanza.getChild("x", "http://jabber.org/protocol/muc#user") != null) {
+			msg.type = MessageChannelPrivate;
+		}
 		msg.senderId = (isGroupchat ? msg.from : msg.from?.asBare())?.asString();
 		final localJidBare = localJid.asBare();
 		final domain = localJid.domain;