git » sdk » commit f5761b5

Put routing back for channels

author Stephen Paul Weber
2026-11-16 18:20:55 UTC
committer Stephen Paul Weber
2026-11-16 18:48:07 UTC
parent d6d16eb45a3883fcc61c397782408ab394f1c0d6

Put routing back for channels

borogove/ChatMessage.hx +1 -1
borogove/ChatMessageBuilder.hx +1 -1
test/TestChat.hx +27 -0
test/TestChatMessage.hx +17 -0

diff --git a/borogove/ChatMessage.hx b/borogove/ChatMessage.hx
index 12fe6fe..958d5da 100644
--- a/borogove/ChatMessage.hx
+++ b/borogove/ChatMessage.hx
@@ -505,7 +505,7 @@ class ChatMessage {
 			// 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();
+			if (type != MessageChannel && type != MessageChannelPrivate && !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 919d1d4..d39666d 100644
--- a/borogove/ChatMessageBuilder.hx
+++ b/borogove/ChatMessageBuilder.hx
@@ -380,7 +380,7 @@ class ChatMessageBuilder {
 			return replyTo.map((r) -> r.asBare().asString()).join("\n");
 		} else {
 			final toJid = to;
-			if (toJid != null) {
+			if (toJid != null && type != MessageChannel && type != MessageChannelPrivate) {
 				final bareTo = toJid.asBare();
 				if (!recipients.exists((r) -> r.asBare().equals(bareTo))) return bareTo.asString();
 			}
diff --git a/test/TestChat.hx b/test/TestChat.hx
index 79a878f..1953398 100644
--- a/test/TestChat.hx
+++ b/test/TestChat.hx
@@ -549,6 +549,33 @@ class TestChat extends utest.Test {
 		chat.setSubject("New Thread Subject", "thread123");
 	}
 
+	public function testOwnChannelMessageEchoStaysInChannel() {
+		final persistence = new Dummy();
+		final client = new Client("test@example.com", persistence);
+		final channel = new Channel(client, client.stream, persistence, "room@example.com");
+		channel.sortId = "a ";
+		client.chats.push(channel);
+
+		// MUC echoes our own message back to us, so `to` is ourselves but the
+		// message still belongs to the channel
+		final stanza = new Stanza("message", {
+			xmlns: "jabber:client",
+			from: "room@example.com/test",
+			to: "test@example.com/device",
+			id: "localid",
+			type: "groupchat"
+		}).textTag("body", "hi");
+
+		final message = borogove.Message.fromStanza(stanza, client.jid, (builder, s) -> channel.prepareIncomingMessage(builder, s));
+		switch (message.parsed) {
+			case ChatMessageStanza(m):
+				Assert.equals("room@example.com", m.chatId());
+				Assert.equals("room@example.com", message.chatId);
+			default:
+				Assert.fail("Expected ChatMessageStanza");
+		}
+	}
+
 	public function testChannelPlaceholderSelf(async: Async) {
 		final persistence = new Dummy();
 		final client = new Client("test@example.com", persistence);
diff --git a/test/TestChatMessage.hx b/test/TestChatMessage.hx
index d681ef5..1285987 100644
--- a/test/TestChatMessage.hx
+++ b/test/TestChatMessage.hx
@@ -484,6 +484,23 @@ class TestChatMessage extends utest.Test {
 		Assert.equals("bob@example.com", msg.chatId());
 	}
 
+	public function testChatIdOutgoingChannelEchoUsesChannelNotTo() {
+		// Our own channel message echoed back to us: `to` is ourselves, the
+		// channel is in recipients, so the chat id must be the channel
+		final msg = new ChatMessage({
+			type: MessageChannel,
+			timeSent: "2026-01-01T00:00:00Z",
+			timeReceived: "2026-01-01T00:00:00Z",
+			to: JID.parse("me@example.com/phone"),
+			from: JID.parse("room@example.com/me"),
+			senderId: "room@example.com/me",
+			recipients: [JID.parse("room@example.com")],
+			replyTo: [JID.parse("room@example.com")],
+			direction: MessageSent
+		});
+		Assert.equals("room@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());