minor refactor to prevent fake calls

pull/687/head
Ryan Zhao 3 years ago
parent aa9019e0b3
commit 3cfee13768

@ -73,13 +73,19 @@ public final class SessionCallManager: NSObject, CallManagerProtocol {
// MARK: - Report calls // MARK: - Report calls
public static func reportFakeCall(info: String) { public static func reportFakeCall(info: String) {
SessionCallManager.sharedProvider(useSystemCallLog: false) let callId = UUID()
.reportNewIncomingCall( let provider = SessionCallManager.sharedProvider(useSystemCallLog: false)
with: UUID(), provider.reportNewIncomingCall(
with: callId,
update: CXCallUpdate() update: CXCallUpdate()
) { _ in ) { _ in
SNLog("[Calls] Reported fake incoming call to CallKit due to: \(info)") SNLog("[Calls] Reported fake incoming call to CallKit due to: \(info)")
} }
provider.reportCall(
with: callId,
endedAt: nil,
reason: .failed
)
} }
public func reportOutgoingCall(_ call: SessionCall) { public func reportOutgoingCall(_ call: SessionCall) {
@ -100,7 +106,7 @@ public final class SessionCallManager: NSObject, CallManagerProtocol {
public func reportIncomingCall(_ call: SessionCall, callerName: String, completion: @escaping (Error?) -> Void) { public func reportIncomingCall(_ call: SessionCall, callerName: String, completion: @escaping (Error?) -> Void) {
AssertIsOnMainThread() AssertIsOnMainThread()
if let provider = provider { let provider = provider ?? Self.sharedProvider(useSystemCallLog: false)
// Construct a CXCallUpdate describing the incoming call, including the caller. // Construct a CXCallUpdate describing the incoming call, including the caller.
let update = CXCallUpdate() let update = CXCallUpdate()
update.localizedCallerName = callerName update.localizedCallerName = callerName
@ -120,12 +126,6 @@ public final class SessionCallManager: NSObject, CallManagerProtocol {
completion(nil) completion(nil)
} }
} }
else {
SessionCallManager.reportFakeCall(info: "No CXProvider instance")
UserDefaults.sharedLokiProject?.set(true, forKey: "isCallOngoing")
completion(nil)
}
}
public func reportCurrentCallEnded(reason: CXCallEndedReason?) { public func reportCurrentCallEnded(reason: CXCallEndedReason?) {
guard Thread.isMainThread else { guard Thread.isMainThread else {
@ -135,7 +135,11 @@ public final class SessionCallManager: NSObject, CallManagerProtocol {
return return
} }
guard let call = currentCall else { return } guard let call = currentCall else {
WebRTCSession.current = nil
UserDefaults.sharedLokiProject?.set(false, forKey: "isCallOngoing")
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)

@ -259,7 +259,13 @@ public enum PushRegistrationError: Error {
) )
) )
guard let messageInfoData: Data = try? JSONEncoder().encode(messageInfo) else { return nil } let messageInfoString: String? = {
if let messageInfoData: Data = try? JSONEncoder().encode(messageInfo) {
return String(data: messageInfoData, encoding: .utf8)
} else {
return "Incoming call." // TODO: We can do better here.
}
}()
let call: SessionCall = SessionCall(db, for: caller, uuid: uuid, mode: .answer) let call: SessionCall = SessionCall(db, for: caller, uuid: uuid, mode: .answer)
let thread: SessionThread = try SessionThread.fetchOrCreate(db, id: caller, variant: .contact) let thread: SessionThread = try SessionThread.fetchOrCreate(db, id: caller, variant: .contact)
@ -269,7 +275,7 @@ public enum PushRegistrationError: Error {
threadId: thread.id, threadId: thread.id,
authorId: caller, authorId: caller,
variant: .infoCall, variant: .infoCall,
body: String(data: messageInfoData, encoding: .utf8), body: messageInfoString,
timestampMs: timestampMs timestampMs: timestampMs
).inserted(db) ).inserted(db)
call.callInteractionId = interaction.id call.callInteractionId = interaction.id

Loading…
Cancel
Save