diff --git a/src/app/modules/main/chat_section/chat_content/messages/module.nim b/src/app/modules/main/chat_section/chat_content/messages/module.nim index 38e20afee9..bbf5608412 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/module.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/module.nim @@ -125,7 +125,7 @@ method messageAdded*(self: Module, message: MessageDto) = sender.icon, sender.isIdenticon, sender.isCurrentUser, message.outgoingStatus, message.text, message.image, message.seen, message.timestamp, message.contentType.ContentType, message.messageType) - self.view.model().prependItem(item) + self.view.model().insertItemBasedOnTimestamp(item) method onSendingMessageSuccess*(self: Module, message: MessageDto) = self.messageAdded(message) diff --git a/src/app/modules/shared_models/message_model.nim b/src/app/modules/shared_models/message_model.nim index 8649e8175e..6071026c67 100644 --- a/src/app/modules/shared_models/message_model.nim +++ b/src/app/modules/shared_models/message_model.nim @@ -145,9 +145,14 @@ QtObject: for i in 0 ..< self.items.len: if(self.items[i].id == messageId): return i - return -1 + proc findIndexBasedOnTimestampToInsertTo(self: Model, timestamp: int64): int = + for i in 0 ..< self.items.len: + if(timestamp > self.items[i].timestamp): + return i + return 0 + proc prependItems*(self: Model, items: seq[Item]) = if(items.len == 0): return @@ -194,6 +199,17 @@ QtObject: self.endInsertRows() self.countChanged() + proc insertItemBasedOnTimestamp*(self: Model, item: Item) = + let parentModelIndex = newQModelIndex() + defer: parentModelIndex.delete + + let position = self.findIndexBasedOnTimestampToInsertTo(item.timestamp) + + self.beginInsertRows(parentModelIndex, position, position) + self.items.insert(item, position) + self.endInsertRows() + self.countChanged() + proc removeItem*(self: Model, messageId: string) = let ind = self.findIndexForMessageId(messageId) if(ind == -1): diff --git a/src/app_service/service/message/service.nim b/src/app_service/service/message/service.nim index 64b2cdefa7..ac071cc0e9 100644 --- a/src/app_service/service/message/service.nim +++ b/src/app_service/service/message/service.nim @@ -82,6 +82,12 @@ QtObject: result.pinnedMsgCursor = initTable[string, string]() result.lastUsedPinnedMsgCursor = initTable[string, string]() + proc removeMessageWithId(messages: var seq[MessageDto], msgId: string) = + for i in 0..< messages.len: + if (messages[i].id == msgId): + messages.delete(i) + return + proc init*(self: Service) = self.events.on(SignalType.Message.event) do(e: Args): var receivedData = MessageSignal(e) @@ -92,6 +98,21 @@ QtObject: # The first element from the `receivedData.chats` array contains details about the chat a messages received in # `receivedData.messages` refer to. let chatId = receivedData.chats[0].id + + # In case of reply to a message we're receiving 2 messages in the `receivedData.messages` array (replied message + # and a message one replied to) but we actually need only a new replied message, that's why we need to filter + # messages here. + # We are not sure if we can receive more replies here, also ordering in the `receivedData.messages` array is not + # the same (once we may have replied messages before once after the messages one replied to), that's why we are + # covering the most general case here. + var messagesOneRepliedTo: seq[string] + for m in receivedData.messages: + if m.responseTo.len > 0: + messagesOneRepliedTo.add(m.responseTo) + + for msgId in messagesOneRepliedTo: + removeMessageWithId(receivedData.messages, msgId) + let data = MessagesArgs(chatId: chatId, messages: receivedData.messages) self.events.emit(SIGNAL_NEW_MESSAGE_RECEIVED, data) # Handling pinned messages updates