From fd6ebae7c0e6e5b5236f5e26e4a04a5bc3ec53df Mon Sep 17 00:00:00 2001 From: Sebastian Wolf Date: Tue, 2 Feb 2021 00:42:42 +0100 Subject: [PATCH] Better chat index handling, see #235 & #271 --- qml/pages/ChatPage.qml | 2 +- src/chatmodel.cpp | 37 +++++++++++++++++++------------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index bc6bc34..045b72b 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -548,7 +548,7 @@ Page { Connections { target: chatModel onMessagesReceived: { - Debug.log("[ChatPage] Messages received, view has ", chatView.count, " messages, setting view to index ", modelIndex, ", own messages were read before index ", lastReadSentIndex); + Debug.log("[ChatPage] Messages received, view has ", chatView.count, " messages, last known message index ", modelIndex, ", own messages were read before index ", lastReadSentIndex); if (totalCount === 0) { if (chatPage.iterativeInitialization) { chatPage.iterativeInitialization = false; diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 089783b..c723d88 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -366,22 +366,7 @@ QVariantMap ChatModel::getMessage(int index) int ChatModel::getLastReadMessageIndex() { LOG("Obtaining last read message index"); - if (this->messages.isEmpty()) { - LOG("Messages are empty, nothing to do..."); - return 0; - } else if (messages.last()->senderUserId() == tdLibWrapper->getUserInformation().value(ID).toInt()) { - LOG("Last message is an own one, then simply set the last read to the last one..."); - return this->messages.size() - 1; - } else { - const int lastReadMessageIndex = messageIndexMap.value(chatInformation.value(LAST_READ_INBOX_MESSAGE_ID).toLongLong(), -1); - if (lastReadMessageIndex < 0) { - LOG("Last read message not found in the list of messages. That shouldn't happen, therefore setting the unread indicator to the end of the list."); - return this->messages.size() - 1; - } else { - LOG("Found last read message in the already loaded messages. Index:" << lastReadMessageIndex); - return lastReadMessageIndex; - } - } + return this->calculateLastKnownMessageId(); } void ChatModel::setSearchQuery(const QString newSearchQuery) @@ -539,6 +524,7 @@ void ChatModel::handleMessageSendSucceeded(qlonglong messageId, qlonglong oldMes const QModelIndex messageIndex(index(pos)); emit dataChanged(messageIndex, messageIndex, changedRoles); emit lastReadSentMessageUpdated(calculateLastReadSentMessageId()); + tdLibWrapper->viewMessage(QString::number(this->chatId), QString::number(messageId), false); } } @@ -756,14 +742,29 @@ int ChatModel::calculateLastKnownMessageId() LOG("calculateLastKnownMessageId"); const qlonglong lastKnownMessageId = this->chatInformation.value(LAST_READ_INBOX_MESSAGE_ID).toLongLong(); LOG("lastKnownMessageId" << lastKnownMessageId); + const int myUserId = tdLibWrapper->getUserInformation().value(ID).toInt(); + qlonglong lastOwnMessageId = 0; + for (int i = (messages.size() - 1); i >= 0; i--) { + MessageData *currentMessage = messages.at(i); + if (currentMessage->senderUserId() == myUserId) { + lastOwnMessageId = currentMessage->messageId; + break; + } + } LOG("size messageIndexMap" << messageIndexMap.size()); - LOG("contains ID?" << messageIndexMap.contains(lastKnownMessageId)); + LOG("contains last read ID?" << messageIndexMap.contains(lastKnownMessageId)); + LOG("contains last own ID?" << messageIndexMap.contains(lastOwnMessageId)); int listInboxPosition = messageIndexMap.value(lastKnownMessageId, messages.size() - 1); + int listOwnPosition = messageIndexMap.value(lastOwnMessageId, messages.size() - 1); if (listInboxPosition > this->messages.size() - 1 ) { listInboxPosition = this->messages.size() - 1; } + if (listOwnPosition > this->messages.size() - 1 ) { + listOwnPosition = this->messages.size() - 1; + } LOG("Last known message is at position" << listInboxPosition); - return listInboxPosition; + LOG("Last own message is at position" << listOwnPosition); + return (listInboxPosition > listOwnPosition) ? listInboxPosition : listOwnPosition ; } int ChatModel::calculateLastReadSentMessageId()