From f22ef91da614621bc4111fb63e3d5fbac464aa6f Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 7 Jul 2023 15:16:59 +0100 Subject: [PATCH] bugfix: distinguish between a 0 invited_count and a missing invited_count --- sync3/handler/connstate.go | 2 +- sync3/handler/connstate_live.go | 2 +- sync3/room.go | 3 ++- tests-e2e/main_test.go | 2 +- tests-e2e/membership_transitions_test.go | 23 ++++++++++++++++++++--- testutils/m/match.go | 16 ++++++++++++++-- 6 files changed, 39 insertions(+), 9 deletions(-) diff --git a/sync3/handler/connstate.go b/sync3/handler/connstate.go index 4184a970..04d5e237 100644 --- a/sync3/handler/connstate.go +++ b/sync3/handler/connstate.go @@ -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, } } diff --git a/sync3/handler/connstate_live.go b/sync3/handler/connstate_live.go index 4f3579c8..8a2e2cdc 100644 --- a/sync3/handler/connstate_live.go +++ b/sync3/handler/connstate_live.go @@ -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 diff --git a/sync3/room.go b/sync3/room.go index 388b06b7..6bf7c35d 100644 --- a/sync3/room.go +++ b/sync3/room.go @@ -2,6 +2,7 @@ package sync3 import ( "encoding/json" + "github.com/matrix-org/sliding-sync/internal" "github.com/matrix-org/sliding-sync/sync3/caches" @@ -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"` } diff --git a/tests-e2e/main_test.go b/tests-e2e/main_test.go index 6d5805fb..52476773 100644 --- a/tests-e2e/main_test.go +++ b/tests-e2e/main_test.go @@ -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 diff --git a/tests-e2e/membership_transitions_test.go b/tests-e2e/membership_transitions_test.go index 9bfb7e1f..2271d78e 100644 --- a/tests-e2e/membership_transitions_test.go +++ b/tests-e2e/membership_transitions_test.go @@ -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: {}, }), @@ -105,6 +120,8 @@ func TestRoomStateTransitions(t *testing.T) { }, }), m.MatchRoomInitial(true), + m.MatchJoinCount(2), + m.MatchInviteCount(0), m.MatchRoomHighlightCount(0), )) } @@ -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 }, })) @@ -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), }, })) diff --git a/testutils/m/match.go b/testutils/m/match.go index e44d0d82..a7b2f635 100644 --- a/testutils/m/match.go +++ b/testutils/m/match.go @@ -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 }