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

Commit 9b928b5

Browse files
committed
Handle remaining cases for room updates in new room list
For element-hq/element-web#13635 This adds support for: * Tag changes * DM changes * Marking our own rooms as read * Our own membership changes The remaining branch we didn't need was the alternate 'new room' branch, so it was removed. This is not optimized - optimization is deferred.
1 parent e809f28 commit 9b928b5

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

src/stores/room-list/RoomListStore2.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,19 @@ export class RoomListStore2 extends AsyncStore<ActionPayload> {
145145
// First see if the receipt event is for our own user. If it was, trigger
146146
// a room update (we probably read the room on a different device).
147147
if (readReceiptChangeIsFor(payload.event, this.matrixClient)) {
148-
// TODO: Update room now that it's been read
149-
console.log(payload);
148+
console.log(`[RoomListDebug] Got own read receipt in ${payload.event.roomId}`);
149+
const room = this.matrixClient.getRoom(payload.event.roomId);
150+
if (!room) {
151+
console.warn(`Own read receipt was in unknown room ${payload.event.roomId}`);
152+
return;
153+
}
154+
await this.handleRoomUpdate(room, RoomUpdateCause.ReadReceipt);
150155
return;
151156
}
152157
} else if (payload.action === 'MatrixActions.Room.tags') {
153-
// TODO: Update room from tags
154-
console.log(payload);
158+
const roomPayload = (<any>payload); // TODO: Type out the dispatcher types
159+
console.log(`[RoomListDebug] Got tag change in ${roomPayload.room.roomId}`);
160+
await this.handleRoomUpdate(roomPayload.room, RoomUpdateCause.PossibleTagChange);
155161
} else if (payload.action === 'MatrixActions.Room.timeline') {
156162
const eventPayload = (<any>payload); // TODO: Type out the dispatcher types
157163

@@ -189,23 +195,39 @@ export class RoomListStore2 extends AsyncStore<ActionPayload> {
189195
// cause inaccuracies with the list ordering. We may have to decrypt the last N messages of every room :(
190196
await this.handleRoomUpdate(room, RoomUpdateCause.Timeline);
191197
} else if (payload.action === 'MatrixActions.accountData' && payload.event_type === 'm.direct') {
192-
// TODO: Update DMs
193-
console.log(payload);
198+
const eventPayload = (<any>payload); // TODO: Type out the dispatcher types
199+
console.log(`[RoomListDebug] Received updated DM map`);
200+
const dmMap = eventPayload.event.getContent();
201+
for (const userId of Object.keys(dmMap)) {
202+
const roomIds = dmMap[userId];
203+
for (const roomId of roomIds) {
204+
const room = this.matrixClient.getRoom(roomId);
205+
if (!room) {
206+
console.warn(`${roomId} was found in DMs but the room is not in the store`);
207+
continue;
208+
}
209+
210+
// We expect this RoomUpdateCause to no-op if there's no change, and we don't expect
211+
// the user to have hundreds of rooms to update in one event. As such, we just hammer
212+
// away at updates until the problem is solved. If we were expecting more than a couple
213+
// of rooms to be updated at once, we would consider batching the rooms up.
214+
await this.handleRoomUpdate(room, RoomUpdateCause.PossibleTagChange);
215+
}
216+
}
194217
} else if (payload.action === 'MatrixActions.Room.myMembership') {
195-
// TODO: Improve new room check
196218
const membershipPayload = (<any>payload); // TODO: Type out the dispatcher types
197-
if (!membershipPayload.oldMembership && membershipPayload.membership === "join") {
219+
if (membershipPayload.oldMembership !== "join" && membershipPayload.membership === "join") {
198220
console.log(`[RoomListDebug] Handling new room ${membershipPayload.room.roomId}`);
199221
await this.algorithm.handleRoomUpdate(membershipPayload.room, RoomUpdateCause.NewRoom);
222+
return;
200223
}
201224

202-
// TODO: Update room from membership change
203-
console.log(payload);
204-
} else if (payload.action === 'MatrixActions.Room') {
205-
// TODO: Improve new room check
206-
// const roomPayload = (<any>payload); // TODO: Type out the dispatcher types
207-
// console.log(`[RoomListDebug] Handling new room ${roomPayload.room.roomId}`);
208-
// await this.algorithm.handleRoomUpdate(roomPayload.room, RoomUpdateCause.NewRoom);
225+
// If it's not a join, it's transitioning into a different list (possibly historical)
226+
if (membershipPayload.oldMembership !== membershipPayload.membership) {
227+
console.log(`[RoomListDebug] Handling membership change in ${membershipPayload.room.roomId}`);
228+
await this.algorithm.handleRoomUpdate(membershipPayload.room, RoomUpdateCause.PossibleTagChange);
229+
return;
230+
}
209231
} else if (payload.action === 'view_room') {
210232
// TODO: Update sticky room
211233
console.log(payload);

src/stores/room-list/algorithms/list-ordering/ImportanceAlgorithm.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ export class ImportanceAlgorithm extends Algorithm {
189189
}
190190

191191
public async handleRoomUpdate(room: Room, cause: RoomUpdateCause): Promise<boolean> {
192+
if (cause === RoomUpdateCause.PossibleTagChange) {
193+
// TODO: Be smarter and splice rather than regen the planet.
194+
// TODO: No-op if no change.
195+
await this.setKnownRooms(this.rooms);
196+
return;
197+
}
198+
192199
if (cause === RoomUpdateCause.NewRoom) {
193200
// TODO: Be smarter and insert rather than regen the planet.
194201
await this.setKnownRooms([room, ...this.rooms]);

src/stores/room-list/models.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export type TagID = string | DefaultTagID;
3838

3939
export enum RoomUpdateCause {
4040
Timeline = "TIMELINE",
41-
RoomRead = "ROOM_READ", // TODO: Use this.
41+
PossibleTagChange = "POSSIBLE_TAG_CHANGE",
42+
ReadReceipt = "READ_RECEIPT",
4243
NewRoom = "NEW_ROOM",
4344
}

0 commit comments

Comments
 (0)