Merge pull request #1908 from famedly/td/ignoreStateIfNoJoin

fix: race condition between getState and requestUser updates
This commit is contained in:
td 2024-08-27 10:11:34 +02:00 committed by GitHub
commit a01880a527
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 6 deletions

View File

@ -1694,10 +1694,9 @@ class Room {
required bool requestProfile, required bool requestProfile,
}) async { }) async {
// Is user already in cache? // Is user already in cache?
final userFromState = getState(EventTypes.RoomMember, mxID)?.asUser(this);
// If not in cache, try the database // If not in cache, try the database
var foundUser = userFromState; User? foundUser = getState(EventTypes.RoomMember, mxID)?.asUser(this);
// If the room is not postloaded, check the database // If the room is not postloaded, check the database
if (partial && foundUser == null) { if (partial && foundUser == null) {
@ -1706,8 +1705,10 @@ class Room {
// If not in the database, try fetching the member from the server // If not in the database, try fetching the member from the server
if (requestState && foundUser == null) { if (requestState && foundUser == null) {
foundUser = await _requestSingleParticipantViaState(mxID, foundUser = await _requestSingleParticipantViaState(
ignoreErrors: ignoreErrors); mxID,
ignoreErrors: ignoreErrors,
);
} }
// If the user isn't found or they have left and no displayname set anymore, request their profile from the server // If the user isn't found or they have left and no displayname set anymore, request their profile from the server
@ -1739,11 +1740,14 @@ class Room {
} }
if (foundUser == null) return null; if (foundUser == null) return null;
// make sure we didn't actually store anything by the time we did those requests
final userFromCurrentState =
getState(EventTypes.RoomMember, mxID)?.asUser(this);
// Set user in the local state if the state changed. // Set user in the local state if the state changed.
// If we set the state unconditionally, we might end up with a client calling this over and over thinking the user changed. // If we set the state unconditionally, we might end up with a client calling this over and over thinking the user changed.
if (userFromState == null || if (userFromCurrentState == null ||
userFromState.displayName != foundUser.displayName) { userFromCurrentState.displayName != foundUser.displayName) {
setState(foundUser); setState(foundUser);
// ignore: deprecated_member_use_from_same_package // ignore: deprecated_member_use_from_same_package
onUpdate.add(id); onUpdate.add(id);