From e5f07993a7e63635925ab5c19346becf9b6e55a4 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Mon, 28 Nov 2022 19:11:26 +0400 Subject: [PATCH] mobile: don't show notifications for certain chat items (#1453) --- .../java/chat/simplex/app/model/ChatModel.kt | 53 +++++++++++-------- .../java/chat/simplex/app/model/SimpleXAPI.kt | 2 +- apps/ios/Shared/Model/SimpleXAPI.swift | 2 +- .../ios/SimpleX NSE/NotificationService.swift | 2 +- apps/ios/SimpleXChat/ChatTypes.swift | 53 +++++++++++++------ 5 files changed, 70 insertions(+), 42 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt b/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt index f7ded89901..931aafb4c9 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt @@ -1065,30 +1065,39 @@ data class ChatItem ( else -> false } - val isCall: Boolean get() = - when (content) { - is CIContent.SndCall -> true - is CIContent.RcvCall -> true - else -> false - } + private val showNtfDir: Boolean get() = !chatDir.sent - val isMutedMemberEvent: Boolean get() = + val showNotification: Boolean get() = when (content) { - is CIContent.RcvGroupEventContent -> - when (content.rcvGroupEvent) { - is RcvGroupEvent.GroupUpdated -> true - is RcvGroupEvent.MemberConnected -> true - is RcvGroupEvent.UserDeleted -> false - is RcvGroupEvent.GroupDeleted -> false - is RcvGroupEvent.MemberAdded -> false - is RcvGroupEvent.MemberLeft -> false - is RcvGroupEvent.MemberRole -> true - is RcvGroupEvent.UserRole -> false - is RcvGroupEvent.MemberDeleted -> false - is RcvGroupEvent.InvitedViaGroupLink -> false - } - is CIContent.SndGroupEventContent -> true - else -> false + is CIContent.SndMsgContent -> showNtfDir + is CIContent.RcvMsgContent -> showNtfDir + is CIContent.SndDeleted -> showNtfDir + is CIContent.RcvDeleted -> showNtfDir + is CIContent.SndCall -> showNtfDir + is CIContent.RcvCall -> false // notification is shown on CallInvitation instead + is CIContent.RcvIntegrityError -> showNtfDir + is CIContent.RcvGroupInvitation -> showNtfDir + is CIContent.SndGroupInvitation -> showNtfDir + is CIContent.RcvGroupEventContent -> when (content.rcvGroupEvent) { + is RcvGroupEvent.MemberAdded -> false + is RcvGroupEvent.MemberConnected -> false + is RcvGroupEvent.MemberLeft -> false + is RcvGroupEvent.MemberRole -> false + is RcvGroupEvent.UserRole -> showNtfDir + is RcvGroupEvent.MemberDeleted -> false + is RcvGroupEvent.UserDeleted -> showNtfDir + is RcvGroupEvent.GroupDeleted -> showNtfDir + is RcvGroupEvent.GroupUpdated -> false + is RcvGroupEvent.InvitedViaGroupLink -> false + } + is CIContent.SndGroupEventContent -> showNtfDir + is CIContent.RcvConnEventContent -> false + is CIContent.SndConnEventContent -> showNtfDir + is CIContent.RcvChatFeature -> false + is CIContent.SndChatFeature -> showNtfDir + is CIContent.RcvGroupFeature -> false + is CIContent.SndGroupFeature -> showNtfDir + is CIContent.RcvChatFeatureRejected -> showNtfDir } fun withStatus(status: CIStatus): ChatItem = this.copy(meta = meta.copy(itemStatus = status)) diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt index 410b8947be..ced948eb88 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt @@ -1064,7 +1064,7 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a } else if (cItem.content.msgContent is MsgContent.MCVoice && file != null && file.fileSize <= MAX_VOICE_SIZE_AUTO_RCV && file.fileSize > MAX_VOICE_SIZE_FOR_SENDING && appPrefs.privacyAcceptImages.get()) { withApi { receiveFile(file.fileId) } // TODO check inlineFileMode != IFMSent } - if (!cItem.chatDir.sent && !cItem.isCall && !cItem.isMutedMemberEvent && (!isAppOnForeground(appContext) || chatModel.chatId.value != cInfo.id)) { + if (cItem.showNotification && (!isAppOnForeground(appContext) || chatModel.chatId.value != cInfo.id)) { ntfManager.notifyMessageReceived(cInfo, cItem) } } diff --git a/apps/ios/Shared/Model/SimpleXAPI.swift b/apps/ios/Shared/Model/SimpleXAPI.swift index af3537435f..df34e9f86f 100644 --- a/apps/ios/Shared/Model/SimpleXAPI.swift +++ b/apps/ios/Shared/Model/SimpleXAPI.swift @@ -988,7 +988,7 @@ func processReceivedMsg(_ res: ChatResponse) async { await receiveFile(fileId: file.fileId) } } - if !cItem.chatDir.sent && !cItem.isCall && !cItem.isMutedMemberEvent { + if cItem.showNotification { NtfManager.shared.notifyMessageReceived(cInfo, cItem) } case let .chatItemStatusUpdated(aChatItem): diff --git a/apps/ios/SimpleX NSE/NotificationService.swift b/apps/ios/SimpleX NSE/NotificationService.swift index 91b0e21606..c2d81ded45 100644 --- a/apps/ios/SimpleX NSE/NotificationService.swift +++ b/apps/ios/SimpleX NSE/NotificationService.swift @@ -232,7 +232,7 @@ func receivedMsgNtf(_ res: ChatResponse) async -> (String, UNMutableNotification cItem = apiReceiveFile(fileId: file.fileId, inline: inline)?.chatItem ?? cItem } } - return cItem.isCall ? nil : (aChatItem.chatId, createMessageReceivedNtf(cInfo, cItem)) + return cItem.showMutableNotification ? (aChatItem.chatId, createMessageReceivedNtf(cInfo, cItem)) : nil case let .callInvitation(invitation): return (invitation.contact.id, createCallInvitationNtf(invitation)) default: diff --git a/apps/ios/SimpleXChat/ChatTypes.swift b/apps/ios/SimpleXChat/ChatTypes.swift index 2d33adaee3..b9c4dea1ef 100644 --- a/apps/ios/SimpleXChat/ChatTypes.swift +++ b/apps/ios/SimpleXChat/ChatTypes.swift @@ -1328,31 +1328,50 @@ public struct ChatItem: Identifiable, Decodable { } } - public var isCall: Bool { - switch content { - case .sndCall: return true - case .rcvCall: return true - default: return false - } + private var showNtfDir: Bool { + return !chatDir.sent } - public var isMutedMemberEvent: Bool { + public var showNotification: Bool { switch content { - case let .rcvGroupEvent(event): - switch event { - case .groupUpdated: return true - case .memberConnected: return true - case .memberRole: return true - case .userRole: return false - case .userDeleted: return false - case .groupDeleted: return false + case .sndMsgContent: return showNtfDir + case .rcvMsgContent: return showNtfDir + case .sndDeleted: return showNtfDir + case .rcvDeleted: return showNtfDir + case .sndCall: return showNtfDir + case .rcvCall: return false // notification is shown on .callInvitation instead + case .rcvIntegrityError: return showNtfDir + case .rcvGroupInvitation: return showNtfDir + case .sndGroupInvitation: return showNtfDir + case .rcvGroupEvent(rcvGroupEvent: let rcvGroupEvent): + switch rcvGroupEvent { + case .groupUpdated: return false + case .memberConnected: return false + case .memberRole: return false + case .userRole: return showNtfDir + case .userDeleted: return showNtfDir + case .groupDeleted: return showNtfDir case .memberAdded: return false case .memberLeft: return false case .memberDeleted: return false case .invitedViaGroupLink: return false } - case .sndGroupEvent: return true - default: return false + case .sndGroupEvent: return showNtfDir + case .rcvConnEvent: return false + case .sndConnEvent: return showNtfDir + case .rcvChatFeature: return false + case .sndChatFeature: return showNtfDir + case .rcvGroupFeature: return false + case .sndGroupFeature: return showNtfDir + case .rcvChatFeatureRejected: return showNtfDir + } + } + + public var showMutableNotification: Bool { + switch content { + case .rcvCall: return false + case .rcvChatFeature: return false + default: return showNtfDir } }