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, userId: String,
roomMember: RoomMemberContent roomMember: RoomMemberContent
) { ) {
val roomMemberEntity = RoomMemberEntityFactory.create( val existingRoomMemberSummary = RoomMemberSummaryEntity.where(realm, roomId, userId).findFirst()
roomId, if (existingRoomMemberSummary != null) {
userId, existingRoomMemberSummary.displayName = roomMember.displayName
roomMember, existingRoomMemberSummary.avatarUrl = roomMember.avatarUrl
// When an update is happening, insertOrUpdate replace existing values with null if they are not provided, existingRoomMemberSummary.membership = roomMember.membership
// but we want to preserve presence record value and not replace it with null } else {
getExistingPresenceState(realm, roomId, userId) val roomMemberEntity = RoomMemberEntityFactory.create(
) roomId,
realm.insertOrUpdate(roomMemberEntity) userId,
} roomMember
)
/** 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( private fun saveUserEntityLocallyIfNecessary(