Handle sync response to delete user and room account data.

This commit is contained in:
Onuray Sahin 2022-12-07 18:58:14 +03:00
parent 765202e05a
commit f4429d4c9c
4 changed files with 34 additions and 48 deletions

View file

@ -1,33 +0,0 @@
/*
* Copyright 2022 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.internal.database.query
import io.realm.Realm
import io.realm.kotlin.where
import org.matrix.android.sdk.internal.database.model.RoomAccountDataEntity
import org.matrix.android.sdk.internal.database.model.RoomAccountDataEntityFields
/**
* Delete an account_data event.
*/
internal fun RoomAccountDataEntity.Companion.delete(realm: Realm, type: String) {
realm
.where<RoomAccountDataEntity>()
.equalTo(RoomAccountDataEntityFields.TYPE, type)
.findFirst()
?.deleteFromRealm()
}

View file

@ -21,6 +21,7 @@ import io.realm.RealmQuery
import io.realm.kotlin.where
import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.internal.database.model.EventEntity
import org.matrix.android.sdk.internal.database.model.RoomAccountDataEntityFields
import org.matrix.android.sdk.internal.database.model.RoomEntity
import org.matrix.android.sdk.internal.database.model.RoomEntityFields
@ -44,3 +45,11 @@ internal fun RoomEntity.Companion.where(realm: Realm, membership: Membership? =
internal fun RoomEntity.fastContains(eventId: String): Boolean {
return EventEntity.where(realm, eventId = eventId).findFirst() != null
}
internal fun RoomEntity.removeAccountData(type: String) {
accountData
.where()
.equalTo(RoomAccountDataEntityFields.TYPE, type)
.findFirst()
?.deleteFromRealm()
}

View file

@ -45,6 +45,7 @@ import org.matrix.android.sdk.internal.database.model.TimelineEventEntity
import org.matrix.android.sdk.internal.database.model.UserAccountDataEntity
import org.matrix.android.sdk.internal.database.model.UserAccountDataEntityFields
import org.matrix.android.sdk.internal.database.model.deleteOnCascade
import org.matrix.android.sdk.internal.database.query.delete
import org.matrix.android.sdk.internal.database.query.findAllFrom
import org.matrix.android.sdk.internal.database.query.getDirectRooms
import org.matrix.android.sdk.internal.database.query.getOrCreate
@ -81,20 +82,24 @@ internal class UserAccountDataSyncHandler @Inject constructor(
fun handle(realm: Realm, accountData: UserAccountDataSync?) {
accountData?.list?.forEach { event ->
// Generic handling, just save in base
handleGenericAccountData(realm, event.type, event.content)
when (event.type) {
UserAccountDataTypes.TYPE_DIRECT_MESSAGES -> handleDirectChatRooms(realm, event)
UserAccountDataTypes.TYPE_PUSH_RULES -> handlePushRules(realm, event)
UserAccountDataTypes.TYPE_IGNORED_USER_LIST -> handleIgnoredUsers(realm, event)
UserAccountDataTypes.TYPE_BREADCRUMBS -> handleBreadcrumbs(realm, event)
if (event.content.isEmpty()) {
UserAccountDataEntity.delete(realm, event.type)
} else {
// Generic handling, just save in base
handleGenericAccountData(realm, event.type, event.content)
when (event.type) {
UserAccountDataTypes.TYPE_DIRECT_MESSAGES -> handleDirectChatRooms(realm, event)
UserAccountDataTypes.TYPE_PUSH_RULES -> handlePushRules(realm, event)
UserAccountDataTypes.TYPE_IGNORED_USER_LIST -> handleIgnoredUsers(realm, event)
UserAccountDataTypes.TYPE_BREADCRUMBS -> handleBreadcrumbs(realm, event)
}
}
}
}
// If we get some direct chat invites, we synchronize the user account data including those.
suspend fun synchronizeWithServerIfNeeded(invites: Map<String, InvitedRoomSync>) {
if (invites.isNullOrEmpty()) return
if (invites.isEmpty()) return
val directChats = directChatsHelper.getLocalDirectMessages().toMutable()
var hasUpdate = false
monarchy.doWithRealm { realm ->

View file

@ -27,6 +27,7 @@ import org.matrix.android.sdk.internal.database.model.RoomAccountDataEntity
import org.matrix.android.sdk.internal.database.model.RoomAccountDataEntityFields
import org.matrix.android.sdk.internal.database.model.RoomEntity
import org.matrix.android.sdk.internal.database.query.getOrCreate
import org.matrix.android.sdk.internal.database.query.removeAccountData
import org.matrix.android.sdk.internal.session.room.read.FullyReadContent
import org.matrix.android.sdk.internal.session.sync.handler.room.RoomFullyReadHandler
import org.matrix.android.sdk.internal.session.sync.handler.room.RoomTagHandler
@ -44,13 +45,17 @@ internal class RoomSyncAccountDataHandler @Inject constructor(
val roomEntity = RoomEntity.getOrCreate(realm, roomId)
for (event in accountData.events) {
val eventType = event.getClearType()
handleGeneric(roomEntity, event.getClearContent(), eventType)
if (eventType == RoomAccountDataTypes.EVENT_TYPE_TAG) {
val content = event.getClearContent().toModel<RoomTagContent>()
roomTagHandler.handle(realm, roomId, content)
} else if (eventType == RoomAccountDataTypes.EVENT_TYPE_FULLY_READ) {
val content = event.getClearContent().toModel<FullyReadContent>()
roomFullyReadHandler.handle(realm, roomId, content)
if (event.getClearContent().isNullOrEmpty()) {
roomEntity.removeAccountData(eventType)
} else {
handleGeneric(roomEntity, event.getClearContent(), eventType)
if (eventType == RoomAccountDataTypes.EVENT_TYPE_TAG) {
val content = event.getClearContent().toModel<RoomTagContent>()
roomTagHandler.handle(realm, roomId, content)
} else if (eventType == RoomAccountDataTypes.EVENT_TYPE_FULLY_READ) {
val content = event.getClearContent().toModel<FullyReadContent>()
roomFullyReadHandler.handle(realm, roomId, content)
}
}
}
}