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:
parent
58ef27a765
commit
e324355b63
4 changed files with 70 additions and 13 deletions
|
|
@ -188,7 +188,15 @@
|
||||||
{{ $t("new_room.link_copied") }}
|
{{ $t("new_room.link_copied") }}
|
||||||
</div>
|
</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> -->
|
<!-- </div> -->
|
||||||
</v-fade-transition>
|
</v-fade-transition>
|
||||||
<input
|
<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() {
|
createRoom() {
|
||||||
this.step = steps.CREATING;
|
this.step = steps.CREATING;
|
||||||
|
|
||||||
const hasUser = this.currentUser ? true : false;
|
const hasUser = this.currentUser ? true : false;
|
||||||
var setProfileData = false;
|
var setProfileData = false;
|
||||||
|
|
||||||
|
var uniqueAliasPromise = Promise.resolve(true);
|
||||||
|
|
||||||
var roomId;
|
var roomId;
|
||||||
this.status = this.$t("new_room.status_creating");
|
this.status = this.$t("new_room.status_creating");
|
||||||
var createRoomOptions = {};
|
var createRoomOptions = {};
|
||||||
if (this.joinRule == "public") {
|
if (this.joinRule == "public") {
|
||||||
createRoomOptions = {
|
createRoomOptions = {
|
||||||
visibility: "private", // Not listed!
|
visibility: "private", // Not listed!
|
||||||
room_alias_name: this.roomName.replace(/\s/g, "").toLowerCase(),
|
|
||||||
name: this.roomName,
|
name: this.roomName,
|
||||||
preset: "public_chat",
|
preset: "public_chat",
|
||||||
initial_state: [
|
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 {
|
} else {
|
||||||
//if (this.joinRule == "invite") {
|
//if (this.joinRule == "invite") {
|
||||||
createRoomOptions = {
|
createRoomOptions = {
|
||||||
|
|
@ -457,6 +469,9 @@ export default {
|
||||||
}
|
}
|
||||||
}.bind(this)
|
}.bind(this)
|
||||||
)
|
)
|
||||||
|
.then(() => {
|
||||||
|
return uniqueAliasPromise;
|
||||||
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return this.$matrix.matrixClient
|
return this.$matrix.matrixClient
|
||||||
.createRoom(createRoomOptions)
|
.createRoom(createRoomOptions)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ export default class User {
|
||||||
this.is_guest = is_guest || false
|
this.is_guest = is_guest || false
|
||||||
}
|
}
|
||||||
|
|
||||||
normalize = function() {
|
normalize = function () {
|
||||||
if (this.user_id.startsWith('@') && this.user_id.includes(':')) {
|
if (this.user_id.startsWith('@') && this.user_id.includes(':')) {
|
||||||
const parts = this.user_id.split(":");
|
const parts = this.user_id.split(":");
|
||||||
this.user_id = parts[0].substring(1);
|
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://")) {
|
if (home_server && !home_server.startsWith("https://")) {
|
||||||
return "https://" + home_server;
|
return "https://" + home_server;
|
||||||
}
|
}
|
||||||
|
|
@ -28,4 +28,12 @@ export default class User {
|
||||||
}
|
}
|
||||||
return user_id;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -580,6 +580,36 @@ class Util {
|
||||||
browserCanRecordAudio() {
|
browserCanRecordAudio() {
|
||||||
return _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();
|
export default new Util();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,6 +66,10 @@ export default {
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
currentUserHomeServer() {
|
||||||
|
return User.serverName(this.currentUserId);
|
||||||
|
},
|
||||||
|
|
||||||
currentRoomId() {
|
currentRoomId() {
|
||||||
return this.$store.state.currentRoomId;
|
return this.$store.state.currentRoomId;
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue