refactor room alias

Signed-off-by: Timo K <toger5@hotmail.de>
This commit is contained in:
Timo K
2023-10-04 12:01:32 +02:00
parent 309d2e7c96
commit 64e5ff449a

View File

@@ -218,37 +218,39 @@ export function getRoomIdentifierFromUrl(
hash: string hash: string
): RoomIdentifier { ): RoomIdentifier {
let roomAlias: string | null = null; let roomAlias: string | null = null;
pathname = pathname.substring(1); // Strip the "/"
const pathComponents = pathname.split("/");
const pathHasRoom = pathComponents[0] == "room";
const hasRoomAlias = pathComponents.length > 1;
// Here we handle the beginning of the alias and make sure it starts with a "#" // What type is our url: roomAlias in hash, room alias as the search path, roomAlias after /room/
if (hash === "" || hash.startsWith("#?")) { if (hash === "" || hash.startsWith("#?")) {
roomAlias = pathname.substring(1); // Strip the "/" if (hasRoomAlias && pathHasRoom) {
roomAlias = pathComponents[1];
// Delete "/room/", if present
if (roomAlias.startsWith("room")) {
roomAlias = roomAlias.substring("room".length);
} }
// We separate `room` and `/` because in widget mode we use room without providing an alias `.../room#?...` if (!pathHasRoom) {
if (roomAlias.startsWith("/")) { roomAlias = pathComponents[0];
roomAlias = roomAlias.substring("/".length);
}
// Add "#", if not present
if (!roomAlias.startsWith("#")) {
roomAlias = `#${roomAlias}`;
} }
} else { } else {
roomAlias = hash; roomAlias = hash;
} }
// Delete "?" and what comes afterwards // Delete "?" and what comes afterwards
roomAlias = roomAlias.split("?")[0]; roomAlias = roomAlias?.split("?")[0] ?? null;
if (roomAlias.length <= 1) { if (roomAlias) {
// Make roomAlias is null, if it only is a "#" // Make roomAlias is null, if it only is a "#"
roomAlias = null; if (roomAlias.length <= 1) {
} else { roomAlias = null;
// Add server part, if not present } else {
if (!roomAlias.includes(":")) { // Add "#", if not present
roomAlias = `${roomAlias}:${Config.defaultServerName()}`; if (!roomAlias.startsWith("#")) {
roomAlias = `#${roomAlias}`;
}
// Add server part, if not present
if (!roomAlias.includes(":")) {
roomAlias = `${roomAlias}:${Config.defaultServerName()}`;
}
} }
} }