diff --git a/Session/Calls/Call Management/SessionCallManager.swift b/Session/Calls/Call Management/SessionCallManager.swift index c0de6a6a5..f7eaef6d4 100644 --- a/Session/Calls/Call Management/SessionCallManager.swift +++ b/Session/Calls/Call Management/SessionCallManager.swift @@ -60,6 +60,7 @@ public final class SessionCallManager: NSObject { // MARK: Report calls public func reportOutgoingCall(_ call: SessionCall) { AssertIsOnMainThread() + UserDefaults(suiteName: "group.com.loki-project.loki-messenger")?.set(true, forKey: "isCallOngoing") call.stateDidChange = { if call.hasStartedConnecting { self.provider.reportOutgoingCall(with: call.callID, startedConnectingAt: call.connectingDate) @@ -88,6 +89,7 @@ public final class SessionCallManager: NSObject { completion(error) return } + UserDefaults(suiteName: "group.com.loki-project.loki-messenger")?.set(true, forKey: "isCallOngoing") completion(nil) } } @@ -108,6 +110,7 @@ public final class SessionCallManager: NSObject { call.webRTCSession.dropConnection() self.currentCall = nil WebRTCSession.current = nil + UserDefaults(suiteName: "group.com.loki-project.loki-messenger")?.set(false, forKey: "isCallOngoing") } // MARK: Util @@ -132,7 +135,6 @@ public final class SessionCallManager: NSObject { SNLog("[Calls] Sending end call message because there is an ongoing call.") MessageSender.sendNonDurably(message, in: thread, using: transaction).retainUntilComplete() let infoMessage = TSInfoMessage.from(offerMessage, associatedWith: thread) - infoMessage.save(with: transaction) infoMessage.updateCallInfoMessage(.missed, using: transaction) } diff --git a/Session/Notifications/PushRegistrationManager.swift b/Session/Notifications/PushRegistrationManager.swift index 3ee3eee60..cd3676624 100644 --- a/Session/Notifications/PushRegistrationManager.swift +++ b/Session/Notifications/PushRegistrationManager.swift @@ -235,6 +235,7 @@ public enum PushRegistrationError: Error { voipTokenResolver.fulfill(pushCredentials.token) } + // NOTE: This function MUST report an incoming call. public func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) { SNLog("[Calls] Receive new voip notification.") owsAssertDebug(CurrentAppContext().isMainApp) diff --git a/SessionMessagingKit/Threads/TSThread.h b/SessionMessagingKit/Threads/TSThread.h index e2c8e8a05..b6629794e 100644 --- a/SessionMessagingKit/Threads/TSThread.h +++ b/SessionMessagingKit/Threads/TSThread.h @@ -40,13 +40,6 @@ BOOL IsNoteToSelfEnabled(void); - (NSString *)nameWithTransaction:(YapDatabaseReadTransaction *)transaction; -/** - * Returns if there is any outgoing interations in this thread. - * - * @return YES if there are outgoing interations, NO otherwise. - */ -- (BOOL)hasOutgoingInteractionWithTransaction:(YapDatabaseReadTransaction *)transaction; - /** * @returns recipientId for each recipient in the thread */ diff --git a/SessionMessagingKit/Threads/TSThread.m b/SessionMessagingKit/Threads/TSThread.m index 95428700e..889dde49a 100644 --- a/SessionMessagingKit/Threads/TSThread.m +++ b/SessionMessagingKit/Threads/TSThread.m @@ -178,18 +178,6 @@ BOOL IsNoteToSelfEnabled(void) return @[]; } -- (BOOL)hasOutgoingInteractionWithTransaction:(YapDatabaseReadTransaction *)transaction -{ - __block BOOL hasOutgoingInteraction = NO; - [self enumerateInteractionsWithTransaction:transaction usingBlock:^(TSInteraction *interaction, BOOL *stop) { - if ([interaction interactionType] == OWSInteractionType_OutgoingMessage) { - hasOutgoingInteraction = YES; - *stop = YES; - } - }]; - return hasOutgoingInteraction; -} - #pragma mark Interactions /** diff --git a/SessionNotificationServiceExtension/NotificationServiceExtension.swift b/SessionNotificationServiceExtension/NotificationServiceExtension.swift index 54294f927..00122f971 100644 --- a/SessionNotificationServiceExtension/NotificationServiceExtension.swift +++ b/SessionNotificationServiceExtension/NotificationServiceExtension.swift @@ -23,8 +23,10 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension // Abort if the main app is running var isMainAppAndActive = false + var isCallOngoing = false if let sharedUserDefaults = UserDefaults(suiteName: "group.com.loki-project.loki-messenger") { isMainAppAndActive = sharedUserDefaults.bool(forKey: "isMainAppActive") + isCallOngoing = sharedUserDefaults.bool(forKey: "isCallOngoing") } guard !isMainAppAndActive else { return self.completeSilenty() } @@ -74,12 +76,25 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension MessageReceiver.handleCallMessage(callMessage, using: transaction) 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.hasOutgoingInteraction(with: transaction) { + 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) } break } + if isCallOngoing { + if let sender = callMessage.sender, let thread = TSContactThread.fetch(for: sender, using: transaction), !thread.isMessageRequest(using: transaction) { + // Handle call in busy state + let message = CallMessage() + message.uuid = callMessage.uuid + 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) + } + break + } self.handleSuccessForIncomingCall(for: callMessage, using: transaction) default: break }