Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new MX3PidAddManager #2747

Merged
merged 6 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ final class SettingsDiscoveryThreePidDetailsViewController: UIViewController {

private func render(error: Error) {
self.activityPresenter.removeCurrentActivityIndicator(animated: true)
self.errorPresenter.presentError(from: self, forError: error, animated: true, handler: nil)
self.errorPresenter.presentError(from: self, forError: error, animated: true, handler: {
self.viewModel.process(viewAction: .cancelThreePidValidation)
})
self.operationButton.isEnabled = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ final class SettingsDiscoveryThreePidDetailsViewModel: SettingsDiscoveryThreePid

// MARK: Private

private let session: MXSession
private var viewState: SettingsDiscoveryThreePidDetailsViewState?
private var currentThreePidRequestTokenInfo: ThreePidRequestTokenInfo?

private let threePidAddManager: MX3PidAddManager
private let identityService: MXIdentityService?
private var currentThreePidAddSession: MX3PidAddSession?

// MARK: Public

Expand All @@ -47,7 +49,8 @@ final class SettingsDiscoveryThreePidDetailsViewModel: SettingsDiscoveryThreePid
// MARK: - Setup

init(session: MXSession, threePid: MX3PID) {
self.session = session
self.threePidAddManager = session.threePidAddManager
self.identityService = session.identityService
self.threePid = threePid
}

Expand Down Expand Up @@ -94,78 +97,49 @@ final class SettingsDiscoveryThreePidDetailsViewModel: SettingsDiscoveryThreePid
}

private func share() {
self.requestToken(for: self.threePid, bind: true)
self.bind(bind: true)
}

private func revoke() {
self.requestToken(for: self.threePid, bind: false)
self.bind(bind: false)
}

private func requestToken(for threePid: MX3PID, bind: Bool, useOlderHomeserver: Bool = false) {
guard let restClient = self.session.matrixRestClient,
let clientSecret = MXTools.generateSecret() else {
return
}

let requestThreePidToken: (() -> Void) = {

switch self.threePid.medium {
case .email:
restClient.requestToken(forEmail: threePid.address, isDuringRegistration: false, clientSecret: clientSecret, sendAttempt: 1, nextLink: nil, success: { (sid) in
if let sid = sid {
self.currentThreePidRequestTokenInfo = ThreePidRequestTokenInfo(clientSecret: clientSecret, sid: sid, bind: bind)
self.update(viewState: .loaded(displayMode: .pendingThreePidVerification))

private func bind(bind: Bool) {
self.update(viewState: .loading)

let completion: ((MXResponse<Bool>) -> Void) = { (response) in
switch response {
case .success(let needValidation):
if needValidation {
self.update(viewState: .loaded(displayMode: .pendingThreePidVerification))

if case .email = self.threePid.medium {
self.registerEmailValidationNotification()
} else {
self.update(viewState: .error(SettingsDiscoveryThreePidDetailsViewModelError.unknown))
}
}, failure: { error in
if let mxError = MXError(nsError: error), mxError.errcode == kMXErrCodeStringThreePIDInUse, useOlderHomeserver == false {
self.requestToken(for: threePid, bind: bind, useOlderHomeserver: true)
} else {
self.update(viewState: .error(error ?? SettingsDiscoveryThreePidDetailsViewModelError.unknown))
}
})
case .msisdn:
let formattedPhoneNumber = self.formattedPhoneNumber(from: threePid.address)
restClient.requestToken(forPhoneNumber: formattedPhoneNumber, isDuringRegistration: false, countryCode: nil, clientSecret: clientSecret, sendAttempt: 1, nextLink: nil, success: { (sid, msisdn) in
if let sid = sid {
self.currentThreePidRequestTokenInfo = ThreePidRequestTokenInfo(clientSecret: clientSecret, sid: sid, bind: bind)
self.update(viewState: .loaded(displayMode: .pendingThreePidVerification))
} else {
self.update(viewState: .error(SettingsDiscoveryThreePidDetailsViewModelError.unknown))
}
}, failure: { error in
if let mxError = MXError(nsError: error), mxError.errcode == kMXErrCodeStringThreePIDInUse, useOlderHomeserver == false {
self.requestToken(for: threePid, bind: bind, useOlderHomeserver: true)
} else {
self.update(viewState: .error(error ?? SettingsDiscoveryThreePidDetailsViewModelError.unknown))
}
})
default:
break
}
}

self.update(viewState: .loading)

if useOlderHomeserver {
restClient.remove3PID(address: threePid.address, medium: threePid.medium.identifier) { (response) in
switch response {
case .success:
requestThreePidToken()
case .failure(let error):
self.update(viewState: .error(error))
} else {
self.checkThreePidDiscoverability()
}

case .failure(let error):
self.update(viewState: .error(error))
}
} else {
requestThreePidToken()
}

switch self.threePid.medium {
case .email:
self.currentThreePidAddSession = self.threePidAddManager.startIdentityServerSession(withEmail: self.threePid.address, bind: bind, completion: completion)
case .msisdn:
let formattedPhoneNumber = self.formattedPhoneNumber(from: threePid.address)
self.currentThreePidAddSession = self.threePidAddManager.startIdentityServerSession(withPhoneNumber: formattedPhoneNumber, countryCode: nil, bind: bind, completion: completion)
default:
break
}
}


@discardableResult
private func isThreePidDiscoverable(_ threePid: MX3PID, completion: @escaping (_ response: MXResponse<Bool>) -> Void) -> MXHTTPOperation? {
guard let identityService = self.session.identityService else {
guard let identityService = self.identityService else {
completion(.failure(SettingsDiscoveryThreePidDetailsViewModelError.unknown))
return nil
}
Expand All @@ -184,55 +158,51 @@ final class SettingsDiscoveryThreePidDetailsViewModel: SettingsDiscoveryThreePid
self.viewDelegate?.settingsDiscoveryThreePidDetailsViewModel(self, didUpdateViewState: viewState)
}

private func bindThreePid(_ threePid: MX3PID, threePidRequestTokenInfo: ThreePidRequestTokenInfo) {
guard let restClient = self.session.matrixRestClient else {
// MARK: Email

private func cancelThreePidValidation() {

if case .email = threePid.medium {
self.unregisterEmailValidationNotification()
}

if let currentThreePidAddSession = self.currentThreePidAddSession {
self.threePidAddManager.cancel(session: currentThreePidAddSession)
self.currentThreePidAddSession = nil
}

self.checkThreePidDiscoverability()
}

private func confirmEmailValidation() {
guard let threePidAddSession = self.currentThreePidAddSession else {
return
}

self.update(viewState: .loading)
restClient.addThirdPartyIdentifier(threePidRequestTokenInfo.sid, clientSecret: threePidRequestTokenInfo.clientSecret, bind: threePidRequestTokenInfo.bind) { response in

self.threePidAddManager.tryFinaliseIdentityServerEmailSession(threePidAddSession) { response in
switch response {
case .success:
if case .email = threePid.medium {

if threePidAddSession.medium == kMX3PIDMediumEmail {
self.unregisterEmailValidationNotification()
}

self.checkThreePidDiscoverability()
case .failure(let error):
if let mxError = MXError(nsError: error), mxError.errcode == kMXErrCodeStringThreePIDAuthFailed {
self.update(viewState: .loaded(displayMode: .pendingThreePidVerification))
} else {
if case .email = threePid.medium {
if threePidAddSession.medium == kMX3PIDMediumEmail {
self.unregisterEmailValidationNotification()
}

self.update(viewState: .error(error))
}
}
}
}

// MARK: Email

private func cancelThreePidValidation() {

if case .email = threePid.medium {
self.unregisterEmailValidationNotification()
}

self.currentThreePidRequestTokenInfo = nil
self.checkThreePidDiscoverability()
}

private func confirmEmailValidation() {
guard let threePidRequestTokenInfo = self.currentThreePidRequestTokenInfo else {
return
}
self.bindThreePid(self.threePid, threePidRequestTokenInfo: threePidRequestTokenInfo)
}

private func registerEmailValidationNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(handleEmailValidationNotification(notification:)), name: .AppDelegateDidValidateEmail, object: nil)
}
Expand All @@ -245,13 +215,13 @@ final class SettingsDiscoveryThreePidDetailsViewModel: SettingsDiscoveryThreePid
guard let userInfo = notification.userInfo,
let clientSecret = userInfo[AppDelegateDidValidateEmailNotificationClientSecretKey] as? String,
let sid = userInfo[AppDelegateDidValidateEmailNotificationSIDKey] as? String,
let threePidRequestTokenInfo = self.currentThreePidRequestTokenInfo,
threePidRequestTokenInfo.clientSecret == clientSecret,
threePidRequestTokenInfo.sid == sid else {
let threePidAddSession = self.currentThreePidAddSession,
threePidAddSession.clientSecret == clientSecret,
threePidAddSession.sid == sid else {
return
}
self.bindThreePid(self.threePid, threePidRequestTokenInfo: threePidRequestTokenInfo)

self.confirmEmailValidation()
}

// MARK: Phone number
Expand All @@ -260,20 +230,20 @@ final class SettingsDiscoveryThreePidDetailsViewModel: SettingsDiscoveryThreePid
guard phoneNumber.starts(with: "+") == false else {
return phoneNumber
}
return "+\(threePid.address)"
return "+\(phoneNumber)"
}

private func validatePhoneNumber(with activationCode: String) {
guard let identityService = self.session.identityService, let threePidRequestTokenInfo = self.currentThreePidRequestTokenInfo else {
guard let threePidAddSession = self.currentThreePidAddSession else {
return
}

identityService.submit3PIDValidationToken(activationCode, medium: MX3PID.Medium.msisdn.identifier, clientSecret: threePidRequestTokenInfo.clientSecret, sid: threePidRequestTokenInfo.sid) { (response) in
self.update(viewState: .loading)

self.threePidAddManager.finaliseIdentityServerPhoneNumberSession(threePidAddSession, token: activationCode) { (response) in
switch response {
case .success:
self.bindThreePid(self.threePid, threePidRequestTokenInfo: threePidRequestTokenInfo)
self.checkThreePidDiscoverability()
case .failure(let error):

if let mxError = MXError(nsError: error), mxError.errcode == kMXErrCodeStringUnknownToken {
self.update(viewState: .loaded(displayMode: .pendingThreePidVerification))
} else {
Expand Down
Loading