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

Commit f784523

Browse files
committed
Support MSC3946 in RoomListStore
1 parent f31abc6 commit f784523

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

Diff for: src/stores/room-list/RoomListStore.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ export class RoomListStoreClass extends AsyncStoreWithClient<IState> implements
286286
// If we're joining an upgraded room, we'll want to make sure we don't proliferate
287287
// the dead room in the list.
288288
const roomState: RoomState = membershipPayload.room.currentState;
289-
const createEvent = roomState.getStateEvents(EventType.RoomCreate, "");
290-
if (createEvent && createEvent.getContent()["predecessor"]) {
291-
const prevRoom = this.matrixClient.getRoom(createEvent.getContent()["predecessor"]["room_id"]);
289+
const predecessor = roomState.findPredecessor(SettingsStore.getValue("feature_dynamic_room_predecessors"));
290+
if (predecessor) {
291+
const prevRoom = this.matrixClient.getRoom(predecessor.roomId);
292292
if (prevRoom) {
293293
const isSticky = this.algorithm.stickyRoom === prevRoom;
294294
if (isSticky) {
@@ -298,6 +298,8 @@ export class RoomListStoreClass extends AsyncStoreWithClient<IState> implements
298298
// Note: we hit the algorithm instead of our handleRoomUpdate() function to
299299
// avoid redundant updates.
300300
this.algorithm.handleRoomUpdate(prevRoom, RoomUpdateCause.RoomRemoved);
301+
} else {
302+
logger.warn(`Unable to find predecessor room with id ${predecessor.roomId}`);
301303
}
302304
}
303305

Diff for: test/stores/room-list/RoomListStore-test.ts

+50-5
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ limitations under the License.
1717
import { EventType, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
1818

1919
import { MatrixDispatcher } from "../../../src/dispatcher/dispatcher";
20+
import SettingsStore from "../../../src/settings/SettingsStore";
2021
import { ListAlgorithm, SortAlgorithm } from "../../../src/stores/room-list/algorithms/models";
2122
import { OrderedDefaultTagIDs, RoomUpdateCause } from "../../../src/stores/room-list/models";
2223
import RoomListStore, { RoomListStoreClass } from "../../../src/stores/room-list/RoomListStore";
2324
import { stubClient, upsertRoomStateEvents } from "../../test-utils";
2425

2526
describe("RoomListStore", () => {
2627
const client = stubClient();
27-
const roomWithCreatePredecessorId = "!roomid:example.com";
28+
const newRoomId = "!roomid:example.com";
2829
const roomNoPredecessorId = "!roomnopreid:example.com";
2930
const oldRoomId = "!oldroomid:example.com";
3031
const userId = "@user:example.com";
3132
const createWithPredecessor = new MatrixEvent({
3233
type: EventType.RoomCreate,
3334
sender: userId,
34-
room_id: roomWithCreatePredecessorId,
35+
room_id: newRoomId,
3536
content: {
3637
predecessor: { room_id: oldRoomId, event_id: "tombstone_event_id" },
3738
},
@@ -41,19 +42,31 @@ describe("RoomListStore", () => {
4142
const createNoPredecessor = new MatrixEvent({
4243
type: EventType.RoomCreate,
4344
sender: userId,
44-
room_id: roomWithCreatePredecessorId,
45+
room_id: newRoomId,
4546
content: {},
4647
event_id: "$create",
4748
state_key: "",
4849
});
49-
const roomWithCreatePredecessor = new Room(roomWithCreatePredecessorId, client, userId, {});
50+
const predecessor = new MatrixEvent({
51+
type: EventType.RoomPredecessor,
52+
sender: userId,
53+
room_id: newRoomId,
54+
content: {
55+
predecessor: { predecessor_room_id: oldRoomId, last_known_event_id: "tombstone_event_id" },
56+
},
57+
event_id: "$pred",
58+
state_key: "",
59+
});
60+
const roomWithPredecessorEvent = new Room(newRoomId, client, userId, {});
61+
upsertRoomStateEvents(roomWithPredecessorEvent, [predecessor]);
62+
const roomWithCreatePredecessor = new Room(newRoomId, client, userId, {});
5063
upsertRoomStateEvents(roomWithCreatePredecessor, [createWithPredecessor]);
5164
const roomNoPredecessor = new Room(roomNoPredecessorId, client, userId, {});
5265
upsertRoomStateEvents(roomNoPredecessor, [createNoPredecessor]);
5366
const oldRoom = new Room(oldRoomId, client, userId, {});
5467
client.getRoom = jest.fn().mockImplementation((roomId) => {
5568
switch (roomId) {
56-
case roomWithCreatePredecessorId:
69+
case newRoomId:
5770
return roomWithCreatePredecessor;
5871
case oldRoomId:
5972
return oldRoom;
@@ -123,4 +136,36 @@ describe("RoomListStore", () => {
123136
// And no other updates happen
124137
expect(handleRoomUpdate).toHaveBeenCalledTimes(1);
125138
});
139+
140+
describe("When feature_dynamic_room_predecessors = true", () => {
141+
beforeEach(() => {
142+
jest.spyOn(SettingsStore, "getValue").mockImplementation(
143+
(settingName) => settingName === "feature_dynamic_room_predecessors",
144+
);
145+
});
146+
147+
afterEach(() => {
148+
jest.spyOn(SettingsStore, "getValue").mockReset();
149+
});
150+
151+
it("Removes old room if it finds a predecessor in the m.predecessor event", () => {
152+
// Given a store we can spy on
153+
const { store, handleRoomUpdate } = createStore();
154+
155+
// When we tell it we joined a new room that has an old room as
156+
// predecessor in the create event
157+
const payload = {
158+
oldMembership: "invite",
159+
membership: "join",
160+
room: roomWithPredecessorEvent,
161+
};
162+
store.onDispatchMyMembership(payload);
163+
164+
// Then the old room is removed
165+
expect(handleRoomUpdate).toHaveBeenCalledWith(oldRoom, RoomUpdateCause.RoomRemoved);
166+
167+
// And the new room is added
168+
expect(handleRoomUpdate).toHaveBeenCalledWith(roomWithCreatePredecessor, RoomUpdateCause.NewRoom);
169+
});
170+
});
126171
});

0 commit comments

Comments
 (0)