fix race condition for callkit

This commit is contained in:
ryanzhao 2022-04-07 12:29:09 +10:00
parent 6cb3e1db22
commit 343df684d9
5 changed files with 20 additions and 21 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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
*/

View File

@ -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
/**

View File

@ -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
}