Skip to content

Commit 3322b47

Browse files
authored
Make self membership less prone to races (#2277)
1 parent b832129 commit 3322b47

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

src/models/room.ts

+21-16
Original file line numberDiff line numberDiff line change
@@ -2228,22 +2228,27 @@ export class Room extends TypedEventEmitter<EmittedEvents, RoomEventHandlerMap>
22282228
// set fake stripped state events if this is an invite room so logic remains
22292229
// consistent elsewhere.
22302230
const membershipEvent = this.currentState.getStateEvents(EventType.RoomMember, this.myUserId);
2231-
if (membershipEvent && membershipEvent.getContent().membership === "invite") {
2232-
const strippedStateEvents = membershipEvent.getUnsigned().invite_room_state || [];
2233-
strippedStateEvents.forEach((strippedEvent) => {
2234-
const existingEvent = this.currentState.getStateEvents(strippedEvent.type, strippedEvent.state_key);
2235-
if (!existingEvent) {
2236-
// set the fake stripped event instead
2237-
this.currentState.setStateEvents([new MatrixEvent({
2238-
type: strippedEvent.type,
2239-
state_key: strippedEvent.state_key,
2240-
content: strippedEvent.content,
2241-
event_id: "$fake" + Date.now(),
2242-
room_id: this.roomId,
2243-
user_id: this.myUserId, // technically a lie
2244-
})]);
2245-
}
2246-
});
2231+
if (membershipEvent) {
2232+
const membership = membershipEvent.getContent().membership;
2233+
this.updateMyMembership(membership);
2234+
2235+
if (membership === "invite") {
2236+
const strippedStateEvents = membershipEvent.getUnsigned().invite_room_state || [];
2237+
strippedStateEvents.forEach((strippedEvent) => {
2238+
const existingEvent = this.currentState.getStateEvents(strippedEvent.type, strippedEvent.state_key);
2239+
if (!existingEvent) {
2240+
// set the fake stripped event instead
2241+
this.currentState.setStateEvents([new MatrixEvent({
2242+
type: strippedEvent.type,
2243+
state_key: strippedEvent.state_key,
2244+
content: strippedEvent.content,
2245+
event_id: "$fake" + Date.now(),
2246+
room_id: this.roomId,
2247+
user_id: this.myUserId, // technically a lie
2248+
})]);
2249+
}
2250+
});
2251+
}
22472252
}
22482253

22492254
const oldName = this.name;

src/sync.ts

-5
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,6 @@ export class SyncApi {
11701170
stateEvents.forEach(function(e) {
11711171
client.emit(ClientEvent.Event, e);
11721172
});
1173-
room.updateMyMembership("invite");
11741173
});
11751174

11761175
// Handle joins
@@ -1317,8 +1316,6 @@ export class SyncApi {
13171316
client.emit(ClientEvent.Event, e);
13181317
});
13191318

1320-
room.updateMyMembership("join");
1321-
13221319
// Decrypt only the last message in all rooms to make sure we can generate a preview
13231320
// And decrypt all events after the recorded read receipt to ensure an accurate
13241321
// notification count
@@ -1352,8 +1349,6 @@ export class SyncApi {
13521349
accountDataEvents.forEach(function(e) {
13531350
client.emit(ClientEvent.Event, e);
13541351
});
1355-
1356-
room.updateMyMembership("leave");
13571352
});
13581353

13591354
// update the notification timeline, if appropriate.

0 commit comments

Comments
 (0)