git » sdk » commit 0c31c6a

publishWithOptions helper

author Stephen Paul Weber
2025-12-16 14:59:02 UTC
committer Stephen Paul Weber
2025-12-16 14:59:02 UTC
parent 423132f82fff1a78d7e450f8f2ac6fd28a5f6263

publishWithOptions helper

XMPP requires us to handle an error and manually set the options, so
let's do that.

borogove/Chat.hx +4 -31
borogove/Client.hx +28 -0

diff --git a/borogove/Chat.hx b/borogove/Chat.hx
index e2fe81b..7ccf84a 100644
--- a/borogove/Chat.hx
+++ b/borogove/Chat.hx
@@ -909,47 +909,20 @@ abstract class Chat {
 	}
 
 	private function publishMds() {
-		stream.sendIq(
+		client.publishWithOptions(
 			new Stanza("iq", { type: "set" })
 				.tag("pubsub", { xmlns: "http://jabber.org/protocol/pubsub" })
 				.tag("publish", { node: "urn:xmpp:mds:displayed:0" })
 				.tag("item", { id: chatId })
 				.tag("displayed", { xmlns: "urn:xmpp:mds:displayed:0"})
 				.tag("stanza-id", { xmlns: "urn:xmpp:sid:0", id: readUpTo(), by: readUpToBy })
-				.up().up().up()
-				.tag("publish-options")
-				.tag("x", { xmlns: "jabber:x:data", type: "submit" })
+				.up().up().up(),
+			new Stanza("x", { xmlns: "jabber:x:data", type: "submit" })
 				.tag("field", { "var": "FORM_TYPE", type: "hidden" }).textTag("value", "http://jabber.org/protocol/pubsub#publish-options").up()
 				.tag("field", { "var": "pubsub#persist_items" }).textTag("value", "true").up()
 				.tag("field", { "var": "pubsub#max_items" }).textTag("value", "max").up()
 				.tag("field", { "var": "pubsub#send_last_published_item" }).textTag("value", "never").up()
-				.tag("field", { "var": "pubsub#access_model" }).textTag("value", "whitelist").up()
-				.up().up(),
-			(response) -> {
-				if (response.attr.get("type") == "error") {
-					final preconditionError = response.getChild("error")?.getChild("precondition-not-met", "http://jabber.org/protocol/pubsub#errors");
-					if (preconditionError != null) {
-						// publish options failed, so force them to be right, what a silly workflow
-						stream.sendIq(
-							new Stanza("iq", { type: "set" })
-								.tag("pubsub", { xmlns: "http://jabber.org/protocol/pubsub#owner" })
-								.tag("configure", { node: "urn:xmpp:mds:displayed:0" })
-								.tag("x", { xmlns: "jabber:x:data", type: "submit" })
-								.tag("field", { "var": "FORM_TYPE", type: "hidden" }).textTag("value", "http://jabber.org/protocol/pubsub#publish-options").up()
-								.tag("field", { "var": "pubsub#persist_items" }).textTag("value", "true").up()
-								.tag("field", { "var": "pubsub#max_items" }).textTag("value", "max").up()
-								.tag("field", { "var": "pubsub#send_last_published_item" }).textTag("value", "never").up()
-								.tag("field", { "var": "pubsub#access_model" }).textTag("value", "whitelist").up()
-								.up().up().up(),
-							(response) -> {
-								if (response.attr.get("type") == "result") {
-									publishMds();
-								}
-							}
-						);
-					}
-				}
-			}
+				.tag("field", { "var": "pubsub#access_model" }).textTag("value", "whitelist").up(),
 		);
 	}
 }
diff --git a/borogove/Client.hx b/borogove/Client.hx
index 69ff722..be701a8 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -1485,6 +1485,34 @@ class Client extends EventEmitter {
 		this.stream.sendIq(query.getQueryStanza(), query.handleResponse);
 	}
 
+	@:allow(borogove)
+	private function publishWithOptions(stanza:Stanza, options:Stanza) {
+		final clone = stanza.clone();
+		clone.findChild("{http://jabber.org/protocol/pubsub}pubsub/publish").tag("publish-options").addChild(options);
+		stream.sendIq(
+			clone,
+			(response) -> {
+				if (response.attr.get("type") == "error") {
+					final preconditionError = response.getChild("error")?.getChild("precondition-not-met", "http://jabber.org/protocol/pubsub#errors");
+					if (preconditionError != null) {
+						// publish options failed, so force them to be right, what a silly workflow
+						stream.sendIq(
+							new Stanza("iq", { type: "set" })
+								.tag("pubsub", { xmlns: "http://jabber.org/protocol/pubsub#owner" })
+								.tag("configure", { node: stanza.findText("{http://jabber.org/protocol/pubsub}pubsub/publish@node") })
+								.addChild(options),
+							(response) -> {
+								if (response.attr.get("type") == "result") {
+									publishWithOptions(stanza, options);
+								}
+							}
+						);
+					}
+				}
+			}
+		);
+	}
+
 	@:allow(borogove)
 	private function sendStanza(stanza:Stanza) {
 		if (stanza.attr.get("id") == null) stanza.attr.set("id", ID.long());