Tweak GetStateDeltas behaviour (#2788)

Improves the control flow of `GetStateDeltas` for clarity and possibly
also fixes a bug where duplicate state delta entries could be inserted
with different memberships instead of being correctly overridden by
`join`.
This commit is contained in:
Neil Alexander 2022-10-11 10:58:34 +01:00 committed by GitHub
parent 9ed8ff6b93
commit 3920b9f9b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -360,34 +360,50 @@ func (d *DatabaseTransaction) GetStateDeltas(
newlyJoinedRooms := make(map[string]bool, len(state)) newlyJoinedRooms := make(map[string]bool, len(state))
for roomID, stateStreamEvents := range state { for roomID, stateStreamEvents := range state {
for _, ev := range stateStreamEvents { for _, ev := range stateStreamEvents {
if membership, prevMembership := getMembershipFromEvent(ev.Event, userID); membership != "" { // Look for our membership in the state events and skip over any
if membership == gomatrixserverlib.Join && prevMembership != membership { // membership events that are not related to us.
// send full room state down instead of a delta membership, prevMembership := getMembershipFromEvent(ev.Event, userID)
if membership == "" {
continue
}
if membership == gomatrixserverlib.Join {
// If our membership is now join but the previous membership wasn't
// then this is a "join transition", so we'll insert this room.
if prevMembership != membership {
// Get the full room state, as we'll send that down for a newly
// joined room instead of a delta.
var s []types.StreamEvent var s []types.StreamEvent
s, err = d.currentStateStreamEventsForRoom(ctx, roomID, stateFilter) if s, err = d.currentStateStreamEventsForRoom(ctx, roomID, stateFilter); err != nil {
if err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
continue continue
} }
return nil, nil, err return nil, nil, err
} }
// Add the information for this room into the state so that
// it will get added with all of the rest of the joined rooms.
state[roomID] = s state[roomID] = s
newlyJoinedRooms[roomID] = true newlyJoinedRooms[roomID] = true
continue // we'll add this room in when we do joined rooms
} }
deltas = append(deltas, types.StateDelta{ // We won't add joined rooms into the delta at this point as they
Membership: membership, // are added later on.
MembershipPos: ev.StreamPosition, continue
StateEvents: d.StreamEventsToEvents(device, stateStreamEvents),
RoomID: roomID,
})
break
} }
deltas = append(deltas, types.StateDelta{
Membership: membership,
MembershipPos: ev.StreamPosition,
StateEvents: d.StreamEventsToEvents(device, stateStreamEvents),
RoomID: roomID,
})
break
} }
} }
// Add in currently joined rooms // Finally, add in currently joined rooms, including those from the
// join transitions above.
for _, joinedRoomID := range joinedRoomIDs { for _, joinedRoomID := range joinedRoomIDs {
deltas = append(deltas, types.StateDelta{ deltas = append(deltas, types.StateDelta{
Membership: gomatrixserverlib.Join, Membership: gomatrixserverlib.Join,