Move route splitting inside route parsing
This commit is contained in:
parent
00059c49ab
commit
fd7c50f683
2 changed files with 13 additions and 9 deletions
|
@ -75,11 +75,19 @@ export default class ElementPermalinkConstructor extends PermalinkConstructor {
|
||||||
throw new Error("Does not appear to be a permalink");
|
throw new Error("Does not appear to be a permalink");
|
||||||
}
|
}
|
||||||
|
|
||||||
const parts = fullUrl.substring(`${this._elementUrl}/#/`.length).split("/");
|
const parts = fullUrl.substring(`${this._elementUrl}/#/`.length);
|
||||||
return ElementPermalinkConstructor.parseLinkParts(parts);
|
return ElementPermalinkConstructor.parseAppRoute(parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
static parseLinkParts(parts: string[]): PermalinkParts {
|
/**
|
||||||
|
* Parses an app route (`(user|room|group)/identifer`) to a Matrix entity
|
||||||
|
* (room, user, group).
|
||||||
|
* @param {string} route The app route
|
||||||
|
* @returns {PermalinkParts}
|
||||||
|
*/
|
||||||
|
static parseAppRoute(route: string): PermalinkParts {
|
||||||
|
const parts = route.split("/");
|
||||||
|
|
||||||
if (parts.length < 2) { // we're expecting an entity and an ID of some kind at least
|
if (parts.length < 2) { // we're expecting an entity and an ID of some kind at least
|
||||||
throw new Error("URL is missing parts");
|
throw new Error("URL is missing parts");
|
||||||
}
|
}
|
||||||
|
@ -93,10 +101,6 @@ export default class ElementPermalinkConstructor extends PermalinkConstructor {
|
||||||
// Probably a group, no further parsing needed.
|
// Probably a group, no further parsing needed.
|
||||||
return PermalinkParts.forGroup(entity);
|
return PermalinkParts.forGroup(entity);
|
||||||
} else if (entityType === 'room') {
|
} else if (entityType === 'room') {
|
||||||
if (parts.length === 2) {
|
|
||||||
return PermalinkParts.forRoom(entity, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
// rejoin the rest because v3 events can have slashes (annoyingly)
|
// rejoin the rest because v3 events can have slashes (annoyingly)
|
||||||
const eventIdAndQuery = parts.length > 2 ? parts.slice(2).join('/') : "";
|
const eventIdAndQuery = parts.length > 2 ? parts.slice(2).join('/') : "";
|
||||||
const secondaryParts = eventIdAndQuery.split("?");
|
const secondaryParts = eventIdAndQuery.split("?");
|
||||||
|
|
|
@ -414,8 +414,8 @@ export function parsePermalink(fullUrl: string): PermalinkParts {
|
||||||
*/
|
*/
|
||||||
export function parseAppLocalLink(localLink: string): PermalinkParts {
|
export function parseAppLocalLink(localLink: string): PermalinkParts {
|
||||||
try {
|
try {
|
||||||
const segments = localLink.replace("#/", "").split("/");
|
const segments = localLink.replace("#/", "");
|
||||||
return ElementPermalinkConstructor.parseLinkParts(segments);
|
return ElementPermalinkConstructor.parseAppRoute(segments);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Ignore failures
|
// Ignore failures
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue