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

Commit 33e2fc2

Browse files
authored
Merge pull request #2254 from matrix-org/travis/permalink-routing-2
Fix and test matrix.to alias permalinks
2 parents 507bfb4 + 0cdc44a commit 33e2fc2

File tree

2 files changed

+135
-5
lines changed

2 files changed

+135
-5
lines changed

src/matrix-to.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,38 @@ export const baseUrl = `https://${host}`;
2424
const MAX_SERVER_CANDIDATES = 3;
2525

2626
export function makeEventPermalink(roomId, eventId) {
27+
const permalinkBase = `${baseUrl}/#/${roomId}/${eventId}`;
28+
29+
// If the roomId isn't actually a room ID, don't try to list the servers.
30+
// Aliases are already routable, and don't need extra information.
31+
if (roomId[0] !== '!') return permalinkBase;
32+
2733
const serverCandidates = pickServerCandidates(roomId);
28-
return `${baseUrl}/#/${roomId}/${eventId}?${encodeServerCandidates(serverCandidates)}`;
34+
return `${permalinkBase}${encodeServerCandidates(serverCandidates)}`;
2935
}
3036

3137
export function makeUserPermalink(userId) {
3238
return `${baseUrl}/#/${userId}`;
3339
}
3440

3541
export function makeRoomPermalink(roomId) {
42+
const permalinkBase = `${baseUrl}/#/${roomId}`;
43+
44+
// If the roomId isn't actually a room ID, don't try to list the servers.
45+
// Aliases are already routable, and don't need extra information.
46+
if (roomId[0] !== '!') return permalinkBase;
47+
3648
const serverCandidates = pickServerCandidates(roomId);
37-
return `${baseUrl}/#/${roomId}?${encodeServerCandidates(serverCandidates)}`;
49+
return `${permalinkBase}${encodeServerCandidates(serverCandidates)}`;
3850
}
3951

4052
export function makeGroupPermalink(groupId) {
4153
return `${baseUrl}/#/${groupId}`;
4254
}
4355

4456
export function encodeServerCandidates(candidates) {
45-
if (!candidates) return '';
46-
return `via=${candidates.map(c => encodeURIComponent(c)).join("&via=")}`;
57+
if (!candidates || candidates.length === 0) return '';
58+
return `?via=${candidates.map(c => encodeURIComponent(c)).join("&via=")}`;
4759
}
4860

4961
export function pickServerCandidates(roomId) {

test/matrix-to-test.js

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ limitations under the License.
1313

1414
import expect from 'expect';
1515
import peg from '../src/MatrixClientPeg';
16-
import {pickServerCandidates} from "../src/matrix-to";
16+
import {
17+
makeEventPermalink,
18+
makeGroupPermalink,
19+
makeRoomPermalink,
20+
makeUserPermalink,
21+
pickServerCandidates,
22+
} from "../src/matrix-to";
1723
import * as testUtils from "./test-utils";
1824

1925

@@ -228,4 +234,116 @@ describe('matrix-to', function() {
228234
expect(pickedServers.length).toBe(1);
229235
expect(pickedServers[0]).toBe("example.org:8448");
230236
});
237+
238+
it('should generate an event permalink for room IDs with no candidate servers', function() {
239+
peg.get().getRoom = () => null;
240+
const result = makeEventPermalink("!somewhere:example.org", "$something:example.com");
241+
expect(result).toBe("https://matrix.to/#/!somewhere:example.org/$something:example.com");
242+
});
243+
244+
it('should generate an event permalink for room IDs with some candidate servers', function() {
245+
peg.get().getRoom = () => {
246+
return {
247+
getJoinedMembers: () => [
248+
{
249+
userId: "@alice:first",
250+
powerLevel: 100,
251+
},
252+
{
253+
userId: "@bob:second",
254+
powerLevel: 0,
255+
},
256+
],
257+
};
258+
};
259+
const result = makeEventPermalink("!somewhere:example.org", "$something:example.com");
260+
expect(result).toBe("https://matrix.to/#/!somewhere:example.org/$something:example.com?via=first&via=second");
261+
});
262+
263+
it('should generate a room permalink for room IDs with no candidate servers', function() {
264+
peg.get().getRoom = () => null;
265+
const result = makeRoomPermalink("!somewhere:example.org");
266+
expect(result).toBe("https://matrix.to/#/!somewhere:example.org");
267+
});
268+
269+
it('should generate a room permalink for room IDs with some candidate servers', function() {
270+
peg.get().getRoom = () => {
271+
return {
272+
getJoinedMembers: () => [
273+
{
274+
userId: "@alice:first",
275+
powerLevel: 100,
276+
},
277+
{
278+
userId: "@bob:second",
279+
powerLevel: 0,
280+
},
281+
],
282+
};
283+
};
284+
const result = makeRoomPermalink("!somewhere:example.org");
285+
expect(result).toBe("https://matrix.to/#/!somewhere:example.org?via=first&via=second");
286+
});
287+
288+
// Technically disallowed but we'll test it anyways
289+
it('should generate an event permalink for room aliases with no candidate servers', function() {
290+
peg.get().getRoom = () => null;
291+
const result = makeEventPermalink("#somewhere:example.org", "$something:example.com");
292+
expect(result).toBe("https://matrix.to/#/#somewhere:example.org/$something:example.com");
293+
});
294+
295+
// Technically disallowed but we'll test it anyways
296+
it('should generate an event permalink for room aliases without candidate servers', function() {
297+
peg.get().getRoom = () => {
298+
return {
299+
getJoinedMembers: () => [
300+
{
301+
userId: "@alice:first",
302+
powerLevel: 100,
303+
},
304+
{
305+
userId: "@bob:second",
306+
powerLevel: 0,
307+
},
308+
],
309+
};
310+
};
311+
const result = makeEventPermalink("#somewhere:example.org", "$something:example.com");
312+
expect(result).toBe("https://matrix.to/#/#somewhere:example.org/$something:example.com");
313+
});
314+
315+
it('should generate a room permalink for room aliases with no candidate servers', function() {
316+
peg.get().getRoom = () => null;
317+
const result = makeRoomPermalink("#somewhere:example.org");
318+
expect(result).toBe("https://matrix.to/#/#somewhere:example.org");
319+
});
320+
321+
it('should generate a room permalink for room aliases without candidate servers', function() {
322+
peg.get().getRoom = () => {
323+
return {
324+
getJoinedMembers: () => [
325+
{
326+
userId: "@alice:first",
327+
powerLevel: 100,
328+
},
329+
{
330+
userId: "@bob:second",
331+
powerLevel: 0,
332+
},
333+
],
334+
};
335+
};
336+
const result = makeRoomPermalink("#somewhere:example.org");
337+
expect(result).toBe("https://matrix.to/#/#somewhere:example.org");
338+
});
339+
340+
it('should generate a user permalink', function() {
341+
const result = makeUserPermalink("@someone:example.org");
342+
expect(result).toBe("https://matrix.to/#/@someone:example.org");
343+
});
344+
345+
it('should generate a group permalink', function() {
346+
const result = makeGroupPermalink("+community:example.org");
347+
expect(result).toBe("https://matrix.to/#/+community:example.org");
348+
});
231349
});

0 commit comments

Comments
 (0)