refactor(@desktop/chat): reply to a message and message ordering

- fixed issue where reply to a message was added as 2 messages in the message list
- messages are added to a model respecting their timestamp
This commit is contained in:
Sale Djenic 2022-01-11 14:59:52 +01:00
parent 051142cf3b
commit f24d6e968b
3 changed files with 39 additions and 2 deletions

View File

@ -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)

View File

@ -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):

View File

@ -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