fix(@desktop/chat): crash when pinning a message for a chat with cleared history

This commit is contained in:
Andrei Smirnov 2021-07-08 11:15:56 +03:00 committed by Iuri Matias
parent a69c2aea73
commit 0eab4009ba
2 changed files with 14 additions and 11 deletions

View File

@ -338,9 +338,11 @@ QtObject:
let bottomRight = self.createIndex(msgIdx, 0, nil)
self.dataChanged(topLeft, bottomRight, @[ChatMessageRoles.EmojiReactions.int])
proc changeMessagePinned*(self: ChatMessageList, messageId: string, pinned: bool, pinnedBy: string) =
if not self.messageIndex.hasKey(messageId): return
proc changeMessagePinned*(self: ChatMessageList, messageId: string, pinned: bool, pinnedBy: string): bool {.discardable.} =
if not self.messageIndex.hasKey(messageId): return false
let msgIdx = self.messageIndex[messageId]
if msgIdx < 0: return false
if msgIdx >= self.messages.len: return false
var message = self.messages[msgIdx]
message.isPinned = pinned
message.pinnedBy = pinnedBy
@ -348,6 +350,7 @@ QtObject:
let topLeft = self.createIndex(msgIdx, 0, nil)
let bottomRight = self.createIndex(msgIdx, 0, nil)
self.dataChanged(topLeft, bottomRight, @[ChatMessageRoles.IsPinned.int, ChatMessageRoles.PinnedBy.int])
return true
proc markMessageAsSent*(self: ChatMessageList, messageId: string)=
let topLeft = self.createIndex(0, 0, nil)

View File

@ -399,18 +399,18 @@ QtObject:
proc addPinMessage*(self: MessageView, messageId: string, chatId: string, pinnedBy: string) =
self.upsertChannel(chatId)
self.messageList[chatId].changeMessagePinned(messageId, true, pinnedBy)
var message = self.messageList[chatId].getMessageById(messageId)
message.pinnedBy = pinnedBy
self.pinnedMessagesList[chatId].add(message)
if self.messageList[chatId].changeMessagePinned(messageId, true, pinnedBy):
var message = self.messageList[chatId].getMessageById(messageId)
message.pinnedBy = pinnedBy
self.pinnedMessagesList[chatId].add(message)
proc removePinMessage*(self: MessageView, messageId: string, chatId: string) =
self.upsertChannel(chatId)
self.messageList[chatId].changeMessagePinned(messageId, false, "")
try:
self.pinnedMessagesList[chatId].remove(messageId)
except Exception as e:
error "Error removing ", msg = e.msg
if self.messageList[chatId].changeMessagePinned(messageId, false, ""):
try:
self.pinnedMessagesList[chatId].remove(messageId)
except Exception as e:
error "Error removing ", msg = e.msg
proc pinMessage*(self: MessageView, messageId: string, chatId: string) {.slot.} =
self.status.chat.setPinMessage(messageId, chatId, true)