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:
parent
051142cf3b
commit
f24d6e968b
|
@ -125,7 +125,7 @@ method messageAdded*(self: Module, message: MessageDto) =
|
||||||
sender.icon, sender.isIdenticon, sender.isCurrentUser, message.outgoingStatus, message.text, message.image,
|
sender.icon, sender.isIdenticon, sender.isCurrentUser, message.outgoingStatus, message.text, message.image,
|
||||||
message.seen, message.timestamp, message.contentType.ContentType, message.messageType)
|
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) =
|
method onSendingMessageSuccess*(self: Module, message: MessageDto) =
|
||||||
self.messageAdded(message)
|
self.messageAdded(message)
|
||||||
|
|
|
@ -145,9 +145,14 @@ QtObject:
|
||||||
for i in 0 ..< self.items.len:
|
for i in 0 ..< self.items.len:
|
||||||
if(self.items[i].id == messageId):
|
if(self.items[i].id == messageId):
|
||||||
return i
|
return i
|
||||||
|
|
||||||
return -1
|
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]) =
|
proc prependItems*(self: Model, items: seq[Item]) =
|
||||||
if(items.len == 0):
|
if(items.len == 0):
|
||||||
return
|
return
|
||||||
|
@ -194,6 +199,17 @@ QtObject:
|
||||||
self.endInsertRows()
|
self.endInsertRows()
|
||||||
self.countChanged()
|
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) =
|
proc removeItem*(self: Model, messageId: string) =
|
||||||
let ind = self.findIndexForMessageId(messageId)
|
let ind = self.findIndexForMessageId(messageId)
|
||||||
if(ind == -1):
|
if(ind == -1):
|
||||||
|
|
|
@ -82,6 +82,12 @@ QtObject:
|
||||||
result.pinnedMsgCursor = initTable[string, string]()
|
result.pinnedMsgCursor = initTable[string, string]()
|
||||||
result.lastUsedPinnedMsgCursor = 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) =
|
proc init*(self: Service) =
|
||||||
self.events.on(SignalType.Message.event) do(e: Args):
|
self.events.on(SignalType.Message.event) do(e: Args):
|
||||||
var receivedData = MessageSignal(e)
|
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
|
# The first element from the `receivedData.chats` array contains details about the chat a messages received in
|
||||||
# `receivedData.messages` refer to.
|
# `receivedData.messages` refer to.
|
||||||
let chatId = receivedData.chats[0].id
|
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)
|
let data = MessagesArgs(chatId: chatId, messages: receivedData.messages)
|
||||||
self.events.emit(SIGNAL_NEW_MESSAGE_RECEIVED, data)
|
self.events.emit(SIGNAL_NEW_MESSAGE_RECEIVED, data)
|
||||||
# Handling pinned messages updates
|
# Handling pinned messages updates
|
||||||
|
|
Loading…
Reference in New Issue