Move "heroes" to RoomSubscription

This commit is contained in:
Till Faelligen 2023-09-15 16:57:12 +02:00
parent ce99d0f911
commit e265e3b220
No known key found for this signature in database
GPG Key ID: ACCDC9606D472758
5 changed files with 55 additions and 3 deletions

View File

@ -680,7 +680,7 @@ func (s *ConnState) getInitialRoomData(ctx context.Context, roomSub sync3.RoomSu
PrevBatch: userRoomData.RequestedLatestEvents.PrevBatch,
Timestamp: maxTs,
}
if calculated {
if roomSub.IncludeHeroes() && calculated {
room.Heroes = metadata.Heroes
}
rooms[roomID] = room

View File

@ -262,7 +262,8 @@ func (s *connStateLive) processLiveUpdate(ctx context.Context, up caches.Update,
roomName, calculated := internal.CalculateRoomName(metadata, 5) // TODO: customisable?
thisRoom.Name = roomName
if calculated {
if s.roomSubscriptions[roomUpdate.RoomID()].IncludeHeroes() && calculated {
thisRoom.Heroes = metadata.Heroes
}
}

View File

@ -57,7 +57,6 @@ type RequestList struct {
SlowGetAllRooms *bool `json:"slow_get_all_rooms,omitempty"`
Deleted bool `json:"deleted,omitempty"`
BumpEventTypes []string `json:"bump_event_types"`
Heroes bool `json:"heroes"`
}
func (rl *RequestList) ShouldGetAllRooms() bool {
@ -395,12 +394,17 @@ func (r *Request) ApplyDelta(nextReq *Request) (result *Request, delta *RequestD
if bumpEventTypes == nil {
bumpEventTypes = existingList.BumpEventTypes
}
heroes := nextList.Heroes
if heroes == nil {
heroes = existingList.Heroes
}
calculatedLists[listKey] = RequestList{
RoomSubscription: RoomSubscription{
RequiredState: reqState,
TimelineLimit: timelineLimit,
IncludeOldRooms: includeOldRooms,
Heroes: heroes,
},
Ranges: rooms,
Sort: sort,
@ -565,6 +569,7 @@ type RoomSubscription struct {
RequiredState [][2]string `json:"required_state"`
TimelineLimit int64 `json:"timeline_limit"`
IncludeOldRooms *RoomSubscription `json:"include_old_rooms"`
Heroes *bool `json:"heroes"`
}
func (rs RoomSubscription) RequiredStateChanged(other RoomSubscription) bool {
@ -588,6 +593,10 @@ func (rs RoomSubscription) LazyLoadMembers() bool {
return false
}
func (rs RoomSubscription) IncludeHeroes() bool {
return rs.Heroes != nil && *rs.Heroes
}
// Combine this subcription with another, returning a union of both as a copy.
func (rs RoomSubscription) Combine(other RoomSubscription) RoomSubscription {
return rs.combineRecursive(other, true)

View File

@ -695,6 +695,7 @@ func (c *CSAPI) SlidingSyncUntilMembership(t *testing.T, pos string, roomID stri
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomID: {
TimelineLimit: 10,
Heroes: &boolTrue,
},
},
}, func(r *sync3.Response) error {
@ -713,6 +714,7 @@ func (c *CSAPI) SlidingSyncUntilMembership(t *testing.T, pos string, roomID stri
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomID: {
TimelineLimit: 10,
Heroes: &boolTrue,
},
},
}, func(r *sync3.Response) error {

View File

@ -7,6 +7,7 @@ import (
"github.com/matrix-org/sliding-sync/sync3"
"github.com/matrix-org/sliding-sync/testutils/m"
"github.com/tidwall/gjson"
)
func TestRoomStateTransitions(t *testing.T) {
@ -620,6 +621,45 @@ func TestHeroesOnMembershipChanges(t *testing.T) {
t.Errorf("expected no heroes, got %#v", res.Rooms[aliasRoomID].Heroes)
}
})
t.Run("can set heroes=true on room subscriptions", func(t *testing.T) {
subRoomID := alice.CreateRoom(t, map[string]interface{}{"preset": "public_chat"})
bob.JoinRoom(t, subRoomID, []string{})
// Start without requesting heroes
res := alice.SlidingSyncUntil(t, "", sync3.Request{
RoomSubscriptions: map[string]sync3.RoomSubscription{
subRoomID: {
TimelineLimit: 10,
Heroes: &boolTrue,
},
},
}, func(response *sync3.Response) error {
r, ok := response.Rooms[subRoomID]
if !ok {
return fmt.Errorf("room %q not in response", subRoomID)
}
// wait for bob to be joined
for _, ev := range r.Timeline {
if gjson.GetBytes(ev, "type").Str != "m.room.member" {
continue
}
if gjson.GetBytes(ev, "state_key").Str != bob.UserID {
continue
}
if gjson.GetBytes(ev, "content.membership").Str == "join" {
return nil
}
}
return fmt.Errorf("%s is not joined to room %q", bob.UserID, subRoomID)
})
if c := len(res.Rooms[subRoomID].Heroes); c > 1 {
t.Errorf("expected 1 room hero, got %d", c)
}
if gotUserID := res.Rooms[subRoomID].Heroes[0].ID; gotUserID != bob.UserID {
t.Errorf("expected userID %q, got %q", gotUserID, bob.UserID)
}
})
}
// test invite/join counts update and are accurate