Skip to content

Commit

Permalink
bugfix: distinguish between a 0 invited_count and a missing invited_c…
Browse files Browse the repository at this point in the history
…ount
  • Loading branch information
kegsay committed Jul 7, 2023
1 parent 150821f commit f22ef91
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion sync3/handler/connstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ func (s *ConnState) getInitialRoomData(ctx context.Context, roomSub sync3.RoomSu
Initial: true,
IsDM: userRoomData.IsDM,
JoinedCount: metadata.JoinCount,
InvitedCount: metadata.InviteCount,
InvitedCount: &metadata.InviteCount,
PrevBatch: userRoomData.RequestedPrevBatch,
}
}
Expand Down
2 changes: 1 addition & 1 deletion sync3/handler/connstate_live.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (s *connStateLive) processLiveUpdate(ctx context.Context, up caches.Update,
thisRoom.Name = internal.CalculateRoomName(metadata, 5) // TODO: customisable?
}
if delta.InviteCountChanged {
thisRoom.InvitedCount = roomUpdate.GlobalRoomMetadata().InviteCount
thisRoom.InvitedCount = &roomUpdate.GlobalRoomMetadata().InviteCount
}
if delta.JoinCountChanged {
thisRoom.JoinedCount = roomUpdate.GlobalRoomMetadata().JoinCount
Expand Down
3 changes: 2 additions & 1 deletion sync3/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sync3

import (
"encoding/json"

"github.com/matrix-org/sliding-sync/internal"

"github.com/matrix-org/sliding-sync/sync3/caches"
Expand All @@ -17,7 +18,7 @@ type Room struct {
Initial bool `json:"initial,omitempty"`
IsDM bool `json:"is_dm,omitempty"`
JoinedCount int `json:"joined_count,omitempty"`
InvitedCount int `json:"invited_count,omitempty"`
InvitedCount *int `json:"invited_count,omitempty"`
PrevBatch string `json:"prev_batch,omitempty"`
NumLive int `json:"num_live,omitempty"`
}
Expand Down
2 changes: 1 addition & 1 deletion tests-e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func MatchRoomInviteState(events []Event, partial bool) m.RoomMatcher {
}
}
if !found {
return fmt.Errorf("MatchRoomInviteState: want event %+v but it does not exist", want)
return fmt.Errorf("MatchRoomInviteState: want event %+v but it does not exist or failed to pass equality checks", want)
}
}
return nil
Expand Down
23 changes: 20 additions & 3 deletions tests-e2e/membership_transitions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,22 @@ func TestRoomStateTransitions(t *testing.T) {
m.MatchRoomHighlightCount(1),
m.MatchRoomInitial(true),
m.MatchRoomRequiredState(nil),
// TODO m.MatchRoomInviteState(inviteStrippedState.InviteState.Events),
m.MatchInviteCount(1),
m.MatchJoinCount(1),
MatchRoomInviteState([]Event{
{
Type: "m.room.create",
StateKey: ptr(""),
// no content as it includes the room version which we don't want to guess/hardcode
},
{
Type: "m.room.join_rules",
StateKey: ptr(""),
Content: map[string]interface{}{
"join_rule": "public",
},
},
}, true),
},
joinRoomID: {},
}),
Expand Down Expand Up @@ -105,6 +120,8 @@ func TestRoomStateTransitions(t *testing.T) {
},
}),
m.MatchRoomInitial(true),
m.MatchJoinCount(2),
m.MatchInviteCount(0),
m.MatchRoomHighlightCount(0),
))
}
Expand Down Expand Up @@ -467,7 +484,7 @@ func TestMemberCounts(t *testing.T) {
m.MatchResponse(t, res, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
secondRoomID: {
m.MatchRoomInitial(false),
m.MatchInviteCount(0),
m.MatchNoInviteCount(),
m.MatchJoinCount(0), // omitempty
},
}))
Expand All @@ -486,7 +503,7 @@ func TestMemberCounts(t *testing.T) {
m.MatchResponse(t, res, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
secondRoomID: {
m.MatchRoomInitial(false),
m.MatchInviteCount(0),
m.MatchNoInviteCount(),
m.MatchJoinCount(2),
},
}))
Expand Down
16 changes: 14 additions & 2 deletions testutils/m/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,22 @@ func MatchJoinCount(count int) RoomMatcher {
}
}

func MatchNoInviteCount() RoomMatcher {
return func(r sync3.Room) error {
if r.InvitedCount != nil {
return fmt.Errorf("MatchInviteCount: invited_count is present when it should be missing: val=%v", *r.InvitedCount)
}
return nil
}
}

func MatchInviteCount(count int) RoomMatcher {
return func(r sync3.Room) error {
if r.InvitedCount != count {
return fmt.Errorf("MatchInviteCount: got %v want %v", r.InvitedCount, count)
if r.InvitedCount == nil {
return fmt.Errorf("MatchInviteCount: invited_count is missing")
}
if *r.InvitedCount != count {
return fmt.Errorf("MatchInviteCount: got %v want %v", *r.InvitedCount, count)
}
return nil
}
Expand Down

0 comments on commit f22ef91

Please sign in to comment.