This commit is contained in:
Kegan Dougal 2021-01-11 19:51:08 +00:00
parent 88faf7104c
commit 0a7855c894
3 changed files with 6 additions and 83 deletions

View file

@ -117,10 +117,7 @@ func spacesHandler(db Database, rsAPI roomserver.RoomserverInternalAPI) func(*ht
rsAPI: rsAPI, rsAPI: rsAPI,
inMemoryBatchCache: inMemoryBatchCache, inMemoryBatchCache: inMemoryBatchCache,
} }
res, resErr := w.walk() res := w.walk()
if resErr != nil {
return *resErr
}
return util.JSONResponse{ return util.JSONResponse{
Code: 200, Code: 200,
JSON: res, JSON: res,
@ -162,7 +159,8 @@ func (w *walker) markSent(id string) {
w.inMemoryBatchCache[w.caller.UserID+"|"+w.caller.ID] = m w.inMemoryBatchCache[w.caller.UserID+"|"+w.caller.ID] = m
} }
func (w *walker) walk() (*SpacesResponse, *util.JSONResponse) { // nolint:gocyclo
func (w *walker) walk() *SpacesResponse {
var res SpacesResponse var res SpacesResponse
// Begin walking the graph starting with the room ID in the request in a queue of unvisited rooms // Begin walking the graph starting with the room ID in the request in a queue of unvisited rooms
unvisited := []string{w.rootRoomID} unvisited := []string{w.rootRoomID}
@ -255,7 +253,7 @@ func (w *walker) walk() (*SpacesResponse, *util.JSONResponse) {
unvisited = append(unvisited, roomID) unvisited = append(unvisited, roomID)
} }
} }
return &res, nil return &res
} }
func (w *walker) stateEvent(roomID, evType, stateKey string) *gomatrixserverlib.HeaderedEvent { func (w *walker) stateEvent(roomID, evType, stateKey string) *gomatrixserverlib.HeaderedEvent {
@ -341,19 +339,6 @@ func (w *walker) references(roomID string) (eventLookup, error) {
// NOT THREAD SAFE // NOT THREAD SAFE
type eventLookup map[string][]*gomatrixserverlib.HeaderedEvent type eventLookup map[string][]*gomatrixserverlib.HeaderedEvent
func (el eventLookup) get(roomID, evType, stateKey string) *gomatrixserverlib.HeaderedEvent {
evs := el[evType]
if len(evs) == 0 {
return nil
}
for _, ev := range evs {
if ev.RoomID() == roomID && ev.StateKeyEquals(stateKey) {
return ev
}
}
return nil
}
func (el eventLookup) set(ev *gomatrixserverlib.HeaderedEvent) { func (el eventLookup) set(ev *gomatrixserverlib.HeaderedEvent) {
evs := el[ev.Type()] evs := el[ev.Type()]
if evs == nil { if evs == nil {

View file

@ -18,15 +18,11 @@ import (
"bytes" "bytes"
"context" "context"
"crypto/ed25519" "crypto/ed25519"
"crypto/sha256"
"encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"sort"
"strings"
"testing" "testing"
"time" "time"
@ -36,7 +32,6 @@ import (
roomserver "github.com/matrix-org/dendrite/roomserver/api" roomserver "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/setup" "github.com/matrix-org/dendrite/setup"
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/setup/mscs/msc2836"
"github.com/matrix-org/dendrite/setup/mscs/msc2946" "github.com/matrix-org/dendrite/setup/mscs/msc2946"
userapi "github.com/matrix-org/dendrite/userapi/api" userapi "github.com/matrix-org/dendrite/userapi/api"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
@ -279,7 +274,7 @@ func newReq(t *testing.T, jsonBody map[string]interface{}) *msc2946.SpacesReques
func runServer(t *testing.T, router *mux.Router) func() { func runServer(t *testing.T, router *mux.Router) func() {
t.Helper() t.Helper()
externalServ := &http.Server{ externalServ := &http.Server{
Addr: string(":8009"), Addr: string(":8010"),
WriteTimeout: 60 * time.Second, WriteTimeout: 60 * time.Second,
Handler: router, Handler: router,
} }
@ -302,7 +297,7 @@ func postSpaces(t *testing.T, expectCode int, accessToken, roomID string, req *m
t.Fatalf("failed to marshal request: %s", err) t.Fatalf("failed to marshal request: %s", err)
} }
httpReq, err := http.NewRequest( httpReq, err := http.NewRequest(
"POST", "http://localhost:8009/_matrix/client/unstable/rooms/"+url.PathEscape(roomID)+"/spaces", "POST", "http://localhost:8010/_matrix/client/unstable/rooms/"+url.PathEscape(roomID)+"/spaces",
bytes.NewBuffer(data), bytes.NewBuffer(data),
) )
httpReq.Header.Set("Authorization", "Bearer "+accessToken) httpReq.Header.Set("Authorization", "Bearer "+accessToken)
@ -332,59 +327,6 @@ func postSpaces(t *testing.T, expectCode int, accessToken, roomID string, req *m
return nil return nil
} }
func assertContains(t *testing.T, result *msc2836.EventRelationshipResponse, wantEventIDs []string) {
t.Helper()
gotEventIDs := make([]string, len(result.Events))
for i, ev := range result.Events {
gotEventIDs[i] = ev.EventID
}
if len(gotEventIDs) != len(wantEventIDs) {
t.Fatalf("length mismatch: got %v want %v", gotEventIDs, wantEventIDs)
}
for i := range gotEventIDs {
if gotEventIDs[i] != wantEventIDs[i] {
t.Errorf("wrong item in position %d - got %s want %s", i, gotEventIDs[i], wantEventIDs[i])
}
}
}
func assertUnsignedChildren(t *testing.T, ev gomatrixserverlib.ClientEvent, relType string, wantCount int, childrenEventIDs []string) {
t.Helper()
unsigned := struct {
Children map[string]int `json:"children"`
Hash string `json:"children_hash"`
}{}
if err := json.Unmarshal(ev.Unsigned, &unsigned); err != nil {
if wantCount == 0 {
return // no children so possible there is no unsigned field at all
}
t.Fatalf("Failed to unmarshal unsigned field: %s", err)
}
// zero checks
if wantCount == 0 {
if len(unsigned.Children) != 0 || unsigned.Hash != "" {
t.Fatalf("want 0 children but got unsigned fields %+v", unsigned)
}
return
}
gotCount := unsigned.Children[relType]
if gotCount != wantCount {
t.Errorf("Got %d count, want %d count for rel_type %s", gotCount, wantCount, relType)
}
// work out the hash
sort.Strings(childrenEventIDs)
var b strings.Builder
for _, s := range childrenEventIDs {
b.WriteString(s)
}
t.Logf("hashing %s", b.String())
hashValBytes := sha256.Sum256([]byte(b.String()))
wantHash := base64.RawStdEncoding.EncodeToString(hashValBytes[:])
if wantHash != unsigned.Hash {
t.Errorf("Got unsigned hash %s want hash %s", unsigned.Hash, wantHash)
}
}
type testUserAPI struct { type testUserAPI struct {
accessTokens map[string]userapi.Device accessTokens map[string]userapi.Device
} }

View file

@ -30,10 +30,6 @@ var (
ConstSpaceChildEventType: 1, ConstSpaceChildEventType: 1,
ConstSpaceParentEventType: 2, ConstSpaceParentEventType: 2,
} }
relTypesEnum = map[int]string{
1: ConstSpaceChildEventType,
2: ConstSpaceParentEventType,
}
) )
type Database interface { type Database interface {