| author | Stephen Paul Weber
<singpolyma@singpolyma.net> 2026-11-08 21:01:35 UTC |
| committer | Stephen Paul Weber
<singpolyma@singpolyma.net> 2026-11-16 14:55:03 UTC |
| parent | 9617e1a1276763cdd8666ea3cac69c6047acc7cb |
| borogove/Chat.hx | +3 | -3 |
| borogove/Client.hx | +43 | -4 |
| test/TestChat.hx | +162 | -4 |
diff --git a/borogove/Chat.hx b/borogove/Chat.hx index 797cb4d..e8f39a8 100644 --- a/borogove/Chat.hx +++ b/borogove/Chat.hx @@ -1108,9 +1108,9 @@ class DirectChat extends Chat { private var _fullCounterparts: Array<String>; @:allow(borogove) - private function new(client:Client, stream:GenericStream, persistence:Persistence, chatId:String, uiState = Open, isBookmarked = false, isBlocked = false, extensions: Null<Stanza> = null, readUpToId: Null<String> = null, readUpToBy: Null<String> = null, omemoContactDeviceIDs: Array<Int> = null) { + private function new(client:Client, stream:GenericStream, persistence:Persistence, chatId:String, ?fullCounterparts:Array<String>, uiState = Open, isBookmarked = false, isBlocked = false, extensions: Null<Stanza> = null, readUpToId: Null<String> = null, readUpToBy: Null<String> = null, omemoContactDeviceIDs: Array<Int> = null) { super(client, stream, persistence, chatId, uiState, isBookmarked, isBlocked, extensions, readUpToId, readUpToBy, omemoContactDeviceIDs); - _fullCounterparts = counterparts(); + _fullCounterparts = fullCounterparts ?? counterparts(); outbox.start(); } @@ -2853,7 +2853,7 @@ class SerializedChat { } final chat = if (klass == "DirectChat") { - new DirectChat(client, stream, persistence, chatId, uiState, isBookmarked, isBlocked, extensions, readUpToId, readUpToBy, omemoContactDeviceIDs); + new DirectChat(client, stream, persistence, chatId, null, uiState, isBookmarked, isBlocked, extensions, readUpToId, readUpToBy, omemoContactDeviceIDs); } else if (klass == "Channel") { final channel = new Channel(client, stream, persistence, chatId, uiState, isBookmarked, isBlocked, extensions, readUpToId, readUpToBy); channel.membersForName = membersForName; diff --git a/borogove/Client.hx b/borogove/Client.hx index 05b5277..8e1cac6 100644 --- a/borogove/Client.hx +++ b/borogove/Client.hx @@ -1561,6 +1561,46 @@ class Client extends EventEmitter { return chat; } + /** + Start multiDM chat from multiple search results + + @param counterparts participants to include + @param target optional service-specific target when needed + @returns the chat that was started + **/ + public function startMultiChat(counterparts: Array<AvailableChat>, ?target: String): DirectChat { + final participants: Array<String> = []; + for (counterpart in counterparts) { + if (counterpart.isChannel()) throw 'Cannot start multiDM with a channel: ${counterpart.chatId}'; + final jid = JID.parse(counterpart.chatId); + if (!jid.isValid()) throw 'Invalid participant: ${counterpart.chatId}'; + final id = jid.asBare().asString(); + if (id == accountId()) continue; + if (!participants.contains(id)) participants.push(id); + } + if (participants.length < 2) throw "A multiDM needs at least two counterparts"; + + participants.sort(Reflect.compare); + + if (target != null) { + final jid = JID.parse(target); + if (!jid.isValid() || !jid.isBare()) throw 'Invalid target: $target'; + } + + final chatId = target ?? participants.join("\n"); + + final existing = getChat(chatId); + if (existing != null) { + final chat = Std.downcast(existing, DirectChat); + if (chat == null) throw 'Chat $chatId already exists and is not a direct chat'; + return chat; + } + + final chat = new DirectChat(this, this.stream, this.persistence, chatId, participants, Closed); + chatActivity(chat); + return chat; + } + /** Create a new unconfigured Channel @@ -2126,10 +2166,9 @@ class Client extends EventEmitter { } final pinnedCount = chat.uiState == Pinned ? 0 : chats.fold((item, result) -> result + (item.uiState == Pinned ? 1 : 0), 0); var idx = chats.findIndex(c -> c.chatId == chat.chatId); - if (idx > pinnedCount) { - chats.splice(idx, 1); - chats.insert(pinnedCount, chat); - } + if (idx >= 0) chats.splice(idx, 1); + chats.insert(pinnedCount, chat); + chatsIndex[chat.chatId] = chat; if (trigger) this.trigger("chats/update", [chat]); } diff --git a/test/TestChat.hx b/test/TestChat.hx index 3d4c897..0143599 100644 --- a/test/TestChat.hx +++ b/test/TestChat.hx @@ -1235,8 +1235,7 @@ class TestChat extends utest.Test { final persistence = new RecordingPersistence(); final client = new Client("me@example.com", persistence); final routing = "multi@example.com"; - final chat = new borogove.Chat.DirectChat(client, client.stream, persistence, routing); - chat._fullCounterparts = ["bob@example.com", "carol@example.com"]; + final chat = new borogove.Chat.DirectChat(client, client.stream, persistence, routing, ["bob@example.com", "carol@example.com"]); client.stream.on("sendStanza", (stanza: Stanza) -> { if (stanza.name != "message" || stanza.getChildText("body") != "hello") return EventUnhandled; @@ -1363,8 +1362,7 @@ class TestChat extends utest.Test { final persistence = new RecordingPersistence(); final client = new Client("me@example.com", persistence); final routing = "multi@example.com"; - final chat = new borogove.Chat.DirectChat(client, client.stream, persistence, routing); - chat._fullCounterparts = ["bob@example.com", "carol@example.com"]; + final chat = new borogove.Chat.DirectChat(client, client.stream, persistence, routing, ["bob@example.com", "carol@example.com"]); client.stream.on("sendStanza", (stanza: Stanza) -> { if (stanza.name != "message" || stanza.getChild("reactions", "urn:xmpp:reactions:0") == null) return EventUnhandled; @@ -1381,6 +1379,161 @@ class TestChat extends utest.Test { chat.removeReaction(reactionTarget(), borogove.Reaction.unicode("👍")); } + // Creating a multi-person DM from search results must produce the same chat + // id the message parser derives, so incoming stanzas resolve back to it. + public function testStartMultiChatExtended() { + final persistence = new RecordingPersistence(); + final client = new Client("me@example.com", persistence); + + final chat = client.startMultiChat([availablePerson("carol@example.com"), availablePerson("bob@example.com")]); + + Assert.equals("bob@example.com\ncarol@example.com", chat.chatId); + Assert.same(["bob@example.com", "carol@example.com"], chat._fullCounterparts); + Assert.equals(chat, client.getChat("bob@example.com\ncarol@example.com")); + Assert.isTrue(client.getChats().contains(chat)); + Assert.isTrue(persistence.storedChats.contains(chat)); + } + + // Search results for different resources of the same person, our own account, + // and duplicates must all collapse to a single participant. + public function testStartMultiChatIgnoresSelfAndDuplicates() { + final persistence = new Dummy(); + final client = new Client("me@example.com", persistence); + + final chat = client.startMultiChat([ + availablePerson("me@example.com"), + availablePerson("bob@example.com"), + availablePerson("bob@example.com/mobile"), + availablePerson("carol@example.com") + ]); + + Assert.equals("bob@example.com\ncarol@example.com", chat.chatId); + Assert.same(["bob@example.com", "carol@example.com"], chat._fullCounterparts); + } + + // A compat multi-person DM is addressed to a single routing JID while its + // participants are kept as counterparts (the chat id cannot carry both). + public function testStartMultiChatCompat() { + final persistence = new Dummy(); + final client = new Client("me@example.com", persistence); + + final chat = client.startMultiChat([availablePerson("bob@example.com"), availablePerson("carol@example.com")], "multi@example.com"); + + Assert.equals("multi@example.com", chat.chatId); + Assert.same(["bob@example.com", "carol@example.com"], chat._fullCounterparts); + Assert.equals(chat, client.getChat("multi@example.com")); + } + + // Starting an already-known chat returns it untouched, even when the caller + // passes a different participant set. + public function testStartMultiChatReturnsExisting() { + final persistence = new Dummy(); + final client = new Client("me@example.com", persistence); + + final first = client.startMultiChat([availablePerson("bob@example.com"), availablePerson("carol@example.com")], "multi@example.com"); + final chatCount = client.getChats().length; + + final second = client.startMultiChat([availablePerson("dave@example.com"), availablePerson("erin@example.com")], "multi@example.com"); + + Assert.equals(first, second); + Assert.equals(chatCount, client.getChats().length); + Assert.same(["bob@example.com", "carol@example.com"], second._fullCounterparts); + } + + public function testStartMultiChatKeepsOtherChats() { + final persistence = new Dummy(); + final client = new Client("me@example.com", persistence); + + final first = client.startMultiChat([availablePerson("bob@example.com"), availablePerson("carol@example.com")]); + final second = client.startMultiChat([availablePerson("dave@example.com"), availablePerson("erin@example.com")]); + + Assert.isTrue(client.getChats().contains(first)); + Assert.isTrue(client.getChats().contains(second)); + Assert.equals(first, client.getChat("bob@example.com\ncarol@example.com")); + Assert.equals(second, client.getChat("dave@example.com\nerin@example.com")); + } + + public function testStartMultiChatRejectsInvalidTarget() { + final persistence = new Dummy(); + final client = new Client("me@example.com", persistence); + final counterparts = [availablePerson("bob@example.com"), availablePerson("carol@example.com")]; + + Assert.raises(() -> client.startMultiChat(counterparts, "multi@example.com/phone"), String); + Assert.raises(() -> client.startMultiChat(counterparts, "not-a-jid"), String); + } + + public function testStartMultiChatTooFewParticipants() { + final persistence = new Dummy(); + final client = new Client("me@example.com", persistence); + + Assert.raises(() -> client.startMultiChat([]), String); + Assert.raises(() -> client.startMultiChat([availablePerson("bob@example.com")]), String); + // Our own account never counts as a participant. + Assert.raises(() -> client.startMultiChat([availablePerson("me@example.com"), availablePerson("bob@example.com")]), String); + } + + public function testStartMultiChatRejectsChannels() { + final persistence = new Dummy(); + final client = new Client("me@example.com", persistence); + final caps = new borogove.Caps("", [new Identity("conference", "text", "Channel")], ["http://jabber.org/protocol/muc"], []); + final channel = new AvailableChat("room@example.com", "Room", "", caps); + + Assert.raises(() -> client.startMultiChat([availablePerson("bob@example.com"), channel]), String); + } + + public function testStartMultiChatExtendedSendsToEachParticipant(async: Async) { + final persistence = new RecordingPersistence(); + final client = new Client("me@example.com", persistence); + final chat = client.startMultiChat([availablePerson("bob@example.com"), availablePerson("carol@example.com")]); + final sent: Array<Stanza> = []; + + client.stream.on("sendStanza", (stanza: Stanza) -> { + if (stanza.name != "message" || stanza.getChildText("body") != "hello") return EventUnhandled; + sent.push(stanza); + if (sent.length < 2) return EventUnhandled; + + final tos = sent.map(s -> s.attr.get("to")); + tos.sort(Reflect.compare); + Assert.same(["bob@example.com", "carol@example.com"], tos); + Assert.equals(chat.chatId, persistence.storedMessages[0]?.chatId()); + async.done(); + return EventHandled; + }); + + final builder = new ChatMessageBuilder(); + builder.text = "hello"; + chat.sendMessage(builder, borogove.Chat.OutgoingE2EEPreference.NoE2EE); + } + + public function testStartMultiChatCompatSendsToRoutingJid(async: Async) { + final persistence = new RecordingPersistence(); + final client = new Client("me@example.com", persistence); + final chat = client.startMultiChat([availablePerson("bob@example.com"), availablePerson("carol@example.com")], "multi@example.com"); + + client.stream.on("sendStanza", (stanza: Stanza) -> { + if (stanza.name != "message" || stanza.getChildText("body") != "hello") return EventUnhandled; + + Assert.equals("multi@example.com", stanza.attr.get("to")); + final addresses = stanza.getChild("addresses", "http://jabber.org/protocol/address"); + Assert.notNull(addresses); + if (addresses != null) { + Assert.same(["bob@example.com", "carol@example.com"], addresses.allTags("address").map(a -> a.attr.get("jid"))); + } + Assert.equals("multi@example.com", persistence.storedMessages[0]?.chatId()); + Assert.same(["bob@example.com", "carol@example.com"], persistence.storedMessages[0]?.recipients.map(r -> r.asBare().asString())); + async.done(); + return EventHandled; + }); + + final builder = new ChatMessageBuilder(); + builder.text = "hello"; + chat.sendMessage(builder, borogove.Chat.OutgoingE2EEPreference.NoE2EE); + } + + private function availablePerson(chatId: String):AvailableChat { + return new AvailableChat(chatId, chatId, "", CapsRepo.empty); + } + private function reactionTarget():borogove.ChatMessage { final builder = new ChatMessageBuilder(); builder.localId = "m1"; @@ -1397,6 +1550,11 @@ class TestChat extends utest.Test { @:access(borogove) class RecordingPersistence extends Dummy { public var storedMessages: Array<borogove.ChatMessage> = []; + public var storedChats: Array<borogove.Chat> = []; + + override public function storeChats(accountId: String, chats: Array<borogove.Chat>) { + storedChats = storedChats.concat(chats); + } override public function storeMessages(accountId: String, messages: Array<borogove.ChatMessage>) { storedMessages = storedMessages.concat(messages);