git » sdk » commit 73ee8b3

Initial channel creation

author Stephen Paul Weber
2026-10-28 19:34:47 UTC
committer Stephen Paul Weber
2026-11-09 14:51:19 UTC
parent 1800012039407818d09885236308844bc73fcedd

Initial channel creation

Create a new Channel from some options and set it to a "Creating" state,
start the creation process with the server.

Do not store a "Creating" channel in the database. It does not yet have
a way to complete the process.

borogove/ChannelCreateOptions.hx +72 -0
borogove/Chat.hx +87 -52
borogove/Client.hx +31 -1

diff --git a/borogove/ChannelCreateOptions.hx b/borogove/ChannelCreateOptions.hx
new file mode 100644
index 0000000..e979233
--- /dev/null
+++ b/borogove/ChannelCreateOptions.hx
@@ -0,0 +1,72 @@
+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 202d3fc..6d169a0 100644
--- a/borogove/Chat.hx
+++ b/borogove/Chat.hx
@@ -77,6 +77,7 @@ enum abstract UiState(Int) {
 	var Open; // or Unspecified
 	var Closed; // Archived
 	var Invited;
+	var Creating; // Room creation in progress
 }
 
 /**
@@ -1564,7 +1565,7 @@ class Channel extends Chat {
 
 	@:allow(borogove)
 	private function selfPing(refresh: Bool) {
-		if (uiState == Invited) return;
+		if (uiState == Invited || uiState == Creating) return;
 
 		if (uiState == Closed) {
 			client.channelPinger.remove(this);
@@ -1634,12 +1635,28 @@ class Channel extends Chat {
 					return stanza;
 				}
 			);
-			persistence.syncPoint(client.accountId(), chatId).then((point) -> doSync(point));
+			if (uiState != Creating) persistence.syncPoint(client.accountId(), chatId).then((point) -> doSync(point));
 
 			return null;
 		});
 	}
 
+	@:allow(borogove)
+	private function setJoinFailed(stanza: Stanza): Void {
+		joinFailed = stanza;
+		if (uiState == Creating) {
+			uiState = Closed;
+			client.sendPresence(
+				getFullJid().asString(),
+				(stanza) -> {
+					stanza.attr.set("type", "unavailable");
+					return stanza;
+				}
+			);
+			disconnected();
+		}
+	}
+
 	private function selfPingSuccess() {
 		if (nickInUse() != client.displayName()) {
 			final desiredFullJid = JID.parse(chatId).withResource(client.displayName());
@@ -1728,6 +1745,7 @@ class Channel extends Chat {
 	}
 
 	override public function canSend() {
+		if (uiState == Creating) return false;
 		if (!super.canSend()) return false;
 
 		final it = self.presence.iterator();
@@ -1825,61 +1843,77 @@ class Channel extends Chat {
 	private function setPresence(resource:String, presence:Presence, noStore:Bool) {
 		final oneTen = presence?.mucUser?.statusCodes?.find((status) -> status == "110");
 		final member = buildMember(resource, presence);
-		if (presence.mucUser != null && oneTen == null && member.isSelf) {
-			// ejabberd sends presence updates for self without 110
-			final mucUser: Stanza = presence.mucUser;
-			mucUser.tag("status", { code: "110" }).up();
-			setPresence(resource, presence, noStore);
-			return;
-		}
-		final occupantId = (presence : Stanza).getChild("occupant-id", "urn:xmpp:occupant-id:0")?.attr?.get("id");
-		if (!noStore && !(disco.features.contains("urn:xmpp:occupant-id:0") && member.id != chatId + "/" + occupantId)) {
-			if (presence.type == "unavailable" && member.photoUri == null) {
-				final memberUpdates = MemberUpdate.extractUpdates(client.accountId(), this, presence);
-				persistence.storeMemberUpdates(client.accountId(), this, memberUpdates, false).then(members -> {
-					trigger("members/update", members);
-				});
-			} else {
-				persistence.storeMembers(client.accountId(), chatId, [member]).then(_ -> {
-					trigger("members/update", [member]);
-				});
+		if (uiState == Creating) {
+			if (member.isSelf) {
+				self = member;
+				if (!isJoined && (presence.mucUser?.statusCodes ?? []).indexOf("201") < 0) {
+					setJoinFailed(
+						new Stanza("presence", { type: "error" })
+							.tag("error", { type: "cancel" })
+							.tag("conflict", { xmlns: "urn:ietf:params:xml:ns:xmpp-stanzas" })
+					);
+				} else {
+					isJoined = true;
+				}
+				client.trigger("chats/update", [this]);
 			}
-		}
-		final mucUser: MucUser = (presence : Stanza).getChild("x", "http://jabber.org/protocol/muc#user");
-		if (mucUser != null) {
-			final mav = mucUser.mav;
-			if (mav?.attr?.get("since") != null && mav?.attr?.get("since") != mavUntil) {
-				trace("MAV update with unknown previous version", mavUntil, presence);
+		} else {
+			if (presence.mucUser != null && oneTen == null && member.isSelf) {
+				// ejabberd sends presence updates for self without 110
+				final mucUser: Stanza = presence.mucUser;
+				mucUser.tag("status", { code: "110" }).up();
+				setPresence(resource, presence, noStore);
+				return;
 			}
-			if (mav?.attr?.get("until") != null && mavUntil != mav?.attr?.get("until")) {
-				mavUntil = mav?.attr?.get("until");
-				if (!noStore) persistence.storeChats(client.accountId(), [this]);
+			final occupantId = (presence : Stanza).getChild("occupant-id", "urn:xmpp:occupant-id:0")?.attr?.get("id");
+			if (!noStore && !(disco.features.contains("urn:xmpp:occupant-id:0") && member.id != chatId + "/" + occupantId)) {
+				if (presence.type == "unavailable" && member.photoUri == null) {
+					final memberUpdates = MemberUpdate.extractUpdates(client.accountId(), this, presence);
+					persistence.storeMemberUpdates(client.accountId(), this, memberUpdates, false).then(members -> {
+						trigger("members/update", members);
+					});
+				} else {
+					persistence.storeMembers(client.accountId(), chatId, [member]).then(_ -> {
+						trigger("members/update", [member]);
+					});
+				}
 			}
-			if (!noStore && mucUser.role != "visitor" && mucUser.jid != null) {
-				// If they're not a visitor they can't be requesting voice
-				persistence.storeVoiceRequest(client.accountId(), this, mucUser.jid.asBare().asString(), false).then(_ -> {
-					client.trigger("chats/update", [this]);
-				});
+			final mucUser: MucUser = (presence : Stanza).getChild("x", "http://jabber.org/protocol/muc#user");
+			if (mucUser != null) {
+				final mav = mucUser.mav;
+				if (mav?.attr?.get("since") != null && mav?.attr?.get("since") != mavUntil) {
+					trace("MAV update with unknown previous version", mavUntil, presence);
+				}
+				if (mav?.attr?.get("until") != null && mavUntil != mav?.attr?.get("until")) {
+					mavUntil = mav?.attr?.get("until");
+					if (!noStore) persistence.storeChats(client.accountId(), [this]);
+				}
+				if (!noStore && mucUser.role != "visitor" && mucUser.jid != null) {
+					// If they're not a visitor they can't be requesting voice
+					persistence.storeVoiceRequest(client.accountId(), this, mucUser.jid.asBare().asString(), false).then(_ -> {
+						client.trigger("chats/update", [this]);
+					});
+				}
 			}
-		}
-		if (member.isSelf) {
-			if (presence.type == "unavailable") {
-				disconnected();
-			} else {
-				joined(member);
+			if (member.isSelf) {
+				if (presence.type == "unavailable") {
+					disconnected();
+				} else {
+					joined(member);
+				}
+				if (!noStore) client.trigger("chats/update", [this]);
+			}
+			if (!member.isSelf && member.id != chatId && membersForName != null) {
+				membersForName = membersForName.filter(m -> m.id != member.id);
+				membersForName.push({ id: member.id, displayName: member.displayName });
+				membersForName.sort((a, b) -> Reflect.compare(a.displayName, b.displayName));
+				if (membersForName.length > 20) membersForName = null;
+				if (displayName == chatId && !noStore) client.trigger("chats/update", [this]);
+			}
+			final tripleThree = presence?.mucUser?.statusCodes?.find((status) -> status == "333");
+			if (member.isSelf && tripleThree != null) {
+				haxe.Timer.delay(() -> selfPing(true), 5000);
 			}
-			if (!noStore) client.trigger("chats/update", [this]);
-		}
-		if (!member.isSelf && member.id != chatId && membersForName != null) {
-			membersForName = membersForName.filter(m -> m.id != member.id);
-			membersForName.push({ id: member.id, displayName: member.displayName });
-			membersForName.sort((a, b) -> Reflect.compare(a.displayName, b.displayName));
-			if (membersForName.length > 20) membersForName = null;
-			if (displayName == chatId && !noStore) client.trigger("chats/update", [this]);
-		}
-		final tripleThree = presence?.mucUser?.statusCodes?.find((status) -> status == "333");
-		if (member.isSelf && tripleThree != null) {
-			haxe.Timer.delay(() -> selfPing(true), 5000);
 		}
 	}
 
@@ -2115,6 +2149,7 @@ class Channel extends Chat {
 	}
 
 	override public function syncing() {
+		if (uiState == Creating) return true;
 		if (joinFailed != null) return false;
 
 		return sync != null || !livePresence();
diff --git a/borogove/Client.hx b/borogove/Client.hx
index 7b30f6f..29add5e 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -471,7 +471,7 @@ class Client extends EventEmitter {
 				final channel = Std.downcast(chat, Channel);
 				if (channel != null && from.asString() == channel.getFullJid().asString()) {
 					trace("Join failed", channel.chatId, stanza);
-					channel.joinFailed = stanza;
+					channel.setJoinFailed(stanza);
 					this.trigger("chats/update", [channel]);
 				}
 			}
@@ -1135,6 +1135,7 @@ class Client extends EventEmitter {
 				// We resumed from disk so these must have been synced before
 				// and will get anything since live
 				for (channel in getChannels()) {
+					if (channel.uiState == Creating) continue;
 					channel.inSync = channel.livePresence() && channel.sortId != null;
 					trace(
 						"SYNC: channel inSync",
@@ -1555,6 +1556,35 @@ class Client extends EventEmitter {
 		return chat;
 	}
 
+	/**
+		Create a new Channel
+
+		@param options channel creation options
+		@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);
+		} 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();
+			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.join();
+
+			chats.unshift(channel);
+			chatsIndex[channel.chatId] = channel;
+			this.trigger("chats/update", [channel]);
+
+			return channel;
+		});
+	}
+
 	/**
 		Find a chat by id