From 0a00c99823818228479f9fdb8c77e61a49a65e18 Mon Sep 17 00:00:00 2001 From: David Robertson Date: Tue, 5 Dec 2023 12:06:21 +0000 Subject: [PATCH] Fix upgrading a room without `events` field in power levels (#16725) --- changelog.d/16725.bugfix | 1 + synapse/handlers/room.py | 2 +- tests/rest/client/test_upgrade_room.py | 28 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 changelog.d/16725.bugfix diff --git a/changelog.d/16725.bugfix b/changelog.d/16725.bugfix new file mode 100644 index 000000000..42dc249fb --- /dev/null +++ b/changelog.d/16725.bugfix @@ -0,0 +1 @@ +Fix a bug introduced in Synapse 1.7.2 where rooms whose power levels lacked an `events` field could not be upgraded. diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index f865bed1e..2823ca6f0 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -549,7 +549,7 @@ class RoomCreationHandler: except (TypeError, ValueError): ban = 50 needed_power_level = max( - state_default_int, ban, max(event_power_levels.values()) + state_default_int, ban, max(event_power_levels.values(), default=0) ) # Get the user's current power level, this matches the logic in get_user_power_level, diff --git a/tests/rest/client/test_upgrade_room.py b/tests/rest/client/test_upgrade_room.py index 0b4c69131..28f772261 100644 --- a/tests/rest/client/test_upgrade_room.py +++ b/tests/rest/client/test_upgrade_room.py @@ -246,6 +246,34 @@ class UpgradeRoomTest(unittest.HomeserverTestCase): # We should now have an integer power level. self.assertEqual(new_power_levels["users"][self.creator], 100, new_power_levels) + def test_events_field_missing(self) -> None: + """Regression test for https://github.com/matrix-org/synapse/issues/16715.""" + # Create a new room. + room_id = self.helper.create_room_as( + self.creator, tok=self.creator_token, room_version="10" + ) + self.helper.join(room_id, self.other, tok=self.other_token) + + # Retrieve the room's current power levels. + power_levels = self.helper.get_state( + room_id, + "m.room.power_levels", + tok=self.creator_token, + ) + + # Remove the events field and re-set the power levels. + del power_levels["events"] + self.helper.send_state( + room_id, + "m.room.power_levels", + body=power_levels, + tok=self.creator_token, + ) + + # Upgrade the room. Check the homeserver reports success. + channel = self._upgrade_room(room_id=room_id) + self.assertEqual(200, channel.code, channel.result) + def test_space(self) -> None: """Test upgrading a space."""