Append random chars to alias if already taken

Issue #143. Also, show a loading indicator while room creation in progress.
This commit is contained in:
N-Pex 2021-06-29 17:02:40 +02:00
parent 58ef27a765
commit e324355b63
4 changed files with 70 additions and 13 deletions

View file

@ -188,7 +188,15 @@
{{ $t("new_room.link_copied") }}
</div>
-->
<div v-if="status">{{ status }}</div>
<div v-if="status" class="text-center">
<v-progress-circular
v-if="step == steps.CREATING"
indeterminate
color="primary"
size="20"
></v-progress-circular>
{{ status }}
</div>
<!-- </div> -->
</v-fade-transition>
<input
@ -341,28 +349,20 @@ export default {
);
});
},
createRoomDebug() {
this.step = steps.CREATING;
return new Promise((resolve, ignoredreject) => {
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)

View file

@ -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;
}
}

View file

@ -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();

View file

@ -66,6 +66,10 @@ export default {
return null;
},
currentUserHomeServer() {
return User.serverName(this.currentUserId);
},
currentRoomId() {
return this.$store.state.currentRoomId;
},