fix duplicated incoming call

pull/560/head
Ryan Zhao 3 years ago
parent bf33030d9a
commit e9f19b9c62

@ -105,10 +105,10 @@ public final class SessionCallManager: NSObject {
guard let call = currentCall else { return } guard let call = currentCall else { return }
if let reason = reason { if let reason = reason {
self.provider.reportCall(with: call.callID, endedAt: nil, reason: reason) self.provider.reportCall(with: call.callID, endedAt: nil, reason: reason)
if reason == .answeredElsewhere { return }
switch (reason) { switch (reason) {
case .unanswered: call.updateCallMessage(mode: .unanswered) case .unanswered: call.updateCallMessage(mode: .unanswered)
case .declinedElsewhere: call.updateCallMessage(mode: .local) case .declinedElsewhere: call.updateCallMessage(mode: .local)
case .answeredElsewhere: break
default: call.updateCallMessage(mode: .remote) default: call.updateCallMessage(mode: .remote)
} }
} else { } else {

@ -19,35 +19,17 @@ extension AppDelegate {
presentingVC.present(callVC, animated: true, completion: nil) presentingVC.present(callVC, animated: true, completion: nil)
} }
@objc func dismissAllCallUI() { private func dismissAllCallUI() {
if let currentBanner = IncomingCallBanner.current { currentBanner.dismiss() } if let currentBanner = IncomingCallBanner.current { currentBanner.dismiss() }
if let callVC = CurrentAppContext().frontmostViewController() as? CallVC { callVC.handleEndCallMessage() } if let callVC = CurrentAppContext().frontmostViewController() as? CallVC { callVC.handleEndCallMessage() }
if let miniCallView = MiniCallView.current { miniCallView.dismiss() } if let miniCallView = MiniCallView.current { miniCallView.dismiss() }
} }
@objc func setUpCallHandling() { private func showCallUIForCall(_ call: SessionCall) {
// Pre offer messages
MessageReceiver.handleNewCallOfferMessageIfNeeded = { (message, transaction) in
guard CurrentAppContext().isMainApp else { return }
let callManager = AppEnvironment.shared.callManager
// Ignore pre offer message afte the same call instance has been generated
if let currentCall = callManager.currentCall, currentCall.uuid == message.uuid! { return }
guard callManager.currentCall == nil && SSKPreferences.areCallsEnabled else {
callManager.handleIncomingCallOfferInBusyOrUnenabledState(offerMessage: message, using: transaction)
return
}
// Create incoming call message
let thread = TSContactThread.getOrCreateThread(withContactSessionID: message.sender!, transaction: transaction)
let tsMessage = TSIncomingMessage.from(message, associatedWith: thread)
tsMessage.save(with: transaction)
// Handle UI
if let caller = message.sender, let uuid = message.uuid {
let call = SessionCall(for: caller, uuid: uuid, mode: .answer)
call.callMessageTimestamp = message.sentTimestamp
DispatchQueue.main.async { DispatchQueue.main.async {
if CurrentAppContext().isMainAppAndActive { if CurrentAppContext().isMainAppAndActive {
guard let presentingVC = CurrentAppContext().frontmostViewController() else { preconditionFailure() } // TODO: Handle more gracefully guard let presentingVC = CurrentAppContext().frontmostViewController() else { preconditionFailure() } // TODO: Handle more gracefully
if let conversationVC = presentingVC as? ConversationVC, let contactThread = conversationVC.thread as? TSContactThread, contactThread.contactSessionID() == caller { if let conversationVC = presentingVC as? ConversationVC, let contactThread = conversationVC.thread as? TSContactThread, contactThread.contactSessionID() == call.sessionID {
let callVC = CallVC(for: call) let callVC = CallVC(for: call)
callVC.conversationVC = conversationVC callVC.conversationVC = conversationVC
conversationVC.inputAccessoryView?.isHidden = true conversationVC.inputAccessoryView?.isHidden = true
@ -64,6 +46,28 @@ extension AppDelegate {
} }
} }
} }
@objc func setUpCallHandling() {
// Pre offer messages
MessageReceiver.handleNewCallOfferMessageIfNeeded = { (message, transaction) in
guard CurrentAppContext().isMainApp else { return }
let callManager = AppEnvironment.shared.callManager
// Ignore pre offer message after the same call instance has been generated
if let currentCall = callManager.currentCall, currentCall.uuid == message.uuid! { return }
guard callManager.currentCall == nil && SSKPreferences.areCallsEnabled else {
callManager.handleIncomingCallOfferInBusyOrUnenabledState(offerMessage: message, using: transaction)
return
}
// Create incoming call message
let thread = TSContactThread.getOrCreateThread(withContactSessionID: message.sender!, transaction: transaction)
let tsMessage = TSIncomingMessage.from(message, associatedWith: thread)
tsMessage.save(with: transaction)
// Handle UI
if let caller = message.sender, let uuid = message.uuid {
let call = SessionCall(for: caller, uuid: uuid, mode: .answer)
call.callMessageTimestamp = message.sentTimestamp
self.showCallUIForCall(call)
}
} }
// Offer messages // Offer messages
MessageReceiver.handleOfferCallMessage = { message in MessageReceiver.handleOfferCallMessage = { message in

@ -271,8 +271,6 @@ extension MessageReceiver {
handleNewCallOfferMessageIfNeeded?(message, transaction) handleNewCallOfferMessageIfNeeded?(message, transaction)
case .offer: case .offer:
print("[Calls] Received offer message.") print("[Calls] Received offer message.")
let transaction = transaction as! YapDatabaseReadWriteTransaction
handleNewCallOfferMessageIfNeeded?(message, transaction)
handleOfferCallMessage?(message) handleOfferCallMessage?(message)
case .answer: case .answer:
print("[Calls] Received answer message.") print("[Calls] Received answer message.")

Loading…
Cancel
Save