LoadRoomMembers: handle room member event a bit more efficiently

This commit is contained in:
ganfra 2022-06-29 11:35:31 +02:00
parent 63f0cce561
commit 61c5bbc444

View file

@ -83,23 +83,19 @@ internal class RoomMemberEventHandler @Inject constructor(
userId: String,
roomMember: RoomMemberContent
) {
val existingRoomMemberSummary = RoomMemberSummaryEntity.where(realm, roomId, userId).findFirst()
if (existingRoomMemberSummary != null) {
existingRoomMemberSummary.displayName = roomMember.displayName
existingRoomMemberSummary.avatarUrl = roomMember.avatarUrl
existingRoomMemberSummary.membership = roomMember.membership
} else {
val roomMemberEntity = RoomMemberEntityFactory.create(
roomId,
userId,
roomMember,
// When an update is happening, insertOrUpdate replace existing values with null if they are not provided,
// but we want to preserve presence record value and not replace it with null
getExistingPresenceState(realm, roomId, userId)
roomMember
)
realm.insertOrUpdate(roomMemberEntity)
realm.insert(roomMemberEntity)
}
/**
* Get the already existing presence state for a specific user & room in order NOT to be replaced in RoomMemberSummaryEntity
* by NULL value.
*/
private fun getExistingPresenceState(realm: Realm, roomId: String, userId: String): UserPresenceEntity? {
return RoomMemberSummaryEntity.where(realm, roomId, userId).findFirst()?.userPresenceEntity
}
private fun saveUserEntityLocallyIfNecessary(