git » sdk » commit bfa192d

Use to for chatId if not in recipients

author Stephen Paul Weber
2026-11-08 19:15:14 UTC
committer Stephen Paul Weber
2026-11-16 14:54:43 UTC
parent 40acc82d24da7ad5bb3b1dd380cc1debde35c8db

Use to for chatId if not in recipients

An outgoing multiDM that goes to a special target (eg
1,2,3@cheogram.com) has a non-recipient to

For outgoing messages, prefer the bare to whenever it is not itself one
of the recipients. 1:1 and regular multiDM messages keep using
the recipient list.

borogove/ChatMessage.hx +5 -1
borogove/ChatMessageBuilder.hx +6 -1
test/TestChatMessage.hx +38 -0
test/TestChatMessageBuilder.hx +47 -0

diff --git a/borogove/ChatMessage.hx b/borogove/ChatMessage.hx
index d9a12d6..f9287d9 100644
--- a/borogove/ChatMessage.hx
+++ b/borogove/ChatMessage.hx
@@ -502,7 +502,11 @@ class ChatMessage {
 		if (isIncoming()) {
 			return replyTo.map((r) -> r.asBare().asString()).join("\n");
 		} else {
-			return recipients.map((r) -> r.asString()).join("\n");
+			// If sent to a non-recipient, use that as the chat id
+			// e.g. a multiDM sent to 1,2,3@cheogram.com
+			final bareTo = to.asBare();
+			if (!recipients.exists((r) -> r.asBare().equals(bareTo))) return bareTo.asString();
+			return recipients.map((r) -> r.asBare().asString()).join("\n");
 		}
 	}
 
diff --git a/borogove/ChatMessageBuilder.hx b/borogove/ChatMessageBuilder.hx
index a79d138..919d1d4 100644
--- a/borogove/ChatMessageBuilder.hx
+++ b/borogove/ChatMessageBuilder.hx
@@ -379,7 +379,12 @@ class ChatMessageBuilder {
 		if (isIncoming()) {
 			return replyTo.map((r) -> r.asBare().asString()).join("\n");
 		} else {
-			return recipients.map((r) -> r.asString()).join("\n");
+			final toJid = to;
+			if (toJid != null) {
+				final bareTo = toJid.asBare();
+				if (!recipients.exists((r) -> r.asBare().equals(bareTo))) return bareTo.asString();
+			}
+			return recipients.map((r) -> r.asBare().asString()).join("\n");
 		}
 	}
 
diff --git a/test/TestChatMessage.hx b/test/TestChatMessage.hx
index 36f864d..a723116 100644
--- a/test/TestChatMessage.hx
+++ b/test/TestChatMessage.hx
@@ -283,4 +283,42 @@ class TestChatMessage extends utest.Test {
 			default:
 		}
 	}
+
+	private function outgoingMessage(to: String, recipients: Array<String>):ChatMessage {
+		return new ChatMessage({
+			timeSent: "2026-01-01T00:00:00Z",
+			timeReceived: "2026-01-01T00:00:00Z",
+			to: JID.parse(to),
+			from: JID.parse("me@example.com"),
+			senderId: "me@example.com",
+			recipients: recipients.map(JID.parse),
+			replyTo: [JID.parse("me@example.com")],
+			direction: MessageSent
+		});
+	}
+
+	public function testChatIdOutgoingUsesToWhenNotInRecipients() {
+		final msg = outgoingMessage("multi@example.com", ["bob@example.com", "carol@example.com"]);
+		Assert.equals("multi@example.com", msg.chatId());
+	}
+
+	public function testChatIdOutgoingUsesBareTo() {
+		final msg = outgoingMessage("multi@example.com/device", ["bob@example.com", "carol@example.com"]);
+		Assert.equals("multi@example.com", msg.chatId());
+	}
+
+	public function testChatIdOutgoingUsesRecipientsWhenToIsAmongThem() {
+		final msg = outgoingMessage("bob@example.com", ["bob@example.com", "carol@example.com"]);
+		Assert.equals("bob@example.com\ncarol@example.com", msg.chatId());
+	}
+
+	public function testChatIdOutgoingSingleRecipient() {
+		final msg = outgoingMessage("bob@example.com", ["bob@example.com"]);
+		Assert.equals("bob@example.com", msg.chatId());
+	}
+
+	public function testChatIdOutgoingRecipientsAreBare() {
+		final msg = outgoingMessage("bob@example.com/phone", ["bob@example.com/phone", "carol@example.com/tablet"]);
+		Assert.equals("bob@example.com\ncarol@example.com", msg.chatId());
+	}
 }
diff --git a/test/TestChatMessageBuilder.hx b/test/TestChatMessageBuilder.hx
index 87d9002..475918e 100644
--- a/test/TestChatMessageBuilder.hx
+++ b/test/TestChatMessageBuilder.hx
@@ -6,6 +6,7 @@ import utest.Async;
 import borogove.ChatMessageBuilder;
 import borogove.Html;
 import borogove.JID;
+import borogove.Message;
 
 @:access(borogove)
 class TestChatMessageBuilder extends utest.Test {
@@ -125,4 +126,50 @@ class TestChatMessageBuilder extends utest.Test {
 		final stanza = msg.asStanza();
 		Assert.isNull(stanza.getChild("request", "urn:xmpp:receipts"));
 	}
+
+	public function testChatIdOutgoingUsesToWhenNotInRecipients() {
+		final builder = new ChatMessageBuilder();
+		builder.direction = MessageSent;
+		builder.from = JID.parse("me@example.com");
+		builder.to = JID.parse("multi@example.com");
+		builder.recipients = [JID.parse("bob@example.com"), JID.parse("carol@example.com")];
+		builder.replyTo = [JID.parse("me@example.com")];
+		Assert.equals("multi@example.com", builder.chatId());
+	}
+
+	public function testChatIdOutgoingUsesBareTo() {
+		final builder = new ChatMessageBuilder();
+		builder.direction = MessageSent;
+		builder.from = JID.parse("me@example.com");
+		builder.to = JID.parse("multi@example.com/device");
+		builder.recipients = [JID.parse("bob@example.com"), JID.parse("carol@example.com")];
+		Assert.equals("multi@example.com", builder.chatId());
+	}
+
+	public function testChatIdOutgoingUsesRecipientsWhenToIsAmongThem() {
+		final builder = new ChatMessageBuilder();
+		builder.direction = MessageSent;
+		builder.from = JID.parse("me@example.com");
+		builder.to = JID.parse("bob@example.com");
+		builder.recipients = [JID.parse("bob@example.com"), JID.parse("carol@example.com")];
+		Assert.equals("bob@example.com\ncarol@example.com", builder.chatId());
+	}
+
+	public function testChatIdOutgoingSingleRecipient() {
+		final builder = new ChatMessageBuilder();
+		builder.direction = MessageSent;
+		builder.from = JID.parse("me@example.com");
+		builder.to = JID.parse("bob@example.com");
+		builder.recipients = [JID.parse("bob@example.com")];
+		Assert.equals("bob@example.com", builder.chatId());
+	}
+
+	public function testChatIdOutgoingRecipientsAreBare() {
+		final builder = new ChatMessageBuilder();
+		builder.direction = MessageSent;
+		builder.from = JID.parse("me@example.com");
+		builder.to = JID.parse("bob@example.com/phone");
+		builder.recipients = [JID.parse("bob@example.com/phone"), JID.parse("carol@example.com/tablet")];
+		Assert.equals("bob@example.com\ncarol@example.com", builder.chatId());
+	}
 }