From e385a5540ea00c78533e2853ea28b79e9b26c595 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 24 Apr 2020 10:49:02 +0100 Subject: [PATCH] Transpose invite_room_state into invite_state.events for sync API --- go.mod | 1 + syncapi/storage/postgres/syncserver.go | 6 +----- syncapi/storage/sqlite3/syncserver.go | 6 +----- syncapi/sync/requestpool.go | 9 +++++++++ syncapi/types/types.go | 10 +++++++--- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index eb5e2e278..30186d146 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.4.1 github.com/sirupsen/logrus v1.4.2 + github.com/tidwall/gjson v1.6.0 github.com/uber/jaeger-client-go v2.15.0+incompatible github.com/uber/jaeger-lib v1.5.0 go.uber.org/atomic v1.4.0 diff --git a/syncapi/storage/postgres/syncserver.go b/syncapi/storage/postgres/syncserver.go index 7fd75f066..1e078ef46 100644 --- a/syncapi/storage/postgres/syncserver.go +++ b/syncapi/storage/postgres/syncserver.go @@ -752,11 +752,7 @@ func (d *SyncServerDatasource) addInvitesToResponse( return err } for roomID, inviteEvent := range invites { - ir := types.NewInviteResponse() - ir.InviteState.Events = gomatrixserverlib.ToClientEvents( - []gomatrixserverlib.Event{inviteEvent.Event}, gomatrixserverlib.FormatSync, - ) - // TODO: add the invite state from the invite event. + ir := types.NewInviteResponse(inviteEvent) res.Rooms.Invite[roomID] = *ir } return nil diff --git a/syncapi/storage/sqlite3/syncserver.go b/syncapi/storage/sqlite3/syncserver.go index 29051cd06..cdfd29b84 100644 --- a/syncapi/storage/sqlite3/syncserver.go +++ b/syncapi/storage/sqlite3/syncserver.go @@ -799,11 +799,7 @@ func (d *SyncServerDatasource) addInvitesToResponse( return err } for roomID, inviteEvent := range invites { - ir := types.NewInviteResponse() - ir.InviteState.Events = gomatrixserverlib.HeaderedToClientEvents( - []gomatrixserverlib.HeaderedEvent{inviteEvent}, gomatrixserverlib.FormatSync, - ) - // TODO: add the invite state from the invite event. + ir := types.NewInviteResponse(inviteEvent) res.Rooms.Invite[roomID] = *ir } return nil diff --git a/syncapi/sync/requestpool.go b/syncapi/sync/requestpool.go index 69efd8aa8..c20f59cd6 100644 --- a/syncapi/sync/requestpool.go +++ b/syncapi/sync/requestpool.go @@ -15,6 +15,8 @@ package sync import ( + "encoding/json" + "fmt" "net/http" "time" @@ -69,6 +71,10 @@ func (rp *RequestPool) OnIncomingSyncRequest(req *http.Request, device *authtype logger.WithError(err).Error("rp.currentSyncForUser failed") return jsonerror.InternalServerError() } + + j, _ := json.MarshalIndent(syncData, "", " ") + fmt.Println("Sync response:", string(j)) + logger.WithField("next", syncData.NextBatch).Info("Responding immediately") return util.JSONResponse{ Code: http.StatusOK, @@ -123,6 +129,9 @@ func (rp *RequestPool) OnIncomingSyncRequest(req *http.Request, device *authtype } if !syncData.IsEmpty() || hasTimedOut { + j, _ := json.MarshalIndent(syncData, "", " ") + fmt.Println("Sync response:", string(j)) + logger.WithField("next", syncData.NextBatch).WithField("timed_out", hasTimedOut).Info("Responding") return util.JSONResponse{ Code: http.StatusOK, diff --git a/syncapi/types/types.go b/syncapi/types/types.go index 718906ecd..cfd49ff1f 100644 --- a/syncapi/types/types.go +++ b/syncapi/types/types.go @@ -23,6 +23,7 @@ import ( "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" + "github.com/tidwall/gjson" ) var ( @@ -247,14 +248,17 @@ func NewJoinResponse() *JoinResponse { // InviteResponse represents a /sync response for a room which is under the 'invite' key. type InviteResponse struct { InviteState struct { - Events []gomatrixserverlib.ClientEvent `json:"events"` + Events json.RawMessage `json:"events"` } `json:"invite_state"` } // NewInviteResponse creates an empty response with initialised arrays. -func NewInviteResponse() *InviteResponse { +func NewInviteResponse(event gomatrixserverlib.HeaderedEvent) *InviteResponse { res := InviteResponse{} - res.InviteState.Events = make([]gomatrixserverlib.ClientEvent, 0) + res.InviteState.Events = json.RawMessage{'[', ']'} + if inviteRoomState := gjson.GetBytes(event.Unsigned(), "invite_room_state"); inviteRoomState.Exists() { + res.InviteState.Events = json.RawMessage(inviteRoomState.Raw) + } return &res }