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

Commit 336f159

Browse files
authored
Update URL when room aliases are modified (#7289)
1 parent c222c55 commit 336f159

File tree

2 files changed

+67
-58
lines changed

2 files changed

+67
-58
lines changed

src/components/structures/MatrixChat.tsx

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import GenericToast from "../views/toasts/GenericToast";
114114
import InfoDialog from "../views/dialogs/InfoDialog";
115115
import FeedbackDialog from "../views/dialogs/FeedbackDialog";
116116
import AccessibleButton from "../views/elements/AccessibleButton";
117+
import { ActionPayload } from "../../dispatcher/payloads";
117118

118119
/** constants for MatrixChat.state.view */
119120
export enum Views {
@@ -542,13 +543,11 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
542543
this.setState(newState);
543544
}
544545

545-
onAction = (payload) => {
546+
private onAction = (payload: ActionPayload) => {
546547
// console.log(`MatrixClientPeg.onAction: ${payload.action}`);
547548

548549
// Start the onboarding process for certain actions
549-
if (MatrixClientPeg.get() && MatrixClientPeg.get().isGuest() &&
550-
ONBOARDING_FLOW_STARTERS.includes(payload.action)
551-
) {
550+
if (MatrixClientPeg.get()?.isGuest() && ONBOARDING_FLOW_STARTERS.includes(payload.action)) {
552551
// This will cause `payload` to be dispatched later, once a
553552
// sync has reached the "prepared" state. Setting a matrix ID
554553
// will cause a full login and sync and finally the deferred
@@ -590,7 +589,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
590589
Lifecycle.logout();
591590
break;
592591
case 'require_registration':
593-
startAnyRegistrationFlow(payload);
592+
startAnyRegistrationFlow(payload as any);
594593
break;
595594
case 'start_registration':
596595
if (Lifecycle.isSoftLogout()) {
@@ -666,7 +665,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
666665
// known to be in (eg. user clicks on a room in the recents panel), supply the ID
667666
// If the user is clicking on a room in the context of the alias being presented
668667
// to them, supply the room alias. If both are supplied, the room ID will be ignored.
669-
const promise = this.viewRoom(payload);
668+
const promise = this.viewRoom(payload as any);
670669
if (payload.deferred_action) {
671670
promise.then(() => {
672671
dis.dispatch(payload.deferred_action);
@@ -897,73 +896,73 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
897896
// @param {Object=} roomInfo.oob_data Object of additional data about the room
898897
// that has been passed out-of-band (eg.
899898
// room name and avatar from an invite email)
900-
private viewRoom(roomInfo: IRoomInfo) {
899+
private async viewRoom(roomInfo: IRoomInfo) {
901900
this.focusComposer = true;
902901

903902
if (roomInfo.room_alias) {
904-
logger.log(
905-
`Switching to room alias ${roomInfo.room_alias} at event ` +
906-
roomInfo.event_id,
907-
);
903+
logger.log(`Switching to room alias ${roomInfo.room_alias} at event ${roomInfo.event_id}`);
908904
} else {
909-
logger.log(`Switching to room id ${roomInfo.room_id} at event ` +
910-
roomInfo.event_id,
911-
);
905+
logger.log(`Switching to room id ${roomInfo.room_id} at event ${roomInfo.event_id}`);
912906
}
913907

914908
// Wait for the first sync to complete so that if a room does have an alias,
915909
// it would have been retrieved.
916-
let waitFor = Promise.resolve(null);
917910
if (!this.firstSyncComplete) {
918911
if (!this.firstSyncPromise) {
919912
logger.warn('Cannot view a room before first sync. room_id:', roomInfo.room_id);
920913
return;
921914
}
922-
waitFor = this.firstSyncPromise.promise;
915+
await this.firstSyncPromise.promise;
923916
}
924917

925-
return waitFor.then(() => {
926-
let presentedId = roomInfo.room_alias || roomInfo.room_id;
927-
const room = MatrixClientPeg.get().getRoom(roomInfo.room_id);
928-
if (room) {
929-
// Not all timeline events are decrypted ahead of time anymore
930-
// Only the critical ones for a typical UI are
931-
// This will start the decryption process for all events when a
932-
// user views a room
933-
room.decryptAllEvents();
934-
const theAlias = Rooms.getDisplayAliasForRoom(room);
935-
if (theAlias) {
936-
presentedId = theAlias;
937-
// Store display alias of the presented room in cache to speed future
938-
// navigation.
939-
storeRoomAliasInCache(theAlias, room.roomId);
940-
}
918+
let presentedId = roomInfo.room_alias || roomInfo.room_id;
919+
const room = MatrixClientPeg.get().getRoom(roomInfo.room_id);
920+
if (room) {
921+
// Not all timeline events are decrypted ahead of time anymore
922+
// Only the critical ones for a typical UI are
923+
// This will start the decryption process for all events when a
924+
// user views a room
925+
room.decryptAllEvents();
926+
const theAlias = Rooms.getDisplayAliasForRoom(room);
927+
if (theAlias) {
928+
presentedId = theAlias;
929+
// Store display alias of the presented room in cache to speed future
930+
// navigation.
931+
storeRoomAliasInCache(theAlias, room.roomId);
932+
}
941933

942-
// Store this as the ID of the last room accessed. This is so that we can
943-
// persist which room is being stored across refreshes and browser quits.
944-
if (localStorage) {
945-
localStorage.setItem('mx_last_room_id', room.roomId);
946-
}
934+
// Store this as the ID of the last room accessed. This is so that we can
935+
// persist which room is being stored across refreshes and browser quits.
936+
if (localStorage) {
937+
localStorage.setItem('mx_last_room_id', room.roomId);
947938
}
939+
}
948940

949-
// If we are redirecting to a Room Alias and it is for the room we already showing then replace history item
950-
const replaceLast = presentedId[0] === "#" && roomInfo.room_id === this.state.currentRoomId;
941+
// If we are redirecting to a Room Alias and it is for the room we already showing then replace history item
942+
const replaceLast = presentedId[0] === "#" && roomInfo.room_id === this.state.currentRoomId;
951943

952-
if (roomInfo.event_id && roomInfo.highlighted) {
953-
presentedId += "/" + roomInfo.event_id;
954-
}
955-
this.setState({
956-
view: Views.LOGGED_IN,
957-
currentRoomId: roomInfo.room_id || null,
958-
page_type: PageType.RoomView,
959-
threepidInvite: roomInfo.threepid_invite,
960-
roomOobData: roomInfo.oob_data,
961-
forceTimeline: roomInfo.forceTimeline,
962-
ready: true,
963-
roomJustCreatedOpts: roomInfo.justCreatedOpts,
964-
}, () => {
965-
this.notifyNewScreen('room/' + presentedId, replaceLast);
966-
});
944+
if (roomInfo.room_id === this.state.currentRoomId) {
945+
// if we are re-viewing the same room then copy any state we already know
946+
roomInfo.threepid_invite = roomInfo.threepid_invite ?? this.state.threepidInvite;
947+
roomInfo.oob_data = roomInfo.oob_data ?? this.state.roomOobData;
948+
roomInfo.forceTimeline = roomInfo.forceTimeline ?? this.state.forceTimeline;
949+
roomInfo.justCreatedOpts = roomInfo.justCreatedOpts ?? this.state.roomJustCreatedOpts;
950+
}
951+
952+
if (roomInfo.event_id && roomInfo.highlighted) {
953+
presentedId += "/" + roomInfo.event_id;
954+
}
955+
this.setState({
956+
view: Views.LOGGED_IN,
957+
currentRoomId: roomInfo.room_id || null,
958+
page_type: PageType.RoomView,
959+
threepidInvite: roomInfo.threepid_invite,
960+
roomOobData: roomInfo.oob_data,
961+
forceTimeline: roomInfo.forceTimeline,
962+
ready: true,
963+
roomJustCreatedOpts: roomInfo.justCreatedOpts,
964+
}, () => {
965+
this.notifyNewScreen('room/' + presentedId, replaceLast);
967966
});
968967
}
969968

src/components/structures/RoomView.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import { IRecommendedVersion, NotificationCountType, Room } from "matrix-js-sdk/
2727
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
2828
import { EventSubscription } from "fbemitter";
2929
import { ISearchResults } from 'matrix-js-sdk/src/@types/search';
30+
import { logger } from "matrix-js-sdk/src/logger";
31+
import { EventTimeline } from 'matrix-js-sdk/src/models/event-timeline';
32+
import { EventType } from 'matrix-js-sdk/src/@types/event';
33+
import { RoomState } from 'matrix-js-sdk/src/models/room-state';
3034

3135
import shouldHideEvent from '../../shouldHideEvent';
3236
import { _t } from '../../languageHandler';
@@ -89,9 +93,6 @@ import MessageComposer from '../views/rooms/MessageComposer';
8993
import JumpToBottomButton from "../views/rooms/JumpToBottomButton";
9094
import TopUnreadMessagesBar from "../views/rooms/TopUnreadMessagesBar";
9195
import SpaceStore from "../../stores/spaces/SpaceStore";
92-
93-
import { logger } from "matrix-js-sdk/src/logger";
94-
import { EventTimeline } from 'matrix-js-sdk/src/models/event-timeline';
9596
import { dispatchShowThreadEvent } from '../../dispatcher/dispatch-actions/threads';
9697
import { fetchInitialEvent } from "../../utils/EventUtils";
9798
import { ComposerType } from "../../dispatcher/payloads/ComposerInsertPayload";
@@ -1164,12 +1165,21 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
11641165
}
11651166
};
11661167

1167-
private onRoomStateEvents = (ev: MatrixEvent, state) => {
1168+
private onRoomStateEvents = (ev: MatrixEvent, state: RoomState) => {
11681169
// ignore if we don't have a room yet
11691170
if (!this.state.room || this.state.room.roomId !== state.roomId) {
11701171
return;
11711172
}
11721173

1174+
if (ev.getType() === EventType.RoomCanonicalAlias) {
1175+
// re-view the room so MatrixChat can manage the alias in the URL properly
1176+
dis.dispatch({
1177+
action: Action.ViewRoom,
1178+
room_id: this.state.room.roomId,
1179+
});
1180+
return; // this event cannot affect permissions so bail
1181+
}
1182+
11731183
this.updatePermissions(this.state.room);
11741184
};
11751185

0 commit comments

Comments
 (0)