Skip to content

Commit 28e6eea

Browse files
committed
feat: cache the UI timelines along the room
1 parent 7f413fc commit 28e6eea

1 file changed

Lines changed: 37 additions & 29 deletions

File tree

internal/api/rust/rust.go

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ const (
5252
const ProcessNameNSE string = "NSE"
5353

5454
type RustRoomInfo struct {
55-
stream *matrix_sdk_ffi.TaskHandle
56-
room *matrix_sdk_ffi.Room
57-
timeline []*api.Event
55+
stream *matrix_sdk_ffi.TaskHandle
56+
room *matrix_sdk_ffi.Room
57+
timeline []*api.Event
58+
ui_timeline *matrix_sdk_ffi.Timeline
5859
}
5960

6061
type RustClient struct {
@@ -326,8 +327,8 @@ func (c *RustClient) Close(t ct.TestLike) {
326327

327328
func (c *RustClient) GetEvent(t ct.TestLike, roomID, eventID string) (*api.Event, error) {
328329
t.Helper()
329-
room := c.findRoom(t, roomID)
330-
timelineItem, err := mustGetTimeline(t, room).GetEventTimelineItemByEventId(eventID)
330+
_, ui_timeline := c.findRoom(t, roomID)
331+
timelineItem, err := ui_timeline.GetEventTimelineItemByEventId(eventID)
331332
if err != nil {
332333
return nil, fmt.Errorf("failed to GetEventTimelineItemByEventId(%s): %s", eventID, err)
333334
}
@@ -464,7 +465,7 @@ func (c *RustClient) StartSyncing(t ct.TestLike) (stopSyncing func(), err error)
464465
// provide a bogus room ID.
465466
func (c *RustClient) IsRoomEncrypted(t ct.TestLike, roomID string) (bool, error) {
466467
t.Helper()
467-
r := c.findRoom(t, roomID)
468+
r, _ := c.findRoom(t, roomID)
468469
if r == nil {
469470
rooms := c.FFIClient.Rooms()
470471
return false, fmt.Errorf("failed to find room %s, got %d rooms", roomID, len(rooms))
@@ -541,7 +542,7 @@ func (c *RustClient) SendMessage(t ct.TestLike, roomID, text string) (eventID st
541542
// we need a timeline listener before we can send messages, AND that listener must be attached to the
542543
// same *Room you call .Send on :S
543544
c.ensureListening(t, roomID)
544-
r := c.findRoom(t, roomID)
545+
r, timeline := c.findRoom(t, roomID)
545546
cancel := c.roomsListener.AddListener(func(broadcastRoomID string) bool {
546547
if roomID != broadcastRoomID {
547548
return false
@@ -573,9 +574,8 @@ func (c *RustClient) SendMessage(t ct.TestLike, roomID, text string) (eventID st
573574
err = fmt.Errorf("SendMessage(rust) %s: failed to find room %s", c.userID, roomID)
574575
return
575576
}
576-
timeline, err := r.Timeline()
577-
if err != nil {
578-
err = fmt.Errorf("SendMessage(rust) %s: %s", c.userID, err)
577+
if timeline == nil {
578+
err = fmt.Errorf("SendMessage(rust) %s: failed to get timeline %s", c.userID, roomID)
579579
return
580580
}
581581
timeline.Send(matrix_sdk_ffi.MessageEventContentFromHtml(text, text))
@@ -590,17 +590,17 @@ func (c *RustClient) SendMessage(t ct.TestLike, roomID, text string) (eventID st
590590

591591
func (c *RustClient) InviteUser(t ct.TestLike, roomID, userID string) error {
592592
t.Helper()
593-
r := c.findRoom(t, roomID)
593+
r, _ := c.findRoom(t, roomID)
594594
return r.InviteUserById(userID)
595595
}
596596

597597
func (c *RustClient) Backpaginate(t ct.TestLike, roomID string, count int) error {
598598
t.Helper()
599-
r := c.findRoom(t, roomID)
600-
if r == nil {
601-
return fmt.Errorf("Backpaginate: cannot find room %s", roomID)
599+
_, timeline := c.findRoom(t, roomID)
600+
if timeline == nil {
601+
return fmt.Errorf("Backpaginate: cannot find timeline for room %s", roomID)
602602
}
603-
_, err := mustGetTimeline(t, r).PaginateBackwards(uint16(count))
603+
_, err := timeline.PaginateBackwards(uint16(count))
604604
if err != nil {
605605
return fmt.Errorf("cannot PaginateBackwards in %s: %s", roomID, err)
606606
}
@@ -611,24 +611,24 @@ func (c *RustClient) UserID() string {
611611
return c.userID
612612
}
613613

614-
func (c *RustClient) findRoomInCache(roomID string) *matrix_sdk_ffi.Room {
614+
func (c *RustClient) findRoomInCache(roomID string) (*matrix_sdk_ffi.Room, *matrix_sdk_ffi.Timeline) {
615615
c.roomsMu.RLock()
616616
defer c.roomsMu.RUnlock()
617617
// do we have a reference to it already?
618618
roomInfo := c.rooms[roomID]
619619
if roomInfo != nil {
620-
return roomInfo.room
620+
return roomInfo.room, roomInfo.ui_timeline
621621
}
622-
return nil
622+
return nil, nil
623623
}
624624

625625
// findRoom tries to find the room in the FFI client. Has a cache of already found rooms to ensure
626626
// the same pointer is always returned for the same room.
627-
func (c *RustClient) findRoom(t ct.TestLike, roomID string) *matrix_sdk_ffi.Room {
627+
func (c *RustClient) findRoom(t ct.TestLike, roomID string) (*matrix_sdk_ffi.Room, *matrix_sdk_ffi.Timeline) {
628628
t.Helper()
629-
room := c.findRoomInCache(roomID)
629+
room, ui_timeline := c.findRoomInCache(roomID)
630630
if room != nil {
631-
return room
631+
return room, ui_timeline
632632
}
633633
// try to find it in all_rooms
634634
if c.allRooms != nil {
@@ -637,11 +637,13 @@ func (c *RustClient) findRoom(t ct.TestLike, roomID string) *matrix_sdk_ffi.Room
637637
c.Logf(t, "allRooms.Room(%s) err: %s", roomID, err)
638638
} else if room != nil {
639639
c.roomsMu.Lock()
640+
ui_timeline := mustGetTimeline(t, room)
640641
c.rooms[roomID] = &RustRoomInfo{
641-
room: room,
642+
room: room,
643+
ui_timeline: ui_timeline,
642644
}
643645
c.roomsMu.Unlock()
644-
return room
646+
return room, ui_timeline
645647
}
646648
}
647649
// try to find it from FFI
@@ -652,17 +654,20 @@ func (c *RustClient) findRoom(t ct.TestLike, roomID string) *matrix_sdk_ffi.Room
652654
_, exists := c.rooms[rid]
653655
if !exists {
654656
c.roomsMu.Lock()
657+
room := rooms[i]
658+
ui_timeline := mustGetTimeline(t, room)
655659
c.rooms[rid] = &RustRoomInfo{
656-
room: rooms[i],
660+
room: room,
661+
ui_timeline: ui_timeline,
657662
}
658663
c.roomsMu.Unlock()
659664
}
660665
if r.Id() == roomID {
661-
return c.rooms[rid].room
666+
return c.rooms[rid].room, c.rooms[rid].ui_timeline
662667
}
663668
}
664669
// we really don't know about this room yet
665-
return nil
670+
return nil, nil
666671
}
667672

668673
func (c *RustClient) Logf(t ct.TestLike, format string, args ...interface{}) {
@@ -679,14 +684,14 @@ func (c *RustClient) logToFile(t ct.TestLike, format string, args ...interface{}
679684

680685
func (c *RustClient) ensureListening(t ct.TestLike, roomID string) {
681686
t.Helper()
682-
r := c.findRoom(t, roomID)
687+
r, ui_timeline := c.findRoom(t, roomID)
683688
if r == nil {
684689
// we allow the room to not exist yet. If this happens, wait until we see the room before continuing
685690
c.roomsListener.AddListener(func(broadcastRoomID string) bool {
686691
if broadcastRoomID != roomID {
687692
return false
688693
}
689-
if room := c.findRoom(t, roomID); room != nil {
694+
if room, _ := c.findRoom(t, roomID); room != nil {
690695
// Do this asynchronously because adding a timeline listener is done synchronously
691696
// which will cause "signal arrived during cgo execution" if it happens within
692697
// this rooms listener callback.
@@ -699,7 +704,9 @@ func (c *RustClient) ensureListening(t ct.TestLike, roomID string) {
699704
})
700705
return
701706
}
707+
702708
must.NotEqual(t, r, nil, fmt.Sprintf("room %s does not exist", roomID))
709+
must.NotEqual(t, ui_timeline, nil, fmt.Sprintf("ui_timeline for room %s does not exist", roomID))
703710

704711
info := c.rooms[roomID]
705712
if info != nil && info.stream != nil {
@@ -713,7 +720,8 @@ func (c *RustClient) ensureListening(t ct.TestLike, roomID string) {
713720
// as setting the initial entries clears the timeline, which can then result in test flakes.
714721
waiter := helpers.NewWaiter()
715722
c.rooms[roomID].timeline = make([]*api.Event, 0)
716-
result := mustGetTimeline(t, r).AddListener(&timelineListener{fn: func(diff []*matrix_sdk_ffi.TimelineDiff) {
723+
724+
result := ui_timeline.AddListener(&timelineListener{fn: func(diff []*matrix_sdk_ffi.TimelineDiff) {
717725
defer waiter.Finish()
718726
timeline := c.rooms[roomID].timeline
719727
var newEvents []*api.Event

0 commit comments

Comments
 (0)