git » sdk » commit 9617e1a

Use a shared helper for adding extended addressing

author Stephen Paul Weber
2026-11-08 19:32:30 UTC
committer Stephen Paul Weber
2026-11-16 14:55:03 UTC
parent c3fa3777c5b75295a701b4800f8dd47e7b823568

Use a shared helper for adding extended addressing

borogove/Chat.hx +1 -8
borogove/ChatMessage.hx +2 -7
borogove/Util.hx +11 -0
test/TestChat.hx +64 -0

diff --git a/borogove/Chat.hx b/borogove/Chat.hx
index be0e678..797cb4d 100644
--- a/borogove/Chat.hx
+++ b/borogove/Chat.hx
@@ -1370,7 +1370,7 @@ class DirectChat extends Chat {
 		final update = new ReactionUpdate(ID.unique(), null, null, m.localId, m.chatId(), client.accountId(), Date.format(std.Date.now()), reactions, EmojiReactions);
 		final outboxItem = outbox.newItem();
 		persistence.storeReaction(client.accountId(), update).then((stored) -> {
-			sendMessageStanza(update.asStanza(), outboxItem);
+			sendMessageStanza(Util.addExtendedAddressing(update.asStanza(), _fullCounterparts.map((p) -> JID.parse(p))), outboxItem);
 			if (stored != null) client.notifyMessageHandlers(stored, ReactionEvent);
 		});
 	}
@@ -1384,13 +1384,6 @@ class DirectChat extends Chat {
 		thenshim.PromiseTools.all(counters.map(counterpart -> {
 			final clone = stanza.clone();
 			clone.attr.set("to", counterpart);
-			if (counters.length > 1 && stanza.getChild("addresses", "http://jabber.org/protocol/address") == null) {
-				final addresses = clone.tag("addresses", { xmlns: "http://jabber.org/protocol/address" });
-				for (counter in counters) {
-					addresses.tag("address", { type: "to", jid: counter, delivered: "true" }).up();
-				}
-				addresses.up();
-			}
 
 			// Default should try all preferences in order?
 			// Otherwise force to the one we were passed?
diff --git a/borogove/ChatMessage.hx b/borogove/ChatMessage.hx
index f9287d9..12fe6fe 100644
--- a/borogove/ChatMessage.hx
+++ b/borogove/ChatMessage.hx
@@ -640,13 +640,8 @@ class ChatMessage {
 		var stanza = new Stanza("message", attrs);
 		if (versions.length > 0 && versions[versions.length-1].localId != null) stanza.tag("replace", { xmlns: "urn:xmpp:message-correct:0", id: versions[versions.length-1].localId }).up();
 		if (threadId != null) stanza.textTag("thread", threadId);
-		if (recipients.length > 1) {
-			final addresses = stanza.tag("addresses", { xmlns: "http://jabber.org/protocol/address" });
-			for (recipient in recipients) {
-				addresses.tag("address", { type: "to", jid: recipient.asString(), delivered: "true" }).up();
-			}
-			addresses.up();
-		} else if (recipients.length == 1 && to == null) {
+		Util.addExtendedAddressing(stanza, recipients);
+		if (recipients.length == 1 && to == null) {
 			attrs.set("to", recipients[0].asString());
 		}
 
diff --git a/borogove/Util.hx b/borogove/Util.hx
index d593b03..fa132f0 100644
--- a/borogove/Util.hx
+++ b/borogove/Util.hx
@@ -1,6 +1,7 @@
 package borogove;
 
 import haxe.io.Bytes;
+import haxe.ds.ReadOnlyArray;
 import thenshim.Promise;
 #if js
 import js.html.TextEncoder;
@@ -121,6 +122,16 @@ function xmlEscape(s: String) {
 	return StringTools.replace(StringTools.replace(StringTools.replace(s, "&", "&amp;"), "<", "&lt;"), ">", "&gt;");
 }
 
+function addExtendedAddressing(stanza: Stanza, recipients: ReadOnlyArray<JID>) {
+	if (recipients.length < 2) return stanza;
+	final addresses = stanza.tag("addresses", { xmlns: "http://jabber.org/protocol/address" });
+	for (recipient in recipients) {
+		addresses.tag("address", { type: "to", jid: recipient.asString(), delivered: "true" }).up();
+	}
+	addresses.up();
+	return stanza;
+}
+
 // Taken from StringTools but modified not to decode + as space
 #if (flash || js || cs || python) inline #end function uriDecode(s:String):String {
 	#if flash
diff --git a/test/TestChat.hx b/test/TestChat.hx
index fe9751d..3d4c897 100644
--- a/test/TestChat.hx
+++ b/test/TestChat.hx
@@ -1328,6 +1328,70 @@ class TestChat extends utest.Test {
 			});
 		});
 	}
+
+	public function testRemoveReactionExtendedMultiDMAddsAddressing(async: Async) {
+		final persistence = new RecordingPersistence();
+		final client = new Client("me@example.com", persistence);
+		final chat = client.getDirectChat("bob@example.com\ncarol@example.com");
+		final sent: Array<Stanza> = [];
+
+		client.stream.on("sendStanza", (stanza: Stanza) -> {
+			if (stanza.name != "message" || stanza.getChild("reactions", "urn:xmpp:reactions:0") == null) 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);
+			for (s in sent) {
+				final addresses = s.getChild("addresses", "http://jabber.org/protocol/address");
+				Assert.notNull(addresses);
+				if (addresses != null) {
+					final jids = addresses.allTags("address").map(a -> a.attr.get("jid"));
+					jids.sort(Reflect.compare);
+					Assert.same(["bob@example.com", "carol@example.com"], jids);
+				}
+			}
+			async.done();
+			return EventHandled;
+		});
+
+		chat.removeReaction(reactionTarget(), borogove.Reaction.unicode("👍"));
+	}
+
+	public function testRemoveReactionCompatMultiDMAddsAddressing(async: Async) {
+		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"];
+
+		client.stream.on("sendStanza", (stanza: Stanza) -> {
+			if (stanza.name != "message" || stanza.getChild("reactions", "urn:xmpp:reactions:0") == null) return EventUnhandled;
+			Assert.equals(routing, 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")));
+			}
+			async.done();
+			return EventHandled;
+		});
+
+		chat.removeReaction(reactionTarget(), borogove.Reaction.unicode("👍"));
+	}
+
+	private function reactionTarget():borogove.ChatMessage {
+		final builder = new ChatMessageBuilder();
+		builder.localId = "m1";
+		builder.sortId = "a0";
+		builder.direction = MessageReceived;
+		builder.from = JID.parse("bob@example.com");
+		builder.senderId = "bob@example.com";
+		builder.to = JID.parse("me@example.com");
+		builder.text = "hi";
+		return builder.build();
+	}
 }
 
 @:access(borogove)