Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Correctly create power level event during initial room creation #14361

Merged
merged 8 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions changelog.d/14361.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug introduced in #14228 where the power level event was incorrectly created during initial room creation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was the bug ever released?
If so, cite the version number instead.
If not, probably just coalesce the changelogs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was released in most recent rc. I've updated the changelog.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, is it serious enough to go into the release branch?
Actually it occurs to me that I don't understand what 'the power level event was incorrectly created' means — at first thought this sounds like the power levels event wasn't meant to be created, but surely that can't be right.

Iit would be best to note the user-visible bug if there is one. I honestly have lost track of this over the weekend so I'm afraid I can't remember enough to advise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't an end-user facing bug afaict. From my understanding, the worst thing that could happen with this bug is the code could break unexpectedly if other changes were made to the surrounding code, so mostly fixing this is to a: prevent confusion in the future and b: set up the code for further changes (such as adding the membership event to the batch or batching up state groups).

12 changes: 10 additions & 2 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,13 +1139,21 @@ async def create_event(
depth += 1
state_map[(EventTypes.Member, creator.user.to_string())] = member_event_id

# we need the state group of the membership event as it is the current state group
event_to_state = (
await self._storage_controllers.state.get_state_group_for_events(
[member_event_id]
)
)
current_state_group = event_to_state[member_event_id]

events_to_send = []
# We treat the power levels override specially as this needs to be one
# of the first events that get sent into a room.
pl_content = initial_state.pop((EventTypes.PowerLevels, ""), None)
if pl_content is not None:
power_event, power_context = await create_event(
EventTypes.PowerLevels, pl_content, False
EventTypes.PowerLevels, pl_content, True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this bool seems to be for_batch, which doesn't have a docstring.
Would you mind adding a docstring on it? I would find it especially useful to know why it's needed and what the semantics are — from the name I'm guessing the usage is just True when you are creating an event to send in a batch, but the other parts are more interesting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a docstring, let me know if it makes more sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, but maybe would benefit from an elaboration about what the difference is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some elaboration, hopefully it is clearer.

)
current_state_group = power_context._state_group
events_to_send.append((power_event, power_context))
Expand Down Expand Up @@ -1194,7 +1202,7 @@ async def create_event(
pl_event, pl_context = await create_event(
EventTypes.PowerLevels,
power_level_content,
False,
True,
)
current_state_group = pl_context._state_group
events_to_send.append((pl_event, pl_context))
Expand Down
4 changes: 2 additions & 2 deletions tests/rest/client/test_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ def test_post_room_no_keys(self) -> None:
self.assertEqual(HTTPStatus.OK, channel.code, channel.result)
self.assertTrue("room_id" in channel.json_body)
assert channel.resource_usage is not None
self.assertEqual(34, channel.resource_usage.db_txn_count)
self.assertEqual(33, channel.resource_usage.db_txn_count)

def test_post_room_initial_state(self) -> None:
# POST with initial_state config key, expect new room id
Expand All @@ -728,7 +728,7 @@ def test_post_room_initial_state(self) -> None:
self.assertEqual(HTTPStatus.OK, channel.code, channel.result)
self.assertTrue("room_id" in channel.json_body)
assert channel.resource_usage is not None
self.assertEqual(37, channel.resource_usage.db_txn_count)
self.assertEqual(36, channel.resource_usage.db_txn_count)

def test_post_room_visibility_key(self) -> None:
# POST with visibility config key, expect new room id
Expand Down