git » sdk » commit 4a22731

Support intant room creation

author Stephen Paul Weber
2026-10-28 20:42:50 UTC
committer Stephen Paul Weber
2026-11-09 14:51:19 UTC
parent 50923459f99729cad30af591b8ddcb46f4f46372

Support intant room creation

Not blindly using the server defaults, but using a small number of
things specified by the user (name and isPrivate)

Also prefill the form with the name for non-instant creation.

borogove/ChannelCreateOptions.hx +0 -72
borogove/Chat.hx +56 -1
borogove/Client.hx +72 -7
borogove/Form.hx +9 -0
borogove/MucSettingsCommand.hx +13 -1

diff --git a/borogove/ChannelCreateOptions.hx b/borogove/ChannelCreateOptions.hx
deleted file mode 100644
index e979233..0000000
--- a/borogove/ChannelCreateOptions.hx
+++ /dev/null
@@ -1,72 +0,0 @@
-package borogove;
-
-using StringTools;
-
-#if cpp
-import HaxeCBridge;
-#end
-
-@:expose
-@:nullSafety(StrictThreaded)
-#if cpp
-@:build(HaxeCBridge.expose())
-@:build(HaxeSwiftBridge.expose())
-#end
-class ChannelCreateOptions {
-	/**
-		Service to create the channel on
-
-		Will use the account default if not set
-	**/
-	public var service: Null<String> = null;
-
-	/**
-		Human-readable name for the channel.
-	**/
-	public var name: Null<String> = null;
-
-	/**
-		Localpart of the room ID
-	**/
-	public var localpart: Null<String> = null;
-
-	/**
-		Whether the room should be members only
-	**/
-	public var isPrivate: Bool = true;
-
-	/**
-		When true, leave the room locked after creation and let the caller
-		submit the full owner configuration form before it is unlocked.
-	**/
-	public var deferConfig: Bool = false;
-
-	public function new() {}
-
-	/**
-		Build the room JID for this channel on a MUC service.
-
-		Uses `localpart` when supplied, otherwise derives one from `name`, and
-		finally falls back to a generated UUID.
-
-		@param service MUC service domain, for example `groups.example.com`
-		@returns the room JID, for example `team@groups.example.com`
-	**/
-	public function jid(service: String) {
-		final requested = localpart;
-		var local = requested != null && requested.trim() != "" ? requested : slug(name);
-		if (local == null || local == "") local = ID.unique();
-
-		return new JID(local, service);
-	}
-
-	private static function slug(name: Null<String>): Null<String> {
-		if (name == null) return null;
-
-		var result = name.trim().toLowerCase();
-		result = ~/[^a-z0-9]+/g.replace(result, "-");
-		result = ~/^-+|-+$/g.replace(result, "");
-
-		return result == "" ? null : result;
-	}
-}
diff --git a/borogove/Chat.hx b/borogove/Chat.hx
index 0b58eb4..0eea49b 100644
--- a/borogove/Chat.hx
+++ b/borogove/Chat.hx
@@ -9,6 +9,7 @@ import borogove.ChatMessage;
 import borogove.Color;
 import borogove.GenericStream;
 import borogove.ID;
+import borogove.Form;
 import borogove.Message;
 import borogove.MessageSync;
 import borogove.Outbox;
@@ -1521,6 +1522,10 @@ class Channel extends Chat {
 	@:allow(borogove)
 	private var joinFailed = null;
 	@:allow(borogove)
+	private var creationFormPrefill: Null<Map<String, Array<String>>> = null;
+	@:allow(borogove)
+	private var creationFormSubmission: Null<Map<String, Array<String>>> = null;
+	@:allow(borogove)
 	private var sync: Null<MessageSync> = null;
 	private var forceLive = false;
 	@:allow(borogove)
@@ -1670,6 +1675,54 @@ class Channel extends Chat {
 		client.trigger("chats/update", [this]);
 	}
 
+	private function maybeConfigureCreation(): Void {
+		if (uiState != Creating || creationFormSubmission == null) return;
+
+		final command = new MucSettingsCommand(
+			client,
+			JID.parse(chatId),
+			() -> completeCreation(),
+			creationFormPrefill
+		);
+		command.execute().then(session -> {
+			if (session.status != "executing" || session.forms.length < 1) {
+				failCreationConfig(session);
+				return null;
+			}
+
+			#if js
+			final submitData = creationFormSubmission;
+			#else
+			final submitData = FormSubmitBuilder.fromMap(creationFormSubmission);
+			#end
+
+			session.execute(null, submitData, 0).then(nextSession -> {
+				if (nextSession.status != "completed") failCreationConfig(nextSession);
+				return null;
+			}, err -> {
+				failCreationConfig(err);
+				return null;
+			});
+			return null;
+		}, err -> {
+			failCreationConfig(err);
+			return null;
+		});
+	}
+
+	private function failCreationConfig(error: Dynamic): Void {
+		if (uiState != Creating) return;
+
+		final stanza: Stanza = Std.isOfType(error, Stanza)
+			? (cast error : Stanza)
+			: new Stanza("presence", { type: "error" })
+				.tag("error", { type: "cancel" })
+				.tag("internal-server-error", { xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas" });
+
+		setJoinFailed(stanza);
+		client.trigger("chats/update", [this]);
+	}
+
 	private function selfPingSuccess() {
 		if (nickInUse() != client.displayName()) {
 			final desiredFullJid = JID.parse(chatId).withResource(client.displayName());
@@ -1798,8 +1851,9 @@ class Channel extends Chat {
 	override public function commands(): Promise<Array<Command>> {
 		return super.commands().then((cmds: Array<Command>) -> {
 			if (isOwner()) {
+				final prefill = uiState == Creating ? creationFormPrefill : null;
 				final onCompleted = uiState == Creating ? () -> completeCreation() : null;
-				cmds.push(new MucSettingsCommand(client, JID.parse(chatId), onCompleted));
+				cmds.push(new MucSettingsCommand(client, JID.parse(chatId), onCompleted, prefill));
 			}
 			return cmds;
 		});
@@ -1867,6 +1921,7 @@ class Channel extends Chat {
 							.tag("conflict", { xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas" })
 					);
 				} else {
+					if (!isJoined) maybeConfigureCreation();
 					isJoined = true;
 				}
 				client.trigger("chats/update", [this]);
diff --git a/borogove/Client.hx b/borogove/Client.hx
index 29add5e..02cfc38 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -1557,24 +1557,71 @@ class Client extends EventEmitter {
 	}
 
 	/**
-		Create a new Channel
+		Create a new unconfigured Channel
 
-		@param options channel creation options
+		The returned channel starts in the `Creating` state and stays locked until
+		the caller submits the muc-settings command.
+
+		@param name human-readable name for the channel
+		@param localpart localpart of the room ID
+		@param service MUC service domain; uses the account default if omitted
+		@returns A promise resolving to the newly created Channel, in the `Creating` state
+	**/
+	public function createChannel(
+		?name: Null<String>,
+		?localpart: Null<String>,
+		?service: Null<String>
+	): Promise<Channel> {
+		return createChannelInternal(name, localpart, service, name == null ? null : ["muc#roomconfig_roomname" => [name]], null);
+	}
+
+	/**
+		Create a new Channel and configure it
+
+		@param name human-readable name for the channel
+		@param isPrivate whether the room should be members only
+		@param localpart localpart of the room ID
+		@param service MUC service domain; uses the account default if omitted
 		@returns A promise resolving to the newly created Channel, in the `Creating` state
 	**/
-	public function createChannel(options: ChannelCreateOptions): Promise<Channel> {
-		return (if (options.service != null) {
-			Promise.resolve(options.service);
+	public function createChannelInstant(
+		?name: Null<String>,
+		isPrivate: Bool = true,
+		?localpart: Null<String>,
+		?service: Null<String>
+	): Promise<Channel> {
+		final submission = new Map<String, Array<String>>();
+		if (name != null && name.trim() != "") {
+			submission.set("muc#roomconfig_roomname", [name]);
+		}
+		submission.set("muc#roomconfig_membersonly", [isPrivate ? "1" : "0"]);
+		submission.set("muc#roomconfig_persistentroom", ["1"]);
+		submission.set("muc#roomconfig_whois", ["anyone"]);
+		submission.set("muc#roomconfig_allowinvites", ["1"]);
+		return createChannelInternal(name, localpart, service, null, submission);
+	}
+
+	private function createChannelInternal(
+		?name: Null<String>,
+		?localpart: Null<String>,
+		?service: Null<String>,
+		?prefill: Null<Map<String, Array<String>>>,
+		?submission: Null<Map<String, Array<String>>>
+	): Promise<Channel> {
+		return (if (service != null) {
+			Promise.resolve(service);
 		} else {
 			persistence.findServicesWithFeature(accountId(), "http://jabber.org/protocol/muc").then(services -> services[0]?.serviceId);
 		}).then((service) -> {
 			if (service == null) throw "No default service on which to make a channel";
 
-			final chatId = options.jid(service).asString();
+			final chatId = channelJid(service, name, localpart).asString();
 			if (getChat(chatId) != null) throw 'Chat already exists: ${chatId}';
 
 			final channel = new Channel(this, this.stream, this.persistence, chatId, Creating, false, false, null);
-			channel.displayName = options.name ?? options.localpart ?? chatId;
+			channel.displayName = name ?? localpart ?? chatId;
+			channel.creationFormPrefill = prefill;
+			channel.creationFormSubmission = submission;
 			channel.join();
 
 			chats.unshift(channel);
@@ -1585,6 +1632,24 @@ class Client extends EventEmitter {
 		});
 	}
 
+	private static function channelJid(service: String, name: Null<String>, localpart: Null<String>): JID {
+		final requested = localpart;
+		var local = requested != null && requested.trim() != "" ? requested : channelSlug(name);
+		if (local == null || local == "") local = ID.unique();
+
+		return new JID(local, service);
+	}
+
+	private static function channelSlug(name: Null<String>): Null<String> {
+		if (name == null) return null;
+
+		var result = name.trim().toLowerCase();
+		result = ~/[^a-z0-9]+/g.replace(result, "-");
+		result = ~/^-+|-+$/g.replace(result, "");
+
+		return result == "" ? null : result;
+	}
+
 	/**
 		Find a chat by id
 
diff --git a/borogove/Form.hx b/borogove/Form.hx
index f03c3f4..e793a94 100644
--- a/borogove/Form.hx
+++ b/borogove/Form.hx
@@ -78,6 +78,15 @@ class FormSubmitBuilder {
 	**/
 	public function new() { }
 
+	@:allow(borogove)
+	private static function fromMap(data: Map<String, Array<String>>): FormSubmitBuilder {
+		final builder = new FormSubmitBuilder();
+		for (k => values in data) {
+			for (v in values) builder.add(k, v);
+		}
+		return builder;
+	}
+
 	/**
 		Add a submitted value for a field.
 
diff --git a/borogove/MucSettingsCommand.hx b/borogove/MucSettingsCommand.hx
index 94d17ba..e1e0713 100644
--- a/borogove/MucSettingsCommand.hx
+++ b/borogove/MucSettingsCommand.hx
@@ -13,11 +13,13 @@ import HaxeCBridge;
 
 class MucSettingsCommand extends Command {
 	private var onCompleted: Null<()->Void>;
+	private var prefill: Null<Map<String, Array<String>>>;
 
 	@:allow(borogove)
-	public function new(client: Client, jid: JID, ?onCompleted: ()->Void) {
+	public function new(client: Client, jid: JID, ?onCompleted: ()->Void, ?prefill: Null<Map<String, Array<String>>>) {
 		super(client, { jid: jid, name: "Settings", node: "muc-settings" });
 		this.onCompleted = onCompleted;
+		this.prefill = prefill;
 	}
 
 	@:allow(borogove.MucSettingsCommandSession)
@@ -27,6 +29,15 @@ class MucSettingsCommand extends Command {
 		if (cb != null) cb();
 	}
 
+	private static function applyPrefill(form: DataForm, prefill: Null<Map<String, Array<String>>>): Void {
+		if (prefill == null) return;
+
+		for (name => values in prefill) {
+			final field = form.field(name);
+			if (field != null) field.value = values;
+		}
+	}
+
 	override public function execute(): Promise<CommandSession> {
 		return new Promise<CommandSession>((resolve, reject) -> {
 			final q = new MucSettingsGet(this.jid.asString());
@@ -43,6 +54,7 @@ class MucSettingsCommand extends Command {
 					return;
 				}
 
+				applyPrefill(form, prefill);
 				resolve(new MucSettingsCommandSession("executing", null, [new FormOption("Submit", "execute"), new FormOption("Cancel", "cancel")], [new Form(form, null)], this));
 			});
 			this.client.sendQuery(q);