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 637163a7307..7bb3000ef84 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 @@ -46,6 +46,7 @@ type initialMessagesLoaded: bool firstUnseenMessageState: FirstUnseenMessageState getMessageRequestId: UUID + pinnedMessages: Table[string, string] # messageId -> pinnedBy proc newModule*(delegate: delegate_interface.AccessInterface, events: EventEmitter, sectionId: string, chatId: string, belongsToCommunity: bool, contactService: contact_service.Service, communityService: community_service.Service, @@ -61,6 +62,7 @@ proc newModule*(delegate: delegate_interface.AccessInterface, events: EventEmitt result.moduleLoaded = false result.initialMessagesLoaded = false result.firstUnseenMessageState = (false, false, false) + result.pinnedMessages = initTable[string, string]() # Forward declaration proc createChatIdentifierItem(self: Module): Item @@ -401,10 +403,18 @@ method reevaluateViewLoadingState*(self: Module) = self.view.getMessageSearchOngoing() self.view.setLoading(loading) +proc applyPinToMessages*(self: Module, messageItems: var seq[Item]) = + for item in messageItems: + if self.pinnedMessages.hasKey(item.id): + item.pinned = true + item.pinnedBy = self.pinnedMessages[item.id] + method newMessagesLoaded*(self: Module, messages: seq[MessageDto], reactions: seq[ReactionDto]) = if messages.len > 0: var viewItems = self.createMessageItemsFromMessageDtos(messages, reactions) + self.applyPinToMessages(viewItems) + if self.controller.getChatDetails().hasMoreMessagesToRequest(): viewItems.add(self.createFetchMoreMessagesItem()) viewItems.add(self.createChatIdentifierItem()) @@ -425,7 +435,9 @@ method newPinnedMessagesLoaded*(self: Module, pinnedMessages: seq[PinnedMessageD self.onPinMessage(p.message.id, p.pinnedBy) method messagesAdded*(self: Module, messages: seq[MessageDto]) = - let items = self.createMessageItemsFromMessageDtos(messages) + var items = self.createMessageItemsFromMessageDtos(messages) + + self.applyPinToMessages(items) self.view.model().insertItemsBasedOnClock(items) @@ -509,10 +521,13 @@ method markMessageAsUnread*(self: Module, messageId: string) = self.controller.markMessageAsUnread(messageId) method onPinMessage*(self: Module, messageId: string, actionInitiatedBy: string) = - self.view.model().pinUnpinMessage(messageId, true, actionInitiatedBy) + self.view.model().pinUnpinMessage(messageId, pinned=true, actionInitiatedBy) + self.pinnedMessages[messageId] = actionInitiatedBy method onUnpinMessage*(self: Module, messageId: string) = self.view.model().pinUnpinMessage(messageId, false, "") + if self.pinnedMessages.hasKey(messageId): + self.pinnedMessages.del(messageId) method onMarkMessageAsUnread*(self: Module, messageId: string) = self.view.model().markMessageAsUnread(messageId) diff --git a/src/app/modules/shared_models/message_model.nim b/src/app/modules/shared_models/message_model.nim index e012195133a..05a79b35e2b 100644 --- a/src/app/modules/shared_models/message_model.nim +++ b/src/app/modules/shared_models/message_model.nim @@ -601,12 +601,15 @@ QtObject: defer: index.delete self.dataChanged(index, index, @[ModelRole.Reactions.int]) - proc pinUnpinMessage*(self: Model, messageId: string, pin: bool, pinnedBy: string) = + proc pinUnpinMessage*(self: Model, messageId: string, pinned: bool, pinnedBy: string) = let ind = self.findIndexForMessageId(messageId) - if(ind == -1): + if ind == -1: + return + + if self.items[ind].pinned == pinned: return - self.items[ind].pinned = pin + self.items[ind].pinned = pinned self.items[ind].pinnedBy = pinnedBy let index = self.createIndex(ind, 0, nil)