From 6efbbae8239de36f5885d41054c571179077eb24 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Thu, 9 Mar 2023 14:01:34 +0100 Subject: [PATCH] Extract creation of fieldMap for pullChatMessages Signed-off-by: Marcel Hibbe --- .../talk/controllers/ChatController.kt | 103 ++++++++++-------- 1 file changed, 56 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt index 67c03be64..2c03e08cb 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt @@ -2251,52 +2251,13 @@ class ChatController(args: Bundle) : Log.d(TAG, "pullChatMessages - pullChatMessagesPending is true, exiting") return } - pullChatMessagesPending = true - Log.d(TAG, "pullChatMessagesPending was set to true") - val fieldMap = HashMap() - fieldMap["includeLastKnown"] = 0 - - if (!lookIntoFuture && isFirstMessagesProcessing) { - if (currentConversation != null) { - globalLastKnownFutureMessageId = currentConversation!!.lastReadMessage - globalLastKnownPastMessageId = currentConversation!!.lastReadMessage - fieldMap["includeLastKnown"] = 1 - } - } - - val lastKnown = if (lookIntoFuture) { - globalLastKnownFutureMessageId - } else { - globalLastKnownPastMessageId - } - - fieldMap["lastKnownMessageId"] = lastKnown - xChatLastCommonRead?.let { - fieldMap["lastCommonReadId"] = it - } - - val timeout = if (lookIntoFuture) { - LOOKING_INTO_FUTURE_TIMEOUT - } else { - 0 - } - - fieldMap["timeout"] = timeout - fieldMap["limit"] = MESSAGE_PULL_LIMIT - - if (lookIntoFuture) { - fieldMap["lookIntoFuture"] = 1 - } else { - fieldMap["lookIntoFuture"] = 0 - } - - if (setReadMarker) { - fieldMap["setReadMarker"] = 1 - } else { - fieldMap["setReadMarker"] = 0 - } + val pullChatMessagesFieldMap = setupFieldsForPullChatMessages( + lookIntoFuture, + xChatLastCommonRead, + setReadMarker + ) var apiVersion = 1 // FIXME this is a best guess, guests would need to get the capabilities themselves @@ -2307,7 +2268,7 @@ class ChatController(args: Bundle) : ncApi.pullChatMessages( credentials, ApiUtils.getUrlForChat(apiVersion, conversationUser?.baseUrl, roomToken), - fieldMap + pullChatMessagesFieldMap ) ?.subscribeOn(Schedulers.io()) ?.observeOn(AndroidSchedulers.mainThread()) @@ -2320,7 +2281,6 @@ class ChatController(args: Bundle) : @Suppress("Detekt.TooGenericExceptionCaught") override fun onNext(response: Response<*>) { pullChatMessagesPending = false - Log.d(TAG, "pullChatMessagesPending was set to false") when (response.code()) { HTTP_CODE_NOT_MODIFIED -> { @@ -2396,6 +2356,56 @@ class ChatController(args: Bundle) : }) } + private fun setupFieldsForPullChatMessages( + lookIntoFuture: Boolean, + xChatLastCommonRead: Int?, + setReadMarker: Boolean + ): HashMap { + val fieldMap = HashMap() + fieldMap["includeLastKnown"] = 0 + + if (!lookIntoFuture && isFirstMessagesProcessing) { + if (currentConversation != null) { + globalLastKnownFutureMessageId = currentConversation!!.lastReadMessage + globalLastKnownPastMessageId = currentConversation!!.lastReadMessage + fieldMap["includeLastKnown"] = 1 + } + } + + val lastKnown = if (lookIntoFuture) { + globalLastKnownFutureMessageId + } else { + globalLastKnownPastMessageId + } + + fieldMap["lastKnownMessageId"] = lastKnown + xChatLastCommonRead?.let { + fieldMap["lastCommonReadId"] = it + } + + val timeout = if (lookIntoFuture) { + LOOKING_INTO_FUTURE_TIMEOUT + } else { + 0 + } + + fieldMap["timeout"] = timeout + fieldMap["limit"] = MESSAGE_PULL_LIMIT + + if (lookIntoFuture) { + fieldMap["lookIntoFuture"] = 1 + } else { + fieldMap["lookIntoFuture"] = 0 + } + + if (setReadMarker) { + fieldMap["setReadMarker"] = 1 + } else { + fieldMap["setReadMarker"] = 0 + } + return fieldMap + } + private fun processExpiredMessages() { fun deleteExpiredMessages() { val messagesToDelete: ArrayList = ArrayList() @@ -2608,7 +2618,6 @@ class ChatController(args: Bundle) : } override fun onLoadMore(page: Int, totalItemsCount: Int) { - Log.d(TAG, "requested onLoadMore to pullChatMessages with lookIntoFuture=false") pullChatMessages(false) }