Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 142a71c

Browse files
authored
Merge pull request #3694 from matrix-org/t3chguy/fix_SpecPermalinkConstructor
Fix ?via= args in SpecPermalinkConstructor.js
2 parents 89fdfd7 + a63fbbf commit 142a71c

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/utils/permalinks/SpecPermalinkConstructor.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,16 @@ export default class SpecPermalinkConstructor extends PermalinkConstructor {
7373
// Probably a group, no further parsing needed.
7474
return PermalinkParts.forGroup(entity);
7575
} else if (entity[0] === '#' || entity[0] === '!') {
76-
if (parts.length === 1) {
77-
return PermalinkParts.forRoom(entity, []);
76+
if (parts.length === 1) { // room without event permalink
77+
const [roomId, query=""] = entity.split("?");
78+
const via = query.split(/&?via=/g).filter(p => !!p);
79+
return PermalinkParts.forRoom(roomId, via);
7880
}
7981

8082
// rejoin the rest because v3 events can have slashes (annoyingly)
8183
const eventIdAndQuery = parts.length > 1 ? parts.slice(1).join('/') : "";
82-
const secondaryParts = eventIdAndQuery.split("?");
83-
84-
const eventId = secondaryParts[0];
85-
const query = secondaryParts.length > 1 ? secondaryParts[1] : "";
86-
87-
const via = query.split("via=").filter(p => !!p);
84+
const [eventId, query=""] = eventIdAndQuery.split("?");
85+
const via = query.split(/&?via=/g).filter(p => !!p);
8886

8987
return PermalinkParts.forEvent(entity, eventId, via);
9088
} else {

test/utils/permalinks/Permalinks-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
makeGroupPermalink,
2020
makeRoomPermalink,
2121
makeUserPermalink,
22+
parsePermalink,
2223
RoomPermalinkCreator,
2324
} from "../../../src/utils/permalinks/Permalinks";
2425
import * as testUtils from "../../test-utils";
@@ -450,4 +451,24 @@ describe('Permalinks', function() {
450451
const result = makeGroupPermalink("+community:example.org");
451452
expect(result).toBe("https://matrix.to/#/+community:example.org");
452453
});
454+
455+
it('should correctly parse room permalinks with a via argument', () => {
456+
const result = parsePermalink("https://matrix.to/#/!room_id:server?via=some.org");
457+
expect(result.roomIdOrAlias).toBe("!room_id:server");
458+
expect(result.viaServers).toEqual(["some.org"]);
459+
});
460+
461+
it('should correctly parse room permalink via arguments', () => {
462+
const result = parsePermalink("https://matrix.to/#/!room_id:server?via=foo.bar&via=bar.foo");
463+
expect(result.roomIdOrAlias).toBe("!room_id:server");
464+
expect(result.viaServers).toEqual(["foo.bar", "bar.foo"]);
465+
});
466+
467+
it('should correctly parse event permalink via arguments', () => {
468+
const result = parsePermalink("https://matrix.to/#/!room_id:server/$event_id/some_thing_here/foobar" +
469+
"?via=m1.org&via=m2.org");
470+
expect(result.eventId).toBe("$event_id/some_thing_here/foobar");
471+
expect(result.roomIdOrAlias).toBe("!room_id:server");
472+
expect(result.viaServers).toEqual(["m1.org", "m2.org"]);
473+
});
453474
});

0 commit comments

Comments
 (0)