2017-04-21 00:40:52 +02:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-10-11 19:16:53 +02:00
|
|
|
package routing
|
2017-03-07 17:11:08 +01:00
|
|
|
|
|
|
|
import (
|
2022-02-18 16:05:03 +01:00
|
|
|
"context"
|
2019-07-24 18:15:36 +02:00
|
|
|
"encoding/json"
|
2017-03-09 12:47:06 +01:00
|
|
|
"fmt"
|
2017-03-07 17:11:08 +01:00
|
|
|
"net/http"
|
2017-03-09 12:47:06 +01:00
|
|
|
"strings"
|
2018-08-06 15:09:25 +02:00
|
|
|
"time"
|
2017-03-07 17:11:08 +01:00
|
|
|
|
2018-08-20 11:45:17 +02:00
|
|
|
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2020-03-17 16:12:01 +01:00
|
|
|
roomserverVersion "github.com/matrix-org/dendrite/roomserver/version"
|
2020-06-16 15:10:55 +02:00
|
|
|
"github.com/matrix-org/dendrite/userapi/api"
|
2017-09-27 17:44:40 +02:00
|
|
|
|
2017-03-10 12:32:53 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
2017-03-09 12:47:06 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2020-06-12 15:55:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/eventutil"
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2017-03-10 12:32:53 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-03-07 17:11:08 +01:00
|
|
|
"github.com/matrix-org/util"
|
2017-11-16 11:12:02 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-03-07 17:11:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom
|
|
|
|
type createRoomRequest struct {
|
2021-02-12 10:43:31 +01:00
|
|
|
Invite []string `json:"invite"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Visibility string `json:"visibility"`
|
|
|
|
Topic string `json:"topic"`
|
|
|
|
Preset string `json:"preset"`
|
2021-07-21 13:31:46 +02:00
|
|
|
CreationContent json.RawMessage `json:"creation_content"`
|
2021-02-12 10:43:31 +01:00
|
|
|
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"`
|
2022-08-16 12:55:06 +02:00
|
|
|
IsDirect bool `json:"is_direct"`
|
2017-03-07 17:11:08 +01:00
|
|
|
}
|
|
|
|
|
2018-01-02 11:32:53 +01:00
|
|
|
const (
|
|
|
|
presetPrivateChat = "private_chat"
|
|
|
|
presetTrustedPrivateChat = "trusted_private_chat"
|
|
|
|
presetPublicChat = "public_chat"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
historyVisibilityShared = "shared"
|
|
|
|
// TODO: These should be implemented once history visibility is implemented
|
|
|
|
// historyVisibilityWorldReadable = "world_readable"
|
|
|
|
// historyVisibilityInvited = "invited"
|
|
|
|
)
|
|
|
|
|
2017-03-09 12:47:06 +01:00
|
|
|
func (r createRoomRequest) Validate() *util.JSONResponse {
|
|
|
|
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
|
|
|
|
// Synapse doesn't check for ':' but we will else it will break parsers badly which split things into 2 segments.
|
|
|
|
if strings.ContainsAny(r.RoomAliasName, whitespace+":") {
|
|
|
|
return &util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2018-08-03 16:56:48 +02:00
|
|
|
JSON: jsonerror.BadJSON("room_alias_name cannot contain whitespace or ':'"),
|
2017-03-09 12:47:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, userID := range r.Invite {
|
|
|
|
// TODO: We should put user ID parsing code into gomatrixserverlib and use that instead
|
|
|
|
// (see https://github.com/matrix-org/gomatrixserverlib/blob/3394e7c7003312043208aa73727d2256eea3d1f6/eventcontent.go#L347 )
|
|
|
|
// It should be a struct (with pointers into a single string to avoid copying) and
|
|
|
|
// we should update all refs to use UserID types rather than strings.
|
|
|
|
// https://github.com/matrix-org/synapse/blob/v0.19.2/synapse/types.py#L92
|
2017-07-07 15:11:32 +02:00
|
|
|
if _, _, err := gomatrixserverlib.SplitID('@', userID); err != nil {
|
2017-03-09 12:47:06 +01:00
|
|
|
return &util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2017-03-09 12:47:06 +01:00
|
|
|
JSON: jsonerror.BadJSON("user id must be in the form @localpart:domain"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-02 11:32:53 +01:00
|
|
|
switch r.Preset {
|
2018-07-06 11:20:17 +02:00
|
|
|
case presetPrivateChat, presetTrustedPrivateChat, presetPublicChat, "":
|
2018-01-02 11:32:53 +01:00
|
|
|
default:
|
|
|
|
return &util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2018-01-02 11:32:53 +01:00
|
|
|
JSON: jsonerror.BadJSON("preset must be any of 'private_chat', 'trusted_private_chat', 'public_chat'"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 18:15:36 +02:00
|
|
|
// Validate creation_content fields defined in the spec by marshalling the
|
|
|
|
// creation_content map into bytes and then unmarshalling the bytes into
|
2020-06-12 15:55:57 +02:00
|
|
|
// eventutil.CreateContent.
|
2019-07-24 18:15:36 +02:00
|
|
|
|
|
|
|
creationContentBytes, err := json.Marshal(r.CreationContent)
|
|
|
|
if err != nil {
|
|
|
|
return &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON("malformed creation_content"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 19:45:11 +02:00
|
|
|
var CreationContent gomatrixserverlib.CreateContent
|
2019-07-24 18:15:36 +02:00
|
|
|
err = json.Unmarshal(creationContentBytes, &CreationContent)
|
|
|
|
if err != nil {
|
|
|
|
return &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON("malformed creation_content"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 12:47:06 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom
|
|
|
|
type createRoomResponse struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
RoomAlias string `json:"room_alias,omitempty"` // in synapse not spec
|
|
|
|
}
|
|
|
|
|
2017-03-10 12:32:53 +01:00
|
|
|
// fledglingEvent is a helper representation of an event used when creating many events in succession.
|
|
|
|
type fledglingEvent struct {
|
2018-01-02 11:32:53 +01:00
|
|
|
Type string `json:"type"`
|
|
|
|
StateKey string `json:"state_key"`
|
|
|
|
Content interface{} `json:"content"`
|
2017-03-10 12:32:53 +01:00
|
|
|
}
|
|
|
|
|
2017-03-07 17:11:08 +01:00
|
|
|
// CreateRoom implements /createRoom
|
2018-07-17 17:45:30 +02:00
|
|
|
func CreateRoom(
|
2020-06-16 15:10:55 +02:00
|
|
|
req *http.Request, device *api.Device,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.ClientAPI,
|
2022-05-05 14:17:38 +02:00
|
|
|
profileAPI api.ClientUserAPI, rsAPI roomserverAPI.ClientRoomserverAPI,
|
2022-05-06 13:39:26 +02:00
|
|
|
asAPI appserviceAPI.AppServiceInternalAPI,
|
2017-07-25 17:10:59 +02:00
|
|
|
) util.JSONResponse {
|
2017-03-07 17:11:08 +01:00
|
|
|
var r createRoomRequest
|
2017-05-23 18:43:05 +02:00
|
|
|
resErr := httputil.UnmarshalJSONRequest(req, &r)
|
2017-03-07 17:11:08 +01:00
|
|
|
if resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
2017-03-09 12:47:06 +01:00
|
|
|
if resErr = r.Validate(); resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
2018-08-22 14:40:25 +02:00
|
|
|
evTime, err := httputil.ParseTSParam(req)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.InvalidArgumentValue(err.Error()),
|
|
|
|
}
|
|
|
|
}
|
2022-03-24 22:45:44 +01:00
|
|
|
return createRoom(req.Context(), r, device, cfg, profileAPI, rsAPI, asAPI, evTime)
|
2022-02-18 16:05:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// createRoom implements /createRoom
|
|
|
|
// nolint: gocyclo
|
|
|
|
func createRoom(
|
|
|
|
ctx context.Context,
|
|
|
|
r createRoomRequest, device *api.Device,
|
|
|
|
cfg *config.ClientAPI,
|
2022-05-05 14:17:38 +02:00
|
|
|
profileAPI api.ClientUserAPI, rsAPI roomserverAPI.ClientRoomserverAPI,
|
2022-05-06 13:39:26 +02:00
|
|
|
asAPI appserviceAPI.AppServiceInternalAPI,
|
2022-02-18 16:05:03 +01:00
|
|
|
evTime time.Time,
|
|
|
|
) util.JSONResponse {
|
2022-10-26 13:59:19 +02:00
|
|
|
_, userDomain, err := gomatrixserverlib.SplitID('@', device.UserID)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("gomatrixserverlib.SplitID failed")
|
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
|
|
|
if !cfg.Matrix.IsLocalServerName(userDomain) {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
JSON: jsonerror.Forbidden(fmt.Sprintf("User domain %q not configured locally", userDomain)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 16:05:03 +01:00
|
|
|
// TODO (#267): Check room ID doesn't clash with an existing one, and we
|
|
|
|
// probably shouldn't be using pseudo-random strings, maybe GUIDs?
|
2022-10-26 13:59:19 +02:00
|
|
|
roomID := fmt.Sprintf("!%s:%s", util.RandomString(16), userDomain)
|
2022-02-18 16:05:03 +01:00
|
|
|
|
|
|
|
logger := util.GetLogger(ctx)
|
|
|
|
userID := device.UserID
|
2019-07-24 18:15:36 +02:00
|
|
|
|
|
|
|
// Clobber keys: creator, room_version
|
|
|
|
|
2020-03-17 16:12:01 +01:00
|
|
|
roomVersion := roomserverVersion.DefaultRoomVersion()
|
|
|
|
if r.RoomVersion != "" {
|
|
|
|
candidateVersion := gomatrixserverlib.RoomVersion(r.RoomVersion)
|
|
|
|
_, roomVersionError := roomserverVersion.SupportedRoomVersion(candidateVersion)
|
|
|
|
if roomVersionError != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.UnsupportedRoomVersion(roomVersionError.Error()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
roomVersion = candidateVersion
|
|
|
|
}
|
2019-07-24 18:15:36 +02:00
|
|
|
|
|
|
|
// TODO: visibility/presets/raw initial state
|
2017-03-09 12:47:06 +01:00
|
|
|
// TODO: Create room alias association
|
2018-06-29 13:09:00 +02:00
|
|
|
// Make sure this doesn't fall into an application service's namespace though!
|
2017-03-07 17:11:08 +01:00
|
|
|
|
|
|
|
logger.WithFields(log.Fields{
|
2020-03-17 16:12:01 +01:00
|
|
|
"userID": userID,
|
|
|
|
"roomID": roomID,
|
2021-07-21 13:31:46 +02:00
|
|
|
"roomVersion": roomVersion,
|
2017-03-10 12:32:53 +01:00
|
|
|
}).Info("Creating new room")
|
|
|
|
|
2022-03-24 22:45:44 +01:00
|
|
|
profile, err := appserviceAPI.RetrieveUserProfile(ctx, userID, asAPI, profileAPI)
|
2017-07-25 17:10:59 +02:00
|
|
|
if err != nil {
|
2022-02-18 16:05:03 +01:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("appserviceAPI.RetrieveUserProfile failed")
|
2020-03-02 17:20:44 +01:00
|
|
|
return jsonerror.InternalServerError()
|
2017-07-25 17:10:59 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 13:31:46 +02:00
|
|
|
createContent := map[string]interface{}{}
|
|
|
|
if len(r.CreationContent) > 0 {
|
|
|
|
if err = json.Unmarshal(r.CreationContent, &createContent); err != nil {
|
2022-02-18 16:05:03 +01:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("json.Unmarshal for creation_content failed")
|
2021-07-21 13:31:46 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON("invalid create content"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createContent["creator"] = userID
|
|
|
|
createContent["room_version"] = roomVersion
|
|
|
|
powerLevelContent := eventutil.InitialPowerLevelsContent(userID)
|
|
|
|
joinRuleContent := gomatrixserverlib.JoinRuleContent{
|
|
|
|
JoinRule: gomatrixserverlib.Invite,
|
|
|
|
}
|
|
|
|
historyVisibilityContent := gomatrixserverlib.HistoryVisibilityContent{
|
|
|
|
HistoryVisibility: historyVisibilityShared,
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.PowerLevelContentOverride != nil {
|
|
|
|
// Merge powerLevelContentOverride fields by unmarshalling it atop the defaults
|
|
|
|
err = json.Unmarshal(r.PowerLevelContentOverride, &powerLevelContent)
|
|
|
|
if err != nil {
|
2022-02-18 16:05:03 +01:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("json.Unmarshal for power_level_content_override failed")
|
2021-07-21 13:31:46 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.BadJSON("malformed power_level_content_override"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Preset {
|
|
|
|
case presetPrivateChat:
|
|
|
|
joinRuleContent.JoinRule = gomatrixserverlib.Invite
|
|
|
|
historyVisibilityContent.HistoryVisibility = historyVisibilityShared
|
|
|
|
case presetTrustedPrivateChat:
|
|
|
|
joinRuleContent.JoinRule = gomatrixserverlib.Invite
|
|
|
|
historyVisibilityContent.HistoryVisibility = historyVisibilityShared
|
2022-05-24 11:10:25 +02:00
|
|
|
for _, invitee := range r.Invite {
|
|
|
|
powerLevelContent.Users[invitee] = 100
|
|
|
|
}
|
2021-07-21 13:31:46 +02:00
|
|
|
case presetPublicChat:
|
|
|
|
joinRuleContent.JoinRule = gomatrixserverlib.Public
|
|
|
|
historyVisibilityContent.HistoryVisibility = historyVisibilityShared
|
|
|
|
}
|
|
|
|
|
|
|
|
createEvent := fledglingEvent{
|
|
|
|
Type: gomatrixserverlib.MRoomCreate,
|
|
|
|
Content: createContent,
|
|
|
|
}
|
|
|
|
powerLevelEvent := fledglingEvent{
|
|
|
|
Type: gomatrixserverlib.MRoomPowerLevels,
|
|
|
|
Content: powerLevelContent,
|
|
|
|
}
|
|
|
|
joinRuleEvent := fledglingEvent{
|
|
|
|
Type: gomatrixserverlib.MRoomJoinRules,
|
|
|
|
Content: joinRuleContent,
|
|
|
|
}
|
|
|
|
historyVisibilityEvent := fledglingEvent{
|
|
|
|
Type: gomatrixserverlib.MRoomHistoryVisibility,
|
|
|
|
Content: historyVisibilityContent,
|
|
|
|
}
|
|
|
|
membershipEvent := fledglingEvent{
|
|
|
|
Type: gomatrixserverlib.MRoomMember,
|
|
|
|
StateKey: userID,
|
|
|
|
Content: gomatrixserverlib.MemberContent{
|
|
|
|
Membership: gomatrixserverlib.Join,
|
|
|
|
DisplayName: profile.DisplayName,
|
|
|
|
AvatarURL: profile.AvatarURL,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var nameEvent *fledglingEvent
|
|
|
|
var topicEvent *fledglingEvent
|
|
|
|
var guestAccessEvent *fledglingEvent
|
|
|
|
var aliasEvent *fledglingEvent
|
|
|
|
|
|
|
|
if r.Name != "" {
|
|
|
|
nameEvent = &fledglingEvent{
|
|
|
|
Type: gomatrixserverlib.MRoomName,
|
|
|
|
Content: eventutil.NameContent{
|
|
|
|
Name: r.Name,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Topic != "" {
|
|
|
|
topicEvent = &fledglingEvent{
|
|
|
|
Type: gomatrixserverlib.MRoomTopic,
|
|
|
|
Content: eventutil.TopicContent{
|
|
|
|
Topic: r.Topic,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.GuestCanJoin {
|
|
|
|
guestAccessEvent = &fledglingEvent{
|
|
|
|
Type: gomatrixserverlib.MRoomGuestAccess,
|
|
|
|
Content: eventutil.GuestAccessContent{
|
|
|
|
GuestAccess: "can_join",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 14:39:01 +02:00
|
|
|
var roomAlias string
|
|
|
|
if r.RoomAliasName != "" {
|
2022-10-26 13:59:19 +02:00
|
|
|
roomAlias = fmt.Sprintf("#%s:%s", r.RoomAliasName, userDomain)
|
2020-05-15 14:39:01 +02:00
|
|
|
// check it's free TODO: This races but is better than nothing
|
|
|
|
hasAliasReq := roomserverAPI.GetRoomIDForAliasRequest{
|
2021-03-03 18:00:31 +01:00
|
|
|
Alias: roomAlias,
|
|
|
|
IncludeAppservices: false,
|
2020-05-15 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var aliasResp roomserverAPI.GetRoomIDForAliasResponse
|
2022-02-18 16:05:03 +01:00
|
|
|
err = rsAPI.GetRoomIDForAlias(ctx, &hasAliasReq, &aliasResp)
|
2020-05-15 14:39:01 +02:00
|
|
|
if err != nil {
|
2022-02-18 16:05:03 +01:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("aliasAPI.GetRoomIDForAlias failed")
|
2020-05-15 14:39:01 +02:00
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
|
|
|
if aliasResp.RoomID != "" {
|
2021-08-18 13:41:30 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.RoomInUse("Room ID already exists."),
|
|
|
|
}
|
2020-05-15 14:39:01 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 13:31:46 +02:00
|
|
|
aliasEvent = &fledglingEvent{
|
|
|
|
Type: gomatrixserverlib.MRoomCanonicalAlias,
|
|
|
|
Content: eventutil.CanonicalAlias{
|
|
|
|
Alias: roomAlias,
|
|
|
|
},
|
|
|
|
}
|
2017-07-25 17:10:59 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 13:31:46 +02:00
|
|
|
var initialStateEvents []fledglingEvent
|
|
|
|
for i := range r.InitialState {
|
|
|
|
if r.InitialState[i].StateKey != "" {
|
|
|
|
initialStateEvents = append(initialStateEvents, r.InitialState[i])
|
|
|
|
continue
|
|
|
|
}
|
2018-01-02 11:32:53 +01:00
|
|
|
|
2021-07-21 13:31:46 +02:00
|
|
|
switch r.InitialState[i].Type {
|
|
|
|
case gomatrixserverlib.MRoomCreate:
|
|
|
|
continue
|
2017-03-09 12:47:06 +01:00
|
|
|
|
2021-07-21 13:31:46 +02:00
|
|
|
case gomatrixserverlib.MRoomPowerLevels:
|
|
|
|
powerLevelEvent = r.InitialState[i]
|
|
|
|
|
|
|
|
case gomatrixserverlib.MRoomJoinRules:
|
|
|
|
joinRuleEvent = r.InitialState[i]
|
|
|
|
|
|
|
|
case gomatrixserverlib.MRoomHistoryVisibility:
|
|
|
|
historyVisibilityEvent = r.InitialState[i]
|
|
|
|
|
|
|
|
case gomatrixserverlib.MRoomGuestAccess:
|
|
|
|
guestAccessEvent = &r.InitialState[i]
|
|
|
|
|
|
|
|
case gomatrixserverlib.MRoomName:
|
|
|
|
nameEvent = &r.InitialState[i]
|
|
|
|
|
|
|
|
case gomatrixserverlib.MRoomTopic:
|
|
|
|
topicEvent = &r.InitialState[i]
|
|
|
|
|
|
|
|
default:
|
|
|
|
initialStateEvents = append(initialStateEvents, r.InitialState[i])
|
2021-02-12 10:43:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 12:47:06 +01:00
|
|
|
// send events into the room in order of:
|
|
|
|
// 1- m.room.create
|
|
|
|
// 2- room creator join member
|
|
|
|
// 3- m.room.power_levels
|
2020-05-15 14:39:01 +02:00
|
|
|
// 4- m.room.join_rules
|
|
|
|
// 5- m.room.history_visibility
|
|
|
|
// 6- m.room.canonical_alias (opt)
|
2017-07-20 14:06:14 +02:00
|
|
|
// 7- m.room.guest_access (opt)
|
2018-01-02 11:32:53 +01:00
|
|
|
// 8- other initial state items
|
2017-03-09 12:47:06 +01:00
|
|
|
// 9- m.room.name (opt)
|
|
|
|
// 10- m.room.topic (opt)
|
|
|
|
// 11- invite events (opt) - with is_direct flag if applicable TODO
|
|
|
|
// 12- 3pid invite events (opt) TODO
|
|
|
|
// This differs from Synapse slightly. Synapse would vary the ordering of 3-7
|
|
|
|
// depending on if those events were in "initial_state" or not. This made it
|
|
|
|
// harder to reason about, hence sticking to a strict static ordering.
|
2017-03-10 12:32:53 +01:00
|
|
|
// TODO: Synapse has txn/token ID on each event. Do we need to do this here?
|
|
|
|
eventsToMake := []fledglingEvent{
|
2021-07-21 13:31:46 +02:00
|
|
|
createEvent, membershipEvent, powerLevelEvent, joinRuleEvent, historyVisibilityEvent,
|
2018-01-02 11:32:53 +01:00
|
|
|
}
|
2021-07-21 13:31:46 +02:00
|
|
|
if guestAccessEvent != nil {
|
|
|
|
eventsToMake = append(eventsToMake, *guestAccessEvent)
|
2020-05-15 14:39:01 +02:00
|
|
|
}
|
2021-07-21 13:31:46 +02:00
|
|
|
eventsToMake = append(eventsToMake, initialStateEvents...)
|
|
|
|
if nameEvent != nil {
|
|
|
|
eventsToMake = append(eventsToMake, *nameEvent)
|
2018-01-02 11:32:53 +01:00
|
|
|
}
|
2021-07-21 13:31:46 +02:00
|
|
|
if topicEvent != nil {
|
|
|
|
eventsToMake = append(eventsToMake, *topicEvent)
|
2018-01-02 11:32:53 +01:00
|
|
|
}
|
2021-07-21 13:31:46 +02:00
|
|
|
if aliasEvent != nil {
|
|
|
|
// TODO: bit of a chicken and egg problem here as the alias doesn't exist and cannot until we have made the room.
|
|
|
|
// This means we might fail creating the alias but say the canonical alias is something that doesn't exist.
|
|
|
|
eventsToMake = append(eventsToMake, *aliasEvent)
|
2017-03-10 12:32:53 +01:00
|
|
|
}
|
2021-07-21 13:31:46 +02:00
|
|
|
|
2018-01-02 11:32:53 +01:00
|
|
|
// TODO: invite events
|
|
|
|
// TODO: 3pid invite events
|
2017-03-10 12:32:53 +01:00
|
|
|
|
2021-07-21 13:31:46 +02:00
|
|
|
var builtEvents []*gomatrixserverlib.HeaderedEvent
|
2017-03-10 18:54:17 +01:00
|
|
|
authEvents := gomatrixserverlib.NewAuthEvents(nil)
|
2017-03-10 12:32:53 +01:00
|
|
|
for i, e := range eventsToMake {
|
|
|
|
depth := i + 1 // depth starts at 1
|
|
|
|
|
|
|
|
builder := gomatrixserverlib.EventBuilder{
|
|
|
|
Sender: userID,
|
|
|
|
RoomID: roomID,
|
|
|
|
Type: e.Type,
|
|
|
|
StateKey: &e.StateKey,
|
|
|
|
Depth: int64(depth),
|
|
|
|
}
|
2017-09-20 11:59:19 +02:00
|
|
|
err = builder.SetContent(e.Content)
|
|
|
|
if err != nil {
|
2022-02-18 16:05:03 +01:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("builder.SetContent failed")
|
2020-03-02 17:20:44 +01:00
|
|
|
return jsonerror.InternalServerError()
|
2017-09-20 11:59:19 +02:00
|
|
|
}
|
2017-03-10 12:32:53 +01:00
|
|
|
if i > 0 {
|
|
|
|
builder.PrevEvents = []gomatrixserverlib.EventReference{builtEvents[i-1].EventReference()}
|
|
|
|
}
|
2017-09-27 17:44:40 +02:00
|
|
|
var ev *gomatrixserverlib.Event
|
2022-10-26 13:59:19 +02:00
|
|
|
ev, err = buildEvent(&builder, userDomain, &authEvents, cfg, evTime, roomVersion)
|
2017-03-10 12:32:53 +01:00
|
|
|
if err != nil {
|
2022-02-18 16:05:03 +01:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("buildEvent failed")
|
2020-03-02 17:20:44 +01:00
|
|
|
return jsonerror.InternalServerError()
|
2017-03-10 12:32:53 +01:00
|
|
|
}
|
|
|
|
|
2020-11-16 16:44:53 +01:00
|
|
|
if err = gomatrixserverlib.Allowed(ev, &authEvents); err != nil {
|
2022-02-18 16:05:03 +01:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("gomatrixserverlib.Allowed failed")
|
2020-03-02 17:20:44 +01:00
|
|
|
return jsonerror.InternalServerError()
|
2017-03-10 12:32:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the event to the list of auth events
|
2020-11-16 16:44:53 +01:00
|
|
|
builtEvents = append(builtEvents, ev.Headered(roomVersion))
|
2017-09-20 11:59:19 +02:00
|
|
|
err = authEvents.AddEvent(ev)
|
|
|
|
if err != nil {
|
2022-02-18 16:05:03 +01:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("authEvents.AddEvent failed")
|
2020-03-02 17:20:44 +01:00
|
|
|
return jsonerror.InternalServerError()
|
2017-09-20 11:59:19 +02:00
|
|
|
}
|
2022-01-27 15:29:14 +01:00
|
|
|
}
|
2017-03-10 12:32:53 +01:00
|
|
|
|
2022-01-27 15:29:14 +01:00
|
|
|
inputs := make([]roomserverAPI.InputRoomEvent, 0, len(builtEvents))
|
|
|
|
for _, event := range builtEvents {
|
|
|
|
inputs = append(inputs, roomserverAPI.InputRoomEvent{
|
|
|
|
Kind: roomserverAPI.KindNew,
|
|
|
|
Event: event,
|
2022-10-26 13:59:19 +02:00
|
|
|
Origin: userDomain,
|
2022-01-27 15:29:14 +01:00
|
|
|
SendAsServer: roomserverAPI.DoNotSendToOtherServers,
|
|
|
|
})
|
|
|
|
}
|
2022-11-15 16:05:23 +01:00
|
|
|
if err = roomserverAPI.SendInputRoomEvents(ctx, rsAPI, device.UserDomain(), inputs, false); err != nil {
|
2022-02-18 16:05:03 +01:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("roomserverAPI.SendInputRoomEvents failed")
|
2022-01-27 15:29:14 +01:00
|
|
|
return jsonerror.InternalServerError()
|
2017-03-10 12:32:53 +01:00
|
|
|
}
|
|
|
|
|
2017-09-27 17:44:40 +02:00
|
|
|
// TODO(#269): Reserve room alias while we create the room. This stops us
|
|
|
|
// from creating the room but still failing due to the alias having already
|
|
|
|
// been taken.
|
2020-05-15 14:39:01 +02:00
|
|
|
if roomAlias != "" {
|
2018-08-20 11:45:17 +02:00
|
|
|
aliasReq := roomserverAPI.SetRoomAliasRequest{
|
2017-09-27 17:44:40 +02:00
|
|
|
Alias: roomAlias,
|
|
|
|
RoomID: roomID,
|
|
|
|
UserID: userID,
|
|
|
|
}
|
|
|
|
|
2018-08-20 11:45:17 +02:00
|
|
|
var aliasResp roomserverAPI.SetRoomAliasResponse
|
2022-02-18 16:05:03 +01:00
|
|
|
err = rsAPI.SetRoomAlias(ctx, &aliasReq, &aliasResp)
|
2017-09-27 17:44:40 +02:00
|
|
|
if err != nil {
|
2022-02-18 16:05:03 +01:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("aliasAPI.SetRoomAlias failed")
|
2020-03-02 17:20:44 +01:00
|
|
|
return jsonerror.InternalServerError()
|
2017-09-27 17:44:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if aliasResp.AliasExists {
|
2021-08-18 13:41:30 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.RoomInUse("Room alias already exists."),
|
|
|
|
}
|
2017-09-27 17:44:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 17:46:11 +02:00
|
|
|
// If this is a direct message then we should invite the participants.
|
2020-08-17 18:33:19 +02:00
|
|
|
if len(r.Invite) > 0 {
|
2020-05-07 17:46:11 +02:00
|
|
|
// Build some stripped state for the invite.
|
2020-08-17 18:33:19 +02:00
|
|
|
var globalStrippedState []gomatrixserverlib.InviteV2StrippedState
|
|
|
|
for _, event := range builtEvents {
|
2022-08-16 12:55:06 +02:00
|
|
|
// Chosen events from the spec:
|
|
|
|
// https://spec.matrix.org/v1.3/client-server-api/#stripped-state
|
2020-05-07 17:46:11 +02:00
|
|
|
switch event.Type() {
|
2022-08-16 12:55:06 +02:00
|
|
|
case gomatrixserverlib.MRoomCreate:
|
|
|
|
fallthrough
|
2020-08-17 12:40:49 +02:00
|
|
|
case gomatrixserverlib.MRoomName:
|
|
|
|
fallthrough
|
2022-08-16 12:55:06 +02:00
|
|
|
case gomatrixserverlib.MRoomAvatar:
|
|
|
|
fallthrough
|
|
|
|
case gomatrixserverlib.MRoomTopic:
|
|
|
|
fallthrough
|
2020-08-17 12:40:49 +02:00
|
|
|
case gomatrixserverlib.MRoomCanonicalAlias:
|
|
|
|
fallthrough
|
2021-07-21 13:31:46 +02:00
|
|
|
case gomatrixserverlib.MRoomEncryption:
|
2020-08-17 12:40:49 +02:00
|
|
|
fallthrough
|
2020-05-07 17:46:11 +02:00
|
|
|
case gomatrixserverlib.MRoomMember:
|
|
|
|
fallthrough
|
|
|
|
case gomatrixserverlib.MRoomJoinRules:
|
2020-08-17 18:33:19 +02:00
|
|
|
ev := event.Event
|
|
|
|
globalStrippedState = append(
|
|
|
|
globalStrippedState,
|
2020-11-16 16:44:53 +01:00
|
|
|
gomatrixserverlib.NewInviteV2StrippedState(ev),
|
2020-05-07 17:46:11 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-08-17 18:33:19 +02:00
|
|
|
|
|
|
|
// Process the invites.
|
|
|
|
for _, invitee := range r.Invite {
|
|
|
|
// Build the invite event.
|
|
|
|
inviteEvent, err := buildMembershipEvent(
|
2022-03-24 22:45:44 +01:00
|
|
|
ctx, invitee, "", profileAPI, device, gomatrixserverlib.Invite,
|
2022-08-16 12:55:06 +02:00
|
|
|
roomID, r.IsDirect, cfg, evTime, rsAPI, asAPI,
|
2020-08-17 18:33:19 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
2022-02-18 16:05:03 +01:00
|
|
|
util.GetLogger(ctx).WithError(err).Error("buildMembershipEvent failed")
|
2020-08-17 18:33:19 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
inviteStrippedState := append(
|
|
|
|
globalStrippedState,
|
2020-11-16 16:44:53 +01:00
|
|
|
gomatrixserverlib.NewInviteV2StrippedState(inviteEvent.Event),
|
2020-08-17 18:33:19 +02:00
|
|
|
)
|
|
|
|
// Send the invite event to the roomserver.
|
2022-05-05 14:17:38 +02:00
|
|
|
var inviteRes roomserverAPI.PerformInviteResponse
|
|
|
|
event := inviteEvent.Headered(roomVersion)
|
|
|
|
if err := rsAPI.PerformInvite(ctx, &roomserverAPI.PerformInviteRequest{
|
|
|
|
Event: event,
|
|
|
|
InviteRoomState: inviteStrippedState,
|
|
|
|
RoomVersion: event.RoomVersion,
|
2022-10-26 13:59:19 +02:00
|
|
|
SendAsServer: string(userDomain),
|
2022-05-05 14:17:38 +02:00
|
|
|
}, &inviteRes); err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("PerformInvite failed")
|
2020-08-17 18:33:19 +02:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: jsonerror.InternalServerError(),
|
|
|
|
}
|
2020-08-17 12:40:49 +02:00
|
|
|
}
|
2022-05-05 14:17:38 +02:00
|
|
|
if inviteRes.Error != nil {
|
|
|
|
return inviteRes.Error.JSONResponse()
|
|
|
|
}
|
2020-05-07 17:46:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:41:18 +02:00
|
|
|
if r.Visibility == "public" {
|
|
|
|
// expose this room in the published room list
|
|
|
|
var pubRes roomserverAPI.PerformPublishResponse
|
2022-08-11 16:29:33 +02:00
|
|
|
if err := rsAPI.PerformPublish(ctx, &roomserverAPI.PerformPublishRequest{
|
2020-07-02 16:41:18 +02:00
|
|
|
RoomID: roomID,
|
|
|
|
Visibility: "public",
|
2022-08-11 16:29:33 +02:00
|
|
|
}, &pubRes); err != nil {
|
|
|
|
return jsonerror.InternalAPIError(ctx, err)
|
|
|
|
}
|
2020-07-02 16:41:18 +02:00
|
|
|
if pubRes.Error != nil {
|
|
|
|
// treat as non-fatal since the room is already made by this point
|
2022-02-18 16:05:03 +01:00
|
|
|
util.GetLogger(ctx).WithError(pubRes.Error).Error("failed to visibility:public")
|
2020-07-02 16:41:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 12:44:15 +02:00
|
|
|
response := createRoomResponse{
|
2017-09-27 17:44:40 +02:00
|
|
|
RoomID: roomID,
|
|
|
|
RoomAlias: roomAlias,
|
2017-07-06 12:44:15 +02:00
|
|
|
}
|
|
|
|
|
2017-03-10 12:32:53 +01:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
2017-07-06 12:44:15 +02:00
|
|
|
JSON: response,
|
2017-03-10 12:32:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildEvent fills out auth_events for the builder then builds the event
|
2018-07-17 17:45:30 +02:00
|
|
|
func buildEvent(
|
|
|
|
builder *gomatrixserverlib.EventBuilder,
|
2022-10-26 13:59:19 +02:00
|
|
|
serverName gomatrixserverlib.ServerName,
|
2017-03-17 17:28:15 +01:00
|
|
|
provider gomatrixserverlib.AuthEventProvider,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.ClientAPI,
|
2018-08-06 15:09:25 +02:00
|
|
|
evTime time.Time,
|
2020-03-27 17:28:22 +01:00
|
|
|
roomVersion gomatrixserverlib.RoomVersion,
|
2018-07-17 17:45:30 +02:00
|
|
|
) (*gomatrixserverlib.Event, error) {
|
2017-03-10 12:32:53 +01:00
|
|
|
eventsNeeded, err := gomatrixserverlib.StateNeededForEventBuilder(builder)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-17 17:28:15 +01:00
|
|
|
refs, err := eventsNeeded.AuthEventReferences(provider)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
builder.AuthEvents = refs
|
2020-03-27 17:28:22 +01:00
|
|
|
event, err := builder.Build(
|
2022-10-26 13:59:19 +02:00
|
|
|
evTime, serverName, cfg.Matrix.KeyID,
|
2020-03-27 17:28:22 +01:00
|
|
|
cfg.Matrix.PrivateKey, roomVersion,
|
|
|
|
)
|
2017-03-10 12:32:53 +01:00
|
|
|
if err != nil {
|
2020-03-18 13:48:51 +01:00
|
|
|
return nil, fmt.Errorf("cannot build event %s : Builder failed to build. %w", builder.Type, err)
|
2017-03-10 12:32:53 +01:00
|
|
|
}
|
2020-11-16 16:44:53 +01:00
|
|
|
return event, nil
|
2017-03-10 12:32:53 +01:00
|
|
|
}
|