Skip to content

Commit

Permalink
Merge pull request #347 from matrix-org/dmr/keys
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Oct 19, 2023
2 parents a89499c + 9230cf9 commit bc47e18
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
14 changes: 14 additions & 0 deletions internal/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package internal

// Keys returns a slice containing copies of the keys of the given map, in no particular
// order.
func Keys[K comparable, V any](m map[K]V) []K {
if m == nil {
return nil
}
output := make([]K, 0, len(m))
for key := range m {
output = append(output, key)
}
return output
}
30 changes: 30 additions & 0 deletions internal/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package internal

import (
"reflect"
"sort"
"testing"
)

func TestKeys(t *testing.T) {
assertSlice(t, Keys((map[string]int)(nil)), nil)
assertSlice(t, Keys(map[string]int{}), []string{})
assertSlice(t, Keys(map[string]int{"a": 1}), []string{"a"})
assertSlice(t, Keys(map[string]int{"a": 1, "b": 2, "c": 3}), []string{"a", "b", "c"})
assertSlice(t, Keys(map[string]int{"": 1, "!": 1, "☃": 1, "\x00": 1}), []string{"", "!", "☃", "\x00"})
}

// assertSlicesSameElements errors the test if "got" and "want" have different elements.
// Both got and want are sorted in-place as a side effect.
func assertSlice(t *testing.T, got, want []string) {
if len(got) != len(want) {
t.Errorf("got length %d, expected length %d", len(got), len(want))
}

sort.Slice(got, func(i, j int) bool { return got[i] < got[j] })
sort.Slice(want, func(i, j int) bool { return want[i] < want[j] })

if !reflect.DeepEqual(got, want) {
t.Errorf("After sorting, got %v but expected %v", got, want)
}
}
17 changes: 2 additions & 15 deletions sync3/handler/connstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (s *ConnState) onIncomingRequest(reqCtx context.Context, req *sync3.Request
RoomIDToTimeline: response.RoomIDsToTimelineEventIDs(),
IsInitial: isInitial,
RoomIDsToLists: s.lists.ListsByVisibleRoomIDs(s.muxedReq.Lists),
AllSubscribedRooms: keys(s.roomSubscriptions),
AllSubscribedRooms: internal.Keys(s.roomSubscriptions),
AllLists: s.muxedReq.ListKeys(),
})
region.End()
Expand Down Expand Up @@ -574,7 +574,7 @@ func (s *ConnState) getInitialRoomData(ctx context.Context, roomSub sync3.RoomSu
for _, ev := range latestEvents.Timeline {
senders[gjson.GetBytes(ev, "sender").Str] = struct{}{}
}
roomToUsersInTimeline[roomID] = keys(senders)
roomToUsersInTimeline[roomID] = internal.Keys(senders)
roomToTimeline[roomID] = latestEvents.Timeline
// remember what we just loaded so if we see these events down the live stream we know to ignore them.
// This means that requesting a direct room subscription causes the connection to jump ahead to whatever
Expand Down Expand Up @@ -783,16 +783,3 @@ func clampSliceRangeToListSize(ctx context.Context, r [2]int64, totalRooms int64
return [2]int64{r[0], lastIndexWithRoom}
}
}

// keys returns a slice containing copies of the keys of the given map, in no particular
// order.
func keys[K comparable, V any](m map[K]V) []K {
if m == nil {
return nil
}
output := make([]K, len(m))
for key := range m {
output = append(output, key)
}
return output
}
2 changes: 1 addition & 1 deletion sync3/handler/connstate_live.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (s *connStateLive) processUpdate(ctx context.Context, update caches.Update,
UserID: s.userID,
DeviceID: s.deviceID,
RoomIDsToLists: roomIDsToLists,
AllSubscribedRooms: keys(s.roomSubscriptions),
AllSubscribedRooms: internal.Keys(s.roomSubscriptions),
AllLists: s.muxedReq.ListKeys(),
})
}
Expand Down

0 comments on commit bc47e18

Please sign in to comment.