hicli/sync: make sorting timestamps more sensible on init sync

This commit is contained in:
Tulir Asokan 2024-10-20 01:20:35 +03:00
parent 56f11fcb8f
commit f38946b96e

View file

@ -12,6 +12,7 @@ import (
"fmt" "fmt"
"slices" "slices"
"strings" "strings"
"time"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
@ -165,7 +166,12 @@ func (h *HiClient) processSyncJoinedRoom(ctx context.Context, roomID id.RoomID,
if err != nil { if err != nil {
return fmt.Errorf("failed to ensure room row exists: %w", err) return fmt.Errorf("failed to ensure room row exists: %w", err)
} }
existingRoomData = &database.Room{ID: roomID, SortingTimestamp: jsontime.UnixMilliNow()} existingRoomData = &database.Room{
ID: roomID,
// Hack to set a default value for SortingTimestamp which is before all existing rooms,
// but not the same for all rooms without a timestamp.
SortingTimestamp: jsontime.UM(time.UnixMilli(time.Now().Unix())),
}
} }
for _, evt := range room.AccountData.Events { for _, evt := range room.AccountData.Events {
@ -466,6 +472,8 @@ func (h *HiClient) processEvent(
return dbEvt, err return dbEvt, err
} }
var unsetSortingTimestamp = time.UnixMilli(1000000000000)
func (h *HiClient) processStateAndTimeline( func (h *HiClient) processStateAndTimeline(
ctx context.Context, ctx context.Context,
room *database.Room, room *database.Room,
@ -629,6 +637,9 @@ func (h *HiClient) processStateAndTimeline(
setNewState(evt.Type, *evt.StateKey, timelineIDs[i]) setNewState(evt.Type, *evt.StateKey, timelineIDs[i])
} }
} }
if updatedRoom.SortingTimestamp.Before(unsetSortingTimestamp) && len(timeline.Events) > 0 {
updatedRoom.SortingTimestamp = jsontime.UM(time.UnixMilli(timeline.Events[len(timeline.Events)-1].Timestamp))
}
for _, entry := range decryptionQueue { for _, entry := range decryptionQueue {
err = h.DB.SessionRequest.Put(ctx, entry) err = h.DB.SessionRequest.Put(ctx, entry)
if err != nil { if err != nil {