fix: handle message reactions for old messages not loaded

This commit is contained in:
Richard Ramos 2020-08-28 13:35:01 -04:00 committed by Iuri Matias
parent 507f510c62
commit 84df214f6f
3 changed files with 13 additions and 9 deletions

View File

@ -259,11 +259,10 @@ QtObject:
self.newMessagePushed() self.newMessagePushed()
proc messageEmojiReactionId(self: ChatsView, chatId: string, messageId: string, emojiId: int): string = proc messageEmojiReactionId(self: ChatsView, chatId: string, messageId: string, emojiId: int): string =
let message = self.messageList[chatId].getMessageById(messageId) if (self.messageList[chatId].getReactions(messageId) == "") :
if (message.emojiReactions == "") :
return "" return ""
let oldReactions = parseJson(message.emojiReactions) let oldReactions = parseJson(self.messageList[chatId].getReactions(messageId))
for pair in oldReactions.pairs: for pair in oldReactions.pairs:
if (pair[1]["emojiId"].getInt == emojiId and pair[1]["from"].getStr == self.pubKey): if (pair[1]["emojiId"].getInt == emojiId and pair[1]["from"].getStr == self.pubKey):
@ -281,12 +280,12 @@ QtObject:
let t = reactions.len let t = reactions.len
for reaction in reactions.mitems: for reaction in reactions.mitems:
let messageList = self.messageList[reaction.chatId] let messageList = self.messageList[reaction.chatId]
var message = messageList.getMessageById(reaction.messageId) var emojiReactions = messageList.getReactions(reaction.messageId)
var oldReactions: JsonNode var oldReactions: JsonNode
if (message.emojiReactions == "") : if (emojiReactions == "") :
oldReactions = %*{} oldReactions = %*{}
else: else:
oldReactions = parseJson(message.emojiReactions) oldReactions = parseJson(emojiReactions)
if (oldReactions.hasKey(reaction.id)): if (oldReactions.hasKey(reaction.id)):
if (reaction.retracted): if (reaction.retracted):

View File

@ -38,6 +38,7 @@ QtObject:
messages*: seq[Message] messages*: seq[Message]
status: Status status: Status
messageIndex: Table[string, int] messageIndex: Table[string, int]
messageReactions*: Table[string, string]
timedoutMessages: HashSet[string] timedoutMessages: HashSet[string]
proc delete(self: ChatMessageList) = proc delete(self: ChatMessageList) =
@ -87,6 +88,10 @@ QtObject:
method rowCount(self: ChatMessageList, index: QModelIndex = nil): int = method rowCount(self: ChatMessageList, index: QModelIndex = nil): int =
return self.messages.len return self.messages.len
proc getReactions*(self:ChatMessageList, messageId: string):string =
if not self.messageReactions.hasKey(messageId): return ""
result = self.messageReactions[messageId]
method data(self: ChatMessageList, index: QModelIndex, role: int): QVariant = method data(self: ChatMessageList, index: QModelIndex, role: int): QVariant =
if not index.isValid: if not index.isValid:
return return
@ -116,7 +121,7 @@ QtObject:
of ChatMessageRoles.Image: result = newQVariant(message.image) of ChatMessageRoles.Image: result = newQVariant(message.image)
of ChatMessageRoles.Audio: result = newQVariant(message.audio) of ChatMessageRoles.Audio: result = newQVariant(message.audio)
of ChatMessageRoles.AudioDurationMs: result = newQVariant(message.audioDurationMs) of ChatMessageRoles.AudioDurationMs: result = newQVariant(message.audioDurationMs)
of ChatMessageRoles.EmojiReactions: result = newQVariant(message.emojiReactions) of ChatMessageRoles.EmojiReactions: result = newQVariant(self.getReactions(message.id))
method roleNames(self: ChatMessageList): Table[int, string] = method roleNames(self: ChatMessageList): Table[int, string] =
{ {
@ -188,8 +193,9 @@ QtObject:
self.endResetModel() self.endResetModel()
proc setMessageReactions*(self: ChatMessageList, messageId: string, newReactions: string)= proc setMessageReactions*(self: ChatMessageList, messageId: string, newReactions: string)=
self.messageReactions[messageId] = newReactions
if not self.messageIndex.hasKey(messageId): return
let msgIdx = self.messageIndex[messageId] let msgIdx = self.messageIndex[messageId]
self.messages[msgIdx].emojiReactions = newReactions
let topLeft = self.createIndex(msgIdx, 0, nil) let topLeft = self.createIndex(msgIdx, 0, nil)
let bottomRight = self.createIndex(msgIdx, 0, nil) let bottomRight = self.createIndex(msgIdx, 0, nil)
self.dataChanged(topLeft, bottomRight, @[ChatMessageRoles.EmojiReactions.int]) self.dataChanged(topLeft, bottomRight, @[ChatMessageRoles.EmojiReactions.int])

View File

@ -48,7 +48,6 @@ type Message* = object
image*: string image*: string
audio*: string audio*: string
audioDurationMs*: int audioDurationMs*: int
emojiReactions*: string
type Reaction* = object type Reaction* = object
id*: string id*: string