minor refactoring to prevent crash after making a call

pull/560/head
Ryan Zhao 3 years ago
parent 4560f7e122
commit cfcd52c5bb

@ -182,12 +182,22 @@ public final class SessionCall: NSObject, WebRTCSessionDelegate {
// MARK: Actions
func startSessionCall() {
guard case .offer = mode else { return }
var promise: Promise<String?>!
guard let thread = TSContactThread.fetch(uniqueId: TSContactThread.threadID(fromContactSessionID: sessionID)) else { return }
let message = CallMessage()
message.sender = getUserHexEncodedPublicKey()
message.sentTimestamp = NSDate.millisecondTimestamp()
message.uuid = self.uuid
message.kind = .preOffer
let infoMessage = TSInfoMessage.from(message, associatedWith: thread)
infoMessage.save()
self.callMessageID = infoMessage.uniqueId
var promise: Promise<Void>!
Storage.write(with: { transaction in
promise = self.webRTCSession.sendPreOffer(to: self.sessionID, using: transaction)
promise = self.webRTCSession.sendPreOffer(message, in: thread, using: transaction)
}, completion: { [weak self] in
let _ = promise.done { messageID in
self?.callMessageID = messageID
let _ = promise.done {
Storage.shared.write { transaction in
self?.webRTCSession.sendOffer(to: self!.sessionID, using: transaction as! YapDatabaseReadWriteTransaction).retainUntilComplete()
}

@ -117,21 +117,13 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
}
// MARK: Signaling
public func sendPreOffer(to sessionID: String, using transaction: YapDatabaseReadWriteTransaction) -> Promise<String?> {
public func sendPreOffer(_ message: CallMessage, in thread: TSThread, using transaction: YapDatabaseReadWriteTransaction) -> Promise<Void> {
print("[Calls] Sending pre-offer message.")
guard let thread = TSContactThread.fetch(for: sessionID, using: transaction) else { return Promise(error: Error.noThread) }
let (promise, seal) = Promise<String?>.pending()
let (promise, seal) = Promise<Void>.pending()
DispatchQueue.main.async {
let message = CallMessage()
message.sender = getUserHexEncodedPublicKey()
message.sentTimestamp = NSDate.millisecondTimestamp()
message.uuid = self.uuid
message.kind = .preOffer
let infoMessage = TSInfoMessage.from(message, associatedWith: thread)
infoMessage.save(with: transaction)
MessageSender.sendNonDurably(message, in: thread, using: transaction).done2 {
print("[Calls] Pre-offer message has been sent.")
seal.fulfill((infoMessage.uniqueId))
seal.fulfill(())
}.catch2 { error in
seal.reject(error)
}

@ -12,7 +12,7 @@
} else {
callState = .incoming
}
let infoMessage = TSInfoMessage.init(timestamp: timestamp, in: thread, messageType: .call)
let infoMessage = TSInfoMessage(timestamp: timestamp, in: thread, messageType: .call)
infoMessage.callState = callState
return infoMessage
}

Loading…
Cancel
Save