2019-04-24 12:42:16 +00:00
|
|
|
// File created from ScreenTemplate
|
|
|
|
// $ createScreen.sh DeviceVerification/Loading DeviceVerificationDataLoading
|
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2019-10-23 15:09:58 +00:00
|
|
|
enum DeviceVerificationDataLoadingViewModelError: Error {
|
|
|
|
case unknown
|
2020-01-14 20:51:05 +00:00
|
|
|
case transactionCancelled
|
|
|
|
case transactionCancelledByMe(reason: MXTransactionCancelCode)
|
2019-10-23 15:09:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 12:42:16 +00:00
|
|
|
final class DeviceVerificationDataLoadingViewModel: DeviceVerificationDataLoadingViewModelType {
|
|
|
|
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
2020-02-14 08:27:56 +00:00
|
|
|
private let session: MXSession
|
2020-01-14 20:51:05 +00:00
|
|
|
private let otherUserId: String?
|
|
|
|
private let otherDeviceId: String?
|
2020-03-13 17:52:02 +00:00
|
|
|
private let keyVerificationService = KeyVerificationService()
|
2020-01-14 20:51:05 +00:00
|
|
|
|
|
|
|
private let keyVerificationRequest: MXKeyVerificationRequest?
|
|
|
|
|
|
|
|
private var currentOperation: MXHTTPOperation?
|
2019-04-24 12:42:16 +00:00
|
|
|
|
|
|
|
// MARK: Public
|
|
|
|
|
|
|
|
weak var viewDelegate: DeviceVerificationDataLoadingViewModelViewDelegate?
|
|
|
|
weak var coordinatorDelegate: DeviceVerificationDataLoadingViewModelCoordinatorDelegate?
|
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
|
|
|
init(session: MXSession, otherUserId: String, otherDeviceId: String) {
|
|
|
|
self.session = session
|
|
|
|
self.otherUserId = otherUserId
|
|
|
|
self.otherDeviceId = otherDeviceId
|
2020-01-14 20:51:05 +00:00
|
|
|
self.keyVerificationRequest = nil
|
|
|
|
}
|
|
|
|
|
2020-02-14 08:27:56 +00:00
|
|
|
init(session: MXSession, keyVerificationRequest: MXKeyVerificationRequest) {
|
|
|
|
self.session = session
|
2020-01-14 20:51:05 +00:00
|
|
|
self.otherUserId = nil
|
|
|
|
self.otherDeviceId = nil
|
|
|
|
self.keyVerificationRequest = keyVerificationRequest
|
2019-04-24 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
2020-01-14 20:51:05 +00:00
|
|
|
self.currentOperation?.cancel()
|
2019-04-24 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
func process(viewAction: DeviceVerificationDataLoadingViewAction) {
|
|
|
|
switch viewAction {
|
2019-10-23 15:09:58 +00:00
|
|
|
case .loadData:
|
|
|
|
self.loadData()
|
2019-04-24 12:42:16 +00:00
|
|
|
case .cancel:
|
|
|
|
self.coordinatorDelegate?.deviceVerificationDataLoadingViewModelDidCancel(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
2020-01-14 20:51:05 +00:00
|
|
|
|
2019-04-24 12:42:16 +00:00
|
|
|
private func loadData() {
|
2020-01-14 20:51:05 +00:00
|
|
|
if let keyVerificationRequest = self.keyVerificationRequest {
|
|
|
|
self.acceptKeyVerificationRequest(keyVerificationRequest)
|
|
|
|
} else {
|
|
|
|
self.downloadOtherDeviceKeys()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func acceptKeyVerificationRequest(_ keyVerificationRequest: MXKeyVerificationRequest) {
|
|
|
|
|
|
|
|
self.update(viewState: .loading)
|
|
|
|
|
2020-03-13 17:52:02 +00:00
|
|
|
keyVerificationRequest.accept(withMethods: self.keyVerificationService.supportedKeyVerificationMethods(), success: { [weak self] in
|
2020-01-14 20:51:05 +00:00
|
|
|
guard let self = self else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-13 17:52:02 +00:00
|
|
|
self.coordinatorDelegate?.deviceVerificationDataLoadingViewModel(self, didAcceptKeyVerificationRequest: keyVerificationRequest)
|
2020-01-14 20:51:05 +00:00
|
|
|
|
|
|
|
}, failure: { [weak self] (error) in
|
|
|
|
guard let self = self else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self.update(viewState: .error(error))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private func downloadOtherDeviceKeys() {
|
2020-02-14 08:27:56 +00:00
|
|
|
guard let crypto = session.crypto,
|
2020-01-14 20:51:05 +00:00
|
|
|
let otherUserId = self.otherUserId,
|
|
|
|
let otherDeviceId = self.otherDeviceId else {
|
2019-10-23 15:09:58 +00:00
|
|
|
self.update(viewState: .errorMessage(VectorL10n.deviceVerificationErrorCannotLoadDevice))
|
|
|
|
NSLog("[DeviceVerificationDataLoadingViewModel] Error session.crypto is nil")
|
|
|
|
return
|
|
|
|
}
|
2020-01-14 20:51:05 +00:00
|
|
|
|
|
|
|
if let otherUser = session.user(withUserId: otherUserId) {
|
2019-10-23 15:09:58 +00:00
|
|
|
|
|
|
|
self.update(viewState: .loading)
|
2020-01-16 08:33:57 +00:00
|
|
|
|
|
|
|
self.currentOperation = crypto.downloadKeys([otherUserId], forceDownload: false, success: { [weak self] (usersDevicesMap, crossSigningKeysMap) in
|
2019-04-24 12:42:16 +00:00
|
|
|
guard let sself = self else {
|
|
|
|
return
|
|
|
|
}
|
2020-01-14 20:51:05 +00:00
|
|
|
|
|
|
|
if let otherDevice = usersDevicesMap?.object(forDevice: otherDeviceId, forUser: otherUserId) {
|
2019-10-23 15:09:58 +00:00
|
|
|
sself.update(viewState: .loaded)
|
2019-04-24 12:42:16 +00:00
|
|
|
sself.coordinatorDelegate?.deviceVerificationDataLoadingViewModel(sself, didLoadUser: otherUser, device: otherDevice)
|
|
|
|
} else {
|
2020-01-14 20:51:05 +00:00
|
|
|
sself.update(viewState: .errorMessage(VectorL10n.deviceVerificationErrorCannotLoadDevice))
|
2019-04-24 12:42:16 +00:00
|
|
|
}
|
2019-10-23 15:09:58 +00:00
|
|
|
|
2020-01-14 20:51:05 +00:00
|
|
|
}, failure: { [weak self] (error) in
|
|
|
|
guard let sself = self else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let finalError = error ?? DeviceVerificationDataLoadingViewModelError.unknown
|
|
|
|
|
|
|
|
sself.update(viewState: .error(finalError))
|
2019-04-24 12:42:16 +00:00
|
|
|
})
|
2020-01-14 20:51:05 +00:00
|
|
|
|
2019-04-24 12:42:16 +00:00
|
|
|
} else {
|
|
|
|
self.update(viewState: .errorMessage(VectorL10n.deviceVerificationErrorCannotLoadDevice))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func update(viewState: DeviceVerificationDataLoadingViewState) {
|
|
|
|
self.viewDelegate?.deviceVerificationDataLoadingViewModel(self, didUpdateViewState: viewState)
|
|
|
|
}
|
|
|
|
}
|