Skip to content

Commit

Permalink
Make self membership less prone to races (#2277)
Browse files Browse the repository at this point in the history
  • Loading branch information
robintown authored Apr 6, 2022
1 parent b832129 commit 3322b47
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
37 changes: 21 additions & 16 deletions src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2228,22 +2228,27 @@ export class Room extends TypedEventEmitter<EmittedEvents, RoomEventHandlerMap>
// set fake stripped state events if this is an invite room so logic remains
// consistent elsewhere.
const membershipEvent = this.currentState.getStateEvents(EventType.RoomMember, this.myUserId);
if (membershipEvent && membershipEvent.getContent().membership === "invite") {
const strippedStateEvents = membershipEvent.getUnsigned().invite_room_state || [];
strippedStateEvents.forEach((strippedEvent) => {
const existingEvent = this.currentState.getStateEvents(strippedEvent.type, strippedEvent.state_key);
if (!existingEvent) {
// set the fake stripped event instead
this.currentState.setStateEvents([new MatrixEvent({
type: strippedEvent.type,
state_key: strippedEvent.state_key,
content: strippedEvent.content,
event_id: "$fake" + Date.now(),
room_id: this.roomId,
user_id: this.myUserId, // technically a lie
})]);
}
});
if (membershipEvent) {
const membership = membershipEvent.getContent().membership;
this.updateMyMembership(membership);

if (membership === "invite") {
const strippedStateEvents = membershipEvent.getUnsigned().invite_room_state || [];
strippedStateEvents.forEach((strippedEvent) => {
const existingEvent = this.currentState.getStateEvents(strippedEvent.type, strippedEvent.state_key);
if (!existingEvent) {
// set the fake stripped event instead
this.currentState.setStateEvents([new MatrixEvent({
type: strippedEvent.type,
state_key: strippedEvent.state_key,
content: strippedEvent.content,
event_id: "$fake" + Date.now(),
room_id: this.roomId,
user_id: this.myUserId, // technically a lie
})]);
}
});
}
}

const oldName = this.name;
Expand Down
5 changes: 0 additions & 5 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,6 @@ export class SyncApi {
stateEvents.forEach(function(e) {
client.emit(ClientEvent.Event, e);
});
room.updateMyMembership("invite");
});

// Handle joins
Expand Down Expand Up @@ -1317,8 +1316,6 @@ export class SyncApi {
client.emit(ClientEvent.Event, e);
});

room.updateMyMembership("join");

// Decrypt only the last message in all rooms to make sure we can generate a preview
// And decrypt all events after the recorded read receipt to ensure an accurate
// notification count
Expand Down Expand Up @@ -1352,8 +1349,6 @@ export class SyncApi {
accountDataEvents.forEach(function(e) {
client.emit(ClientEvent.Event, e);
});

room.updateMyMembership("leave");
});

// update the notification timeline, if appropriate.
Expand Down

0 comments on commit 3322b47

Please sign in to comment.