From fa34785cc2ce4ae8252a1866c86aa4655097a294 Mon Sep 17 00:00:00 2001 From: Devon Hudson Date: Wed, 31 May 2023 08:31:09 -0600 Subject: [PATCH] Move create room preset consts to gmsl --- clientapi/routing/createroom.go | 2 +- clientapi/routing/joinroom_test.go | 6 ++--- clientapi/routing/server_notices.go | 2 +- cmd/dendrite-upgrade-tests/tests.go | 5 ++-- go.mod | 2 +- go.sum | 4 +-- roomserver/api/api.go | 7 ------ .../internal/perform/perform_create_room.go | 25 ++++++++++--------- 8 files changed, 24 insertions(+), 29 deletions(-) diff --git a/clientapi/routing/createroom.go b/clientapi/routing/createroom.go index 9d14b46f6..aaa305f06 100644 --- a/clientapi/routing/createroom.go +++ b/clientapi/routing/createroom.go @@ -69,7 +69,7 @@ func (r createRoomRequest) Validate() *util.JSONResponse { } } switch r.Preset { - case roomserverAPI.PresetPrivateChat, roomserverAPI.PresetTrustedPrivateChat, roomserverAPI.PresetPublicChat, "": + case spec.PresetPrivateChat, spec.PresetTrustedPrivateChat, spec.PresetPublicChat, "": default: return &util.JSONResponse{ Code: http.StatusBadRequest, diff --git a/clientapi/routing/joinroom_test.go b/clientapi/routing/joinroom_test.go index fb3f00c11..0ddff8a95 100644 --- a/clientapi/routing/joinroom_test.go +++ b/clientapi/routing/joinroom_test.go @@ -11,10 +11,10 @@ import ( "github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/dendrite/setup/jetstream" "github.com/matrix-org/gomatrixserverlib" + "github.com/matrix-org/gomatrixserverlib/spec" "github.com/matrix-org/dendrite/appservice" "github.com/matrix-org/dendrite/roomserver" - roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/test" "github.com/matrix-org/dendrite/test/testrig" "github.com/matrix-org/dendrite/userapi" @@ -64,7 +64,7 @@ func TestJoinRoomByIDOrAlias(t *testing.T) { IsDirect: true, Topic: "testing", Visibility: "public", - Preset: roomserverAPI.PresetPublicChat, + Preset: spec.PresetPublicChat, RoomAliasName: "alias", Invite: []string{bob.ID}, }, aliceDev, &cfg.ClientAPI, userAPI, rsAPI, asAPI, time.Now()) @@ -79,7 +79,7 @@ func TestJoinRoomByIDOrAlias(t *testing.T) { IsDirect: true, Topic: "testing", Visibility: "public", - Preset: roomserverAPI.PresetPublicChat, + Preset: spec.PresetPublicChat, Invite: []string{charlie.ID}, }, aliceDev, &cfg.ClientAPI, userAPI, rsAPI, asAPI, time.Now()) crRespWithGuestAccess, ok := resp.JSON.(createRoomResponse) diff --git a/clientapi/routing/server_notices.go b/clientapi/routing/server_notices.go index a6b5f1395..06714ed1f 100644 --- a/clientapi/routing/server_notices.go +++ b/clientapi/routing/server_notices.go @@ -155,7 +155,7 @@ func SendServerNotice( Invite: []string{r.UserID}, Name: cfgNotices.RoomName, Visibility: "private", - Preset: api.PresetPrivateChat, + Preset: spec.PresetPrivateChat, CreationContent: cc, RoomVersion: roomVersion, PowerLevelContentOverride: pl, diff --git a/cmd/dendrite-upgrade-tests/tests.go b/cmd/dendrite-upgrade-tests/tests.go index 03438bd4d..692ab34ef 100644 --- a/cmd/dendrite-upgrade-tests/tests.go +++ b/cmd/dendrite-upgrade-tests/tests.go @@ -9,6 +9,7 @@ import ( "github.com/Masterminds/semver/v3" "github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrixserverlib" + "github.com/matrix-org/gomatrixserverlib/spec" ) const userPassword = "this_is_a_long_password" @@ -56,7 +57,7 @@ func runTests(baseURL string, v *semver.Version) error { // create DM room, join it and exchange messages createRoomResp, err := users[0].client.CreateRoom(&gomatrix.ReqCreateRoom{ - Preset: "trusted_private_chat", + Preset: spec.PresetTrustedPrivateChat, Invite: []string{users[1].userID}, IsDirect: true, }) @@ -98,7 +99,7 @@ func runTests(baseURL string, v *semver.Version) error { publicRoomID := "" createRoomResp, err = users[0].client.CreateRoom(&gomatrix.ReqCreateRoom{ RoomAliasName: "global", - Preset: "public_chat", + Preset: spec.PresetPublicChat, }) if err != nil { // this is okay and expected if the room already exists and the aliases clash // try to join it diff --git a/go.mod b/go.mod index 3db9ac872..419fda60d 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/matrix-org/dugong v0.0.0-20210921133753-66e6b1c67e2e github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91 github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 - github.com/matrix-org/gomatrixserverlib v0.0.0-20230531024850-f490eabb6a41 + github.com/matrix-org/gomatrixserverlib v0.0.0-20230531142922-6582336ad0a0 github.com/matrix-org/pinecone v0.11.1-0.20230210171230-8c3b24f2649a github.com/matrix-org/util v0.0.0-20221111132719-399730281e66 github.com/mattn/go-sqlite3 v1.14.16 diff --git a/go.sum b/go.sum index 72f965d6e..3bdb4810a 100644 --- a/go.sum +++ b/go.sum @@ -323,8 +323,8 @@ github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91 h1:s7fexw github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91/go.mod h1:e+cg2q7C7yE5QnAXgzo512tgFh1RbQLC0+jozuegKgo= github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 h1:kHKxCOLcHH8r4Fzarl4+Y3K5hjothkVW5z7T1dUM11U= github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s= -github.com/matrix-org/gomatrixserverlib v0.0.0-20230531024850-f490eabb6a41 h1:ODPxn6DjKOPNO54fQBqAI0GHMlbj98EQMPXho/PFbFY= -github.com/matrix-org/gomatrixserverlib v0.0.0-20230531024850-f490eabb6a41/go.mod h1:H9V9N3Uqn1bBJqYJNGK1noqtgJTaCEhtTdcH/mp50uU= +github.com/matrix-org/gomatrixserverlib v0.0.0-20230531142922-6582336ad0a0 h1:oepaS/Bfj7dU5wMVKYbilMReZK25W5gDwkO26W2T6yk= +github.com/matrix-org/gomatrixserverlib v0.0.0-20230531142922-6582336ad0a0/go.mod h1:H9V9N3Uqn1bBJqYJNGK1noqtgJTaCEhtTdcH/mp50uU= github.com/matrix-org/pinecone v0.11.1-0.20230210171230-8c3b24f2649a h1:awrPDf9LEFySxTLKYBMCiObelNx/cBuv/wzllvCCH3A= github.com/matrix-org/pinecone v0.11.1-0.20230210171230-8c3b24f2649a/go.mod h1:HchJX9oKMXaT2xYFs0Ha/6Zs06mxLU8k6F1ODnrGkeQ= github.com/matrix-org/util v0.0.0-20221111132719-399730281e66 h1:6z4KxomXSIGWqhHcfzExgkH3Z3UkIXry4ibJS4Aqz2Y= diff --git a/roomserver/api/api.go b/roomserver/api/api.go index 6be2007c9..571aa40b3 100644 --- a/roomserver/api/api.go +++ b/roomserver/api/api.go @@ -13,13 +13,6 @@ import ( userapi "github.com/matrix-org/dendrite/userapi/api" ) -// TODO: Move to GMSL? -const ( - PresetPrivateChat = "private_chat" - PresetTrustedPrivateChat = "trusted_private_chat" - PresetPublicChat = "public_chat" -) - // ErrInvalidID is an error returned if the userID is invalid type ErrInvalidID struct { Err error diff --git a/roomserver/internal/perform/perform_create_room.go b/roomserver/internal/perform/perform_create_room.go index 30b47d80e..2617fa3a7 100644 --- a/roomserver/internal/perform/perform_create_room.go +++ b/roomserver/internal/perform/perform_create_room.go @@ -45,6 +45,14 @@ type Creator struct { // PerformCreateRoom handles all the steps necessary to create a new room. // nolint: gocyclo func (c *Creator) PerformCreateRoom(ctx context.Context, userID spec.UserID, roomID spec.RoomID, createRequest *api.PerformCreateRoomRequest) (string, *util.JSONResponse) { + verImpl, err := gomatrixserverlib.GetRoomVersion(createRequest.RoomVersion) + if err != nil { + return "", &util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: spec.BadJSON("unknown room version"), + } + } + createContent := map[string]interface{}{} if len(createRequest.CreationContent) > 0 { if err := json.Unmarshal(createRequest.CreationContent, &createContent); err != nil { @@ -79,18 +87,18 @@ func (c *Creator) PerformCreateRoom(ctx context.Context, userID spec.UserID, roo var guestsCanJoin bool switch createRequest.StatePreset { - case api.PresetPrivateChat: + case spec.PresetPrivateChat: joinRuleContent.JoinRule = spec.Invite historyVisibilityContent.HistoryVisibility = historyVisibilityShared guestsCanJoin = true - case api.PresetTrustedPrivateChat: + case spec.PresetTrustedPrivateChat: joinRuleContent.JoinRule = spec.Invite historyVisibilityContent.HistoryVisibility = historyVisibilityShared for _, invitee := range createRequest.InvitedUsers { powerLevelContent.Users[invitee] = 100 } guestsCanJoin = true - case api.PresetPublicChat: + case spec.PresetPublicChat: joinRuleContent.JoinRule = spec.Public historyVisibilityContent.HistoryVisibility = historyVisibilityShared } @@ -156,7 +164,8 @@ func (c *Creator) PerformCreateRoom(ctx context.Context, userID spec.UserID, roo var roomAlias string if createRequest.RoomAliasName != "" { roomAlias = fmt.Sprintf("#%s:%s", createRequest.RoomAliasName, userID.Domain()) - // check it's free TODO: This races but is better than nothing + // check it's free + // TODO: This races but is better than nothing hasAliasReq := api.GetRoomIDForAliasRequest{ Alias: roomAlias, IncludeAppservices: false, @@ -259,14 +268,6 @@ func (c *Creator) PerformCreateRoom(ctx context.Context, userID spec.UserID, roo // TODO: invite events // TODO: 3pid invite events - verImpl, err := gomatrixserverlib.GetRoomVersion(createRequest.RoomVersion) - if err != nil { - return "", &util.JSONResponse{ - Code: http.StatusBadRequest, - JSON: spec.BadJSON("unknown room version"), - } - } - var builtEvents []*types.HeaderedEvent authEvents := gomatrixserverlib.NewAuthEvents(nil) for i, e := range eventsToMake {