<commit_before>"use strict";

var Faye                 = require('gitter-faye');
var log                  = require('loglevel');
var _                    = require('underscore');
var Backbone             = require('backbone');
var TemplateSubscription = require('./template-subscription');
var fayeDebug            = require('debug-proxy')('grc:faye');
var debug                = require('debug-proxy')('grc:client');

/* @const */
var FAYE_PREFIX = '/api';
var FAYE_PREFIX_RE = /^\/api/;
var DEFAULT_FAYE_URL = 'https://ws.gitter.im/faye';
var PING_RESPONSE_TIMEOUT = 30;

Faye.logger = {
  fatal: log.error.bind(log),
  error: log.error.bind(log),
  warn: log.warn.bind(log)
};

if (fayeDebug.enabled) {
  Faye.logger.info = fayeDebug;
  Faye.logger.debug = fayeDebug;
}

var ErrorLogger = function() {};
ErrorLogger.prototype.incoming = function(message, callback) {
  if(message.error) {
    debug('Bayeux error: %j', message);
  }

  callback(message);
};

var ClientAuth = function(client, options) {
  this.client = client;
  if(options.authProvider) {
    this.authProvider = options.authProvider;
  } else {
    this.authProvider = function(callback) {
      return callback({ token: options.token });
    };
  }
};

ClientAuth.prototype.outgoing = function(message, callback) {
  if(message.channel !== '/meta/handshake') return callback(message);

  var uniqueClientId = this.client.uniqueClientId;

  this.client.clientId = null;
  debug("Rehandshaking realtime connection");

  this.authProvider(function(authInfo) {
      if(!message.ext) message.ext = {};
      _.extend(message.ext, authInfo);
      message.ext.uniqueClientId = uniqueClientId;
      callback(message);
   });
};


ClientAuth.prototype.incoming = function(message, callback) {
  if(message.channel !== '/meta/handshake') return callback(message);

  if(message.successful) {
    // New clientId?
    if(this.client.clientId !== message.clientId) {
      this.client.clientId = message.clientId;
      debug("Realtime reestablished. New id is %s", this.client.clientId);
      this.client.trigger('newConnectionEstablished');
    }

    if (message.ext && message.ext.context) {
      if (message.ext.context.user) {
        this.client.user.set(message.ext.context.user);
      } else if(message.ext.context.userId) {
        this.client.user.set({ id: message.ext.context.userId });
      }
    }

    // Clear any transport problem indicators
    this.client._transportUp();
  }

  callback(message);
};<commit_msg>Add realtimeLibrary<commit_after>"use strict";

var Faye                 = require('gitter-faye');
var log                  = require('loglevel');
var _                    = require('underscore');
var Backbone             = require('backbone');
var TemplateSubscription = require('./template-subscription');
var fayeDebug            = require('debug-proxy')('grc:faye');
var debug                = require('debug-proxy')('grc:client');

/* @const */
var FAYE_PREFIX = '/api';
var FAYE_PREFIX_RE = /^\/api/;
var DEFAULT_FAYE_URL = 'https://ws.gitter.im/faye';
var PING_RESPONSE_TIMEOUT = 30;

Faye.logger = {
  fatal: log.error.bind(log),
  error: log.error.bind(log),
  warn: log.warn.bind(log)
};

if (fayeDebug.enabled) {
  Faye.logger.info = fayeDebug;
  Faye.logger.debug = fayeDebug;
}

var ErrorLogger = function() {};
ErrorLogger.prototype.incoming = function(message, callback) {
  if(message.error) {
    debug('Bayeux error: %j', message);
  }

  callback(message);
};

var ClientAuth = function(client, options) {
  this.client = client;
  if(options.authProvider) {
    this.authProvider = options.authProvider;
  } else {
    this.authProvider = function(callback) {
      return callback({ token: options.token });
    };
  }
};

ClientAuth.prototype.outgoing = function(message, callback) {
  if(message.channel !== '/meta/handshake') return callback(message);

  var uniqueClientId = this.client.uniqueClientId;

  this.client.clientId = null;
  debug("Rehandshaking realtime connection");

  this.authProvider(function(authInfo) {
      if(!message.ext) message.ext = {};
      _.extend(message.ext, authInfo);
      message.ext.uniqueClientId = uniqueClientId;
      message.ext.realtimeLibrary = 'faye';
      callback(message);
   });
};


ClientAuth.prototype.incoming = function(message, callback) {
  if(message.channel !== '/meta/handshake') return callback(message);

  if(message.successful) {
    // New clientId?
    if(this.client.clientId !== message.clientId) {
      this.client.clientId = message.clientId;
      debug("Realtime reestablished. New id is %s", this.client.clientId);
      this.client.trigger('newConnectionEstablished');
    }

    if (message.ext && message.ext.context) {
      if (message.ext.context.user) {
        this.client.user.set(message.ext.context.user);
      } else if(message.ext.context.userId) {
        this.client.user.set({ id: message.ext.context.userId });
      }
    }

    // Clear any transport problem indicators
    this.client._transportUp();
  }

  callback(message);
};
{'fraction_non_alphanumeric': 0.1497029702970297, 'fraction_numerical': 0.0007920792079207921, 'mean_word_length': 3.2552653748946927, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3673279', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1571, 'n_words': 281}