From 739d4dae0dfd1f37626d5520398741b453735abb Mon Sep 17 00:00:00 2001 From: Andy Uhnak Date: Mon, 3 Oct 2022 17:56:58 +0100 Subject: [PATCH] Incoming verification requests with Crypto V2 --- ...erificationVerifyByScanningViewModel.swift | 39 ++++++++++++------- .../Modules/Room/DataSources/RoomDataSource.m | 1 + changelog.d/6809.change | 1 + 3 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 changelog.d/6809.change diff --git a/Riot/Modules/KeyVerification/Common/Verify/Scanning/KeyVerificationVerifyByScanningViewModel.swift b/Riot/Modules/KeyVerification/Common/Verify/Scanning/KeyVerificationVerifyByScanningViewModel.swift index fe54a2467..284ea3276 100644 --- a/Riot/Modules/KeyVerification/Common/Verify/Scanning/KeyVerificationVerifyByScanningViewModel.swift +++ b/Riot/Modules/KeyVerification/Common/Verify/Scanning/KeyVerificationVerifyByScanningViewModel.swift @@ -102,7 +102,7 @@ final class KeyVerificationVerifyByScanningViewModel: KeyVerificationVerifyBySca self.update(viewState: .loaded(viewData: viewData)) - self.registerTransactionDidStateChangeNotification() + self.registerDidStateChangeNotification() } private func canShowScanAction(from verificationMethods: [String]) -> Bool { @@ -112,7 +112,7 @@ final class KeyVerificationVerifyByScanningViewModel: KeyVerificationVerifyBySca private func cancel() { self.cancelQRCodeTransaction() self.keyVerificationRequest.cancel(with: MXTransactionCancelCode.user(), success: nil, failure: nil) - self.unregisterTransactionDidStateChangeNotification() + self.unregisterDidStateChangeNotification() self.coordinatorDelegate?.keyVerificationVerifyByScanningViewModelDidCancel(self) } @@ -148,7 +148,7 @@ final class KeyVerificationVerifyByScanningViewModel: KeyVerificationVerifyBySca return } - self.unregisterTransactionDidStateChangeNotification() + self.unregisterDidStateChangeNotification() self.coordinatorDelegate?.keyVerificationVerifyByScanningViewModel(self, didScanOtherQRCodeData: scannedQRCodeData, withTransaction: qrCodeTransaction) } @@ -176,7 +176,7 @@ final class KeyVerificationVerifyByScanningViewModel: KeyVerificationVerifyBySca // Check due to legacy implementation of key verification which could pass incorrect type of transaction if keyVerificationTransaction is MXIncomingSASTransaction { MXLog.debug("[KeyVerificationVerifyByScanningViewModel] SAS transaction should be outgoing") - self.unregisterTransactionDidStateChangeNotification() + self.unregisterDidStateChangeNotification() self.update(viewState: .error(KeyVerificationVerifyByScanningViewModelError.unknown)) } @@ -191,14 +191,27 @@ final class KeyVerificationVerifyByScanningViewModel: KeyVerificationVerifyBySca // MARK: - MXKeyVerificationTransactionDidChange - private func registerTransactionDidStateChangeNotification() { + private func registerDidStateChangeNotification() { + NotificationCenter.default.addObserver(self, selector: #selector(requestDidStateChange(notification:)), name: .MXKeyVerificationRequestDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(transactionDidStateChange(notification:)), name: .MXKeyVerificationTransactionDidChange, object: nil) } - private func unregisterTransactionDidStateChangeNotification() { + private func unregisterDidStateChangeNotification() { + NotificationCenter.default.removeObserver(self, name: .MXKeyVerificationRequestDidChange, object: nil) NotificationCenter.default.removeObserver(self, name: .MXKeyVerificationTransactionDidChange, object: nil) } + @objc private func requestDidStateChange(notification: Notification) { + guard let request = notification.object as? MXKeyVerificationRequest else { + return + } + + if request.state == MXKeyVerificationRequestStateCancelled, let reason = request.reasonCancelCode { + self.unregisterDidStateChangeNotification() + self.update(viewState: .cancelled(cancelCode: reason, verificationKind: verificationKind)) + } + } + @objc private func transactionDidStateChange(notification: Notification) { guard let transaction = notification.object as? MXKeyVerificationTransaction else { return @@ -219,19 +232,19 @@ final class KeyVerificationVerifyByScanningViewModel: KeyVerificationVerifyBySca private func sasTransactionDidStateChange(_ transaction: MXSASTransaction) { switch transaction.state { case MXSASTransactionStateShowSAS: - self.unregisterTransactionDidStateChangeNotification() + self.unregisterDidStateChangeNotification() self.coordinatorDelegate?.keyVerificationVerifyByScanningViewModel(self, didStartSASVerificationWithTransaction: transaction) case MXSASTransactionStateCancelled: guard let reason = transaction.reasonCancelCode else { return } - self.unregisterTransactionDidStateChangeNotification() + self.unregisterDidStateChangeNotification() self.update(viewState: .cancelled(cancelCode: reason, verificationKind: verificationKind)) case MXSASTransactionStateCancelledByMe: guard let reason = transaction.reasonCancelCode else { return } - self.unregisterTransactionDidStateChangeNotification() + self.unregisterDidStateChangeNotification() self.update(viewState: .cancelledByMe(reason)) default: break @@ -242,22 +255,22 @@ final class KeyVerificationVerifyByScanningViewModel: KeyVerificationVerifyBySca switch transaction.state { case .verified: // Should not happen - self.unregisterTransactionDidStateChangeNotification() + self.unregisterDidStateChangeNotification() self.coordinatorDelegate?.keyVerificationVerifyByScanningViewModelDidCancel(self) case .qrScannedByOther: - self.unregisterTransactionDidStateChangeNotification() + self.unregisterDidStateChangeNotification() self.coordinatorDelegate?.keyVerificationVerifyByScanningViewModel(self, qrCodeDidScannedByOtherWithTransaction: transaction) case .cancelled: guard let reason = transaction.reasonCancelCode else { return } - self.unregisterTransactionDidStateChangeNotification() + self.unregisterDidStateChangeNotification() self.update(viewState: .cancelled(cancelCode: reason, verificationKind: verificationKind)) case .cancelledByMe: guard let reason = transaction.reasonCancelCode else { return } - self.unregisterTransactionDidStateChangeNotification() + self.unregisterDidStateChangeNotification() self.update(viewState: .cancelledByMe(reason)) default: break diff --git a/Riot/Modules/Room/DataSources/RoomDataSource.m b/Riot/Modules/Room/DataSources/RoomDataSource.m index 8c409730a..842623818 100644 --- a/Riot/Modules/Room/DataSources/RoomDataSource.m +++ b/Riot/Modules/Room/DataSources/RoomDataSource.m @@ -866,6 +866,7 @@ const CGFloat kTypingCellHeight = 24; } __block MXHTTPOperation *operation = [self.mxSession.crypto.keyVerificationManager keyVerificationFromKeyVerificationEvent:event + roomId:self.roomId success:^(MXKeyVerification * _Nonnull keyVerification) { BOOL shouldRefreshCells = bubbleCellData.isKeyVerificationOperationPending || bubbleCellData.keyVerification == nil; diff --git a/changelog.d/6809.change b/changelog.d/6809.change new file mode 100644 index 000000000..e6ac64490 --- /dev/null +++ b/changelog.d/6809.change @@ -0,0 +1 @@ +CryptoV2: Incoming verification requests