Skip to content

Commit

Permalink
Implement room creation content - fixes matrix-org#660
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Chen <minecnly@gmail.com>
  • Loading branch information
Cnly committed Jul 22, 2019
1 parent bdd1a87 commit 764b459
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
32 changes: 30 additions & 2 deletions clientapi/routing/createroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package routing

import (
"encoding/json"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -97,6 +98,23 @@ func (r createRoomRequest) Validate() *util.JSONResponse {
}
}

creationContentBytes, err := json.Marshal(r.CreationContent)
if err != nil {
return &util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("malformed creation_content"),
}
}

var CreationContent common.CreateContent
err = json.Unmarshal(creationContentBytes, &CreationContent)
if err != nil {
return &util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.BadJSON("malformed creation_content"),
}
}

return nil
}

Expand Down Expand Up @@ -154,7 +172,17 @@ func createRoom(
JSON: jsonerror.InvalidArgumentValue(err.Error()),
}
}
// TODO: visibility/presets/raw initial state/creation content

// Clobber keys: creator, room_version
if r.CreationContent == nil {
r.CreationContent = make(map[string]interface{}, 2)
}

// Clobber keys: creator, room_version
r.CreationContent["creator"] = userID
r.CreationContent["room_version"] = "1" // TODO: We set this to 1 before we support Room versioning

// TODO: visibility/presets/raw initial state
// TODO: Create room alias association
// Make sure this doesn't fall into an application service's namespace though!

Expand Down Expand Up @@ -214,7 +242,7 @@ func createRoom(
// harder to reason about, hence sticking to a strict static ordering.
// TODO: Synapse has txn/token ID on each event. Do we need to do this here?
eventsToMake := []fledglingEvent{
{"m.room.create", "", common.CreateContent{Creator: userID}},
{"m.room.create", "", r.CreationContent},
{"m.room.member", userID, membershipContent},
{"m.room.power_levels", "", common.InitialPowerLevelsContent(userID)},
// TODO: m.room.canonical_alias
Expand Down
12 changes: 10 additions & 2 deletions common/eventcontent.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ package common

// CreateContent is the event content for http://matrix.org/docs/spec/client_server/r0.2.0.html#m-room-create
type CreateContent struct {
Creator string `json:"creator"`
Federate *bool `json:"m.federate,omitempty"`
Creator string `json:"creator"`
Federate *bool `json:"m.federate,omitempty"`
RoomVersion string `json:"room_version,omitempty"`
Predecessor PreviousRoom `json:"predecessor"`
}

// PreviousRoom is the "Previous Room" structure defined at https://matrix.org/docs/spec/client_server/r0.5.0#m-room-create
type PreviousRoom struct {
RoomID string `json:"room_id"`
EventID string `json:"event_id"`
}

// MemberContent is the event content for http://matrix.org/docs/spec/client_server/r0.2.0.html#m-room-member
Expand Down
3 changes: 3 additions & 0 deletions testfile
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,6 @@ Typing events appear in incremental sync
Typing events appear in gapped sync
Inbound federation of state requires event_id as a mandatory paramater
Inbound federation of state_ids requires event_id as a mandatory paramater
POST /createRoom with creation content
User can create and send/receive messages in a room with version 1
POST /createRoom ignores attempts to set the room version via creation_content

0 comments on commit 764b459

Please sign in to comment.