-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Delete empty dm room on createRoom error #1912
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -795,27 +795,49 @@ class Client extends MatrixApi { | |
)); | ||
} | ||
} | ||
try { | ||
// Start a new direct chat | ||
final roomId = await createRoom( | ||
invite: [mxid], | ||
isDirect: true, | ||
preset: preset, | ||
initialState: initialState, | ||
powerLevelContentOverride: powerLevelContentOverride, | ||
); | ||
|
||
// Start a new direct chat | ||
final roomId = await createRoom( | ||
invite: [mxid], | ||
isDirect: true, | ||
preset: preset, | ||
initialState: initialState, | ||
powerLevelContentOverride: powerLevelContentOverride, | ||
); | ||
|
||
if (waitForSync) { | ||
final room = getRoomById(roomId); | ||
if (room == null || room.membership != Membership.join) { | ||
// Wait for room actually appears in sync | ||
await waitForRoomInSync(roomId, join: true); | ||
if (waitForSync) { | ||
final room = getRoomById(roomId); | ||
if (room == null || room.membership != Membership.join) { | ||
// Wait for room actually appears in sync | ||
await waitForRoomInSync(roomId, join: true); | ||
} | ||
} | ||
} | ||
|
||
await Room(id: roomId, client: this).addToDirectChat(mxid); | ||
await Room(id: roomId, client: this).addToDirectChat(mxid); | ||
|
||
return roomId; | ||
return roomId; | ||
} catch (e) { | ||
/// This looks up the room id of the room that was just created. | ||
/// The room is empty since createRoom threw an error. To make sure not to | ||
/// delete other empty rooms by accident, the creation time is checked. | ||
/// Should be fixed in Synapse and then be removed | ||
Comment on lines
+821
to
+823
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should really document, why this throws an error, because the synapse code tries to handle all errors ahead of time, so it is not clear, why you are getting an error on room creation. If the problem is invites, we can for example move those out of the room creation. |
||
final sortedRooms = rooms.sortedBy((room) => room.timeCreated).reversed; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you know, that the room is already synced back to the client? |
||
final emptyRoom = sortedRooms.firstWhereOrNull((room) => | ||
room.name.isEmpty && | ||
!room.isDirectChat && | ||
room.summary.mJoinedMemberCount == 1 && | ||
room.summary.mInvitedMemberCount == 0 && | ||
room.timeCreated | ||
.isAfter(DateTime.now().subtract(Duration(seconds: 20)))); | ||
final roomId = emptyRoom?.id; | ||
Comment on lines
+824
to
+832
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's probably more robust to handle the room id list differences from before and after the room creation instead of going purely by timestamp? |
||
|
||
/// If we can find an empty unnamed room that was recently created, we leave and delete it | ||
if (roomId != null) { | ||
await leaveRoom(roomId); | ||
await forgetRoom(roomId); | ||
Comment on lines
+836
to
+837
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can throw and needs error handling to not cause weird error messages. |
||
} | ||
rethrow; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code needs tests imo. |
||
|
||
/// Simplified method to create a new group chat. By default it is a private | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is catching any error, not just the room creation error. You should narrowly scope the try catch and handle specific errors, not all of them and assume it is the one error you intended to handle.