From 0237df8023ce260fbc8bbc5b8959929ef42c02ba Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Mon, 30 May 2022 13:14:13 +1000 Subject: [PATCH] fix notification service extension creates duplicated call info message --- Session/Meta/AppDelegate.swift | 2 +- .../NotificationServiceExtension.swift | 21 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Session/Meta/AppDelegate.swift b/Session/Meta/AppDelegate.swift index 540d734bd..78d0924f6 100644 --- a/Session/Meta/AppDelegate.swift +++ b/Session/Meta/AppDelegate.swift @@ -63,7 +63,7 @@ extension AppDelegate { let thread = TSContactThread.getOrCreateThread(withContactSessionID: message.sender!, transaction: transaction) let infoMessage = TSInfoMessage.from(message, associatedWith: thread) infoMessage.save(with: transaction) - receivedCalls.insert(message.uuid!) + receivedCalls.insert(uuid) Storage.shared.setReceivedCalls(to: receivedCalls, for: sender, using: transaction) return infoMessage } diff --git a/SessionNotificationServiceExtension/NotificationServiceExtension.swift b/SessionNotificationServiceExtension/NotificationServiceExtension.swift index db7e22d51..d1d598191 100644 --- a/SessionNotificationServiceExtension/NotificationServiceExtension.swift +++ b/SessionNotificationServiceExtension/NotificationServiceExtension.swift @@ -78,9 +78,7 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension guard case .preOffer = callMessage.kind else { return self.completeSilenty() } if !SSKPreferences.areCallsEnabled { if let sender = callMessage.sender, let thread = TSContactThread.fetch(for: sender, using: transaction), !thread.isMessageRequest(using: transaction) { - let infoMessage = TSInfoMessage.from(callMessage, associatedWith: thread) - infoMessage.updateCallInfoMessage(.permissionDenied, using: transaction) - SSKEnvironment.shared.notificationsManager?.notifyUser(forIncomingCall: infoMessage, in: thread, transaction: transaction) + self.insertCallInfoMessage(for: callMessage, in: thread, reason: .permissionDenied, using: transaction) } break } @@ -92,9 +90,7 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension message.kind = .endCall SNLog("[Calls] Sending end call message because there is an ongoing call.") MessageSender.sendNonDurably(message, in: thread, using: transaction).retainUntilComplete() - let infoMessage = TSInfoMessage.from(callMessage, associatedWith: thread) - infoMessage.updateCallInfoMessage(.missed, using: transaction) - SSKEnvironment.shared.notificationsManager?.notifyUser(forIncomingCall: infoMessage, in: thread, transaction: transaction) + self.insertCallInfoMessage(for: callMessage, in: thread, reason: .missed, using: transaction) } break } @@ -112,6 +108,18 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension } } } + + private func insertCallInfoMessage(for message: CallMessage, in thread: TSThread, reason: TSInfoMessageCallState, using transaction: YapDatabaseReadWriteTransaction) { + guard let sender = message.sender, let uuid = message.uuid else { return } + var receivedCalls = Storage.shared.getReceivedCalls(for: sender, using: transaction) + if !receivedCalls.contains(uuid) { + let infoMessage = TSInfoMessage.from(message, associatedWith: thread) + infoMessage.updateCallInfoMessage(reason, using: transaction) + SSKEnvironment.shared.notificationsManager?.notifyUser(forIncomingCall: infoMessage, in: thread, transaction: transaction) + receivedCalls.insert(uuid) + Storage.shared.setReceivedCalls(to: receivedCalls, for: sender, using: transaction) + } + } // MARK: Setup @@ -263,6 +271,7 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension } // MARK: Poll for open groups + private func pollForOpenGroups() -> [Promise] { var promises: [Promise] = [] let servers = Set(Storage.shared.getAllV2OpenGroups().values.map { $0.server })