Sort child rooms

This commit is contained in:
Kegan Dougal 2022-02-28 17:15:44 +00:00
parent 29f4f1e45a
commit dd21572f6d

View file

@ -20,6 +20,7 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"net/url" "net/url"
"sort"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -219,7 +220,7 @@ func (w *walker) walk() util.JSONResponse {
}} }}
processed := make(set) processed := make(set)
for len(unvisited) > 0 { for len(unvisited) > 0 {
if len(discoveredRooms) > w.limit { if len(discoveredRooms) >= w.limit {
break break
} }
@ -290,22 +291,21 @@ func (w *walker) walk() util.JSONResponse {
continue continue
} }
uniqueRooms := make(map[string][]string) // For each referenced room ID in the child events being returned to the caller
for _, ev := range discoveredChildEvents { // add the room ID to the queue of unvisited rooms. Loop from the beginning.
// We need to invert the order here because the child events are lo->hi on the timestamp,
// so we need to ensure we pop in the same lo->hi order, which won't be the case if we
// insert the highest timestamp last in a stack.
for i := len(discoveredChildEvents) - 1; i >= 0; i-- {
spaceContent := struct { spaceContent := struct {
Via []string `json:"via"` Via []string `json:"via"`
}{} }{}
ev := discoveredChildEvents[i]
_ = json.Unmarshal(ev.Content, &spaceContent) _ = json.Unmarshal(ev.Content, &spaceContent)
uniqueRooms[ev.StateKey] = spaceContent.Via
}
// For each referenced room ID in the child events being returned to the caller
// add the room ID to the queue of unvisited rooms. Loop from the beginning.
for roomID, vias := range uniqueRooms {
unvisited = append(unvisited, roomVisit{ unvisited = append(unvisited, roomVisit{
roomID: roomID, roomID: ev.StateKey,
depth: rv.depth + 1, depth: rv.depth + 1,
vias: vias, vias: spaceContent.Via,
}) })
} }
} }
@ -540,6 +540,11 @@ func (w *walker) childReferences(roomID string) ([]gomatrixserverlib.MSC2946Stri
el = append(el, *strip) el = append(el, *strip)
} }
} }
// sort by origin_server_ts as per MSC2946
sort.Slice(el, func(i, j int) bool {
return el[i].OriginServerTS < el[j].OriginServerTS
})
return el, nil return el, nil
} }
@ -555,6 +560,7 @@ func stripped(ev *gomatrixserverlib.Event) *gomatrixserverlib.MSC2946StrippedEve
Content: ev.Content(), Content: ev.Content(),
Sender: ev.Sender(), Sender: ev.Sender(),
RoomID: ev.RoomID(), RoomID: ev.RoomID(),
OriginServerTS: ev.OriginServerTS(),
} }
} }