Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix '/' in room-id or mxid breaking navigation #939

Merged
merged 7 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/domain/navigation/URLRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class URLRouter<T extends {session: string | boolean}> implements IURLRou

openRoomActionUrl(roomId: string): string {
// not a segment to navigation knowns about, so append it manually
const urlPath = `${this._stringifyPath(this._navigation.path.until("session"))}/open-room/${roomId}`;
const urlPath = `${this._stringifyPath(this._navigation.path.until("session"))}/open-room/${encodeURIComponent(roomId)}`;
return this._history.pathAsUrl(urlPath);
}

Expand Down
38 changes: 29 additions & 9 deletions src/domain/navigation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function parseUrlPath(urlPath: string, currentNavPath: Path<SegmentType>,
if (type === "rooms") {
const roomsValue = iterator.next().value;
if (roomsValue === undefined) { break; }
const roomIds = roomsValue.split(",");
const roomIds = roomsValue.split(",").map(id => decodeURIComponent(id));
segments.push(new Segment(type, roomIds));
const selectedIndex = parseInt(iterator.next().value || "0", 10);
const roomId = roomIds[selectedIndex];
Expand All @@ -147,8 +147,9 @@ export function parseUrlPath(urlPath: string, currentNavPath: Path<SegmentType>,
segments.push(new Segment("empty-grid-tile", selectedIndex));
}
} else if (type === "open-room") {
const roomId = iterator.next().value;
let roomId = iterator.next().value;
if (!roomId) { break; }
roomId = decodeURIComponent(roomId);
const rooms = currentNavPath.get("rooms");
if (rooms) {
segments.push(roomsSegmentWithRoom(rooms, roomId, currentNavPath));
Expand Down Expand Up @@ -176,16 +177,21 @@ export function parseUrlPath(urlPath: string, currentNavPath: Path<SegmentType>,
} else if (type === "details" || type === "members") {
pushRightPanelSegment(segments, type);
} else if (type === "member") {
const userId = iterator.next().value;
let userId = iterator.next().value;
if (!userId) { break; }
userId = decodeURIComponent(userId);
pushRightPanelSegment(segments, type, userId);
} else if (type.includes("loginToken")) {
// Special case for SSO-login with query parameter loginToken=<token>
const loginToken = type.split("=").pop();
segments.push(new Segment("sso", loginToken));
} else {
// might be undefined, which will be turned into true by Segment
const value = iterator.next().value;
let value = iterator.next().value;
if (value) {
// decode only if value isn't undefined!
value = decodeURIComponent(value)
}
segments.push(new Segment(type, value));
}
}
Expand All @@ -196,19 +202,20 @@ export function stringifyPath(path: Path<SegmentType>): string {
let urlPath = "";
let prevSegment: Segment<SegmentType> | undefined;
for (const segment of path.segments) {
const encodedSegmentValue = encodeSegmentValue(segment.value);
switch (segment.type) {
case "rooms":
urlPath += `/rooms/${segment.value.join(",")}`;
urlPath += `/rooms/${encodedSegmentValue}`;
break;
case "empty-grid-tile":
urlPath += `/${segment.value}`;
urlPath += `/${encodedSegmentValue}`;
break;
case "room":
if (prevSegment?.type === "rooms") {
const index = prevSegment.value.indexOf(segment.value);
urlPath += `/${index}`;
} else {
urlPath += `/${segment.type}/${segment.value}`;
urlPath += `/${segment.type}/${encodedSegmentValue}`;
}
break;
case "right-panel":
Expand All @@ -217,15 +224,28 @@ export function stringifyPath(path: Path<SegmentType>): string {
continue;
default:
urlPath += `/${segment.type}`;
if (segment.value && segment.value !== true) {
urlPath += `/${segment.value}`;
if (encodedSegmentValue) {
urlPath += `/${encodedSegmentValue}`;
}
}
prevSegment = segment;
}
return urlPath;
}

function encodeSegmentValue(value: SegmentType[keyof SegmentType]): string {
if (value === true) {
// Nothing to encode for boolean
return "";
}
else if (Array.isArray(value)) {
return value.map(v => encodeURIComponent(v)).join(",");
}
else {
return encodeURIComponent(value);
}
}

export function tests() {
function createEmptyPath() {
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
Expand Down
2 changes: 1 addition & 1 deletion src/domain/session/rightpanel/MemberTileViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class MemberTileViewModel extends ViewModel {

get detailsUrl() {
const roomId = this.navigation.path.get("room").value;
return `${this.urlRouter.openRoomActionUrl(roomId)}/member/${this._member.userId}`;
return `${this.urlRouter.openRoomActionUrl(roomId)}/member/${encodeURIComponent(this._member.userId)}`;
}

_updatePreviousName(newName) {
Expand Down