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

Commit f9c85ac

Browse files
authored
Fix codepath which can wrongly cause automatic space switch from all rooms (#8560)
* Fix codepath which can wrongly cause automatic space switch from all rooms * Improve typing
1 parent 13aa610 commit f9c85ac

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

src/stores/RoomViewStore.tsx

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { JoinedRoom as JoinedRoomEvent } from "matrix-analytics-events/types/typ
2525
import { JoinRule } from "matrix-js-sdk/src/@types/partials";
2626
import { Room } from "matrix-js-sdk/src/models/room";
2727
import { ClientEvent } from "matrix-js-sdk/src/client";
28+
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
29+
import { Optional } from "matrix-events-sdk";
2830

2931
import dis from '../dispatcher/dispatcher';
3032
import { MatrixClientPeg } from '../MatrixClientPeg';
@@ -53,30 +55,30 @@ const INITIAL_STATE = {
5355
// Whether we're joining the currently viewed room (see isJoining())
5456
joining: false,
5557
// Any error that has occurred during joining
56-
joinError: null,
58+
joinError: null as Error,
5759
// The room ID of the room currently being viewed
58-
roomId: null,
60+
roomId: null as string,
5961

6062
// The event to scroll to when the room is first viewed
61-
initialEventId: null,
62-
initialEventPixelOffset: null,
63+
initialEventId: null as string,
64+
initialEventPixelOffset: null as number,
6365
// Whether to highlight the initial event
6466
isInitialEventHighlighted: false,
6567
// whether to scroll `event_id` into view
6668
initialEventScrollIntoView: true,
6769

6870
// The room alias of the room (or null if not originally specified in view_room)
69-
roomAlias: null,
71+
roomAlias: null as string,
7072
// Whether the current room is loading
7173
roomLoading: false,
7274
// Any error that has occurred during loading
73-
roomLoadError: null,
75+
roomLoadError: null as MatrixError,
7476

75-
replyingToEvent: null,
77+
replyingToEvent: null as MatrixEvent,
7678

7779
shouldPeek: false,
7880

79-
viaServers: [],
81+
viaServers: [] as string[],
8082

8183
wasContextSwitch: false,
8284
};
@@ -103,12 +105,12 @@ export class RoomViewStore extends Store<ActionPayload> {
103105
super(dis);
104106
}
105107

106-
public addRoomListener(roomId: string, fn: Listener) {
108+
public addRoomListener(roomId: string, fn: Listener): void {
107109
if (!this.roomIdActivityListeners[roomId]) this.roomIdActivityListeners[roomId] = [];
108110
this.roomIdActivityListeners[roomId].push(fn);
109111
}
110112

111-
public removeRoomListener(roomId: string, fn: Listener) {
113+
public removeRoomListener(roomId: string, fn: Listener): void {
112114
if (this.roomIdActivityListeners[roomId]) {
113115
const i = this.roomIdActivityListeners[roomId].indexOf(fn);
114116
if (i > -1) {
@@ -119,15 +121,15 @@ export class RoomViewStore extends Store<ActionPayload> {
119121
}
120122
}
121123

122-
private emitForRoom(roomId: string, isActive: boolean) {
124+
private emitForRoom(roomId: string, isActive: boolean): void {
123125
if (!this.roomIdActivityListeners[roomId]) return;
124126

125127
for (const fn of this.roomIdActivityListeners[roomId]) {
126128
fn.call(null, isActive);
127129
}
128130
}
129131

130-
private setState(newState: Partial<typeof INITIAL_STATE>) {
132+
private setState(newState: Partial<typeof INITIAL_STATE>): void {
131133
// If values haven't changed, there's nothing to do.
132134
// This only tries a shallow comparison, so unchanged objects will slip
133135
// through, but that's probably okay for now.
@@ -160,7 +162,7 @@ export class RoomViewStore extends Store<ActionPayload> {
160162
this.__emitChange();
161163
}
162164

163-
protected __onDispatch(payload) { // eslint-disable-line @typescript-eslint/naming-convention
165+
protected __onDispatch(payload): void { // eslint-disable-line @typescript-eslint/naming-convention
164166
switch (payload.action) {
165167
// view_room:
166168
// - room_alias: '#somealias:matrix.org'
@@ -367,7 +369,7 @@ export class RoomViewStore extends Store<ActionPayload> {
367369
}
368370
}
369371

370-
private viewRoomError(payload: ViewRoomErrorPayload) {
372+
private viewRoomError(payload: ViewRoomErrorPayload): void {
371373
this.setState({
372374
roomId: payload.room_id,
373375
roomAlias: payload.room_alias,
@@ -376,7 +378,7 @@ export class RoomViewStore extends Store<ActionPayload> {
376378
});
377379
}
378380

379-
private async joinRoom(payload: JoinRoomPayload) {
381+
private async joinRoom(payload: JoinRoomPayload): Promise<void> {
380382
this.setState({
381383
joining: true,
382384
});
@@ -415,14 +417,14 @@ export class RoomViewStore extends Store<ActionPayload> {
415417
private getInvitingUserId(roomId: string): string {
416418
const cli = MatrixClientPeg.get();
417419
const room = cli.getRoom(roomId);
418-
if (room && room.getMyMembership() === "invite") {
420+
if (room?.getMyMembership() === "invite") {
419421
const myMember = room.getMember(cli.getUserId());
420422
const inviteEvent = myMember ? myMember.events.member : null;
421423
return inviteEvent && inviteEvent.getSender();
422424
}
423425
}
424426

425-
public showJoinRoomError(err: MatrixError, roomId: string) {
427+
public showJoinRoomError(err: MatrixError, roomId: string): void {
426428
let description: ReactNode = err.message ? err.message : JSON.stringify(err);
427429
logger.log("Failed to join room:", description);
428430

@@ -452,50 +454,50 @@ export class RoomViewStore extends Store<ActionPayload> {
452454
});
453455
}
454456

455-
private joinRoomError(payload: JoinRoomErrorPayload) {
457+
private joinRoomError(payload: JoinRoomErrorPayload): void {
456458
this.setState({
457459
joining: false,
458460
joinError: payload.err,
459461
});
460462
this.showJoinRoomError(payload.err, payload.roomId);
461463
}
462464

463-
public reset() {
465+
public reset(): void {
464466
this.state = Object.assign({}, INITIAL_STATE);
465467
}
466468

467469
// The room ID of the room currently being viewed
468-
public getRoomId() {
470+
public getRoomId(): Optional<string> {
469471
return this.state.roomId;
470472
}
471473

472474
// The event to scroll to when the room is first viewed
473-
public getInitialEventId() {
475+
public getInitialEventId(): Optional<string> {
474476
return this.state.initialEventId;
475477
}
476478

477479
// Whether to highlight the initial event
478-
public isInitialEventHighlighted() {
480+
public isInitialEventHighlighted(): boolean {
479481
return this.state.isInitialEventHighlighted;
480482
}
481483

482484
// Whether to avoid jumping to the initial event
483-
public initialEventScrollIntoView() {
485+
public initialEventScrollIntoView(): boolean {
484486
return this.state.initialEventScrollIntoView;
485487
}
486488

487489
// The room alias of the room (or null if not originally specified in view_room)
488-
public getRoomAlias() {
490+
public getRoomAlias(): Optional<string> {
489491
return this.state.roomAlias;
490492
}
491493

492494
// Whether the current room is loading (true whilst resolving an alias)
493-
public isRoomLoading() {
495+
public isRoomLoading(): boolean {
494496
return this.state.roomLoading;
495497
}
496498

497499
// Any error that has occurred during loading
498-
public getRoomLoadError() {
500+
public getRoomLoadError(): Optional<MatrixError> {
499501
return this.state.roomLoadError;
500502
}
501503

@@ -522,25 +524,25 @@ export class RoomViewStore extends Store<ActionPayload> {
522524
// // show join prompt
523525
// }
524526
// }
525-
public isJoining() {
527+
public isJoining(): boolean {
526528
return this.state.joining;
527529
}
528530

529531
// Any error that has occurred during joining
530-
public getJoinError() {
532+
public getJoinError(): Optional<Error> {
531533
return this.state.joinError;
532534
}
533535

534536
// The mxEvent if one is currently being replied to/quoted
535-
public getQuotingEvent() {
537+
public getQuotingEvent(): Optional<MatrixEvent> {
536538
return this.state.replyingToEvent;
537539
}
538540

539-
public shouldPeek() {
541+
public shouldPeek(): boolean {
540542
return this.state.shouldPeek;
541543
}
542544

543-
public getWasContextSwitch() {
545+
public getWasContextSwitch(): boolean {
544546
return this.state.wasContextSwitch;
545547
}
546548
}

src/stores/spaces/SpaceStore.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
803803
this.updateNotificationStates(notificationStatesToUpdate);
804804
};
805805

806-
private switchSpaceIfNeeded = () => {
807-
const roomId = RoomViewStore.instance.getRoomId();
806+
private switchSpaceIfNeeded = (roomId = RoomViewStore.instance.getRoomId()) => {
808807
if (!this.isRoomInSpace(this.activeSpace, roomId) && !this.matrixClient.getRoom(roomId)?.isSpaceRoom()) {
809808
this.switchToRelatedSpace(roomId);
810809
}
@@ -856,7 +855,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
856855

857856
// if the room currently being viewed was just joined then switch to its related space
858857
if (newMembership === "join" && room.roomId === RoomViewStore.instance.getRoomId()) {
859-
this.switchToRelatedSpace(room.roomId);
858+
this.switchSpaceIfNeeded(room.roomId);
860859
}
861860
}
862861
return;
@@ -1131,8 +1130,8 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
11311130
// Don't context switch when navigating to the space room
11321131
// as it will cause you to end up in the wrong room
11331132
this.setActiveSpace(room.roomId, false);
1134-
} else if (!this.isRoomInSpace(this.activeSpace, roomId)) {
1135-
this.switchToRelatedSpace(roomId);
1133+
} else {
1134+
this.switchSpaceIfNeeded(roomId);
11361135
}
11371136

11381137
// Persist last viewed room from a space

0 commit comments

Comments
 (0)