<commit_before>// connection.js

MDChatConnection = function(spec) {
  var self = this;
  self.user_id = spec.user_id || null;
  self.not_use_open = spec.not_use_open;
  self.not_use_push = spec.not_use_push;
};

MDChatConnection.prototype.open = function() {
  var self = this;
  var user_id = self.user_id;
  self.socket = io.connect(location.origin);
  self.registerCallback("connect", function() {
    console.log("connected");
    self.emit('msg update', {
      user_id: user_id
    });
  });
  if (!self.not_use_open) {
    self.registerCallback("msg open", function(data) {
      if (data.length > 0) {
        $('#chats').empty(); // ensure to clear #chats
        $.each(data, function(key, value) {
          var say = new Say(value);
          say.appendTo($("#chats"), self.socket);
        });
      }
    });
  }
  if (!self.not_use_push) {
    self.registerCallback("msg push", function(data) {
      if (self.user_id.toString() == data.user_id.toString()) {
        var say = new Say({
          html: data.me_html,
          date: data.date,
          _id: data._id
        });
      }
      else {
        var say = new Say({
          html: data.other_html,
          date: data.date,
          _id: data._id
        });
      }
      var $data = say.appendTo($("#chats"), self);
      // scroll to bottom
      $data.ready(function() {
        $("html,body").animate({scrollTop: document.body.scrollHeight}, "slow");
        $("#loading-area").addClass("hidden");
      });
    });
  }
  self.registerCallback('msg delete-one', function(data) {
    var say_id = data.say_id;
    $("#say_" + say_id).remove();
  });
  
};

MDChatConnection.prototype.registerCallback = function(event, cb) {
  console.log("registering " + event);
  this.socket.on(event, cb);
};

MDChatConnection.prototype.emit = function(msg, data) {
  this.socket.emit(msg, data);
}

MDChatConnection.prototype.postMessage = function(name, msg) {
  var self = this;
  self.emit('msg send', {"name": name, "msg": msg});<commit_msg>#37: showing new messages to the users who are not logging in<commit_after>// connection.js

MDChatConnection = function(spec) {
  var self = this;
  self.user_id = spec.user_id || null;
  self.not_use_open = spec.not_use_open;
  self.not_use_push = spec.not_use_push;
};

MDChatConnection.prototype.open = function() {
  var self = this;
  var user_id = self.user_id;
  self.socket = io.connect(location.origin);
  self.registerCallback("connect", function() {
    console.log("connected");
    self.emit('msg update', {
      user_id: user_id
    });
  });
  if (!self.not_use_open) {
    self.registerCallback("msg open", function(data) {
      if (data.length > 0) {
        $('#chats').empty(); // ensure to clear #chats
        $.each(data, function(key, value) {
          var say = new Say(value);
          say.appendTo($("#chats"), self.socket);
        });
      }
    });
  }
  if (!self.not_use_push) {
    self.registerCallback("msg push", function(data) {
      if (!self.user_id || self.user_id.toString() !== data.user_id.toString()) {
        var say = new Say({
          html: data.other_html,
          date: data.date,
          _id: data._id
        });
      }
      else {
        var say = new Say({
          html: data.me_html,
          date: data.date,
          _id: data._id
        });
      }
      var $data = say.appendTo($("#chats"), self);
      // scroll to bottom
      $data.ready(function() {
        $("html,body").animate({scrollTop: document.body.scrollHeight}, "slow");
        $("#loading-area").addClass("hidden");
      });
    });
  }
  self.registerCallback('msg delete-one', function(data) {
    var say_id = data.say_id;
    $("#say_" + say_id).remove();
  });
  
};

MDChatConnection.prototype.registerCallback = function(event, cb) {
  console.log("registering " + event);
  this.socket.on(event, cb);
};

MDChatConnection.prototype.emit = function(msg, data) {
  this.socket.emit(msg, data);
}

MDChatConnection.prototype.postMessage = function(name, msg) {
  var self = this;
  self.emit('msg send', {"name": name, "msg": msg});
{'fraction_non_alphanumeric': 0.1678849902534113, 'fraction_numerical': 0.0009746588693957114, 'mean_word_length': 2.668453976764969, 'pattern_counts': {'":': 4, '<': 3, '<?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': '4114743', 'n_tokens_mistral': 1347, 'n_tokens_neox': 1343, 'n_words': 263}