<commit_before>//
//  AuthViewControllerAuthenticationHandler.swift
//  Rocket.Chat
//
//  Created by <NAME> on 3/26/18.
//  Copyright © 2018 Rocket.Chat. All rights reserved.
//

import Foundation
import RealmSwift

extension AuthViewController {
    internal func handleAuthenticationResponse(_ response: LoginResponse) {
        switch response {
        case .resource(let resource):
            guard let error = resource.error else { break }

            stopLoading()

            switch error.lowercased() {
            case "totp-required":
                performSegue(withIdentifier: "TwoFactor", sender: nil)
            case "unauthorized":
                Alert(key: "error.login_unauthorized").present()
                return
            default:
                Alert(key: "error.login").present()
                return
            }

            if let publicSettings = serverPublicSettings {
                AuthSettingsManager.persistPublicSettings(settings: publicSettings)
            }

            if let realm = Realm.current, let auth = AuthManager.isAuthenticated(realm: realm), let version = serverVersion {
                try? realm.write {
                    auth.serverVersion = version.description
                }
            }
        case .error(let error):
            alert(title: localized("error.login.title"), message: error.description)
        }

        API.current()?.fetch(MeRequest()) { [weak self] response in
            switch response {
            case .resource(let resource):
                if let token = self?.socketHandlerToken {
                    SocketManager.removeConnectionHandler(token: token)
                }

                if let user = resource.user {

                    let realm = Realm.current
                    try? realm?.write {
                        realm?.add(user, update: true)
                    }

                    if user.username != nil {
                        self?.dismiss(animated: true, completion: nil)
                        AppManager.reloadApp()
                    } else {
                        self?.performSegue(withIdentifier: "RequestUsername", sender: nil)
                    }
                } else {
                    self?.stopLoading()
                    Alert(key: "error.socket.default_error").present()
                }
            case .error:
                self?.stopLoading()<commit_msg>Improve cyclomatic complexity<commit_after>//
//  AuthViewControllerAuthenticationHandler.swift
//  Rocket.Chat
//
//  Created by <NAME> on 3/26/18.
//  Copyright © 2018 Rocket.Chat. All rights reserved.
//

import Foundation
import RealmSwift

extension AuthViewController {
    internal func handleAuthenticationResponse(_ response: LoginResponse) {
        switch response {
        case .resource(let resource):
            guard let error = resource.error else { break }

            stopLoading()

            switch error.lowercased() {
            case "totp-required":
                performSegue(withIdentifier: "TwoFactor", sender: nil)
            case "unauthorized":
                Alert(key: "error.login_unauthorized").present()
                return
            default:
                Alert(key: "error.login").present()
                return
            }

            if let publicSettings = serverPublicSettings {
                AuthSettingsManager.persistPublicSettings(settings: publicSettings)
            }

            if let realm = Realm.current, let auth = AuthManager.isAuthenticated(realm: realm), let version = serverVersion {
                try? realm.write {
                    auth.serverVersion = version.description
                }
            }
        case .error(let error):
            stopLoading()
            alert(title: localized("error.login.title"), message: error.description)
        }

        performMeRequest()
    }

    internal func performMeRequest() {
        API.current()?.fetch(MeRequest()) { [weak self] response in
            switch response {
            case .resource(let resource):
                if let token = self?.socketHandlerToken {
                    SocketManager.removeConnectionHandler(token: token)
                }

                if let user = resource.user {

                    let realm = Realm.current
                    try? realm?.write {
                        realm?.add(user, update: true)
                    }

                    if user.username != nil {
                        self?.dismiss(animated: true, completion: nil)
                        AppManager.reloadApp()
                    } else {
                        self?.performSegue(withIdentifier: "RequestUsername", sender: nil)
                    }
                } else {
                    self?.stopLoading()
                    Alert(key: "error.socket.default_error").present()
                }
            case .error:
                self?.stopLoading()
{'fraction_non_alphanumeric': 0.08862034239677745, 'fraction_numerical': 0.0036253776435045317, 'mean_word_length': 1.4571994062345373, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5565368', 'n_tokens_mistral': 1199, 'n_tokens_neox': 1091, 'n_words': 268}