Skip to content

Commit

Permalink
fix(members): fix slow chat switching by improving model (#16279)
Browse files Browse the repository at this point in the history
Fixes #16132
  • Loading branch information
jrainville authored Sep 25, 2024
1 parent 86fdc66 commit bb7e5be
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 126 deletions.
54 changes: 54 additions & 0 deletions src/app/modules/main/chat_section/chat_content/chat_details.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ QtObject:
canPostReactions: bool
hideIfPermissionsNotMet: bool
missingEncryptionKey: bool
requiresPermissions: bool

proc delete*(self: ChatDetails) =
self.QObject.delete
Expand Down Expand Up @@ -58,6 +59,7 @@ QtObject:
canPostReactions: bool = true,
hideIfPermissionsNotMet: bool,
missingEncryptionKey: bool,
requiresPermissions: bool,
) =
self.id = id
self.`type` = `type`
Expand All @@ -82,6 +84,7 @@ QtObject:
self.canPostReactions = canPostReactions
self.hideIfPermissionsNotMet = hideIfPermissionsNotMet
self.missingEncryptionKey = missingEncryptionKey
self.requiresPermissions = requiresPermissions

proc getId(self: ChatDetails): string {.slot.} =
return self.id
Expand Down Expand Up @@ -111,6 +114,8 @@ QtObject:
notify = nameChanged

proc setName*(self: ChatDetails, value: string) = # this is not a slot
if self.name == value:
return
self.name = value
self.nameChanged()

Expand All @@ -122,6 +127,8 @@ QtObject:
notify = iconChanged

proc setIcon*(self: ChatDetails, icon: string) = # this is not a slot
if self.icon == icon:
return
self.icon = icon
self.iconChanged()

Expand All @@ -133,6 +140,8 @@ QtObject:
notify = colorChanged

proc setColor*(self: ChatDetails, value: string) = # this is not a slot
if self.color == value:
return
self.color = value
self.colorChanged()

Expand All @@ -144,6 +153,8 @@ QtObject:
notify = emojiChanged

proc setEmoji*(self: ChatDetails, value: string) = # this is not a slot
if self.emoji == value:
return
self.emoji = value
self.emojiChanged()

Expand All @@ -155,6 +166,8 @@ QtObject:
notify = descriptionChanged

proc setDescription*(self: ChatDetails, value: string) = # this is not a slot
if self.description == value:
return
self.description = value
self.descriptionChanged()

Expand All @@ -166,6 +179,8 @@ QtObject:
notify = hasUnreadMessages

proc setHasUnreadMessages*(self: ChatDetails, value: bool) = # this is not a slot
if self.hasUnreadMessages == value:
return
self.hasUnreadMessages = value
self.hasUnreadMessagesChanged()

Expand All @@ -177,6 +192,8 @@ QtObject:
notify = notificationCountChanged

proc setNotificationCount*(self: ChatDetails, value: int) = # this is not a slot
if self.notificationsCount == value:
return
self.notificationsCount = value
self.notificationCountChanged()

Expand All @@ -188,6 +205,8 @@ QtObject:
notify = highlightChanged

proc setHighlight*(self: ChatDetails, value: bool) = # this is not a slot
if self.highlight == value:
return
self.highlight = value
self.highlightChanged()

Expand All @@ -199,6 +218,8 @@ QtObject:
notify = mutedChanged

proc setMuted*(self: ChatDetails, value: bool) = # this is not a slot
if self.muted == value:
return
self.muted = value
self.mutedChanged()

Expand All @@ -210,6 +231,8 @@ QtObject:
notify = positionChanged

proc setPotion*(self: ChatDetails, value: int) = # this is not a slot
if self.position == value:
return
self.position = value
self.positionChanged()

Expand All @@ -221,6 +244,8 @@ QtObject:
notify = isMutualContactChanged

proc setIsMutualContact*(self: ChatDetails, value: bool) = # this is not a slot
if self.isContact == value:
return
self.isContact = value
self.isMutualContactChanged()

Expand All @@ -232,6 +257,8 @@ QtObject:
notify = isUntrustworthyChanged

proc setIsUntrustworthy*(self: ChatDetails, value: bool) = # this is not a slot
if self.isUntrustworthy == value:
return
self.isUntrustworthy = value
self.isUntrustworthyChanged()

Expand All @@ -243,6 +270,8 @@ QtObject:
notify = activeChanged

proc setActive*(self: ChatDetails, value: bool) =
if self.active == value:
return
self.active = value
self.activeChanged()

Expand All @@ -254,6 +283,8 @@ QtObject:
notify = blockedChanged

proc setBlocked*(self: ChatDetails, value: bool) =
if self.blocked == value:
return
self.blocked = value
self.blockedChanged()

Expand All @@ -265,6 +296,8 @@ QtObject:
notify = canPostChanged

proc setCanPost*(self: ChatDetails, value: bool) =
if self.canPost == value:
return
self.canPost = value
self.canPostChanged()

Expand All @@ -276,6 +309,8 @@ QtObject:
notify = canViewChanged

proc setCanView*(self: ChatDetails, value: bool) =
if self.canView == value:
return
self.canView = value
self.canViewChanged()

Expand All @@ -287,6 +322,8 @@ QtObject:
notify = canPostReactionsChanged

proc setCanPostReactions*(self: ChatDetails, value: bool) =
if self.canPostReactions == value:
return
self.canPostReactions = value
self.canPostReactionsChanged()

Expand All @@ -298,6 +335,8 @@ QtObject:
notify = hideIfPermissionsNotMetChanged

proc setHideIfPermissionsNotMet*(self: ChatDetails, value: bool) =
if self.hideIfPermissionsNotMet == value:
return
self.hideIfPermissionsNotMet = value
self.hideIfPermissionsNotMetChanged()

Expand All @@ -309,5 +348,20 @@ QtObject:
notify = missingEncryptionKeyChanged

proc setMissingEncryptionKey*(self: ChatDetails, value: bool) =
if self.missingEncryptionKey == value:
return
self.missingEncryptionKey = value
self.missingEncryptionKeyChanged()

proc requiresPermissionsChanged(self: ChatDetails) {.signal.}
proc getRequiresPermissions(self: ChatDetails): bool {.slot.} =
return self.requiresPermissions
QtProperty[bool] requiresPermissions:
read = getRequiresPermissions
notify = requiresPermissionsChanged

proc setRequiresPermissions*(self: ChatDetails, value: bool) =
if self.requiresPermissions == value:
return
self.requiresPermissions = value
self.requiresPermissionsChanged()
4 changes: 3 additions & 1 deletion src/app/modules/main/chat_section/chat_content/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ method load*(self: Module, chatItem: chat_item.Item) =
chatItem.color, chatItem.description, chatItem.emoji, chatItem.hasUnreadMessages, chatItem.notificationsCount,
chatItem.highlight, chatItem.muted, chatItem.position, isUntrustworthy = trustStatus == TrustStatus.Untrustworthy,
isContact, chatItem.blocked, chatItem.canPost, chatItem.canView, chatItem.canPostReactions,
chatItem.hideIfPermissionsNotMet, chatItem.missingEncryptionKey)
chatItem.hideIfPermissionsNotMet, chatItem.missingEncryptionKey, chatItem.requiresPermissions)

self.view.chatDetailsChanged()

Expand Down Expand Up @@ -383,6 +383,7 @@ method onChatUpdated*(self: Module, chatItem: chat_item.Item) =
self.view.chatDetails.setCanPostReactions(chatItem.canPostReactions)
self.view.chatDetails.setHideIfPermissionsNotMet(chat_item.hideIfPermissionsNotMet)
self.view.chatDetails.setMissingEncryptionKey(chat_item.missingEncryptionKey)
self.view.chatDetails.setRequiresPermissions(chat_item.requiresPermissions)

self.messagesModule.updateChatFetchMoreMessages()
self.messagesModule.updateChatIdentifier()
Expand All @@ -400,6 +401,7 @@ method onCommunityChannelEdited*(self: Module, chatDto: ChatDto) =
self.view.chatDetails.setName(chatDto.name)
self.view.chatDetails.setIcon(chatDto.icon)
self.view.chatDetails.setMissingEncryptionKey(chatDto.missingEncryptionKey)
self.view.chatDetails.setRequiresPermissions(chatDto.tokenGated)

self.messagesModule.updateChatFetchMoreMessages()
self.messagesModule.updateChatIdentifier()
Expand Down
32 changes: 25 additions & 7 deletions src/app/modules/main/chat_section/chat_content/users/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type
moduleLoaded: bool

# Forward declaration
proc addChatMember(self: Module, member: ChatMember)
proc processChatMember(self: Module, member: ChatMember): MemberItem

proc newModule*(
events: EventEmitter, sectionId: string, chatId: string,
Expand Down Expand Up @@ -95,7 +95,9 @@ method userProfileUpdated*(self: Module) =
method loggedInUserImageChanged*(self: Module) =
self.view.model().setIcon(singletonInstance.userProfile.getPubKey(), singletonInstance.userProfile.getIcon())

proc addChatMember(self: Module, member: ChatMember) =
# This function either removes the member if it is no longer part of the community,
# does nothing if the member is already in the model or creates the MemberItem
proc processChatMember(self: Module, member: ChatMember): MemberItem =
if member.id == "":
return

Expand All @@ -118,7 +120,7 @@ proc addChatMember(self: Module, member: ChatMember) =
let statusUpdateDto = self.controller.getStatusForContact(member.id)
status = toOnlineStatus(statusUpdateDto.statusType)

self.view.model().addItem(initMemberItem(
return initMemberItem(
pubKey = member.id,
displayName = contactDetails.dto.displayName,
ensName = contactDetails.dto.name,
Expand All @@ -134,11 +136,17 @@ proc addChatMember(self: Module, member: ChatMember) =
memberRole = member.role,
joined = member.joined,
isUntrustworthy = contactDetails.dto.trustStatus == TrustStatus.Untrustworthy,
))
)

method onChatMembersAdded*(self: Module, ids: seq[string]) =
var memberItems: seq[MemberItem] = @[]

for memberId in ids:
self.addChatMember(ChatMember(id: memberId, role: MemberRole.None, joined: true))
let item = self.processChatMember(ChatMember(id: memberId, role: MemberRole.None, joined: true))
if item.pubKey != "":
memberItems.add(item)

self.view.model().addItems(memberItems)

method onChatMemberRemoved*(self: Module, id: string) =
self.view.model().removeItemById(id)
Expand All @@ -148,8 +156,12 @@ method onMembersChanged*(self: Module, members: seq[ChatMember]) =
let membersAdded = filter(members, member => not modelIDs.contains(member.id))
let membersRemoved = filter(modelIDs, id => not members.any(member => member.id == id))

var memberItems: seq[MemberItem] = @[]
for member in membersAdded:
self.addChatMember(member)
let item = self.processChatMember(member)
if item.pubKey != "":
memberItems.add(item)
self.view.model().addItems(memberItems)

for id in membersRemoved:
self.onChatMemberRemoved(id)
Expand Down Expand Up @@ -181,5 +193,11 @@ method removeGroupMembers*(self: Module, pubKeys: seq[string]) =

method updateMembersList*(self: Module) =
let members = self.controller.getChatMembers()
var memberItems: seq[MemberItem] = @[]

for member in members:
self.addChatMember(member)
let item = self.processChatMember(member)
if item.pubKey != "":
memberItems.add(item)

self.view.model().addItems(memberItems)
Loading

0 comments on commit bb7e5be

Please sign in to comment.