From f448e8972a1a6974916afc3ab686b342eaf568e2 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 12 Feb 2021 09:43:31 +0000 Subject: [PATCH] Implement /createRoom power_level_content_override (#1761) * Implement /createRoom power_level_content_override * delint * Skip having to re-marshal PowerLevelContentOverride by making use of `json.RawMessage` --- clientapi/routing/createroom.go | 35 ++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/clientapi/routing/createroom.go b/clientapi/routing/createroom.go index 5a2ffea34..9be0b5126 100644 --- a/clientapi/routing/createroom.go +++ b/clientapi/routing/createroom.go @@ -38,16 +38,17 @@ import ( // https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom type createRoomRequest struct { - Invite []string `json:"invite"` - Name string `json:"name"` - Visibility string `json:"visibility"` - Topic string `json:"topic"` - Preset string `json:"preset"` - CreationContent map[string]interface{} `json:"creation_content"` - InitialState []fledglingEvent `json:"initial_state"` - RoomAliasName string `json:"room_alias_name"` - GuestCanJoin bool `json:"guest_can_join"` - RoomVersion gomatrixserverlib.RoomVersion `json:"room_version"` + Invite []string `json:"invite"` + Name string `json:"name"` + Visibility string `json:"visibility"` + Topic string `json:"topic"` + Preset string `json:"preset"` + CreationContent map[string]interface{} `json:"creation_content"` + InitialState []fledglingEvent `json:"initial_state"` + RoomAliasName string `json:"room_alias_name"` + GuestCanJoin bool `json:"guest_can_join"` + RoomVersion gomatrixserverlib.RoomVersion `json:"room_version"` + PowerLevelContentOverride json.RawMessage `json:"power_level_content_override"` } const ( @@ -257,6 +258,18 @@ func createRoom( var builtEvents []*gomatrixserverlib.HeaderedEvent + powerLevelContent := eventutil.InitialPowerLevelsContent(userID) + if r.PowerLevelContentOverride != nil { + // Merge powerLevelContentOverride fields by unmarshalling it atop the defaults + err = json.Unmarshal(r.PowerLevelContentOverride, &powerLevelContent) + if err != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.BadJSON("malformed power_level_content_override"), + } + } + } + // send events into the room in order of: // 1- m.room.create // 2- room creator join member @@ -278,7 +291,7 @@ func createRoom( eventsToMake := []fledglingEvent{ {"m.room.create", "", r.CreationContent}, {"m.room.member", userID, membershipContent}, - {"m.room.power_levels", "", eventutil.InitialPowerLevelsContent(userID)}, + {"m.room.power_levels", "", powerLevelContent}, {"m.room.join_rules", "", gomatrixserverlib.JoinRuleContent{JoinRule: joinRules}}, {"m.room.history_visibility", "", eventutil.HistoryVisibilityContent{HistoryVisibility: historyVisibility}}, }