diff --git a/src/components/CreateRoom.vue b/src/components/CreateRoom.vue index 9e711fe..97edf25 100644 --- a/src/components/CreateRoom.vue +++ b/src/components/CreateRoom.vue @@ -188,7 +188,15 @@ {{ $t("new_room.link_copied") }} --> -
{{ status }}
+
+ + {{ status }} +
{ - setTimeout(() => { - this.step = steps.CREATED; - resolve("#NpexPublicRoom2:neo.keanu.im"); - }, 5000); - }); - }, createRoom() { this.step = steps.CREATING; const hasUser = this.currentUser ? true : false; var setProfileData = false; + var uniqueAliasPromise = Promise.resolve(true); + var roomId; this.status = this.$t("new_room.status_creating"); var createRoomOptions = {}; if (this.joinRule == "public") { createRoomOptions = { visibility: "private", // Not listed! - room_alias_name: this.roomName.replace(/\s/g, "").toLowerCase(), name: this.roomName, preset: "public_chat", initial_state: [ @@ -375,6 +375,18 @@ export default { }, ], }; + + // Promise to get a unique alias and use it in room creation options. + // + uniqueAliasPromise = util + .getUniqueAliasForRoomName( + this.$matrix.matrixClient, + this.roomName, + this.$matrix.currentUserHomeServer + ) + .then((alias) => { + createRoomOptions.room_alias_name = alias; + }); } else { //if (this.joinRule == "invite") { createRoomOptions = { @@ -457,6 +469,9 @@ export default { } }.bind(this) ) + .then(() => { + return uniqueAliasPromise; + }) .then(() => { return this.$matrix.matrixClient .createRoom(createRoomOptions) diff --git a/src/models/user.js b/src/models/user.js index 125ac9c..6217ec3 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -6,7 +6,7 @@ export default class User { this.is_guest = is_guest || false } - normalize = function() { + normalize = function () { if (this.user_id.startsWith('@') && this.user_id.includes(':')) { const parts = this.user_id.split(":"); this.user_id = parts[0].substring(1); @@ -14,7 +14,7 @@ export default class User { } }; - static homeServerUrl = function(home_server) { + static homeServerUrl = function (home_server) { if (home_server && !home_server.startsWith("https://")) { return "https://" + home_server; } @@ -28,4 +28,12 @@ export default class User { } return user_id; } + + static serverName(user_id) { + if (user_id && user_id.startsWith('@') && user_id.includes(':')) { + const parts = user_id.split(":"); + return parts[1]; + } + return user_id; + } } \ No newline at end of file diff --git a/src/plugins/utils.js b/src/plugins/utils.js index 0afbd6d..ab3709f 100644 --- a/src/plugins/utils.js +++ b/src/plugins/utils.js @@ -580,6 +580,36 @@ class Util { browserCanRecordAudio() { return _browserCanRecordAudio; } + + getUniqueAliasForRoomName(matrixClient, roomName, homeServer, iterationCount) { + return new Promise((resolve, reject) => { + var preferredAlias = roomName.replace(/\s/g, "").toLowerCase(); + var tryAlias = "#" + preferredAlias + ":" + homeServer; + matrixClient.getRoomIdForAlias(tryAlias) + .then(ignoredid => { + // We got a response, this means the tryAlias already exists. + // Try again, with appended random chars + if (iterationCount) { + // Not the first time around. Reached max tries? + if (iterationCount == 5) { + reject("Failed to get unique room alias"); + return; + } + // Else, strip random chars from end so we can try again + roomName = roomName.substring(0, roomName.length - 5); + } + const randomChars = this.randomString(4, "abcdefghijklmnopqrstuvwxyz0123456789"); + resolve(this.getUniqueAliasForRoomName(matrixClient, roomName + "-" + randomChars, homeServer, (iterationCount || 0) + 1)) + }) + .catch(err => { + if (err.errcode == 'M_NOT_FOUND') { + resolve(preferredAlias); + } else { + reject(err); + } + }) + }); + } } export default new Util(); diff --git a/src/services/matrix.service.js b/src/services/matrix.service.js index 45b95a3..9190fd3 100644 --- a/src/services/matrix.service.js +++ b/src/services/matrix.service.js @@ -66,6 +66,10 @@ export default { return null; }, + currentUserHomeServer() { + return User.serverName(this.currentUserId); + }, + currentRoomId() { return this.$store.state.currentRoomId; },