From f9745381e54a5b0a0eae486eb5ca6a20894ed790 Mon Sep 17 00:00:00 2001 From: Tristian Celestin Date: Wed, 19 Mar 2025 08:43:25 -0400 Subject: [PATCH 1/5] Use visibility to determine the room preset if it isn't otherwise specified --- .../internal/perform/perform_create_room.go | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/roomserver/internal/perform/perform_create_room.go b/roomserver/internal/perform/perform_create_room.go index 020e7495..8f820fba 100644 --- a/roomserver/internal/perform/perform_create_room.go +++ b/roomserver/internal/perform/perform_create_room.go @@ -114,23 +114,34 @@ func (c *Creator) PerformCreateRoom(ctx context.Context, userID spec.UserID, roo } var guestsCanJoin bool - switch createRequest.StatePreset { - case spec.PresetPrivateChat: - joinRuleContent.JoinRule = spec.Invite - historyVisibilityContent.HistoryVisibility = historyVisibilityShared - guestsCanJoin = true - case spec.PresetTrustedPrivateChat: - joinRuleContent.JoinRule = spec.Invite - historyVisibilityContent.HistoryVisibility = historyVisibilityShared - for _, invitee := range createRequest.InvitedUsers { - powerLevelContent.Users[invitee] = 100 + if createRequest.StatePreset == "" { + switch createRequest.Visibility { + case "private", "": + joinRuleContent.JoinRule = spec.Invite + historyVisibilityContent.HistoryVisibility = historyVisibilityShared + guestsCanJoin = true + case "public": + joinRuleContent.JoinRule = spec.Public + historyVisibilityContent.HistoryVisibility = historyVisibilityShared + } + } else { + switch createRequest.StatePreset { + case spec.PresetPrivateChat: + joinRuleContent.JoinRule = spec.Invite + historyVisibilityContent.HistoryVisibility = historyVisibilityShared + guestsCanJoin = true + case spec.PresetTrustedPrivateChat: + joinRuleContent.JoinRule = spec.Invite + historyVisibilityContent.HistoryVisibility = historyVisibilityShared + for _, invitee := range createRequest.InvitedUsers { + powerLevelContent.Users[invitee] = 100 + } + guestsCanJoin = true + case spec.PresetPublicChat: + joinRuleContent.JoinRule = spec.Public + historyVisibilityContent.HistoryVisibility = historyVisibilityShared } - guestsCanJoin = true - case spec.PresetPublicChat: - joinRuleContent.JoinRule = spec.Public - historyVisibilityContent.HistoryVisibility = historyVisibilityShared } - createEvent := gomatrixserverlib.FledglingEvent{ Type: spec.MRoomCreate, Content: createContent, From 2849080417c10719e0f6064532d99cce2f1b95b7 Mon Sep 17 00:00:00 2001 From: Tristian Celestin Date: Sun, 23 Mar 2025 10:00:47 -0400 Subject: [PATCH 2/5] Add check to confirm that state preset is effectively private when present and visibility are unset --- clientapi/clientapi_test.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/clientapi/clientapi_test.go b/clientapi/clientapi_test.go index ad2d4ad4..cf86e024 100644 --- a/clientapi/clientapi_test.go +++ b/clientapi/clientapi_test.go @@ -2330,8 +2330,23 @@ func TestCreateRoomInvite(t *testing.T) { roomID := gjson.GetBytes(w.Body.Bytes(), "room_id").Str validRoomID, _ := spec.NewRoomID(roomID) + + // Confirm that the room matches the private state preset + ev, err := rsAPI.CurrentStateEvent(context.Background(), *validRoomID, spec.MRoomJoinRules, spec.Invite) + if err != nil { + t.Fatal(err) + } + ev, err = rsAPI.CurrentStateEvent(context.Background(), *validRoomID, spec.MRoomHistoryVisibility, string(gomatrixserverlib.HistoryVisibilityShared)) + if err != nil { + t.Fatal(err) + } + ev, err = rsAPI.CurrentStateEvent(context.Background(), *validRoomID, spec.MRoomGuestAccess, "can_join") + if err != nil { + t.Fatal(err) + } + // Now ask the roomserver about the membership event of Bob - ev, err := rsAPI.CurrentStateEvent(context.Background(), *validRoomID, spec.MRoomMember, bob.ID) + ev, err = rsAPI.CurrentStateEvent(context.Background(), *validRoomID, spec.MRoomMember, bob.ID) if err != nil { t.Fatal(err) } From 5f7b6c8525d72e4258e0326782a02c5ada7dc6c9 Mon Sep 17 00:00:00 2001 From: Tristian Celestin Date: Sun, 18 May 2025 22:33:52 -0400 Subject: [PATCH 3/5] Don't reimplement state preset logic Instead, if the StatePreset is unset, set it based on the Visibility. --- roomserver/internal/perform/perform_create_room.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/roomserver/internal/perform/perform_create_room.go b/roomserver/internal/perform/perform_create_room.go index 8f820fba..e51e652d 100644 --- a/roomserver/internal/perform/perform_create_room.go +++ b/roomserver/internal/perform/perform_create_room.go @@ -117,12 +117,9 @@ func (c *Creator) PerformCreateRoom(ctx context.Context, userID spec.UserID, roo if createRequest.StatePreset == "" { switch createRequest.Visibility { case "private", "": - joinRuleContent.JoinRule = spec.Invite - historyVisibilityContent.HistoryVisibility = historyVisibilityShared - guestsCanJoin = true + createRequest.StatePreset = "private_chat" case "public": - joinRuleContent.JoinRule = spec.Public - historyVisibilityContent.HistoryVisibility = historyVisibilityShared + createRequest.StatePreset = "public_chat" } } else { switch createRequest.StatePreset { From ce54d7a7e81e094d903c8f9d5e01822e2c0db233 Mon Sep 17 00:00:00 2001 From: Tristian Celestin Date: Sun, 18 May 2025 22:50:18 -0400 Subject: [PATCH 4/5] Use existing constants for setting StatePreset --- roomserver/internal/perform/perform_create_room.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roomserver/internal/perform/perform_create_room.go b/roomserver/internal/perform/perform_create_room.go index e51e652d..40b0d48a 100644 --- a/roomserver/internal/perform/perform_create_room.go +++ b/roomserver/internal/perform/perform_create_room.go @@ -117,9 +117,9 @@ func (c *Creator) PerformCreateRoom(ctx context.Context, userID spec.UserID, roo if createRequest.StatePreset == "" { switch createRequest.Visibility { case "private", "": - createRequest.StatePreset = "private_chat" + createRequest.StatePreset = spec.PresetPrivateChat case "public": - createRequest.StatePreset = "public_chat" + createRequest.StatePreset = spec.PresetPublicChat } } else { switch createRequest.StatePreset { From 6d1c47d2a01b68f28bf5a17df321ddf97b8898d2 Mon Sep 17 00:00:00 2001 From: Till Faelligen <2353100+S7evinK@users.noreply.github.com> Date: Thu, 19 Jun 2025 08:13:43 +0200 Subject: [PATCH 5/5] Update code as suggested --- .../internal/perform/perform_create_room.go | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/roomserver/internal/perform/perform_create_room.go b/roomserver/internal/perform/perform_create_room.go index 40b0d48a..c7e84a12 100644 --- a/roomserver/internal/perform/perform_create_room.go +++ b/roomserver/internal/perform/perform_create_room.go @@ -114,6 +114,10 @@ func (c *Creator) PerformCreateRoom(ctx context.Context, userID spec.UserID, roo } var guestsCanJoin bool + + // If unspecified, the server should use the visibility to determine which preset to use. + // A visibility of public equates to a preset of public_chat + // and private visibility equates to a preset of private_chat. if createRequest.StatePreset == "" { switch createRequest.Visibility { case "private", "": @@ -121,23 +125,22 @@ func (c *Creator) PerformCreateRoom(ctx context.Context, userID spec.UserID, roo case "public": createRequest.StatePreset = spec.PresetPublicChat } - } else { - switch createRequest.StatePreset { - case spec.PresetPrivateChat: - joinRuleContent.JoinRule = spec.Invite - historyVisibilityContent.HistoryVisibility = historyVisibilityShared - guestsCanJoin = true - case spec.PresetTrustedPrivateChat: - joinRuleContent.JoinRule = spec.Invite - historyVisibilityContent.HistoryVisibility = historyVisibilityShared - for _, invitee := range createRequest.InvitedUsers { - powerLevelContent.Users[invitee] = 100 - } - guestsCanJoin = true - case spec.PresetPublicChat: - joinRuleContent.JoinRule = spec.Public - historyVisibilityContent.HistoryVisibility = historyVisibilityShared + } + switch createRequest.StatePreset { + case spec.PresetPrivateChat: + joinRuleContent.JoinRule = spec.Invite + historyVisibilityContent.HistoryVisibility = historyVisibilityShared + guestsCanJoin = true + case spec.PresetTrustedPrivateChat: + joinRuleContent.JoinRule = spec.Invite + historyVisibilityContent.HistoryVisibility = historyVisibilityShared + for _, invitee := range createRequest.InvitedUsers { + powerLevelContent.Users[invitee] = 100 } + guestsCanJoin = true + case spec.PresetPublicChat: + joinRuleContent.JoinRule = spec.Public + historyVisibilityContent.HistoryVisibility = historyVisibilityShared } createEvent := gomatrixserverlib.FledglingEvent{ Type: spec.MRoomCreate,