From 4c61c115d43f39e3c10e5f9e3e79b9770ef62d67 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 17 May 2024 14:25:30 -0400 Subject: [PATCH] fix(mute): fix muted community still sending notifs and having a badge (#14822) Fixes #14816 The muted property on the community was never checked anywhere. I don't know how it ever worked, but oh well. I also made it so that when the mute property on the community changes, the badge gets re-evaluated. --- src/app/modules/main/chat_section/controller.nim | 9 +++++++-- .../modules/main/chat_section/io_interface.nim | 3 +++ src/app/modules/main/chat_section/module.nim | 16 +++++++++++++--- src/app/modules/main/controller.nim | 4 ++-- src/app/modules/main/module.nim | 13 ++++++++++--- src/app_service/service/chat/service.nim | 12 ++++++------ src/app_service/service/community/service.nim | 3 +++ 7 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/app/modules/main/chat_section/controller.nim b/src/app/modules/main/chat_section/controller.nim index 6143f09da0..aee2ad6bc9 100644 --- a/src/app/modules/main/chat_section/controller.nim +++ b/src/app/modules/main/chat_section/controller.nim @@ -372,6 +372,11 @@ proc init*(self: Controller) = if args.communityId == self.sectionId: self.delegate.communityMemberReevaluationStatusUpdated(args.status) + self.events.on(SIGNAL_COMMUNITY_MUTED) do(e: Args): + let args = CommunityMutedArgs(e) + if args.communityId == self.sectionId: + self.delegate.onSectionMutedChanged() + self.events.on(SIGNAL_CONTACT_NICKNAME_CHANGED) do(e: Args): var args = ContactArgs(e) self.delegate.onContactDetailsUpdated(args.contactId) @@ -490,9 +495,9 @@ proc getChatsAndBuildUI*(self: Controller) = self.sharedUrlsService, ) -proc sectionUnreadMessagesAndMentionsCount*(self: Controller, communityId: string): +proc sectionUnreadMessagesAndMentionsCount*(self: Controller, communityId: string, sectionIsMuted: bool): tuple[unviewedMessagesCount: int, unviewedMentionsCount: int] = - return self.chatService.sectionUnreadMessagesAndMentionsCount(communityId) + return self.chatService.sectionUnreadMessagesAndMentionsCount(communityId, sectionIsMuted) proc getChatDetails*(self: Controller, chatId: string): ChatDto = return self.chatService.getChatById(chatId) diff --git a/src/app/modules/main/chat_section/io_interface.nim b/src/app/modules/main/chat_section/io_interface.nim index c02ef55120..3bc96483d7 100644 --- a/src/app/modules/main/chat_section/io_interface.nim +++ b/src/app/modules/main/chat_section/io_interface.nim @@ -97,6 +97,9 @@ method onMarkAllMessagesRead*(self: AccessInterface, chat: ChatDto) {.base.} = method onMarkMessageAsUnread*(self: AccessInterface, chat: ChatDto) {.base.} = raise newException(ValueError, "No implementation available") +method onSectionMutedChanged*(self: AccessInterface) {.base.} = + raise newException(ValueError, "No implementation available") + method onCommunityMuted*(self: AccessInterface, chatId: string, muted: bool) {.base.} = raise newException(ValueError, "No implementation available") diff --git a/src/app/modules/main/chat_section/module.nim b/src/app/modules/main/chat_section/module.nim index e29cd23194..62f1d4531a 100644 --- a/src/app/modules/main/chat_section/module.nim +++ b/src/app/modules/main/chat_section/module.nim @@ -549,8 +549,14 @@ method getChatContentModule*(self: Module, chatId: string): QVariant = return self.chatContentModules[chatId].getModuleAsVariant() proc updateParentBadgeNotifications(self: Module) = + var sectionIsMuted = false + if self.controller.isCommunity: + let myCommunity = self.controller.getMyCommunity() + sectionIsMuted = myCommunity.muted + let (unviewedMessagesCount, unviewedMentionsCount) = self.controller.sectionUnreadMessagesAndMentionsCount( - self.controller.getMySectionId() + self.controller.getMySectionId(), + sectionIsMuted, ) self.delegate.onNotificationsUpdated( self.controller.getMySectionId(), @@ -1098,6 +1104,9 @@ method onMarkAllMessagesRead*(self: Module, chat: ChatDto) = method onMarkMessageAsUnread*(self: Module, chat: ChatDto) = self.updateBadgeNotifications(chat, hasUnreadMessages=true, chat.unviewedMentionsCount) +method onSectionMutedChanged*(self: Module) = + self.updateParentBadgeNotifications() + method markAllMessagesRead*(self: Module, chatId: string) = self.controller.markAllMessagesRead(chatId) @@ -1178,8 +1187,9 @@ method onNewMessagesReceived*(self: Module, sectionIdMsgBelongsTo: string, chatI return let chatDetails = self.controller.getChatDetails(chatIdMsgBelongsTo) + let community = self.controller.getMyCommunity() - if (chatDetails.muted): + if (chatDetails.muted or community.muted): # No need to send a notification return @@ -1194,7 +1204,7 @@ method onNewMessagesReceived*(self: Module, sectionIdMsgBelongsTo: string, chatI notificationType = notification_details.NotificationType.NewMessageWithGlobalMention var senderDisplayName = self.controller.getContactDetails(message.`from`).defaultDisplayName - let communityChats = self.controller.getMyCommunity().chats + let communityChats = community.chats let renderedMessageText = self.controller.getRenderedText(message.parsedText, communityChats) var plainText = singletonInstance.utils.plainText(renderedMessageText) if message.contentType == ContentType.Sticker or (message.contentType == ContentType.Image and len(plainText) == 0): diff --git a/src/app/modules/main/controller.nim b/src/app/modules/main/controller.nim index 9332268bf5..1fa28fbc25 100644 --- a/src/app/modules/main/controller.nim +++ b/src/app/modules/main/controller.nim @@ -496,9 +496,9 @@ proc setActiveSectionId*(self: Controller, sectionId: string) = proc getAllChats*(self: Controller): seq[ChatDto] = result = self.chatService.getAllChats() -proc sectionUnreadMessagesAndMentionsCount*(self: Controller, communityId: string): +proc sectionUnreadMessagesAndMentionsCount*(self: Controller, communityId: string, sectionIsMuted: bool): tuple[unviewedMessagesCount: int, unviewedMentionsCount: int] = - return self.chatService.sectionUnreadMessagesAndMentionsCount(communityId) + return self.chatService.sectionUnreadMessagesAndMentionsCount(communityId, sectionIsMuted) proc setCurrentUserStatus*(self: Controller, status: StatusType) = if(self.settingsService.saveSendStatusUpdates(status)): diff --git a/src/app/modules/main/module.nim b/src/app/modules/main/module.nim index 4d5dc438a2..f5882d147a 100644 --- a/src/app/modules/main/module.nim +++ b/src/app/modules/main/module.nim @@ -331,7 +331,10 @@ proc createCommunitySectionItem[T](self: Module[T], communityDetails: CommunityD # We will update the model later when we finish loading the accounts self.controller.asyncGetRevealedAccountsForAllMembers(communityDetails.id) - let (unviewedCount, notificationsCount) = self.controller.sectionUnreadMessagesAndMentionsCount(communityDetails.id) + let (unviewedCount, notificationsCount) = self.controller.sectionUnreadMessagesAndMentionsCount( + communityDetails.id, + communityDetails.muted, + ) let hasNotification = unviewedCount > 0 or notificationsCount > 0 let active = self.getActiveSectionId() == communityDetails.id # We must pass on if the current item section is currently active to keep that property as it is @@ -665,7 +668,10 @@ method onChatsLoaded*[T]( sharedUrlsService, networkService ) - let (unviewedMessagesCount, unviewedMentionsCount) = self.controller.sectionUnreadMessagesAndMentionsCount(myPubKey) + let (unviewedMessagesCount, unviewedMentionsCount) = self.controller.sectionUnreadMessagesAndMentionsCount( + myPubKey, + sectionIsMuted = false + ) let personalChatSectionItem = initItem( myPubKey, sectionType = SectionType.Chat, @@ -1091,7 +1097,8 @@ method communityEdited*[T]( var communitySectionItem = self.createCommunitySectionItem(community) # We need to calculate the unread counts because the community update doesn't come with it let (unviewedMessagesCount, unviewedMentionsCount) = self.controller.sectionUnreadMessagesAndMentionsCount( - communitySectionItem.id + communitySectionItem.id, + communitySectionItem.muted, ) communitySectionItem.setHasNotification(unviewedMessagesCount > 0) communitySectionItem.setNotificationsCount(unviewedMentionsCount) diff --git a/src/app_service/service/chat/service.nim b/src/app_service/service/chat/service.nim index 0a30b91e09..24725a3864 100644 --- a/src/app_service/service/chat/service.nim +++ b/src/app_service/service/chat/service.nim @@ -219,24 +219,24 @@ QtObject: proc hasChannel*(self: Service, chatId: string): bool = self.chats.hasKey(chatId) - proc sectionUnreadMessagesAndMentionsCount*(self: Service, sectionId: string): + proc sectionUnreadMessagesAndMentionsCount*(self: Service, sectionId: string, sectionIsMuted: bool): tuple[unviewedMessagesCount: int, unviewedMentionsCount: int] = result.unviewedMentionsCount = 0 result.unviewedMessagesCount = 0 let myPubKey = singletonInstance.userProfile.getPubKey() - var seactionIdToFind = sectionId + var sectionIdToFind = sectionId if sectionId == myPubKey: - # If the section is the personal one (ID == pubKey), then we set the seactionIdToFind to "" + # If the section is the personal one (ID == pubKey), then we set the sectionIdToFind to "" # because personal chats have communityId == "" - seactionIdToFind = "" + sectionIdToFind = "" for _, chat in self.chats: - if chat.communityId != seactionIdToFind: + if chat.communityId != sectionIdToFind: continue result.unviewedMentionsCount += chat.unviewedMentionsCount # We count the unread messages if we are unmuted and it's not a mention, we want to show a badge on mentions - if chat.unviewedMentionsCount == 0 and chat.muted: + if chat.unviewedMentionsCount == 0 and (chat.muted or sectionIsMuted): continue if chat.unviewedMessagesCount > 0: result.unviewedMessagesCount = result.unviewedMessagesCount + chat.unviewedMessagesCount diff --git a/src/app_service/service/community/service.nim b/src/app_service/service/community/service.nim index f3e85ae947..2ac9df93af 100644 --- a/src/app_service/service/community/service.nim +++ b/src/app_service/service/community/service.nim @@ -2148,6 +2148,9 @@ QtObject: return let muted = if (MutedType(mutedType) == MutedType.Unmuted): false else: true + + self.communities[communityId].muted = muted + self.events.emit(SIGNAL_COMMUNITY_MUTED, CommunityMutedArgs(communityId: communityId, muted: muted)) except Exception as e: