refactor on call ending

pull/1061/head
Ryan ZHAO 3 months ago
parent 03eec6ae5a
commit 1fee262ee4

@ -305,15 +305,16 @@ public final class SessionCall: CurrentCallProtocol, WebRTCSessionDelegate {
func endSessionCall() {
guard !hasEnded else { return }
let sessionId: String = self.sessionId
webRTCSession.hangUp()
Storage.shared.writeAsync { [weak self] db in
try self?.webRTCSession.endCall(db, with: sessionId)
webRTCSession.endCall(
with: self.sessionId
)
.sinkUntilComplete(
receiveCompletion: { [weak self] _ in
self?.hasEnded = true
Singleton.callManager.cleanUpPreviousCall()
}
hasEnded = true
)
}
func handleCallInitializationFailed() {

@ -143,20 +143,8 @@ public final class SessionCallManager: NSObject, CallManagerProtocol {
return
}
func handleCallEnded() {
SNLog("[Calls] Call ended.")
WebRTCSession.current = nil
UserDefaults.sharedLokiProject?[.isCallOngoing] = false
UserDefaults.sharedLokiProject?[.lastCallPreOffer] = nil
if Singleton.hasAppContext && Singleton.appContext.isNotInForeground {
(UIApplication.shared.delegate as? AppDelegate)?.stopPollers()
Log.flush()
}
}
guard let call = currentCall else {
handleCallEnded()
self.cleanUpPreviousCall()
suspendDatabaseIfCallEndedInBackground()
return
}
@ -175,9 +163,7 @@ public final class SessionCallManager: NSObject, CallManagerProtocol {
call.updateCallMessage(mode: .local, using: dependencies)
}
(call as? SessionCall)?.webRTCSession.dropConnection()
self.currentCall = nil
handleCallEnded()
self.cleanUpPreviousCall()
}
public func currentWebRTCSessionMatches(callId: String) -> Bool {
@ -301,4 +287,19 @@ public final class SessionCallManager: NSObject, CallManagerProtocol {
(Singleton.appContext.frontmostViewController as? CallVC)?.handleEndCallMessage()
MiniCallView.current?.dismiss()
}
public func cleanUpPreviousCall() {
SNLog("[Calls] Clean up calls")
WebRTCSession.current?.dropConnection()
WebRTCSession.current = nil
currentCall = nil
UserDefaults.sharedLokiProject?[.isCallOngoing] = false
UserDefaults.sharedLokiProject?[.lastCallPreOffer] = nil
if Singleton.hasAppContext && Singleton.appContext.isNotInForeground {
(UIApplication.shared.delegate as? AppDelegate)?.stopPollers()
Log.flush()
}
}
}

@ -358,15 +358,16 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
)
}
public func endCall(
_ db: Database,
with sessionId: String
) throws {
guard let thread: SessionThread = try SessionThread.fetchOne(db, id: sessionId) else { return }
public func endCall(with sessionId: String) -> AnyPublisher<Void, Error> {
return dependencies.storage
.writePublisher { [dependencies = self.dependencies] db in
guard let thread: SessionThread = try SessionThread.fetchOne(db, id: sessionId) else {
throw WebRTCSessionError.noThread
}
SNLog("[Calls] Sending end call message.")
let preparedSendData: MessageSender.PreparedSendData = try MessageSender
return try MessageSender
.preparedSendData(
db,
message: CallMessage(
@ -385,12 +386,22 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
interactionId: nil,
using: dependencies
)
MessageSender
.sendImmediate(data: preparedSendData, using: dependencies)
}
.subscribe(on: DispatchQueue.global(qos: .userInitiated))
.flatMap { [dependencies = self.dependencies] sendData in
MessageSender
.sendImmediate(data: sendData, using: dependencies)
.retry(5)
.sinkUntilComplete()
}
.handleEvents(receiveCompletion: { result in
switch result {
case .finished:
SNLog("[Calls] End call message sent")
case .failure(let error):
SNLog("[Calls] Error sending End call message due to error: \(error)")
}
})
.eraseToAnyPublisher()
}
public func dropConnection() {

@ -38,4 +38,5 @@ public protocol CallManagerProtocol {
func currentWebRTCSessionMatches(callId: String) -> Bool
func dismissAllCallUI()
func cleanUpPreviousCall()
}

@ -23,4 +23,5 @@ internal struct NoopSessionCallManager: CallManagerProtocol {
func currentWebRTCSessionMatches(callId: String) -> Bool { return false }
func dismissAllCallUI() {}
func cleanUpPreviousCall() {}
}

@ -38,7 +38,7 @@ extension MessageReceiver {
// MARK: - Specific Handling
private static func handleNewCallMessage(_ db: Database, message: CallMessage, using dependencies: Dependencies) throws {
SNLog("[Calls] Received pre-offer message.")
SNLog("[Calls] Received pre-offer message with uuid: \(message.uuid).")
// Determine whether the app is active based on the prefs rather than the UIApplication state to avoid
// requiring main-thread execution
@ -59,6 +59,7 @@ extension MessageReceiver {
else { return }
guard let timestamp = message.sentTimestamp, TimestampUtils.isWithinOneMinute(timestampMs: timestamp) else {
// Add missed call message for call offer messages from more than one minute
SNLog("[Calls] Got an expired call offer message with uuid: \(message.uuid). Sent at \(message.sentTimestamp), now is \(Date().timeIntervalSince1970 * 1000)")
if let interaction: Interaction = try MessageReceiver.insertCallInfoMessage(db, for: message, state: .missed, using: dependencies) {
let thread: SessionThread = try SessionThread.upsert(
db,

Loading…
Cancel
Save