<commit_before>import * as constants from '../config/constants';
import NotificationActions from '../actions/NotificationActions';
import WebSocketActions from '../actions/WebSocketActions';
import AuthService from '../services/AuthService';
import { browserHistory } from 'react-router';

// Hold the set interval identifier which will try to reconnect every 5 sec
let watchdog = null;

/**
 * Service which will create and manage webSocket connection
 */
class WebSocketService {

    /**
     * connect - Connect to the websocket server if we are not already connected
     */
    connect() {
        if(!global.iosocket || !global.iosocket.isConnected()) {
            global.iosocket = io.sails.connect(constants.webSocketUri);

            iosocket.on('connect', () => {this._handleConnected()});
            iosocket.on('disconnect', () => {this._handleDisconnected()});

            // Show this notification only if it takes some time to connect
            setTimeout(() => {
                if(!iosocket.isConnected()) {
                    NotificationActions.loading('Connexion au serveur perdue..');
                }
            }, 1000);
        }

        // Try to reconnect every 5 seconds
        if(!watchdog) {
            watchdog = setInterval(() => {this.connect()}, 5000);
        }
    }

    _handleConnected() {
        let jwtError = null;
        // Authenticate with stored jwt
        let jwt = localStorage.getItem(constants.jwtName);
        AuthService.tryToAuthenticateConnexion(jwt)
        .then(() => {
            WebSocketActions.connected();
            NotificationActions.hideLoading();
        })
        .catch((error) => {
            if(jwt) {
                jwtError = error;
                localStorage.removeItem(constants.jwtName);
            }

            // Authenticate with jwt stored before 'login as'
            jwt = localStorage.getItem(constants.firstJwtName);
            AuthService.tryToAuthenticateConnexion(jwt)
            .then(() => {
                localStorage.removeItem(constants.firstJwtName);
                WebSocketActions.connected();
                NotificationActions.hideLoading();
            })
            .catch((error) => {
                if(jwt) {
                    jwtError = error;
                }

                // Authenticate via EtuUTT
                let authCode = AuthService.getAuthorizationCode();
                if(authCode) {
                    NotificationActions.loading('Connexion depuis EtuUTT en cours..');
                }
                AuthService.sendAuthorizationCode(authCode)
                .then(() => {
                    browserHistory.push(window.location.pathname);
                    NotificationActions.hideLoading();
                })
                .catch((error) => {
                    browserHistory.push(window.location.pathname);
                    NotificationActions.hideLoading();
                    NotificationActions.error('Une erreur s\'est produite pendant votre authentification via EtuUTT', error);

                    // Authenticate with IP
                    AuthService.tryToAuthenticateWithIP()
                    .then(() => {
                        WebSocketActions.connected();
                        NotificationActions.hideLoading();
                    })
                    // Ignore this error
                    .catch((error) => {
                        WebSocketActions.connected();
                        NotificationActions.hideLoading();
                        if(jwtError) {
                            NotificationActions.error('Votre connexion a expiré, veuillez vous reconnecter.', jwtError);
                        }
                    });
                });
            });
        });
    }

    _handleDisconnected() {
        WebSocketActions.disconnected();<commit_msg>Fix unwanted ettuutt auth error modal<commit_after>import * as constants from '../config/constants';
import NotificationActions from '../actions/NotificationActions';
import WebSocketActions from '../actions/WebSocketActions';
import AuthService from '../services/AuthService';
import { browserHistory } from 'react-router';

// Hold the set interval identifier which will try to reconnect every 5 sec
let watchdog = null;

/**
 * Service which will create and manage webSocket connection
 */
class WebSocketService {

    /**
     * connect - Connect to the websocket server if we are not already connected
     */
    connect() {
        if(!global.iosocket || !global.iosocket.isConnected()) {
            global.iosocket = io.sails.connect(constants.webSocketUri);

            iosocket.on('connect', () => {this._handleConnected()});
            iosocket.on('disconnect', () => {this._handleDisconnected()});

            // Show this notification only if it takes some time to connect
            setTimeout(() => {
                if(!iosocket.isConnected()) {
                    NotificationActions.loading('Connexion au serveur perdue..');
                }
            }, 1000);
        }

        // Try to reconnect every 5 seconds
        if(!watchdog) {
            watchdog = setInterval(() => {this.connect()}, 5000);
        }
    }

    _handleConnected() {
        let jwtError = null;
        // Authenticate with stored jwt
        let jwt = localStorage.getItem(constants.jwtName);
        AuthService.tryToAuthenticateConnexion(jwt)
        .then(() => {
            WebSocketActions.connected();
            NotificationActions.hideLoading();
        })
        .catch((error) => {
            if(jwt) {
                jwtError = error;
                localStorage.removeItem(constants.jwtName);
            }

            // Authenticate with jwt stored before 'login as'
            jwt = localStorage.getItem(constants.firstJwtName);
            AuthService.tryToAuthenticateConnexion(jwt)
            .then(() => {
                localStorage.removeItem(constants.firstJwtName);
                WebSocketActions.connected();
                NotificationActions.hideLoading();
            })
            .catch((error) => {
                if(jwt) {
                    jwtError = error;
                }

                // Authenticate via EtuUTT
                let authCode = AuthService.getAuthorizationCode();
                if(authCode) {
                    NotificationActions.loading('Connexion depuis EtuUTT en cours..');
                }
                AuthService.sendAuthorizationCode(authCode)
                .then(() => {
                    browserHistory.push(window.location.pathname);
                    NotificationActions.hideLoading();
                })
                .catch((error) => {
                    browserHistory.push(window.location.pathname);
                    NotificationActions.hideLoading();
                    if(authCode) {
                        NotificationActions.error('Une erreur s\'est produite pendant votre authentification via EtuUTT', error);
                    }

                    // Authenticate with IP
                    AuthService.tryToAuthenticateWithIP()
                    .then(() => {
                        WebSocketActions.connected();
                        NotificationActions.hideLoading();
                    })
                    // Ignore this error
                    .catch((error) => {
                        WebSocketActions.connected();
                        NotificationActions.hideLoading();
                        if(jwtError) {
                            NotificationActions.error('Votre connexion a expiré, veuillez vous reconnecter.', jwtError);
                        }
                    });
                });
            });
        });
    }

    _handleDisconnected() {
        WebSocketActions.disconnected();
{'fraction_non_alphanumeric': 0.10261646458200382, 'fraction_numerical': 0.0025526483726866626, 'mean_word_length': 1.5649754500818331, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '797742', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1804, 'n_words': 381}