Skip to content

Commit f9345b0

Browse files
committed
re add insurance call
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
1 parent e16b5bf commit f9345b0

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class OfflineFirstChatRepository @Inject constructor(
122122
private lateinit var urlForChatting: String
123123
private var threadId: Long? = null
124124

125+
private var latestKnownMessageIdFromSync: Long = 0
126+
125127
override fun initData(
126128
currentUser: User,
127129
credentials: String,
@@ -220,7 +222,7 @@ class OfflineFirstChatRepository @Inject constructor(
220222
if (!hasHighPerformanceBackend) {
221223
initLongPolling(newestMessageIdFromDb)
222224
} else {
223-
// initRepeatingInsuranceRequest(newestMessageIdFromDb)
225+
initRepeatingInsuranceRequest()
224226
}
225227
}
226228

@@ -377,33 +379,30 @@ class OfflineFirstChatRepository @Inject constructor(
377379
}
378380
}
379381

380-
private fun initRepeatingInsuranceRequest(initialMessageId: Long) {
382+
private fun initRepeatingInsuranceRequest() {
381383
scope.launch {
382384
Log.d(TAG, "---- initRepeatingInsuranceRequest ------------")
383385

384-
Log.d(TAG, "newestMessage: $initialMessageId")
385-
386386
var showUnreadMessagesMarker = true
387387

388-
// the message id we know from the initial request or last insurance request. NOT any message id from signaling received messages)
389-
var lastKnownMessageIdFromInsuranceRequest = initialMessageId.toInt()
388+
delay(INSURANCE_REQUEST_DELAY)
390389

391390
while (isActive) {
392-
Log.d(TAG, "lastKnownMessageIdFromInsuranceRequest: $lastKnownMessageIdFromInsuranceRequest")
391+
Log.d(TAG, "latestKnownMessageIdFromSync: $latestKnownMessageIdFromSync")
393392

394393
var fieldMap = getFieldMap(
395394
lookIntoFuture = true,
396395
timeout = 0,
397396
includeLastKnown = false,
398-
lastKnown = lastKnownMessageIdFromInsuranceRequest,
397+
lastKnown = latestKnownMessageIdFromSync.toInt(),
399398
limit = 200
400399
)
401400
val networkParams = Bundle()
402401
networkParams.putSerializable(BundleKeys.KEY_FIELD_MAP, fieldMap)
403402

404403
getAndPersistMessages(networkParams)
405404

406-
delay(1 * 60 * 1000)
405+
delay(INSURANCE_REQUEST_DELAY)
407406
}
408407
}
409408
}
@@ -645,11 +644,15 @@ class OfflineFirstChatRepository @Inject constructor(
645644
it.asEntity(currentUser.id!!)
646645
}
647646

647+
chatDao.upsertChatMessages(chatMessagesFromSyncToProcess)
648+
648649
val oldestIdFromSync = chatMessagesFromSyncToProcess.minByOrNull { it.id }!!.id
649650
val newestIdFromSync = chatMessagesFromSyncToProcess.maxByOrNull { it.id }!!.id
650651
Log.d(TAG, "oldestIdFromSync: $oldestIdFromSync")
651652
Log.d(TAG, "newestIdFromSync: $newestIdFromSync")
652653

654+
latestKnownMessageIdFromSync = maxOf(latestKnownMessageIdFromSync, newestIdFromSync)
655+
653656
var oldestMessageIdForNewChatBlock = oldestIdFromSync
654657
var newestMessageIdForNewChatBlock = newestIdFromSync
655658

@@ -679,8 +682,6 @@ class OfflineFirstChatRepository @Inject constructor(
679682
)
680683
chatBlocksDao.upsertChatBlock(newChatBlock)
681684
updateBlocks(newChatBlock)
682-
683-
chatDao.upsertChatMessages(chatMessagesFromSyncToProcess)
684685
}
685686

686687
private suspend fun handleUpdateMessages(messagesJson: List<ChatMessageJson>) {
@@ -1064,11 +1065,12 @@ class OfflineFirstChatRepository @Inject constructor(
10641065
}
10651066

10661067
scope.launch {
1067-
updateNewestChatBlockWithNewLatestMessageId(chatMessage.id)
1068-
10691068
val chatMessageEntity = chatMessage.asEntity(currentUser.id!!)
10701069
chatDao.upsertChatMessages(listOf(chatMessageEntity))
1070+
1071+
updateNewestChatBlockWithNewLatestMessageId(chatMessage.id)
10711072
}
1073+
10721074
}
10731075

10741076
override fun observeMessages(
@@ -1176,6 +1178,7 @@ class OfflineFirstChatRepository @Inject constructor(
11761178
private const val HALF_SECOND = 500L
11771179
private const val DELAY_TO_ENSURE_MESSAGES_ARE_ADDED: Long = 100
11781180
private const val DEFAULT_MESSAGES_LIMIT = 100
1179-
private const val MILLIES = 1000
1181+
private const val MILLIES = 1000L
1182+
private const val INSURANCE_REQUEST_DELAY = 1 * 60 * MILLIES
11801183
}
11811184
}

0 commit comments

Comments
 (0)