From b22f66bab31370e29a05e92072d6d1aae7753883 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Fri, 30 Jul 2021 14:23:17 +1000 Subject: [PATCH] Resolve more errors --- Session/Calls/Individual/IndividualCall.swift | 19 +--- .../Individual/IndividualCallService.swift | 104 +++++++++--------- .../Signaling/WebRTCCallMessageHandler.swift | 38 +++---- Session/Meta/AppEnvironment.swift | 3 + 4 files changed, 76 insertions(+), 88 deletions(-) diff --git a/Session/Calls/Individual/IndividualCall.swift b/Session/Calls/Individual/IndividualCall.swift index c36a3a79b..64c687b18 100644 --- a/Session/Calls/Individual/IndividualCall.swift +++ b/Session/Calls/Individual/IndividualCall.swift @@ -52,19 +52,11 @@ public class IndividualCall: NSObject, IndividualCallNotificationInfo { // Mark - var backgroundTask: OWSBackgroundTask? { - didSet { - AssertIsOnMainThread() - - Logger.info("") - } + didSet { AssertIsOnMainThread() } } var callId: UInt64? { - didSet { - AssertIsOnMainThread() - - Logger.info("") - } + didSet { AssertIsOnMainThread() } } let callAdapterType: CallAdapterType @@ -72,8 +64,6 @@ public class IndividualCall: NSObject, IndividualCallNotificationInfo { weak var remoteVideoTrack: RTCVideoTrack? { didSet { AssertIsOnMainThread() - - Logger.info("") delegate?.individualCallRemoteVideoMuteDidChange(self, isVideoMuted: !isRemoteVideoEnabled) } } @@ -81,8 +71,6 @@ public class IndividualCall: NSObject, IndividualCallNotificationInfo { var isRemoteVideoEnabled = false { didSet { AssertIsOnMainThread() - - Logger.info("\(isRemoteVideoEnabled)") delegate?.individualCallRemoteVideoMuteDidChange(self, isVideoMuted: !isRemoteVideoEnabled) } } @@ -90,8 +78,6 @@ public class IndividualCall: NSObject, IndividualCallNotificationInfo { var isRemoteSharingScreen = false { didSet { AssertIsOnMainThread() - - Logger.info("\(isRemoteSharingScreen)") delegate?.individualCallRemoteSharingScreenDidChange(self, isRemoteSharingScreen: isRemoteSharingScreen) } } @@ -191,7 +177,6 @@ public class IndividualCall: NSObject, IndividualCallNotificationInfo { } deinit { - Logger.debug("") if !isEnded { owsFailDebug("isEnded was unexpectedly false") } diff --git a/Session/Calls/Individual/IndividualCallService.swift b/Session/Calls/Individual/IndividualCallService.swift index d29fd1e39..5799bd4ac 100644 --- a/Session/Calls/Individual/IndividualCallService.swift +++ b/Session/Calls/Individual/IndividualCallService.swift @@ -69,8 +69,8 @@ import WebRTC thread: call.individualCall.thread, sentAtTimestamp: call.individualCall.sentAtTimestamp ) - databaseStorage.asyncWrite { transaction in - callRecord.anyInsert(transaction: transaction) + Storage.write { transaction in + callRecord.save(with: transaction) } call.individualCall.callRecord = callRecord @@ -105,8 +105,8 @@ import WebRTC thread: call.individualCall.thread, sentAtTimestamp: call.individualCall.sentAtTimestamp ) - databaseStorage.asyncWrite { transaction in - callRecord.anyInsert(transaction: transaction) + Storage.write { transaction in + callRecord.save(with: transaction) } call.individualCall.callRecord = callRecord @@ -177,15 +177,9 @@ import WebRTC // MARK: - Signaling Functions private func allowsInboundCallsInThread(_ thread: TSContactThread) -> Bool { - return databaseStorage.read { transaction in - // IFF one of the following things is true, we can handle inbound call offers - // * The thread is in our profile whitelist - // * The thread belongs to someone in our system contacts - // * The thread existed before messages requests - return self.profileManager.isThread(inProfileWhitelist: thread, transaction: transaction) - || self.contactsManager.isSystemContact(address: thread.contactAddress) - || GRDBThreadFinder.isPreMessageRequestsThread(thread, transaction: transaction.unwrapGrdbRead) - } + // TODO: We might want to add some conditions here, like whether people have messaged + // eachother, whether the contact is blocked, etc. + return true } private struct CallIdentityKeys { @@ -194,17 +188,16 @@ import WebRTC } private func getIdentityKeys(thread: TSContactThread) -> CallIdentityKeys? { - return databaseStorage.read { transaction -> CallIdentityKeys? in - guard let localIdentityKey = self.identityManager.identityKeyPair(with: transaction)?.publicKey else { - owsFailDebug("missing localIdentityKey") - return nil - } - guard let contactIdentityKey = self.identityManager.identityKey(for: thread.contactAddress, transaction: transaction) else { - owsFailDebug("missing contactIdentityKey") - return nil - } - return CallIdentityKeys(localIdentityKey: localIdentityKey, contactIdentityKey: contactIdentityKey) + let identityManager = OWSIdentityManager.shared() + guard let localIdentityKey = identityManager.identityKeyPair()?.publicKey else { + owsFailDebug("missing localIdentityKey") + return nil } + guard let contactIdentityKey = identityManager.recipientIdentity(forRecipientId: thread.contactSessionID())?.identityKey else { + owsFailDebug("Looks like we're not actually maintaining the identity key for contacts. How will we fix that given that CallIdentityKeys wants this?") + return nil + } + return CallIdentityKeys(localIdentityKey: localIdentityKey, contactIdentityKey: contactIdentityKey) } /** @@ -239,6 +232,8 @@ import WebRTC BenchEventStart(title: "Incoming Call Connection", eventId: "call-\(newCall.individualCall.localId)") + /* + * We might not need the below code, since we don't have the concept of untrusted identities if let untrustedIdentity = self.identityManager.untrustedIdentityForSending(to: thread.contactAddress) { Logger.warn("missed a call due to untrusted identity: \(newCall)") @@ -272,6 +267,7 @@ import WebRTC return } + */ guard let identityKeys = getIdentityKeys(thread: thread) else { owsFailDebug("missing identity keys, skipping call.") @@ -283,8 +279,8 @@ import WebRTC ) assert(newCall.individualCall.callRecord == nil) newCall.individualCall.callRecord = callRecord - databaseStorage.write { transaction in - callRecord.anyInsert(transaction: transaction) + Storage.write { transaction in + callRecord.save(with: transaction) } newCall.individualCall.state = .localFailure @@ -294,7 +290,7 @@ import WebRTC } guard allowsInboundCallsInThread(thread) else { - Logger.info("Ignoring call offer from \(thread.contactAddress) due to insufficient permissions.") + Logger.info("Ignoring call offer from \(thread.contactSessionID()) due to insufficient permissions.") // Send the need permission message to the caller, so they know why we rejected their call. callManager( @@ -318,8 +314,8 @@ import WebRTC ) assert(newCall.individualCall.callRecord == nil) newCall.individualCall.callRecord = callRecord - databaseStorage.asyncWrite { transaction in - callRecord.anyInsert(transaction: transaction) + Storage.write { transaction in + callRecord.save(with: transaction) } newCall.individualCall.state = .localFailure @@ -469,13 +465,13 @@ import WebRTC var isUnknownCaller = false if call.individualCall.direction == .incoming { - isUnknownCaller = !self.contactsManagerImpl.isSystemContactWithSignalAccount(call.individualCall.thread.contactAddress) + isUnknownCaller = !Storage.shared.getAllContacts().contains { $0.sessionID == call.individualCall.thread.contactSessionID() } if isUnknownCaller { Logger.warn("Using relay server because remote user is an unknown caller") } } - let useTurnOnly = isUnknownCaller || Self.preferences.doCallsHideIPAddress() + let useTurnOnly = isUnknownCaller let useLowBandwidth = CallService.useLowBandwidthWithSneakyTransaction() Logger.info("Configuring call for \(useLowBandwidth ? "low" : "standard") bandwidth") @@ -668,7 +664,9 @@ import WebRTC thread: call.individualCall.thread, sentAtTimestamp: call.individualCall.sentAtTimestamp ) - databaseStorage.asyncWrite { callRecord.anyInsert(transaction: $0) } + Storage.write { transaction in + callRecord.save(with: transaction) + } call.individualCall.callRecord = callRecord callUIAdapter.reportMissedCall(call) } @@ -808,10 +806,10 @@ import WebRTC let callMessage = OWSOutgoingCallMessage(thread: call.individualCall.thread, offerMessage: try offerBuilder.build(), destinationDeviceId: NSNumber(value: destinationDeviceId)) return messageSender.sendMessage(.promise, callMessage.asPreparer) }.done { - Logger.info("sent offer message to \(call.individualCall.thread.contactAddress) device: \((destinationDeviceId != nil) ? String(destinationDeviceId!) : "nil")") + Logger.info("sent offer message to \(call.individualCall.thread.contactSessionID()) device: \((destinationDeviceId != nil) ? String(destinationDeviceId!) : "nil")") try self.callManager.signalingMessageDidSend(callId: callId) }.catch { error in - Logger.error("failed to send offer message to \(call.individualCall.thread.contactAddress) with error: \(error)") + Logger.error("failed to send offer message to \(call.individualCall.thread.contactSessionID()) with error: \(error)") self.callManager.signalingMessageDidFail(callId: callId) } } @@ -827,10 +825,10 @@ import WebRTC let callMessage = OWSOutgoingCallMessage(thread: call.individualCall.thread, answerMessage: try answerBuilder.build(), destinationDeviceId: NSNumber(value: destinationDeviceId)) return messageSender.sendMessage(.promise, callMessage.asPreparer) }.done { - Logger.debug("sent answer message to \(call.individualCall.thread.contactAddress) device: \((destinationDeviceId != nil) ? String(destinationDeviceId!) : "nil")") + Logger.debug("sent answer message to \(call.individualCall.thread.contactSessionID()) device: \((destinationDeviceId != nil) ? String(destinationDeviceId!) : "nil")") try self.callManager.signalingMessageDidSend(callId: callId) }.catch { error in - Logger.error("failed to send answer message to \(call.individualCall.thread.contactAddress) with error: \(error)") + Logger.error("failed to send answer message to \(call.individualCall.thread.contactSessionID()) with error: \(error)") self.callManager.signalingMessageDidFail(callId: callId) } } @@ -859,10 +857,10 @@ import WebRTC let callMessage = OWSOutgoingCallMessage(thread: call.individualCall.thread, iceUpdateMessages: iceUpdateProtos, destinationDeviceId: NSNumber(value: destinationDeviceId)) return messageSender.sendMessage(.promise, callMessage.asPreparer) }.done { - Logger.debug("sent ice update message to \(call.individualCall.thread.contactAddress) device: \((destinationDeviceId != nil) ? String(destinationDeviceId!) : "nil")") + Logger.debug("sent ice update message to \(call.individualCall.thread.contactSessionID()) device: \((destinationDeviceId != nil) ? String(destinationDeviceId!) : "nil")") try self.callManager.signalingMessageDidSend(callId: callId) }.catch { error in - Logger.error("failed to send ice update message to \(call.individualCall.thread.contactAddress) with error: \(error)") + Logger.error("failed to send ice update message to \(call.individualCall.thread.contactSessionID()) with error: \(error)") callManager.signalingMessageDidFail(callId: callId) } } @@ -897,10 +895,10 @@ import WebRTC } return messageSender.sendMessage(.promise, callMessage.asPreparer) }.done { - Logger.debug("sent hangup message to \(call.individualCall.thread.contactAddress) device: \((destinationDeviceId != nil) ? String(destinationDeviceId!) : "nil")") + Logger.debug("sent hangup message to \(call.individualCall.thread.contactSessionID()) device: \((destinationDeviceId != nil) ? String(destinationDeviceId!) : "nil")") try self.callManager.signalingMessageDidSend(callId: callId) }.catch { error in - Logger.error("failed to send hangup message to \(call.individualCall.thread.contactAddress) with error: \(error)") + Logger.error("failed to send hangup message to \(call.individualCall.thread.contactSessionID()) with error: \(error)") self.callManager.signalingMessageDidFail(callId: callId) } } @@ -915,10 +913,10 @@ import WebRTC let callMessage = OWSOutgoingCallMessage(thread: call.individualCall.thread, busyMessage: try busyBuilder.build(), destinationDeviceId: NSNumber(value: destinationDeviceId)) return messageSender.sendMessage(.promise, callMessage.asPreparer) }.done { - Logger.debug("sent busy message to \(call.individualCall.thread.contactAddress) device: \((destinationDeviceId != nil) ? String(destinationDeviceId!) : "nil")") + Logger.debug("sent busy message to \(call.individualCall.thread.contactSessionID()) device: \((destinationDeviceId != nil) ? String(destinationDeviceId!) : "nil")") try self.callManager.signalingMessageDidSend(callId: callId) }.catch { error in - Logger.error("failed to send busy message to \(call.individualCall.thread.contactAddress) with error: \(error)") + Logger.error("failed to send busy message to \(call.individualCall.thread.contactSessionID()) with error: \(error)") self.callManager.signalingMessageDidFail(callId: callId) } } @@ -947,8 +945,8 @@ import WebRTC switch callRecord.callType { case .incomingMissed: - databaseStorage.asyncWrite { transaction in - callRecord.anyUpsert(transaction: transaction) + Storage.write { transaction in + callRecord.save(with: transaction) } callUIAdapter.reportMissedCall(call) case .incomingIncomplete, .incoming: @@ -958,12 +956,12 @@ import WebRTC callRecord.updateCallType(.outgoingMissed) case .incomingMissedBecauseOfChangedIdentity, .incomingDeclined, .outgoingMissed, .outgoing, .incomingAnsweredElsewhere, .incomingDeclinedElsewhere, .incomingBusyElsewhere: owsFailDebug("unexpected RPRecentCallType: \(callRecord.callType)") - databaseStorage.asyncWrite { transaction in - callRecord.anyUpsert(transaction: transaction) + Storage.write { transaction in + callRecord.save(with: transaction) } @unknown default: - databaseStorage.asyncWrite { transaction in - callRecord.anyUpsert(transaction: transaction) + Storage.write { transaction in + callRecord.save(with: transaction) } owsFailDebug("unknown RPRecentCallType: \(callRecord.callType)") } @@ -983,7 +981,9 @@ import WebRTC sentAtTimestamp: call.individualCall.sentAtTimestamp ) call.individualCall.callRecord = callRecord - databaseStorage.asyncWrite { callRecord.anyInsert(transaction: $0) } + Storage.write { transaction in + callRecord.save(with: transaction) + } } call.individualCall.state = .answeredElsewhere @@ -1008,7 +1008,9 @@ import WebRTC sentAtTimestamp: call.individualCall.sentAtTimestamp ) call.individualCall.callRecord = callRecord - databaseStorage.asyncWrite { callRecord.anyInsert(transaction: $0) } + Storage.write { transaction in + callRecord.save(with: transaction) + } } call.individualCall.state = .declinedElsewhere @@ -1033,7 +1035,9 @@ import WebRTC sentAtTimestamp: call.individualCall.sentAtTimestamp ) call.individualCall.callRecord = callRecord - databaseStorage.asyncWrite { callRecord.anyInsert(transaction: $0) } + Storage.write { transaction in + callRecord.save(with: transaction) + } } call.individualCall.state = .busyElsewhere diff --git a/Session/Calls/Signaling/WebRTCCallMessageHandler.swift b/Session/Calls/Signaling/WebRTCCallMessageHandler.swift index 753ff52a0..c2b49a6eb 100644 --- a/Session/Calls/Signaling/WebRTCCallMessageHandler.swift +++ b/Session/Calls/Signaling/WebRTCCallMessageHandler.swift @@ -20,7 +20,7 @@ public class WebRTCCallMessageHandler: NSObject, OWSCallMessageHandler { public func receivedOffer( _ offer: SNProtoCallMessageOffer, - from caller: SignalServiceAddress, + from caller: String, sourceDevice: UInt32, sentAtTimestamp: UInt64, serverReceivedTimestamp: UInt64, @@ -31,13 +31,13 @@ public class WebRTCCallMessageHandler: NSObject, OWSCallMessageHandler { let callType: SNProtoCallMessageOfferType if offer.hasType { - callType = offer.unwrappedType + callType = SNProtoCallMessageOfferType(rawValue: offer.type.rawValue)! } else { // The type is not defined so assume the default, audio. callType = .offerAudioCall } - let thread = TSContactThread.getOrCreateThread(contactAddress: caller) + let thread = TSContactThread.getOrCreateThread(contactSessionID: caller) self.callService.individualCallService.handleReceivedOffer( thread: thread, callId: offer.id, @@ -52,10 +52,10 @@ public class WebRTCCallMessageHandler: NSObject, OWSCallMessageHandler { ) } - public func receivedAnswer(_ answer: SNProtoCallMessageAnswer, from caller: SignalServiceAddress, sourceDevice: UInt32, supportsMultiRing: Bool) { + public func receivedAnswer(_ answer: SNProtoCallMessageAnswer, from caller: String, sourceDevice: UInt32, supportsMultiRing: Bool) { AssertIsOnMainThread() - let thread = TSContactThread.getOrCreateThread(contactAddress: caller) + let thread = TSContactThread.getOrCreateThread(contactSessionID: caller) self.callService.individualCallService.handleReceivedAnswer( thread: thread, callId: answer.id, @@ -66,10 +66,10 @@ public class WebRTCCallMessageHandler: NSObject, OWSCallMessageHandler { ) } - public func receivedIceUpdate(_ iceUpdate: [SNProtoCallMessageIceUpdate], from caller: SignalServiceAddress, sourceDevice: UInt32) { + public func receivedIceUpdate(_ iceUpdate: [SNProtoCallMessageIceUpdate], from caller: String, sourceDevice: UInt32) { AssertIsOnMainThread() - let thread = TSContactThread.getOrCreateThread(contactAddress: caller) + let thread = TSContactThread.getOrCreateThread(contactSessionID: caller) self.callService.individualCallService.handleReceivedIceCandidates( thread: thread, callId: iceUpdate[0].id, @@ -78,7 +78,7 @@ public class WebRTCCallMessageHandler: NSObject, OWSCallMessageHandler { ) } - public func receivedHangup(_ hangup: SNProtoCallMessageHangup, from caller: SignalServiceAddress, sourceDevice: UInt32) { + public func receivedHangup(_ hangup: SNProtoCallMessageHangup, from caller: String, sourceDevice: UInt32) { AssertIsOnMainThread() // deviceId is optional and defaults to 0. @@ -86,7 +86,7 @@ public class WebRTCCallMessageHandler: NSObject, OWSCallMessageHandler { let type: SNProtoCallMessageHangupType if hangup.hasType { - type = hangup.unwrappedType + type = SNProtoCallMessageHangupType(rawValue: hangup.type.rawValue)! if hangup.hasDeviceID { deviceId = hangup.deviceID @@ -96,7 +96,7 @@ public class WebRTCCallMessageHandler: NSObject, OWSCallMessageHandler { type = .hangupNormal } - let thread = TSContactThread.getOrCreateThread(contactAddress: caller) + let thread = TSContactThread.getOrCreateThread(contactSessionID: caller) self.callService.individualCallService.handleReceivedHangup( thread: thread, callId: hangup.id, @@ -106,10 +106,10 @@ public class WebRTCCallMessageHandler: NSObject, OWSCallMessageHandler { ) } - public func receivedBusy(_ busy: SNProtoCallMessageBusy, from caller: SignalServiceAddress, sourceDevice: UInt32) { + public func receivedBusy(_ busy: SNProtoCallMessageBusy, from caller: String, sourceDevice: UInt32) { AssertIsOnMainThread() - let thread = TSContactThread.getOrCreateThread(contactAddress: caller) + let thread = TSContactThread.getOrCreateThread(contactSessionID: caller) self.callService.individualCallService.handleReceivedBusy( thread: thread, callId: busy.id, @@ -119,7 +119,7 @@ public class WebRTCCallMessageHandler: NSObject, OWSCallMessageHandler { public func receivedOpaque( _ opaque: SNProtoCallMessageOpaque, - from caller: SignalServiceAddress, + from caller: String, sourceDevice: UInt32, serverReceivedTimestamp: UInt64, serverDeliveryTimestamp: UInt64 @@ -131,19 +131,15 @@ public class WebRTCCallMessageHandler: NSObject, OWSCallMessageHandler { return owsFailDebug("Received opaque call message without data") } - guard let senderUuid = caller.uuid else { - return owsFailDebug("Received opaque call message from sender without UUID") - } - var messageAgeSec: UInt64 = 0 if serverReceivedTimestamp > 0 && serverDeliveryTimestamp >= serverReceivedTimestamp { messageAgeSec = (serverDeliveryTimestamp - serverReceivedTimestamp) / 1000 } self.callService.callManager.receivedCallMessage( - senderUuid: senderUuid, + senderUuid: caller, senderDeviceId: sourceDevice, - localDeviceId: TSAccountManager.shared.storedDeviceId(), + localDeviceId: 1, message: message, messageAgeSec: messageAgeSec ) @@ -154,11 +150,11 @@ public class WebRTCCallMessageHandler: NSObject, OWSCallMessageHandler { for groupThread: TSGroupThread, serverReceivedTimestamp: UInt64) { - Logger.info("Received group call update for thread \(groupThread.uniqueId)") + Logger.info("Received group call update for thread \(groupThread.uniqueId!)") callService.groupCallMessageHandler.handleUpdateMessage(update, for: groupThread, serverReceivedTimestamp: serverReceivedTimestamp) } - public func externallyHandleCallMessage(envelope: SNProtoEnvelope, plaintextData: Data, wasReceivedByUD: Bool, serverDeliveryTimestamp: UInt64, transaction: SDSAnyWriteTransaction) -> Bool { + public func externallyHandleCallMessage(envelope: SNProtoEnvelope, plaintextData: Data, wasReceivedByUD: Bool, serverDeliveryTimestamp: UInt64, transaction: YapDatabaseReadWriteTransaction) -> Bool { return false } } diff --git a/Session/Meta/AppEnvironment.swift b/Session/Meta/AppEnvironment.swift index dda8dd809..8f5045d9d 100644 --- a/Session/Meta/AppEnvironment.swift +++ b/Session/Meta/AppEnvironment.swift @@ -59,6 +59,9 @@ import SignalUtilitiesKit public var backupLazyRestore: BackupLazyRestore private override init() { + self.callMessageHandlerRef = WebRTCCallMessageHandler() + self.callServiceRef = CallService() + self.outboundIndividualCallInitiatorRef = OutboundIndividualCallInitiator() self.accountManager = AccountManager() self.notificationPresenter = NotificationPresenter() self.pushRegistrationManager = PushRegistrationManager()