From 9d480d58c95ad4626ec443ac916a86ff7f863ad3 Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Fri, 16 Oct 2020 19:10:07 +0200 Subject: [PATCH] Better creation/usage of JoinResponse --- syncapi/storage/shared/syncserver.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/syncapi/storage/shared/syncserver.go b/syncapi/storage/shared/syncserver.go index f2133dc89..e994b0d4e 100644 --- a/syncapi/storage/shared/syncserver.go +++ b/syncapi/storage/shared/syncserver.go @@ -540,10 +540,10 @@ func (d *Database) addTypingDeltaToResponse( joinedRoomIDs []string, res *types.Response, ) error { - var jr types.JoinResponse var ok bool var err error for _, roomID := range joinedRoomIDs { + var jr types.JoinResponse if typingUsers, updated := d.EDUCache.GetTypingUsersIfUpdatedAfter( roomID, int64(since.EDUPosition()), ); updated { @@ -574,9 +574,16 @@ func (d *Database) addReceiptDeltaToResponse( joinedRoomIDs []string, res *types.Response, ) error { - var jr types.JoinResponse + // TODO: pass joinedRoomIDs to SelectRoomReceiptsAfter instead of iterating over every room // check all joinedRooms for receipts for _, roomID := range joinedRoomIDs { + var jr types.JoinResponse + var ok bool + // Check if there's already a JoinResponse for this room, + // otherwise use a new one + if jr, ok = res.Rooms.Join[roomID]; !ok { + jr = types.JoinResponse{} + } receipts, err := d.Receipts.SelectRoomReceiptsAfter(context.TODO(), roomID, since.EDUPosition()) if err != nil { return err @@ -605,6 +612,11 @@ func (d *Database) addReceiptDeltaToResponse( jr.Ephemeral.Events = append(jr.Ephemeral.Events, ev) res.Rooms.Join[roomID] = jr } + // Only add new events if we didn't find the room in the map. + // If we found the room, they should already be added + if !ok { + res.Rooms.Join[roomID] = jr + } } return nil