Merge pull request #6410 from vector-im/feature/fga/load_room_members_by_chunk
Feature/fga/load room members by chunk
This commit is contained in:
commit
778269f831
|
@ -0,0 +1 @@
|
||||||
|
Let LoadRoomMembersTask insert by chunk to release db.
|
|
@ -58,7 +58,7 @@ internal class DefaultMembershipService @AssistedInject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun loadRoomMembersIfNeeded() {
|
override suspend fun loadRoomMembersIfNeeded() {
|
||||||
val params = LoadRoomMembersTask.Params(roomId, Membership.LEAVE)
|
val params = LoadRoomMembersTask.Params(roomId, excludeMembership = Membership.LEAVE)
|
||||||
loadRoomMembersTask.execute(params)
|
loadRoomMembersTask.execute(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,7 @@ import org.matrix.android.sdk.internal.session.sync.SyncTokenStore
|
||||||
import org.matrix.android.sdk.internal.task.Task
|
import org.matrix.android.sdk.internal.task.Task
|
||||||
import org.matrix.android.sdk.internal.util.awaitTransaction
|
import org.matrix.android.sdk.internal.util.awaitTransaction
|
||||||
import org.matrix.android.sdk.internal.util.time.Clock
|
import org.matrix.android.sdk.internal.util.time.Clock
|
||||||
|
import timber.log.Timber
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@ -105,32 +106,37 @@ internal class DefaultLoadRoomMembersTask @Inject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun insertInDb(response: RoomMembersResponse, roomId: String) {
|
private suspend fun insertInDb(response: RoomMembersResponse, roomId: String) {
|
||||||
|
val chunks = response.roomMemberEvents.chunked(500)
|
||||||
|
chunks.forEach { roomMemberEvents ->
|
||||||
|
monarchy.awaitTransaction { realm ->
|
||||||
|
Timber.v("Insert ${roomMemberEvents.size} member events in room $roomId")
|
||||||
|
// We ignore all the already known members
|
||||||
|
val now = clock.epochMillis()
|
||||||
|
for (roomMemberEvent in roomMemberEvents) {
|
||||||
|
if (roomMemberEvent.eventId == null || roomMemberEvent.stateKey == null || roomMemberEvent.type == null) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val ageLocalTs = now - (roomMemberEvent.unsignedData?.age ?: 0)
|
||||||
|
val eventEntity = roomMemberEvent.toEntity(roomId, SendState.SYNCED, ageLocalTs).copyToRealmOrIgnore(realm, EventInsertType.PAGINATION)
|
||||||
|
CurrentStateEventEntity.getOrCreate(
|
||||||
|
realm,
|
||||||
|
roomId,
|
||||||
|
roomMemberEvent.stateKey,
|
||||||
|
roomMemberEvent.type
|
||||||
|
).apply {
|
||||||
|
eventId = roomMemberEvent.eventId
|
||||||
|
root = eventEntity
|
||||||
|
}
|
||||||
|
roomMemberEventHandler.handle(realm, roomId, roomMemberEvent, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
monarchy.awaitTransaction { realm ->
|
monarchy.awaitTransaction { realm ->
|
||||||
// We ignore all the already known members
|
|
||||||
val roomEntity = RoomEntity.where(realm, roomId).findFirst()
|
val roomEntity = RoomEntity.where(realm, roomId).findFirst()
|
||||||
?: realm.createObject(roomId)
|
?: realm.createObject(roomId)
|
||||||
val now = clock.epochMillis()
|
|
||||||
for (roomMemberEvent in response.roomMemberEvents) {
|
|
||||||
if (roomMemberEvent.eventId == null || roomMemberEvent.stateKey == null || roomMemberEvent.type == null) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
val ageLocalTs = now - (roomMemberEvent.unsignedData?.age ?: 0)
|
|
||||||
val eventEntity = roomMemberEvent.toEntity(roomId, SendState.SYNCED, ageLocalTs).copyToRealmOrIgnore(realm, EventInsertType.PAGINATION)
|
|
||||||
CurrentStateEventEntity.getOrCreate(
|
|
||||||
realm,
|
|
||||||
roomId,
|
|
||||||
roomMemberEvent.stateKey,
|
|
||||||
roomMemberEvent.type
|
|
||||||
).apply {
|
|
||||||
eventId = roomMemberEvent.eventId
|
|
||||||
root = eventEntity
|
|
||||||
}
|
|
||||||
roomMemberEventHandler.handle(realm, roomId, roomMemberEvent, false)
|
|
||||||
}
|
|
||||||
roomEntity.membersLoadStatus = RoomMembersLoadStatusType.LOADED
|
roomEntity.membersLoadStatus = RoomMembersLoadStatusType.LOADED
|
||||||
roomSummaryUpdater.update(realm, roomId, updateMembers = true)
|
roomSummaryUpdater.update(realm, roomId, updateMembers = true)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cryptoSessionInfoProvider.isRoomEncrypted(roomId)) {
|
if (cryptoSessionInfoProvider.isRoomEncrypted(roomId)) {
|
||||||
deviceListManager.onRoomMembersLoadedFor(roomId)
|
deviceListManager.onRoomMembersLoadedFor(roomId)
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,23 +83,21 @@ 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 presenceEntity = UserPresenceEntity.where(realm, userId).findFirst()
|
||||||
)
|
val roomMemberEntity = RoomMemberEntityFactory.create(
|
||||||
realm.insertOrUpdate(roomMemberEntity)
|
roomId,
|
||||||
}
|
userId,
|
||||||
|
roomMember,
|
||||||
/**
|
presenceEntity
|
||||||
* Get the already existing presence state for a specific user & room in order NOT to be replaced in RoomMemberSummaryEntity
|
)
|
||||||
* by NULL value.
|
realm.insert(roomMemberEntity)
|
||||||
*/
|
}
|
||||||
private fun getExistingPresenceState(realm: Realm, roomId: String, userId: String): UserPresenceEntity? {
|
|
||||||
return RoomMemberSummaryEntity.where(realm, roomId, userId).findFirst()?.userPresenceEntity
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun saveUserEntityLocallyIfNecessary(
|
private fun saveUserEntityLocallyIfNecessary(
|
||||||
|
|
|
@ -33,6 +33,7 @@ import kotlinx.coroutines.withContext
|
||||||
import okhttp3.internal.closeQuietly
|
import okhttp3.internal.closeQuietly
|
||||||
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
|
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
|
||||||
import org.matrix.android.sdk.api.extensions.tryOrNull
|
import org.matrix.android.sdk.api.extensions.tryOrNull
|
||||||
|
import org.matrix.android.sdk.api.session.room.model.Membership
|
||||||
import org.matrix.android.sdk.api.session.room.timeline.Timeline
|
import org.matrix.android.sdk.api.session.room.timeline.Timeline
|
||||||
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
|
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
|
||||||
import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings
|
import org.matrix.android.sdk.api.session.room.timeline.TimelineSettings
|
||||||
|
@ -388,7 +389,7 @@ internal class DefaultTimeline(
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun loadRoomMembersIfNeeded() {
|
private suspend fun loadRoomMembersIfNeeded() {
|
||||||
val loadRoomMembersParam = LoadRoomMembersTask.Params(roomId)
|
val loadRoomMembersParam = LoadRoomMembersTask.Params(roomId, excludeMembership = Membership.LEAVE)
|
||||||
try {
|
try {
|
||||||
loadRoomMembersTask.execute(loadRoomMembersParam)
|
loadRoomMembersTask.execute(loadRoomMembersParam)
|
||||||
} catch (failure: Throwable) {
|
} catch (failure: Throwable) {
|
||||||
|
|
Loading…
Reference in New Issue