Fix peeking

Sorts out more of the room joining mess. currentRoom which held the room ID is now more appropriately called currentRoomId. RoomView will now take a roomID or alias as before but will now look up the room ID as required if given the alias. Also, now look up the alias every time you click on it so it's never stale, rather than looking in your current rooms for a room that thinks it has that ID.
This commit is contained in:
David Baker 2016-06-14 11:37:04 +01:00
parent 469c415557
commit 11a38fce48
2 changed files with 50 additions and 33 deletions

View File

@ -409,7 +409,7 @@ module.exports = React.createClass({
); );
var roomIndex = -1; var roomIndex = -1;
for (var i = 0; i < allRooms.length; ++i) { for (var i = 0; i < allRooms.length; ++i) {
if (allRooms[i].roomId == this.state.currentRoom) { if (allRooms[i].roomId == this.state.currentRoomId) {
roomIndex = i; roomIndex = i;
break; break;
} }
@ -506,20 +506,10 @@ module.exports = React.createClass({
page_type: this.PageTypes.RoomView, page_type: this.PageTypes.RoomView,
thirdPartyInvite: thirdPartyInvite, thirdPartyInvite: thirdPartyInvite,
roomOobData: oob_data, roomOobData: oob_data,
newState.currentRoomAlias: roomAlias,
newState.currentRoomId: roomId,
}; };
// If an alias has been provided, we use that and only that,
// since otherwise we'll prefer to pass in an ID to RoomView
// but if we're not in the room, we should join by alias rather
// than ID.
if (roomAlias) {
newState.currentRoomAlias = roomAlias;
newState.currentRoom = null;
} else {
newState.currentRoomAlias = null;
newState.currentRoom = roomId;
}
// if we aren't given an explicit event id, look for one in the // if we aren't given an explicit event id, look for one in the
// scrollStateMap. // scrollStateMap.
if (!eventId) { if (!eventId) {
@ -612,13 +602,13 @@ module.exports = React.createClass({
dis.dispatch(self.starting_room_alias_payload); dis.dispatch(self.starting_room_alias_payload);
delete self.starting_room_alias_payload; delete self.starting_room_alias_payload;
} else if (!self.state.page_type) { } else if (!self.state.page_type) {
if (!self.state.currentRoom) { if (!self.state.currentRoomId) {
var firstRoom = null; var firstRoom = null;
if (cli.getRooms() && cli.getRooms().length) { if (cli.getRooms() && cli.getRooms().length) {
firstRoom = RoomListSorter.mostRecentActivityFirst( firstRoom = RoomListSorter.mostRecentActivityFirst(
cli.getRooms() cli.getRooms()
)[0].roomId; )[0].roomId;
self.setState({ready: true, currentRoom: firstRoom, page_type: self.PageTypes.RoomView}); self.setState({ready: true, currentRoomId: firstRoom, page_type: self.PageTypes.RoomView});
} else { } else {
self.setState({ready: true, page_type: self.PageTypes.RoomDirectory}); self.setState({ready: true, page_type: self.PageTypes.RoomDirectory});
} }
@ -628,8 +618,8 @@ module.exports = React.createClass({
// we notifyNewScreen now because now the room will actually be displayed, // we notifyNewScreen now because now the room will actually be displayed,
// and (mostly) now we can get the correct alias. // and (mostly) now we can get the correct alias.
var presentedId = self.state.currentRoom; var presentedId = self.state.currentRoomId;
var room = MatrixClientPeg.get().getRoom(self.state.currentRoom); var room = MatrixClientPeg.get().getRoom(self.state.currentRoomId);
if (room) { if (room) {
var theAlias = MatrixTools.getCanonicalAliasForRoom(room); var theAlias = MatrixTools.getCanonicalAliasForRoom(room);
if (theAlias) presentedId = theAlias; if (theAlias) presentedId = theAlias;
@ -979,10 +969,10 @@ module.exports = React.createClass({
onUserSettingsClose: function() { onUserSettingsClose: function() {
// XXX: use browser history instead to find the previous room? // XXX: use browser history instead to find the previous room?
// or maintain a this.state.pageHistory in _setPage()? // or maintain a this.state.pageHistory in _setPage()?
if (this.state.currentRoom) { if (this.state.currentRoomId) {
dis.dispatch({ dis.dispatch({
action: 'view_room', action: 'view_room',
room_id: this.state.currentRoom, room_id: this.state.currentRoomId,
}); });
} }
else { else {
@ -1022,17 +1012,18 @@ module.exports = React.createClass({
page_element = ( page_element = (
<RoomView <RoomView
ref="roomView" ref="roomView"
roomAddress={this.state.currentRoom || this.state.currentRoomAlias} roomId={this.state.currentRoomId}
roomAlias={this.state.currentRoomAlias}
eventId={this.state.initialEventId} eventId={this.state.initialEventId}
thirdPartyInvite={this.state.thirdPartyInvite} thirdPartyInvite={this.state.thirdPartyInvite}
oobData={this.state.roomOobData} oobData={this.state.roomOobData}
highlightedEventId={this.state.highlightedEventId} highlightedEventId={this.state.highlightedEventId}
eventPixelOffset={this.state.initialEventPixelOffset} eventPixelOffset={this.state.initialEventPixelOffset}
key={this.state.currentRoom || this.state.currentRoomAlias} key={this.state.currentRoomId || this.state.currentRoomAlias}
opacity={this.state.middleOpacity} opacity={this.state.middleOpacity}
ConferenceHandler={this.props.ConferenceHandler} /> ConferenceHandler={this.props.ConferenceHandler} />
); );
right_panel = <RightPanel roomId={this.state.currentRoom} collapsed={this.state.collapse_rhs} opacity={this.state.sideOpacity} /> right_panel = <RightPanel roomId={this.state.currentRoomId} collapsed={this.state.collapse_rhs} opacity={this.state.sideOpacity} />
break; break;
case this.PageTypes.UserSettings: case this.PageTypes.UserSettings:
page_element = <UserSettings onClose={this.onUserSettingsClose} version={this.state.version} brand={this.props.config.brand} /> page_element = <UserSettings onClose={this.onUserSettingsClose} version={this.state.version} brand={this.props.config.brand} />
@ -1068,7 +1059,7 @@ module.exports = React.createClass({
<div className="mx_MatrixChat_wrapper"> <div className="mx_MatrixChat_wrapper">
{topBar} {topBar}
<div className={bodyClasses}> <div className={bodyClasses}>
<LeftPanel selectedRoom={this.state.currentRoom} collapsed={this.state.collapse_lhs} opacity={this.state.sideOpacity}/> <LeftPanel selectedRoom={this.state.currentRoomId} collapsed={this.state.collapse_lhs} opacity={this.state.sideOpacity}/>
<main className="mx_MatrixChat_middlePanel"> <main className="mx_MatrixChat_middlePanel">
{page_element} {page_element}
</main> </main>

View File

@ -55,7 +55,10 @@ module.exports = React.createClass({
propTypes: { propTypes: {
ConferenceHandler: React.PropTypes.any, ConferenceHandler: React.PropTypes.any,
// the ID for this room (or, if we don't know it, an alias for it) // Either a room ID or room alias for the room to display.
// If the room is being displayed as a result of the user clicking
// on a room alias, the alias should be supplied. Otherwise, a room
// ID should be supplied.
roomAddress: React.PropTypes.string.isRequired, roomAddress: React.PropTypes.string.isRequired,
// An object representing a third party invite to join this room // An object representing a third party invite to join this room
@ -95,15 +98,14 @@ module.exports = React.createClass({
getInitialState: function() { getInitialState: function() {
var room; var room;
var room_id;
if (this.props.roomAddress[0] == '!') { if (this.props.roomAddress[0] == '!') {
room = MatrixClientPeg.get().getRoom(this.props.roomAddress); room_id = this.props.roomAddress;
} else { room = MatrixClientPeg.get().getRoom(room_id);
room = MatrixTools.getRoomForAlias(
MatrixClientPeg.get().getRooms(), this.props.roomAddress
);
} }
return { return {
room: room, room: room,
roomId: room_id,
roomLoading: !room, roomLoading: !room,
editingRoomSettings: false, editingRoomSettings: false,
uploadingRoomSettings: false, uploadingRoomSettings: false,
@ -143,6 +145,27 @@ module.exports = React.createClass({
} }
}); });
if (this.props.roomAddress[0] == '#') {
// we always look up the alias from the directory server:
// we want the room that the given alias is pointing to
// right now. We may have joined that alias before but there's
// no guarantee the alias hasn't subsequently been remapped.
MatrixClientPeg.get().getRoomIdForAlias(this.props.roomAddress).done((result) => {
this.setState({
roomId: result.room_id,
roomLoading: false,
}, this.updatePeeking);
}, (err) => {
this.setState({
roomLoading: false,
});
});
}
this.updatePeeking();
},
updatePeeking: function() {
// if this is an unknown room then we're in one of three states: // if this is an unknown room then we're in one of three states:
// - This is a room we can peek into (search engine) (we can /peek) // - This is a room we can peek into (search engine) (we can /peek)
// - This is a room we can publicly join or were invited to. (we can /join) // - This is a room we can publicly join or were invited to. (we can /join)
@ -150,10 +173,13 @@ module.exports = React.createClass({
// We can't try to /join because this may implicitly accept invites (!) // We can't try to /join because this may implicitly accept invites (!)
// We can /peek though. If it fails then we present the join UI. If it // We can /peek though. If it fails then we present the join UI. If it
// succeeds then great, show the preview (but we still may be able to /join!). // succeeds then great, show the preview (but we still may be able to /join!).
if (!this.state.room) { // Note that peeking works by room ID and room ID only, as opposed to joining
console.log("Attempting to peek into room %s", this.props.roomAddress); // which must be by alias or invite wherever possible (peeking currently does
// not work over federation).
if (!this.state.room && this.state.roomId) {
console.log("Attempting to peek into room %s", this.state.roomId);
MatrixClientPeg.get().peekInRoom(this.props.roomAddress).then((room) => { MatrixClientPeg.get().peekInRoom(this.state.roomId).then((room) => {
this.setState({ this.setState({
room: room, room: room,
roomLoading: false, roomLoading: false,
@ -172,7 +198,7 @@ module.exports = React.createClass({
throw err; throw err;
} }
}).done(); }).done();
} else { } else if (this.state.room) {
MatrixClientPeg.get().stopPeeking(); MatrixClientPeg.get().stopPeeking();
this._onRoomLoaded(this.state.room); this._onRoomLoaded(this.state.room);
} }