mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-01-28 18:09:30 +01: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) {
|
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 ->
|
monarchy.runTransactionSync { realm ->
|
||||||
roomsSyncResponse.join.forEach { (roomId, roomSync) ->
|
handleRoomSync(realm, RoomSyncHandler.HandlingStrategy.JOINED(roomsSyncResponse.join))
|
||||||
handleEphemeral(realm, roomId, roomSync.ephemeral)
|
handleRoomSync(realm, RoomSyncHandler.HandlingStrategy.INVITED(roomsSyncResponse.invite))
|
||||||
}
|
handleRoomSync(realm, RoomSyncHandler.HandlingStrategy.LEFT(roomsSyncResponse.leave))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PRIVATE METHODS *****************************************************************************
|
// PRIVATE METHODS *****************************************************************************
|
||||||
|
|
||||||
private fun handleRoomSync(handlingStrategy: HandlingStrategy) {
|
private fun handleRoomSync(realm: Realm, handlingStrategy: HandlingStrategy) {
|
||||||
monarchy.runTransactionSync { realm ->
|
val rooms = when (handlingStrategy) {
|
||||||
val rooms = when (handlingStrategy) {
|
is HandlingStrategy.JOINED -> handlingStrategy.data.map { handleJoinedRoom(realm, it.key, it.value) }
|
||||||
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.INVITED -> handlingStrategy.data.map { handleInvitedRoom(realm, it.key, it.value) }
|
is HandlingStrategy.LEFT -> handlingStrategy.data.map { handleLeftRoom(it.key, it.value) }
|
||||||
is HandlingStrategy.LEFT -> handlingStrategy.data.map { handleLeftRoom(it.key, it.value) }
|
|
||||||
}
|
|
||||||
realm.insertOrUpdate(rooms)
|
|
||||||
}
|
}
|
||||||
|
realm.insertOrUpdate(rooms)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleJoinedRoom(realm: Realm,
|
private fun handleJoinedRoom(realm: Realm,
|
||||||
@ -80,9 +74,14 @@ internal class RoomSyncHandler(private val monarchy: Monarchy,
|
|||||||
roomEntity.chunks.add(chunkEntity)
|
roomEntity.chunks.add(chunkEntity)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (roomSync.summary != null) {
|
if (roomSync.summary != null) {
|
||||||
handleRoomSummary(realm, roomId, roomSync.summary)
|
handleRoomSummary(realm, roomId, roomSync.summary)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (roomSync.ephemeral != null && roomSync.ephemeral.events.isNotEmpty()) {
|
||||||
|
handleEphemeral(realm, roomId, roomSync.ephemeral)
|
||||||
|
}
|
||||||
return roomEntity
|
return roomEntity
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,10 +151,7 @@ internal class RoomSyncHandler(private val monarchy: Monarchy,
|
|||||||
|
|
||||||
private fun handleEphemeral(realm: Realm,
|
private fun handleEphemeral(realm: Realm,
|
||||||
roomId: String,
|
roomId: String,
|
||||||
ephemeral: RoomSyncEphemeral?) {
|
ephemeral: RoomSyncEphemeral) {
|
||||||
if (ephemeral == null || ephemeral.events.isNullOrEmpty()) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ephemeral.events
|
ephemeral.events
|
||||||
.filter { it.type == EventType.RECEIPT }
|
.filter { it.type == EventType.RECEIPT }
|
||||||
.map { it.content<ReadReceiptContent>() }
|
.map { it.content<ReadReceiptContent>() }
|
||||||
|
@ -11,5 +11,5 @@ data class RoomSyncEphemeral(
|
|||||||
/**
|
/**
|
||||||
* List of ephemeral events (array of Event).
|
* List of ephemeral events (array of Event).
|
||||||
*/
|
*/
|
||||||
@Json(name = "events") val events: List<Event>? = null
|
@Json(name = "events") val events: List<Event> = emptyList()
|
||||||
)
|
)
|
Loading…
x
Reference in New Issue
Block a user