Merge pull request #5464 from matrix-org/t3chguy/fix/15882

Jump to home page when leaving a room
This commit is contained in:
Michael Telatynski 2020-12-03 15:19:58 +00:00 committed by GitHub
commit 1e80c936c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 9 additions and 78 deletions

View file

@ -1,31 +0,0 @@
/*
Copyright 2015, 2016 OpenMarket Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict';
function tsOfNewestEvent(room) {
if (room.timeline.length) {
return room.timeline[room.timeline.length - 1].getTs();
} else {
return Number.MAX_SAFE_INTEGER;
}
}
export function mostRecentActivityFirst(roomList) {
return roomList.sort(function(a, b) {
return tsOfNewestEvent(b) - tsOfNewestEvent(a);
});
}

View file

@ -34,7 +34,6 @@ import { DecryptionFailureTracker } from "../../DecryptionFailureTracker";
import { MatrixClientPeg, IMatrixClientCreds } from "../../MatrixClientPeg"; import { MatrixClientPeg, IMatrixClientCreds } from "../../MatrixClientPeg";
import PlatformPeg from "../../PlatformPeg"; import PlatformPeg from "../../PlatformPeg";
import SdkConfig from "../../SdkConfig"; import SdkConfig from "../../SdkConfig";
import * as RoomListSorter from "../../RoomListSorter";
import dis from "../../dispatcher/dispatcher"; import dis from "../../dispatcher/dispatcher";
import Notifier from '../../Notifier'; import Notifier from '../../Notifier';
@ -48,7 +47,6 @@ import * as Lifecycle from '../../Lifecycle';
// LifecycleStore is not used but does listen to and dispatch actions // LifecycleStore is not used but does listen to and dispatch actions
import '../../stores/LifecycleStore'; import '../../stores/LifecycleStore';
import PageTypes from '../../PageTypes'; import PageTypes from '../../PageTypes';
import { getHomePageUrl } from '../../utils/pages';
import createRoom from "../../createRoom"; import createRoom from "../../createRoom";
import {_t, _td, getCurrentLanguage} from '../../languageHandler'; import {_t, _td, getCurrentLanguage} from '../../languageHandler';
@ -591,7 +589,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
MatrixClientPeg.get().leave(payload.room_id).then(() => { MatrixClientPeg.get().leave(payload.room_id).then(() => {
modal.close(); modal.close();
if (this.state.currentRoomId === payload.room_id) { if (this.state.currentRoomId === payload.room_id) {
dis.dispatch({action: 'view_next_room'}); dis.dispatch({action: 'view_home_page'});
} }
}, (err) => { }, (err) => {
modal.close(); modal.close();
@ -620,9 +618,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
} }
break; break;
} }
case 'view_next_room':
this.viewNextRoom(1);
break;
case Action.ViewUserSettings: { case Action.ViewUserSettings: {
const tabPayload = payload as OpenToTabPayload; const tabPayload = payload as OpenToTabPayload;
const UserSettingsDialog = sdk.getComponent("dialogs.UserSettingsDialog"); const UserSettingsDialog = sdk.getComponent("dialogs.UserSettingsDialog");
@ -802,35 +797,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
this.notifyNewScreen('register'); this.notifyNewScreen('register');
} }
// TODO: Move to RoomViewStore
private viewNextRoom(roomIndexDelta: number) {
const allRooms = RoomListSorter.mostRecentActivityFirst(
MatrixClientPeg.get().getRooms(),
);
// If there are 0 rooms or 1 room, view the home page because otherwise
// if there are 0, we end up trying to index into an empty array, and
// if there is 1, we end up viewing the same room.
if (allRooms.length < 2) {
dis.dispatch({
action: 'view_home_page',
});
return;
}
let roomIndex = -1;
for (let i = 0; i < allRooms.length; ++i) {
if (allRooms[i].roomId === this.state.currentRoomId) {
roomIndex = i;
break;
}
}
roomIndex = (roomIndex + roomIndexDelta) % allRooms.length;
if (roomIndex < 0) roomIndex = allRooms.length - 1;
dis.dispatch({
action: 'view_room',
room_id: allRooms[roomIndex].roomId,
});
}
// switch view to the given room // switch view to the given room
// //
// @param {Object} roomInfo Object containing data about the room to be joined // @param {Object} roomInfo Object containing data about the room to be joined
@ -1097,9 +1063,9 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
private forgetRoom(roomId: string) { private forgetRoom(roomId: string) {
MatrixClientPeg.get().forget(roomId).then(() => { MatrixClientPeg.get().forget(roomId).then(() => {
// Switch to another room view if we're currently viewing the historical room // Switch to home page if we're currently viewing the forgotten room
if (this.state.currentRoomId === roomId) { if (this.state.currentRoomId === roomId) {
dis.dispatch({ action: "view_next_room" }); dis.dispatch({ action: "view_home_page" });
} }
}).catch((err) => { }).catch((err) => {
const errCode = err.errcode || _td("unknown error code"); const errCode = err.errcode || _td("unknown error code");
@ -1233,12 +1199,8 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
} else { } else {
if (MatrixClientPeg.get().isGuest()) { if (MatrixClientPeg.get().isGuest()) {
dis.dispatch({action: 'view_welcome_page'}); dis.dispatch({action: 'view_welcome_page'});
} else if (getHomePageUrl(this.props.config)) {
dis.dispatch({action: 'view_home_page'});
} else { } else {
this.firstSyncPromise.promise.then(() => { dis.dispatch({action: 'view_home_page'});
dis.dispatch({action: 'view_next_room'});
});
} }
} }
} }

View file

@ -1332,7 +1332,7 @@ export default class RoomView extends React.Component<IProps, IState> {
rejecting: true, rejecting: true,
}); });
this.context.leave(this.state.roomId).then(() => { this.context.leave(this.state.roomId).then(() => {
dis.dispatch({ action: 'view_next_room' }); dis.dispatch({ action: 'view_home_page' });
this.setState({ this.setState({
rejecting: false, rejecting: false,
}); });
@ -1366,7 +1366,7 @@ export default class RoomView extends React.Component<IProps, IState> {
await this.context.setIgnoredUsers(ignoredUsers); await this.context.setIgnoredUsers(ignoredUsers);
await this.context.leave(this.state.roomId); await this.context.leave(this.state.roomId);
dis.dispatch({ action: 'view_next_room' }); dis.dispatch({ action: 'view_home_page' });
this.setState({ this.setState({
rejecting: false, rejecting: false,
}); });

View file

@ -53,9 +53,9 @@ export default class RoomSettingsDialog extends React.Component {
} }
_onAction = (payload) => { _onAction = (payload) => {
// When room changes below us, close the room settings // When view changes below us, close the room settings
// whilst the modal is open this can only be triggered when someone hits Leave Room // whilst the modal is open this can only be triggered when someone hits Leave Room
if (payload.action === 'view_next_room') { if (payload.action === 'view_home_page') {
this.props.onFinished(); this.props.onFinished();
} }
}; };

View file

@ -140,6 +140,6 @@ export async function leaveRoomBehaviour(roomId: string) {
} }
if (RoomViewStore.getRoomId() === roomId) { if (RoomViewStore.getRoomId() === roomId) {
dis.dispatch({action: 'view_next_room'}); dis.dispatch({action: 'view_home_page'});
} }
} }