Skip to content

Commit

Permalink
fix(pins): apply pins to messages being loaded by fetching more
Browse files Browse the repository at this point in the history
Fixes #16896

Fixed by keeping a table of the pins in the module and applying the pin to the messages when they are loaded (if applicable)
  • Loading branch information
jrainville committed Dec 6, 2024
1 parent 017bae3 commit 27ef71f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
19 changes: 17 additions & 2 deletions src/app/modules/main/chat_section/chat_content/messages/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions src/app/modules/shared_models/message_model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 27ef71f

Please sign in to comment.