From 1ac571973c5bd913f13d8f1c2aaf0616cd883352 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 28 Feb 2020 16:29:42 +0000 Subject: [PATCH] Try to take room version from createRoomReq --- clientapi/routing/createroom.go | 19 +++++++++++++------ roomserver/version/version.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/clientapi/routing/createroom.go b/clientapi/routing/createroom.go index dedc84e9e..91f2c7da4 100644 --- a/clientapi/routing/createroom.go +++ b/clientapi/routing/createroom.go @@ -18,7 +18,6 @@ import ( "encoding/json" "fmt" "net/http" - "strconv" "strings" "time" @@ -49,6 +48,7 @@ type createRoomRequest struct { InitialState []fledglingEvent `json:"initial_state"` RoomAliasName string `json:"room_alias_name"` GuestCanJoin bool `json:"guest_can_join"` + RoomVersion string `json:"room_version"` } const ( @@ -181,18 +181,25 @@ func createRoom( r.CreationContent = make(map[string]interface{}, 2) } - r.CreationContent["creator"] = userID + roomVersion := roomserverVersion.GetDefaultRoomVersion() + if r.RoomVersion != "" { + id, meta, verr := roomserverVersion.GetSupportedRoomVersionFromString(r.RoomVersion) + if verr == nil && meta.Supported { + roomVersion = id + } + } - defaultVersion := roomserverVersion.GetDefaultRoomVersion() - r.CreationContent["room_version"] = strconv.Itoa(int(defaultVersion)) + r.CreationContent["room_version"] = roomVersion.String() + r.CreationContent["creator"] = userID // TODO: visibility/presets/raw initial state // TODO: Create room alias association // Make sure this doesn't fall into an application service's namespace though! logger.WithFields(log.Fields{ - "userID": userID, - "roomID": roomID, + "userID": userID, + "roomID": roomID, + "roomVersion": r.CreationContent["room_version"], }).Info("Creating new room") profile, err := appserviceAPI.RetrieveUserProfile(req.Context(), userID, asAPI, accountDB) diff --git a/roomserver/version/version.go b/roomserver/version/version.go index 6822412a2..e31351cad 100644 --- a/roomserver/version/version.go +++ b/roomserver/version/version.go @@ -16,6 +16,7 @@ package version import ( "errors" + "strconv" "github.com/matrix-org/dendrite/roomserver/state" ) @@ -101,8 +102,13 @@ func GetSupportedRoomVersions() map[RoomVersionID]RoomVersionDescription { return versions } -func GetSupportedRoomVersion(version RoomVersionID) (desc RoomVersionDescription, err error) { - if version, ok := roomVersions[version]; ok { +func GetSupportedRoomVersion(id RoomVersionID) ( + ver RoomVersionID, + desc RoomVersionDescription, + err error, +) { + if version, ok := roomVersions[id]; ok { + ver = id desc = version } if !desc.Supported { @@ -110,3 +116,21 @@ func GetSupportedRoomVersion(version RoomVersionID) (desc RoomVersionDescription } return } + +func (v RoomVersionID) String() string { + return strconv.Itoa(int(v)) +} + +func GetSupportedRoomVersionFromString(version string) ( + ver RoomVersionID, + desc RoomVersionDescription, + err error, +) { + var v RoomVersionID + if version, err := strconv.Atoi(version); err == nil { + v = RoomVersionID(version) + } else { + v = GetDefaultRoomVersion() + } + return GetSupportedRoomVersion(v) +}