From 3f374ca2325135f12f65b149be94ca9914946ac2 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Fri, 13 Sep 2019 15:48:19 +0200 Subject: [PATCH] Fix messages stuff Signed-off-by: Mario Danic --- .../talk/controllers/ChatController.java | 57 +++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java index d617bae1f..fe67b8cf3 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java @@ -998,13 +998,14 @@ public class ChatController extends BaseController implements MessagesListAdapte fieldMap.put("includeLastKnown", 0); int timeout = 30; - if (!lookingIntoFuture ) { + if (lookIntoFuture == 0) { timeout = 0; } + fieldMap.put("timeout", timeout); + if (lookIntoFuture > 0) { lookingIntoFuture = true; - fieldMap.put("timeout", timeout); } else if (isFirstMessagesProcessing) { globalLastKnownFutureMessageId = currentConversation.getLastReadMessage(); globalLastKnownPastMessageId = currentConversation.getLastReadMessage(); @@ -1024,6 +1025,7 @@ public class ChatController extends BaseController implements MessagesListAdapte fieldMap.put("lastKnownMessageId", lastKnown); + Log.d("MARIO-PUSH", lastKnown + " " + lookIntoFuture); if (!wasDetached) { if (lookIntoFuture > 0) { int finalTimeout = timeout; @@ -1051,9 +1053,7 @@ public class ChatController extends BaseController implements MessagesListAdapte @Override public void onComplete() { - if (!currentConversation.shouldShowLobby(conversationUser)) { - pullChatMessages(1); - } + } }); @@ -1090,23 +1090,27 @@ public class ChatController extends BaseController implements MessagesListAdapte } private void processMessages(Response response, boolean isFromTheFuture, int timeout) { - if (response.code() == 200) { + String xChatLastGivenHeader; + if (response.headers().size() > 0 && !TextUtils.isEmpty((xChatLastGivenHeader = response.headers().get("X-Chat-Last-Given")))) { - String xChatLastGivenHeader; - if (response.headers().size() > 0 && !TextUtils.isEmpty((xChatLastGivenHeader = response.headers().get - ("X-Chat-Last-Given")))) { - if (xChatLastGivenHeader != null) { - if (isFromTheFuture) { - globalLastKnownFutureMessageId = Integer.parseInt(xChatLastGivenHeader); - } else { - globalLastKnownPastMessageId = Integer.parseInt(xChatLastGivenHeader); - } + int header = Integer.parseInt(xChatLastGivenHeader); + if (header > 0) { + if (isFromTheFuture) { + globalLastKnownFutureMessageId = header; + } else { + globalLastKnownPastMessageId = header; } } + } + + Log.d("MARIO-PUSH PROCESSING", String.valueOf(isFromTheFuture)); + if (response.code() == 200) { ChatOverall chatOverall = (ChatOverall) response.body(); List chatMessageList = chatOverall.getOcs().getData(); + boolean wasFirstMessageProcessing = isFirstMessagesProcessing; + if (isFirstMessagesProcessing) { cancelNotificationsForCurrentConversation(); @@ -1124,6 +1128,7 @@ public class ChatController extends BaseController implements MessagesListAdapte int countGroupedMessages = 0; if (!isFromTheFuture) { + Log.d("MARIO-PUSH", "ADDING MESSAGES TO PAST " + chatMessageList.size()); for (int i = 0; i < chatMessageList.size(); i++) { if (chatMessageList.size() > i + 1) { if (TextUtils.isEmpty(chatMessageList.get(i).getSystemMessage()) && @@ -1144,6 +1149,10 @@ public class ChatController extends BaseController implements MessagesListAdapte chatMessage.setActiveUser(conversationUser); } + if (wasFirstMessageProcessing) { + globalLastKnownFutureMessageId = chatMessageList.get(0).getJsonMessageId(); + } + if (adapter != null) { adapter.addToEnd(chatMessageList, false); } @@ -1166,6 +1175,8 @@ public class ChatController extends BaseController implements MessagesListAdapte boolean isThereANewNotice = shouldAddNewMessagesNotice || adapter.getMessagePositionByIdInReverse("-1") != -1; + Log.d("MARIO-PUSH", "ADDING MESSAGES TO FUTURE " + chatMessageList.size()); + for (int i = 0; i < chatMessageList.size(); i++) { chatMessage = chatMessageList.get(i); @@ -1209,20 +1220,20 @@ public class ChatController extends BaseController implements MessagesListAdapte } - if (!lookingIntoFuture && inChat) { + if (inChat) { pullChatMessages(1); } } else if (response.code() == 304 && !isFromTheFuture) { - if (isFirstMessagesProcessing) { - cancelNotificationsForCurrentConversation(); + if (isFirstMessagesProcessing) { + cancelNotificationsForCurrentConversation(); - isFirstMessagesProcessing = false; - if (loadingProgressBar != null) { - loadingProgressBar.setVisibility(View.GONE); + isFirstMessagesProcessing = false; + if (loadingProgressBar != null) { + loadingProgressBar.setVisibility(View.GONE); + } } - } - historyRead = true; + historyRead = true; if (!lookingIntoFuture && inChat) { pullChatMessages(1);