Merge pull request #2963 from matrix-org/travis/permalink-without-room

Support a backup room ID in PermalinkCreator
pull/21833/head
Travis Ralston 2019-05-14 10:28:26 -06:00 committed by GitHub
commit 92c67cdcab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 20 deletions

View File

@ -49,7 +49,7 @@ module.exports = React.createClass({
return <div />; // We should never have been instaniated in this case return <div />; // We should never have been instaniated in this case
} }
const prevRoom = MatrixClientPeg.get().getRoom(predecessor['room_id']); const prevRoom = MatrixClientPeg.get().getRoom(predecessor['room_id']);
const permalinkCreator = new RoomPermalinkCreator(prevRoom); const permalinkCreator = new RoomPermalinkCreator(prevRoom, predecessor['room_id']);
permalinkCreator.load(); permalinkCreator.load();
const predecessorPermalink = permalinkCreator.forEvent(predecessor['event_id']); const predecessorPermalink = permalinkCreator.forEvent(predecessor['event_id']);
return <div className="mx_CreateEvent"> return <div className="mx_CreateEvent">

View File

@ -70,8 +70,12 @@ const MAX_SERVER_CANDIDATES = 3;
// the list and magically have the link work. // the list and magically have the link work.
export class RoomPermalinkCreator { export class RoomPermalinkCreator {
constructor(room) { // We support being given a roomId as a fallback in the event the `room` object
// doesn't exist or is not healthy for us to rely on. For example, loading a
// permalink to a room which the MatrixClient doesn't know about.
constructor(room, roomId=null) {
this._room = room; this._room = room;
this._roomId = room ? room.roomId : roomId;
this._highestPlUserId = null; this._highestPlUserId = null;
this._populationMap = null; this._populationMap = null;
this._bannedHostsRegexps = null; this._bannedHostsRegexps = null;
@ -79,6 +83,10 @@ export class RoomPermalinkCreator {
this._serverCandidates = null; this._serverCandidates = null;
this._started = false; this._started = false;
if (!this._roomId) {
throw new Error("Failed to resolve a roomId for the permalink creator to use");
}
this.onMembership = this.onMembership.bind(this); this.onMembership = this.onMembership.bind(this);
this.onRoomState = this.onRoomState.bind(this); this.onRoomState = this.onRoomState.bind(this);
} }
@ -116,13 +124,13 @@ export class RoomPermalinkCreator {
} }
forEvent(eventId) { forEvent(eventId) {
const roomId = this._room.roomId; const roomId = this._roomId;
const permalinkBase = `${baseUrl}/#/${roomId}/${eventId}`; const permalinkBase = `${baseUrl}/#/${roomId}/${eventId}`;
return `${permalinkBase}${encodeServerCandidates(this._serverCandidates)}`; return `${permalinkBase}${encodeServerCandidates(this._serverCandidates)}`;
} }
forRoom() { forRoom() {
const roomId = this._room.roomId; const roomId = this._roomId;
const permalinkBase = `${baseUrl}/#/${roomId}`; const permalinkBase = `${baseUrl}/#/${roomId}`;
return `${permalinkBase}${encodeServerCandidates(this._serverCandidates)}`; return `${permalinkBase}${encodeServerCandidates(this._serverCandidates)}`;
} }
@ -246,7 +254,6 @@ export class RoomPermalinkCreator {
} }
} }
export function makeUserPermalink(userId) { export function makeUserPermalink(userId) {
return `${baseUrl}/#/${userId}`; return `${baseUrl}/#/${userId}`;
} }

View File

@ -74,7 +74,7 @@ describe('matrix-to', function() {
}); });
it('should pick no candidate servers when the room has no members', function() { it('should pick no candidate servers when the room has no members', function() {
const room = mockRoom(null, []); const room = mockRoom("!fake:example.org", []);
const creator = new RoomPermalinkCreator(room); const creator = new RoomPermalinkCreator(room);
creator.load(); creator.load();
expect(creator._serverCandidates).toBeTruthy(); expect(creator._serverCandidates).toBeTruthy();
@ -82,7 +82,7 @@ describe('matrix-to', function() {
}); });
it('should pick a candidate server for the highest power level user in the room', function() { it('should pick a candidate server for the highest power level user in the room', function() {
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:pl_50", userId: "@alice:pl_50",
powerLevel: 50, powerLevel: 50,
@ -109,7 +109,7 @@ describe('matrix-to', function() {
userId: "@alice:pl_95", userId: "@alice:pl_95",
powerLevel: 95, powerLevel: 95,
}; };
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:pl_50", userId: "@alice:pl_50",
powerLevel: 50, powerLevel: 50,
@ -132,7 +132,7 @@ describe('matrix-to', function() {
}); });
it('should pick candidate servers based on user population', function() { it('should pick candidate servers based on user population', function() {
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:first", userId: "@alice:first",
powerLevel: 0, powerLevel: 0,
@ -168,7 +168,7 @@ describe('matrix-to', function() {
}); });
it('should pick prefer candidate servers with higher power levels', function() { it('should pick prefer candidate servers with higher power levels', function() {
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:first", userId: "@alice:first",
powerLevel: 100, powerLevel: 100,
@ -195,7 +195,7 @@ describe('matrix-to', function() {
}); });
it('should pick a maximum of 3 candidate servers', function() { it('should pick a maximum of 3 candidate servers', function() {
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:alpha", userId: "@alice:alpha",
powerLevel: 100, powerLevel: 100,
@ -224,7 +224,7 @@ describe('matrix-to', function() {
}); });
it('should not consider IPv4 hosts', function() { it('should not consider IPv4 hosts', function() {
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:127.0.0.1", userId: "@alice:127.0.0.1",
powerLevel: 100, powerLevel: 100,
@ -237,7 +237,7 @@ describe('matrix-to', function() {
}); });
it('should not consider IPv6 hosts', function() { it('should not consider IPv6 hosts', function() {
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:[::1]", userId: "@alice:[::1]",
powerLevel: 100, powerLevel: 100,
@ -250,7 +250,7 @@ describe('matrix-to', function() {
}); });
it('should not consider IPv4 hostnames with ports', function() { it('should not consider IPv4 hostnames with ports', function() {
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:127.0.0.1:8448", userId: "@alice:127.0.0.1:8448",
powerLevel: 100, powerLevel: 100,
@ -263,7 +263,7 @@ describe('matrix-to', function() {
}); });
it('should not consider IPv6 hostnames with ports', function() { it('should not consider IPv6 hostnames with ports', function() {
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:[::1]:8448", userId: "@alice:[::1]:8448",
powerLevel: 100, powerLevel: 100,
@ -276,7 +276,7 @@ describe('matrix-to', function() {
}); });
it('should work with hostnames with ports', function() { it('should work with hostnames with ports', function() {
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:example.org:8448", userId: "@alice:example.org:8448",
powerLevel: 100, powerLevel: 100,
@ -291,7 +291,7 @@ describe('matrix-to', function() {
}); });
it('should not consider servers explicitly denied by ACLs', function() { it('should not consider servers explicitly denied by ACLs', function() {
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:evilcorp.com", userId: "@alice:evilcorp.com",
powerLevel: 100, powerLevel: 100,
@ -311,7 +311,7 @@ describe('matrix-to', function() {
}); });
it('should not consider servers not allowed by ACLs', function() { it('should not consider servers not allowed by ACLs', function() {
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:evilcorp.com", userId: "@alice:evilcorp.com",
powerLevel: 100, powerLevel: 100,
@ -331,7 +331,7 @@ describe('matrix-to', function() {
}); });
it('should consider servers not explicitly banned by ACLs', function() { it('should consider servers not explicitly banned by ACLs', function() {
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:evilcorp.com", userId: "@alice:evilcorp.com",
powerLevel: 100, powerLevel: 100,
@ -352,7 +352,7 @@ describe('matrix-to', function() {
}); });
it('should consider servers not disallowed by ACLs', function() { it('should consider servers not disallowed by ACLs', function() {
const room = mockRoom(null, [ const room = mockRoom("!fake:example.org", [
{ {
userId: "@alice:evilcorp.com", userId: "@alice:evilcorp.com",
powerLevel: 100, powerLevel: 100,