Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions synapse_invite_checker/invite_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,27 @@ async def on_create_room(
errors.Codes.FORBIDDEN,
)

# A_25481: Set default history visibility to "invited" if not explicitly set
# Users can still override this by providing their own history_visibility in initial_state
initial_state: list[dict[str, Any]] = request_content.get("initial_state", [])

# Check if history_visibility is already set in initial_state
has_history_visibility = any(
event.get("type") == EventTypes.RoomHistoryVisibility
for event in initial_state
)

# If not set, add default history_visibility as "invited"
if not has_history_visibility:
initial_state.append(
{
"type": EventTypes.RoomHistoryVisibility,
"state_key": "",
"content": {"history_visibility": HistoryVisibility.INVITED},
}
)
request_content["initial_state"] = initial_state

if is_request_admin:
return

Expand Down
125 changes: 125 additions & 0 deletions tests/test_createrooms_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,89 @@ def test_create_room_then_modify_history_visibility(
expect_code=HTTPStatus.FORBIDDEN,
)

def test_create_room_default_history_visibility_invited(self) -> None:
"""
Test that rooms are created with history visibility "invited" by default (A_25481),
but users can override this setting during room creation
"""
# Test 1: Private room created without explicit history_visibility should default to "invited"
room_id_private = self.create_local_room(self.pro_user_a, [], is_public=False)
assert room_id_private, "Private room should be created"

# Get the history visibility state event
state_events = self.helper.get_state(
room_id_private,
EventTypes.RoomHistoryVisibility,
tok=self.access_token_a,
)
assert (
state_events["history_visibility"] == HistoryVisibility.INVITED
), "Default history visibility should be 'invited'"

# Test 2: Public room created without explicit history_visibility should default to "invited"
room_id_public = self.create_local_room(self.pro_user_a, [], is_public=True)
assert room_id_public, "Public room should be created"

# Get the history visibility state event
state_events = self.helper.get_state(
room_id_public,
EventTypes.RoomHistoryVisibility,
tok=self.access_token_a,
)
assert (
state_events["history_visibility"] == HistoryVisibility.INVITED
), "Default history visibility should be 'invited'"

# Test 3: User can override history visibility during private room creation
custom_history_visibility = {
"type": EventTypes.RoomHistoryVisibility,
"state_key": "",
"content": {"history_visibility": HistoryVisibility.SHARED},
}
override_content = {"initial_state": [custom_history_visibility]}

room_id_private_custom = self.create_local_room(
self.pro_user_a, [], is_public=False, override_content=override_content
)
assert (
room_id_private_custom
), "Private room with custom history visibility should be created"

# Get the history visibility state event for the custom room
state_events_custom = self.helper.get_state(
room_id_private_custom,
EventTypes.RoomHistoryVisibility,
tok=self.access_token_a,
)
assert (
state_events_custom["history_visibility"] == HistoryVisibility.SHARED
), "Custom history visibility should be respected"

# Test 4: User can override history visibility during public room creation
custom_history_visibility = {
"type": EventTypes.RoomHistoryVisibility,
"state_key": "",
"content": {"history_visibility": HistoryVisibility.SHARED},
}
override_content = {"initial_state": [custom_history_visibility]}

room_id_public_custom = self.create_local_room(
self.pro_user_a, [], is_public=True, override_content=override_content
)
assert (
room_id_public_custom
), "Public room with custom history visibility should be created"

# Get the history visibility state event for the custom room
state_events_custom = self.helper.get_state(
room_id_public_custom,
EventTypes.RoomHistoryVisibility,
tok=self.access_token_a,
)
assert (
state_events_custom["history_visibility"] == HistoryVisibility.SHARED
), "Custom history visibility should be respected"


class LocalEpaModeCreateRoomTest(FederatingModuleApiTestCase):
"""
Expand Down Expand Up @@ -323,3 +406,45 @@ def test_create_server_notices_room(self) -> None:
# Retrieving the room_id is a sign that the room was created, the user was
# invited, and the message was sent
assert room_id, "Server notices room should have been found"

def test_create_room_default_history_visibility_invited(self) -> None:
"""
Test that rooms are created with history visibility "invited" by default (A_25481),
but users can override this setting during room creation
"""
# Test 1: Room created without explicit history_visibility should default to "invited"
room_id = self.create_local_room(self.epa_user_d, [], is_public=False)
assert room_id, "Room should be created"

# Get the history visibility state event
state_events = self.helper.get_state(
room_id,
EventTypes.RoomHistoryVisibility,
tok=self.access_token,
)
assert (
state_events["history_visibility"] == HistoryVisibility.INVITED
), "Default history visibility should be 'invited'"

# Test 2: User can override history visibility during room creation
custom_history_visibility = {
"type": EventTypes.RoomHistoryVisibility,
"state_key": "",
"content": {"history_visibility": HistoryVisibility.SHARED},
}
override_content = {"initial_state": [custom_history_visibility]}

room_id_custom = self.create_local_room(
self.epa_user_d, [], is_public=False, override_content=override_content
)
assert room_id_custom, "Room with custom history visibility should be created"

# Get the history visibility state event for the custom room
state_events_custom = self.helper.get_state(
room_id_custom,
EventTypes.RoomHistoryVisibility,
tok=self.access_token,
)
assert (
state_events_custom["history_visibility"] == HistoryVisibility.SHARED
), "Custom history visibility should be respected"