Implement some createRoom parameters (#374)

* Impl some createRoom parameters

* Fix linter warnings

* Cleaned comments
This commit is contained in:
Thibaut CHARLES 2018-01-02 11:32:53 +01:00 committed by Erik Johnston
parent d22fb24a66
commit 8a3f9b0561

View File

@ -15,7 +15,6 @@
package routing package routing
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
@ -43,10 +42,28 @@ type createRoomRequest struct {
Topic string `json:"topic"` Topic string `json:"topic"`
Preset string `json:"preset"` Preset string `json:"preset"`
CreationContent map[string]interface{} `json:"creation_content"` CreationContent map[string]interface{} `json:"creation_content"`
InitialState json.RawMessage `json:"initial_state"` // TODO InitialState []fledglingEvent `json:"initial_state"`
RoomAliasName string `json:"room_alias_name"` RoomAliasName string `json:"room_alias_name"`
GuestCanJoin bool `json:"guest_can_join"`
} }
const (
presetPrivateChat = "private_chat"
presetTrustedPrivateChat = "trusted_private_chat"
presetPublicChat = "public_chat"
)
const (
joinRulePublic = "public"
joinRuleInvite = "invite"
)
const (
historyVisibilityShared = "shared"
// TODO: These should be implemented once history visibility is implemented
// historyVisibilityWorldReadable = "world_readable"
// historyVisibilityInvited = "invited"
)
func (r createRoomRequest) Validate() *util.JSONResponse { func (r createRoomRequest) Validate() *util.JSONResponse {
whitespace := "\t\n\x0b\x0c\r " // https://docs.python.org/2/library/string.html#string.whitespace whitespace := "\t\n\x0b\x0c\r " // https://docs.python.org/2/library/string.html#string.whitespace
// https://github.com/matrix-org/synapse/blob/v0.19.2/synapse/handlers/room.py#L81 // https://github.com/matrix-org/synapse/blob/v0.19.2/synapse/handlers/room.py#L81
@ -70,6 +87,16 @@ func (r createRoomRequest) Validate() *util.JSONResponse {
} }
} }
} }
switch r.Preset {
case presetPrivateChat, presetTrustedPrivateChat, presetPublicChat:
break
default:
return &util.JSONResponse{
Code: 400,
JSON: jsonerror.BadJSON("preset must be any of 'private_chat', 'trusted_private_chat', 'public_chat'"),
}
}
return nil return nil
} }
@ -81,9 +108,9 @@ type createRoomResponse struct {
// fledglingEvent is a helper representation of an event used when creating many events in succession. // fledglingEvent is a helper representation of an event used when creating many events in succession.
type fledglingEvent struct { type fledglingEvent struct {
Type string Type string `json:"type"`
StateKey string StateKey string `json:"state_key"`
Content interface{} Content interface{} `json:"content"`
} }
// CreateRoom implements /createRoom // CreateRoom implements /createRoom
@ -141,6 +168,20 @@ func createRoom(req *http.Request, device *authtypes.Device,
AvatarURL: profile.AvatarURL, AvatarURL: profile.AvatarURL,
} }
var joinRules, historyVisibility string
switch r.Preset {
case presetPrivateChat:
joinRules = joinRuleInvite
historyVisibility = historyVisibilityShared
case presetTrustedPrivateChat:
joinRules = joinRuleInvite
historyVisibility = historyVisibilityShared
// TODO If trusted_private_chat, all invitees are given the same power level as the room creator.
case presetPublicChat:
joinRules = joinRulePublic
historyVisibility = historyVisibilityShared
}
var builtEvents []gomatrixserverlib.Event var builtEvents []gomatrixserverlib.Event
// send events into the room in order of: // send events into the room in order of:
@ -151,7 +192,7 @@ func createRoom(req *http.Request, device *authtypes.Device,
// 5- m.room.join_rules // 5- m.room.join_rules
// 6- m.room.history_visibility // 6- m.room.history_visibility
// 7- m.room.guest_access (opt) // 7- m.room.guest_access (opt)
// 8- other initial state items TODO // 8- other initial state items
// 9- m.room.name (opt) // 9- m.room.name (opt)
// 10- m.room.topic (opt) // 10- m.room.topic (opt)
// 11- invite events (opt) - with is_direct flag if applicable TODO // 11- invite events (opt) - with is_direct flag if applicable TODO
@ -166,16 +207,22 @@ func createRoom(req *http.Request, device *authtypes.Device,
{"m.room.member", userID, membershipContent}, {"m.room.member", userID, membershipContent},
{"m.room.power_levels", "", common.InitialPowerLevelsContent(userID)}, {"m.room.power_levels", "", common.InitialPowerLevelsContent(userID)},
// TODO: m.room.canonical_alias // TODO: m.room.canonical_alias
{"m.room.join_rules", "", common.JoinRulesContent{JoinRule: "public"}}, // FIXME: Allow this to be changed {"m.room.join_rules", "", common.JoinRulesContent{JoinRule: joinRules}},
{"m.room.history_visibility", "", common.HistoryVisibilityContent{HistoryVisibility: "joined"}}, // FIXME: Allow this to be changed {"m.room.history_visibility", "", common.HistoryVisibilityContent{HistoryVisibility: historyVisibility}},
{"m.room.guest_access", "", common.GuestAccessContent{GuestAccess: "can_join"}}, // FIXME: Allow this to be changed
// TODO: Other initial state items
{"m.room.name", "", common.NameContent{Name: r.Name}}, // FIXME: Only send the name event if a name is supplied, to avoid sending a false room name removal event
{"m.room.topic", "", common.TopicContent{Topic: r.Topic}},
// TODO: invite events
// TODO: 3pid invite events
// TODO: m.room.aliases
} }
if r.GuestCanJoin {
eventsToMake = append(eventsToMake, fledglingEvent{"m.room.guest_access", "", common.GuestAccessContent{GuestAccess: "can_join"}})
}
eventsToMake = append(eventsToMake, r.InitialState...)
if r.Name != "" {
eventsToMake = append(eventsToMake, fledglingEvent{"m.room.name", "", common.NameContent{Name: r.Name}})
}
if r.Topic != "" {
eventsToMake = append(eventsToMake, fledglingEvent{"m.room.topic", "", common.TopicContent{Topic: r.Topic}})
}
// TODO: invite events
// TODO: 3pid invite events
// TODO: m.room.aliases
authEvents := gomatrixserverlib.NewAuthEvents(nil) authEvents := gomatrixserverlib.NewAuthEvents(nil)
for i, e := range eventsToMake { for i, e := range eventsToMake {