Sync : handle as a single write transaction

This commit is contained in:
ganfra 2018-11-05 14:31:45 +01:00
parent c6bcd1c022
commit 3199f5dcd6
2 changed files with 16 additions and 20 deletions

View file

@ -31,21 +31,16 @@ 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 ->
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) }
@ -53,7 +48,6 @@ internal class RoomSyncHandler(private val monarchy: Monarchy,
}
realm.insertOrUpdate(rooms)
}
}
private fun handleJoinedRoom(realm: Realm,
roomId: String,
@ -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>() }

View file

@ -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()
)