mirror of
https://github.com/vector-im/element-android.git
synced 2024-11-15 01:35:07 +08:00
Sync : handle as a single write transaction
This commit is contained in:
parent
c6bcd1c022
commit
3199f5dcd6
@ -31,28 +31,22 @@ internal class RoomSyncHandler(private val monarchy: Monarchy,
|
||||
}
|
||||
|
||||
fun handle(roomsSyncResponse: RoomsSyncResponse) {
|
||||
handleRoomSync(RoomSyncHandler.HandlingStrategy.JOINED(roomsSyncResponse.join))
|
||||
handleRoomSync(RoomSyncHandler.HandlingStrategy.INVITED(roomsSyncResponse.invite))
|
||||
handleRoomSync(RoomSyncHandler.HandlingStrategy.LEFT(roomsSyncResponse.leave))
|
||||
|
||||
monarchy.runTransactionSync { realm ->
|
||||
roomsSyncResponse.join.forEach { (roomId, roomSync) ->
|
||||
handleEphemeral(realm, roomId, roomSync.ephemeral)
|
||||
}
|
||||
handleRoomSync(realm, RoomSyncHandler.HandlingStrategy.JOINED(roomsSyncResponse.join))
|
||||
handleRoomSync(realm, RoomSyncHandler.HandlingStrategy.INVITED(roomsSyncResponse.invite))
|
||||
handleRoomSync(realm, RoomSyncHandler.HandlingStrategy.LEFT(roomsSyncResponse.leave))
|
||||
}
|
||||
}
|
||||
|
||||
// PRIVATE METHODS *****************************************************************************
|
||||
|
||||
private fun handleRoomSync(handlingStrategy: HandlingStrategy) {
|
||||
monarchy.runTransactionSync { realm ->
|
||||
val rooms = when (handlingStrategy) {
|
||||
is HandlingStrategy.JOINED -> handlingStrategy.data.map { handleJoinedRoom(realm, it.key, it.value) }
|
||||
is HandlingStrategy.INVITED -> handlingStrategy.data.map { handleInvitedRoom(realm, it.key, it.value) }
|
||||
is HandlingStrategy.LEFT -> handlingStrategy.data.map { handleLeftRoom(it.key, it.value) }
|
||||
}
|
||||
realm.insertOrUpdate(rooms)
|
||||
private fun handleRoomSync(realm: Realm, handlingStrategy: HandlingStrategy) {
|
||||
val rooms = when (handlingStrategy) {
|
||||
is HandlingStrategy.JOINED -> handlingStrategy.data.map { handleJoinedRoom(realm, it.key, it.value) }
|
||||
is HandlingStrategy.INVITED -> handlingStrategy.data.map { handleInvitedRoom(realm, it.key, it.value) }
|
||||
is HandlingStrategy.LEFT -> handlingStrategy.data.map { handleLeftRoom(it.key, it.value) }
|
||||
}
|
||||
realm.insertOrUpdate(rooms)
|
||||
}
|
||||
|
||||
private fun handleJoinedRoom(realm: Realm,
|
||||
@ -80,9 +74,14 @@ internal class RoomSyncHandler(private val monarchy: Monarchy,
|
||||
roomEntity.chunks.add(chunkEntity)
|
||||
}
|
||||
}
|
||||
|
||||
if (roomSync.summary != null) {
|
||||
handleRoomSummary(realm, roomId, roomSync.summary)
|
||||
}
|
||||
|
||||
if (roomSync.ephemeral != null && roomSync.ephemeral.events.isNotEmpty()) {
|
||||
handleEphemeral(realm, roomId, roomSync.ephemeral)
|
||||
}
|
||||
return roomEntity
|
||||
}
|
||||
|
||||
@ -152,10 +151,7 @@ internal class RoomSyncHandler(private val monarchy: Monarchy,
|
||||
|
||||
private fun handleEphemeral(realm: Realm,
|
||||
roomId: String,
|
||||
ephemeral: RoomSyncEphemeral?) {
|
||||
if (ephemeral == null || ephemeral.events.isNullOrEmpty()) {
|
||||
return
|
||||
}
|
||||
ephemeral: RoomSyncEphemeral) {
|
||||
ephemeral.events
|
||||
.filter { it.type == EventType.RECEIPT }
|
||||
.map { it.content<ReadReceiptContent>() }
|
||||
|
@ -11,5 +11,5 @@ data class RoomSyncEphemeral(
|
||||
/**
|
||||
* List of ephemeral events (array of Event).
|
||||
*/
|
||||
@Json(name = "events") val events: List<Event>? = null
|
||||
@Json(name = "events") val events: List<Event> = emptyList()
|
||||
)
|
Loading…
Reference in New Issue
Block a user