| author | Stephen Paul Weber
<singpolyma@singpolyma.net> 2026-11-08 19:20:46 UTC |
| committer | Stephen Paul Weber
<singpolyma@singpolyma.net> 2026-11-16 14:55:03 UTC |
| parent | 751099e2a3e7e665e00e4c89bff646e2ffe82d95 |
| borogove/Message.hx | +5 | -0 |
| test/TestChatMessage.hx | +142 | -0 |
| test/TestClient.hx | +27 | -0 |
| test/TestSqlite.hx | +142 | -0 |
| test/persistence-tests.ts | +168 | -0 |
diff --git a/borogove/Message.hx b/borogove/Message.hx index aa5b9ca..369f28e 100644 --- a/borogove/Message.hx +++ b/borogove/Message.hx @@ -179,6 +179,11 @@ class Message { final addresses = stanza.getChild("addresses", "http://jabber.org/protocol/address"); var anyExtendedReplyTo = false; if (addresses != null) { + if (msg.direction == MessageSent) { + if (addresses.allTags("address").exists((address) -> address.attr.get("type") == "to")) { + recipients.clear(); + } + } for (address in addresses.allTags("address")) { final jid = address.attr.get("jid"); if (address.attr.get("type") == "noreply") { diff --git a/test/TestChatMessage.hx b/test/TestChatMessage.hx index a723116..87b5436 100644 --- a/test/TestChatMessage.hx +++ b/test/TestChatMessage.hx @@ -221,6 +221,148 @@ class TestChatMessage extends utest.Test { Assert.equals("> line one\n> line two\n> line three\n\nok sure", body); } + public function testIncomingExtendedAddressingIncludesSelfInRecipients() { + final stanza = new Stanza("message"); + stanza.attr.set("type", "chat"); + stanza.attr.set("id", "extended-in-1"); + stanza.attr.set("from", "alice@example.com"); + stanza.attr.set("to", "me@example.com"); + final addresses = stanza.tag("addresses", {xmlns: "http://jabber.org/protocol/address"}); + addresses.tag("address", {type: "to", jid: "alice@example.com"}).up(); + addresses.tag("address", {type: "to", jid: "bob@example.com"}).up(); + addresses.up(); + stanza.addChild(new Stanza("body").text("hi")); + + final msg = Message.fromStanza(stanza, JID.parse("me@example.com")); + switch (msg.parsed) { + case ChatMessageStanza(m): + Assert.equals("alice@example.com\nbob@example.com", m.chatId()); + Assert.equals("me@example.com", m.to?.asString()); + Assert.same(["alice@example.com", "bob@example.com", "me@example.com"], m.recipients.map(r -> r.asBare().asString())); + default: + Assert.fail("Expected ChatMessageStanza"); + } + } + + public function testIncomingAddressingDoesNotDuplicateSelfInRecipients() { + final stanza = new Stanza("message"); + stanza.attr.set("type", "chat"); + stanza.attr.set("id", "extended-in-2"); + stanza.attr.set("from", "alice@example.com"); + stanza.attr.set("to", "me@example.com"); + final addresses = stanza.tag("addresses", {xmlns: "http://jabber.org/protocol/address"}); + addresses.tag("address", {type: "to", jid: "alice@example.com"}).up(); + addresses.tag("address", {type: "to", jid: "me@example.com"}).up(); + addresses.up(); + stanza.addChild(new Stanza("body").text("hi")); + + final msg = Message.fromStanza(stanza, JID.parse("me@example.com")); + switch (msg.parsed) { + case ChatMessageStanza(m): + Assert.same(["alice@example.com", "me@example.com"], m.recipients.map(r -> r.asBare().asString())); + default: + Assert.fail("Expected ChatMessageStanza"); + } + } + + public function testSentNonRecipientAddressingKeepsEnvelopeToInRecipients() { + final stanza = new Stanza("message"); + stanza.attr.set("type", "chat"); + stanza.attr.set("id", "extended-out-1"); + stanza.attr.set("from", "me@example.com"); + stanza.attr.set("to", "bob@example.com"); + final addresses = stanza.tag("addresses", {xmlns: "http://jabber.org/protocol/address"}); + addresses.tag("address", {type: "replyto", jid: "multi@example.com"}).up(); + addresses.up(); + stanza.addChild(new Stanza("body").text("hi")); + + final msg = Message.fromStanza(stanza, JID.parse("me@example.com")); + switch (msg.parsed) { + case ChatMessageStanza(m): + Assert.equals("bob@example.com", m.chatId()); + Assert.same(["bob@example.com"], m.recipients.map(r -> r.asBare().asString())); + Assert.same(["multi@example.com"], m.replyTo.map(r -> r.asBare().asString())); + default: + Assert.fail("Expected ChatMessageStanza"); + } + } + + public function testSentCcAddressingDoesNotDropEnvelopeTo() { + final stanza = new Stanza("message"); + stanza.attr.set("type", "chat"); + stanza.attr.set("id", "extended-out-2"); + stanza.attr.set("from", "me@example.com"); + stanza.attr.set("to", "bob@example.com"); + final addresses = stanza.tag("addresses", {xmlns: "http://jabber.org/protocol/address"}); + addresses.tag("address", {type: "cc", jid: "carol@example.com"}).up(); + addresses.up(); + stanza.addChild(new Stanza("body").text("hi")); + + final msg = Message.fromStanza(stanza, JID.parse("me@example.com")); + switch (msg.parsed) { + case ChatMessageStanza(m): + Assert.same(["bob@example.com", "carol@example.com"], m.recipients.map(r -> r.asBare().asString())); + default: + Assert.fail("Expected ChatMessageStanza"); + } + } + + public function testIncomingCompatAddressingUsesRoutingJidAsChatId() { + final stanza = new Stanza("message"); + stanza.attr.set("type", "chat"); + stanza.attr.set("id", "compat-in-1"); + stanza.attr.set("from", "multi@example.com"); + stanza.attr.set("to", "me@example.com"); + final addresses = stanza.tag("addresses", {xmlns: "http://jabber.org/protocol/address"}); + addresses.tag("address", {type: "replyto", jid: "multi@example.com"}).up(); + addresses.tag("address", {type: "to", jid: "alice@example.com"}).up(); + addresses.tag("address", {type: "to", jid: "bob@example.com"}).up(); + addresses.tag("address", {type: "ofrom", jid: "alice@example.com"}).up(); + addresses.up(); + stanza.addChild(new Stanza("body").text("hi")); + + final msg = Message.fromStanza(stanza, JID.parse("me@example.com")); + switch (msg.parsed) { + case ChatMessageStanza(m): + Assert.equals("multi@example.com", m.chatId()); + Assert.equals("me@example.com", m.to?.asString()); + Assert.same(["alice@example.com", "bob@example.com", "me@example.com"], m.recipients.map(r -> r.asBare().asString())); + default: + Assert.fail("Expected ChatMessageStanza"); + } + } + + public function testOutgoingCompatAddressingRoundTripsThroughStanza() { + final builder = new borogove.ChatMessageBuilder(); + builder.localId = "compat-out-1"; + builder.direction = MessageSent; + builder.from = JID.parse("me@example.com"); + builder.senderId = "me@example.com"; + builder.to = JID.parse("multi@example.com"); + builder.recipients = [JID.parse("alice@example.com"), JID.parse("bob@example.com")]; + builder.replyTo = [JID.parse("me@example.com")]; + builder.setBody(borogove.Html.text("hi")); + + final built = builder.build(); + Assert.equals("multi@example.com", built.chatId()); + + final stanza = built.asStanza(); + Assert.equals("multi@example.com", stanza.attr.get("to")); + final addresses = stanza.getChild("addresses", "http://jabber.org/protocol/address"); + Assert.notNull(addresses); + Assert.same(["alice@example.com", "bob@example.com"], addresses.allTags("address").map(a -> a.attr.get("jid"))); + + final reparsed = Message.fromStanza(stanza, JID.parse("me@example.com")); + switch (reparsed.parsed) { + case ChatMessageStanza(m): + Assert.equals("multi@example.com", m.chatId()); + Assert.equals("multi@example.com", m.to?.asString()); + Assert.same(["alice@example.com", "bob@example.com"], m.recipients.map(r -> r.asBare().asString())); + default: + Assert.fail("Expected ChatMessageStanza"); + } + } + public function testReactionFallbackSkipsNestedQuotes() { // Build a parent message that itself contains a quote and regular text final parentBuilder = new borogove.ChatMessageBuilder(); diff --git a/test/TestClient.hx b/test/TestClient.hx index 106734f..a7bae2c 100644 --- a/test/TestClient.hx +++ b/test/TestClient.hx @@ -620,6 +620,33 @@ class TestClient extends utest.Test { }); } + public function testIncomingExtendedMultiDMResolvesToParticipantChatId(async: Async) { + final persistence = new MessageMockPersistence(); + final client = new Client("me@example.com", persistence); + final chatId = "alice@example.com\nbob@example.com"; + + client.addChatsUpdatedListener(chats -> { + final chat = chats.find(c -> c.chatId == chatId); + if (chat == null) return; + Assert.notNull(chat.lastMessage); + if (chat.lastMessage != null) { + Assert.equals(chatId, chat.lastMessage.chatId()); + Assert.equals("hi", chat.lastMessage.body().toPlainText()); + } + Assert.isNull(client.getChat("alice@example.com")); + async.done(); + }); + + final stanza = new Stanza("message", { xmlns: "jabber:client", type: "chat", from: "alice@example.com", to: "me@example.com", id: "srv-multi-1" }); + stanza.tag("body").text("hi").up(); + stanza.tag("stanza-id", { xmlns: "urn:xmpp:sid:0", by: "me@example.com", id: "srv-multi-1" }).up(); + final addresses = stanza.tag("addresses", { xmlns: "http://jabber.org/protocol/address" }); + addresses.tag("address", { type: "to", jid: "alice@example.com" }).up(); + addresses.tag("address", { type: "to", jid: "bob@example.com" }).up(); + addresses.up(); + client.stream.onStanza(stanza); + } + public function testDecryptionFailurePreservesLocalMessage(async: Async) { final persistence = new MessageMockPersistence(); final client = new Client("test@example.com", persistence); diff --git a/test/TestSqlite.hx b/test/TestSqlite.hx index b8d60b7..81048be 100644 --- a/test/TestSqlite.hx +++ b/test/TestSqlite.hx @@ -2124,6 +2124,148 @@ class TestSqlite extends utest.Test { Assert.same(expected, actual); } + public function testCompatMultiDMOutgoingRoundTrip(async: Async) { + final account = "alice@example.com"; + final routing = "multi@example.com"; + final builder = new ChatMessageBuilder(); + builder.localId = "compat-out-1"; + builder.sortId = "a0"; + builder.direction = MessageSent; + builder.from = JID.parse(account); + builder.senderId = account; + builder.to = JID.parse(routing); + builder.recipients = [JID.parse("bob@example.com"), JID.parse("carol@example.com")]; + builder.replyTo = [JID.parse(account)]; + builder.timeReceived = "2020-01-01T00:00:00Z"; + builder.setBody(Html.text("hi")); + final message = builder.build(); + + persistence.storeMessages(account, [message]).then(_ -> { + Assert.equals(routing, message.chatId()); + return persistence.getMessagesBefore(account, routing, null); + }).then(result -> { + Assert.equals(1, result.length); + if (result.length < 1) { + async.done(); + return; + } + final fetched = result[0]; + Assert.equals(routing, fetched.chatId()); + Assert.equals(routing, fetched.to?.asString()); + Assert.same(["bob@example.com", "carol@example.com"], fetched.recipients.map(r -> r.asBare().asString())); + async.done(); + }).catchError(e -> { + Assert.fail(Std.string(e)); + async.done(); + }); + } + + public function testCompatMultiDMIncomingRoundTrip(async: Async) { + final account = "alice@example.com"; + final routing = "multi@example.com"; + final stanza = Stanza.parse('<message type="chat" from="multi@example.com" to="alice@example.com" id="compat-in-1"><body>hi</body><stanza-id xmlns="urn:xmpp:sid:0" by="alice@example.com" id="srv-compat-1"/><addresses xmlns="http://jabber.org/protocol/address"><address type="replyto" jid="multi@example.com"/><address type="to" jid="bob@example.com"/><address type="to" jid="carol@example.com"/><address type="ofrom" jid="bob@example.com"/></addresses></message>'); + final parsed = Message.fromStanza(stanza, JID.parse(account), (builder, _) -> { + builder.sortId = "a0"; + return builder; + }); + switch (parsed.parsed) { + case ChatMessageStanza(message): + persistence.storeMessages(account, [message]).then(_ -> { + Assert.equals(routing, message.chatId()); + Assert.equals(account, message.to?.asString()); + return persistence.getMessagesBefore(account, routing, null); + }).then(result -> { + Assert.equals(1, result.length); + if (result.length < 1) { + async.done(); + return; + } + final fetched = result[0]; + Assert.equals(routing, fetched.chatId()); + Assert.equals(account, fetched.to?.asString()); + Assert.same(["alice@example.com", "bob@example.com", "carol@example.com"], fetched.recipients.map(r -> r.asBare().asString())); + Assert.same([routing], fetched.replyTo.map(r -> r.asBare().asString())); + async.done(); + }).catchError(e -> { + Assert.fail(Std.string(e)); + async.done(); + }); + default: + Assert.fail("Expected ChatMessageStanza"); + async.done(); + } + } + + public function testExtendedMultiDMIncomingRoundTrip(async: Async) { + final account = "alice@example.com"; + final chatId = "bob@example.com\ncarol@example.com"; + final stanza = Stanza.parse('<message type="chat" from="bob@example.com" to="alice@example.com" id="extended-in-1"><body>hi</body><stanza-id xmlns="urn:xmpp:sid:0" by="alice@example.com" id="srv-extended-1"/><addresses xmlns="http://jabber.org/protocol/address"><address type="to" jid="bob@example.com"/><address type="to" jid="carol@example.com"/></addresses></message>'); + final parsed = Message.fromStanza(stanza, JID.parse(account), (builder, _) -> { + builder.sortId = "a0"; + return builder; + }); + switch (parsed.parsed) { + case ChatMessageStanza(message): + persistence.storeMessages(account, [message]).then(_ -> { + Assert.equals(chatId, message.chatId()); + Assert.equals(account, message.to?.asString()); + return persistence.getMessagesBefore(account, chatId, null); + }).then(result -> { + Assert.equals(1, result.length); + if (result.length < 1) { + async.done(); + return; + } + final fetched = result[0]; + Assert.equals(chatId, fetched.chatId()); + Assert.equals(account, fetched.to?.asString()); + Assert.same(["alice@example.com", "bob@example.com", "carol@example.com"], fetched.recipients.map(r -> r.asBare().asString())); + Assert.same(["bob@example.com", "carol@example.com"], fetched.replyTo.map(r -> r.asBare().asString())); + async.done(); + }).catchError(e -> { + Assert.fail(Std.string(e)); + async.done(); + }); + default: + Assert.fail("Expected ChatMessageStanza"); + async.done(); + } + } + + public function testExtendedMultiDMOutgoingRoundTrip(async: Async) { + final account = "alice@example.com"; + final chatId = "bob@example.com\ncarol@example.com"; + final builder = new ChatMessageBuilder(); + builder.localId = "extended-out-1"; + builder.sortId = "a0"; + builder.direction = MessageSent; + builder.from = JID.parse(account); + builder.senderId = account; + builder.to = JID.parse("bob@example.com"); + builder.recipients = [JID.parse("bob@example.com"), JID.parse("carol@example.com")]; + builder.replyTo = [JID.parse(account)]; + builder.timeReceived = "2020-01-01T00:00:00Z"; + builder.setBody(Html.text("hi")); + + persistence.storeMessages(account, [builder.build()]).then(_ -> { + return persistence.getMessagesBefore(account, chatId, null); + }).then(result -> { + Assert.equals(1, result.length); + if (result.length < 1) { + async.done(); + return; + } + final fetched = result[0]; + Assert.equals(chatId, fetched.chatId()); + Assert.equals("bob@example.com", fetched.to?.asString()); + Assert.same(["bob@example.com", "carol@example.com"], fetched.recipients.map(r -> r.asBare().asString())); + async.done(); + }).catchError(e -> { + Assert.fail(Std.string(e)); + async.done(); + }); + } + public function testMessageColumnsAllowsAddingColumns() { final defaultColumns = [ "stanza", diff --git a/test/persistence-tests.ts b/test/persistence-tests.ts index cc52ea9..3a879df 100644 --- a/test/persistence-tests.ts +++ b/test/persistence-tests.ts @@ -2559,6 +2559,174 @@ export function sharedPersistenceTests(test) { "propose", ]); }); + + test("compat multiDM outgoing keeps to and recipients separate", async ({ + borogove, + persistence, + }) => { + const account = "alice@example.com"; + const routing = "multi@example.com"; + const builder = new borogove.ChatMessageBuilder({ + localId: "compat-out-1", + senderId: account, + direction: borogove.MessageDirection.MessageSent, + }); + builder.sortId = "compat-out-a0"; + builder.from = borogove.JID.parse(account); + builder.to = borogove.JID.parse(routing); + builder.recipients = [ + borogove.JID.parse("bob@example.com"), + borogove.JID.parse("carol@example.com"), + ]; + builder.replyTo = [borogove.JID.parse(account)]; + builder.timeReceived = "2020-01-01T00:00:00Z"; + const message = builder.build(); + expect(message.chatId()).toBe(routing); + + await persistence.storeMessages(account, [message]); + const messages = await persistence.getMessagesBefore(account, routing); + expect(messages.length).toBe(1); + if (messages.length < 1) return; + const [fetched] = messages; + expect(fetched.chatId()).toBe(routing); + expect(fetched.to?.asString()).toBe(routing); + expect(fetched.recipients.map((r) => r.asBare().asString())).toEqual([ + "bob@example.com", + "carol@example.com", + ]); + }); + + test("compat multiDM incoming keeps routing jid as chat id", async ({ + borogove, + persistence, + }) => { + const account = "alice@example.com"; + const routing = "multi@example.com"; + const stanza = borogove.Stanza.parse( + '<message type="chat" from="multi@example.com" to="alice@example.com" id="compat-in-1"><body>hi</body><stanza-id xmlns="urn:xmpp:sid:0" by="alice@example.com" id="compat-in-srv-1"/><addresses xmlns="http://jabber.org/protocol/address"><address type="replyto" jid="multi@example.com"/><address type="to" jid="bob@example.com"/><address type="to" jid="carol@example.com"/><address type="ofrom" jid="bob@example.com"/></addresses></message>', + ); + const builder = new borogove.ChatMessageBuilder({ + serverId: "compat-in-srv-1", + serverIdBy: account, + senderId: "bob@example.com", + direction: borogove.MessageDirection.MessageReceived, + }); + builder.sortId = "compat-in-a0"; + builder.from = borogove.JID.parse(routing); + builder.to = borogove.JID.parse(account); + builder.recipients = [ + borogove.JID.parse(account), + borogove.JID.parse("bob@example.com"), + borogove.JID.parse("carol@example.com"), + ]; + builder.replyTo = [borogove.JID.parse(routing)]; + builder.timeReceived = "2020-01-01T00:00:01Z"; + (builder as any).stanza = stanza; + const message = builder.build(); + expect(message.chatId()).toBe(routing); + + await persistence.storeMessages(account, [message]); + const messages = await persistence.getMessagesBefore(account, routing); + expect(messages.length).toBe(1); + if (messages.length < 1) return; + const [fetched] = messages; + expect(fetched.chatId()).toBe(routing); + expect(fetched.to?.asString()).toBe(account); + expect(fetched.recipients.map((r) => r.asBare().asString())).toEqual([ + "alice@example.com", + "bob@example.com", + "carol@example.com", + ]); + expect(fetched.replyTo.map((r) => r.asBare().asString())).toEqual([ + routing, + ]); + }); + + test("extended multiDM outgoing round trips participant chat id", async ({ + borogove, + persistence, + }) => { + const account = "alice@example.com"; + const chatId = "bob@example.com\ncarol@example.com"; + const builder = new borogove.ChatMessageBuilder({ + localId: "extended-out-1", + senderId: account, + direction: borogove.MessageDirection.MessageSent, + }); + builder.sortId = "extended-out-a0"; + builder.from = borogove.JID.parse(account); + builder.to = borogove.JID.parse("bob@example.com"); + builder.recipients = [ + borogove.JID.parse("bob@example.com"), + borogove.JID.parse("carol@example.com"), + ]; + builder.replyTo = [borogove.JID.parse(account)]; + builder.timeReceived = "2020-01-01T00:00:00Z"; + const message = builder.build(); + expect(message.chatId()).toBe(chatId); + + await persistence.storeMessages(account, [message]); + const messages = await persistence.getMessagesBefore(account, chatId); + expect(messages.length).toBe(1); + if (messages.length < 1) return; + const [fetched] = messages; + expect(fetched.chatId()).toBe(chatId); + expect(fetched.to?.asString()).toBe("bob@example.com"); + expect(fetched.recipients.map((r) => r.asBare().asString())).toEqual([ + "bob@example.com", + "carol@example.com", + ]); + }); + + test("extended multiDM incoming round trips participant chat id", async ({ + borogove, + persistence, + }) => { + const account = "alice@example.com"; + const chatId = "bob@example.com\ncarol@example.com"; + const stanza = borogove.Stanza.parse( + '<message type="chat" from="bob@example.com" to="alice@example.com" id="extended-in-1"><body>hi</body><stanza-id xmlns="urn:xmpp:sid:0" by="alice@example.com" id="extended-in-srv-1"/><addresses xmlns="http://jabber.org/protocol/address"><address type="to" jid="bob@example.com"/><address type="to" jid="carol@example.com"/></addresses></message>', + ); + const builder = new borogove.ChatMessageBuilder({ + serverId: "extended-in-srv-1", + serverIdBy: account, + senderId: "bob@example.com", + direction: borogove.MessageDirection.MessageReceived, + }); + builder.sortId = "extended-in-a0"; + builder.from = borogove.JID.parse("bob@example.com"); + builder.to = borogove.JID.parse(account); + builder.recipients = [ + borogove.JID.parse(account), + borogove.JID.parse("bob@example.com"), + borogove.JID.parse("carol@example.com"), + ]; + builder.replyTo = [ + borogove.JID.parse("bob@example.com"), + borogove.JID.parse("carol@example.com"), + ]; + builder.timeReceived = "2020-01-01T00:00:02Z"; + (builder as any).stanza = stanza; + const message = builder.build(); + expect(message.chatId()).toBe(chatId); + + await persistence.storeMessages(account, [message]); + const messages = await persistence.getMessagesBefore(account, chatId); + expect(messages.length).toBe(1); + if (messages.length < 1) return; + const [fetched] = messages; + expect(fetched.chatId()).toBe(chatId); + expect(fetched.to?.asString()).toBe(account); + expect(fetched.recipients.map((r) => r.asBare().asString())).toEqual([ + "alice@example.com", + "bob@example.com", + "carol@example.com", + ]); + expect(fetched.replyTo.map((r) => r.asBare().asString())).toEqual([ + "bob@example.com", + "carol@example.com", + ]); + }); } type TestKeyPair = {