<commit_before>//= require jquery
//= require handlebars.runtime
//= require ember
//= require_tree .

var EMOJI_REGEXES = emojis.map(function(emoji) {
  // need to escape for regex
  emoji = emoji.replace("+", "\\+");
  return new RegExp(":(" + emoji + "):", "g");
});

Ember.Handlebars.registerBoundHelper("emojify", function(text) {
  var escape = Handlebars.Utils.escapeExpression;

  if (text) {
    text = escape(text);
  } else {
    return "";
  }

  EMOJI_REGEXES.forEach(function(regexp) {
    text = text.replace(regexp, '<img src="/assets/emoji/$1.png">');
  });

  return new Handlebars.SafeString(text);
});

var CATSOCKET_API_KEY = $("meta[name=catsocket-api-key]").attr("content");

var App = Ember.Application.create();

App.Router.map(function() {
  this.route("room", {path: "/rooms/:room_id"});
});

App.Room = Ember.Object.extend({
  id: null,

  messages: function() {
    var records = Ember.ArrayProxy.create({content: Ember.A(), isLoaded: false});

    // hack hack hack
    var params = {channel: this.get('id'), timestamp: 0, guid: "d8363d8e-2ccf-9e75-09d5-22c1912d2a6f", api_key: CATSOCKET_API_KEY};
    $.getJSON("http://catsocket.com:5000", params).then(function(json) {
      var messages = json.data.map(function(m) {
        var messageData = {};
        try {
          messageData = JSON.parse(m);
        } catch(e) {}

        if (messageData) {
          return App.Message.create(messageData);
        }
      }).filter(function(m) {
        return !!m;
      });
      records.set('content', messages);
      records.set('isLoaded', true);
    });
    return records;
  }.property()
});

App.Room.find = function(id) {
  return App.Room.create({id: id});
};

App.Message = Ember.Object.extend({
  from: null,
  text: null,
  type: 'm', // m for message, j for join, p for part
  timestamp: function() {
    return +new Date();
  }.property().volatile(),

  toJSON: function() {
    return this.getProperties('from', 'text', 'timestamp');
  }
});

App.IndexRoute = Ember.Route.extend({
  redirect: function() {
    this.transitionTo('room', App.Room.find("chat"));
  }
});

App.RoomRoute = Ember.Route.extend({
  model: function(params) {
    return App.Room.find(params.room_id);
  }
});

function getNickname() {
  var nickname = localStorage.getItem('nickname');
  if (!nickname) {
    nickname = prompt("Enter a nickname:");
    localStorage.setItem('nickname', nickname);
  }
  return nickname;
}

App.RoomController = Ember.ObjectController.extend({
  nickname: getNickname(),
  messageText: null,
  _timer: null,

  users: function() {
    return [];
  }.property(),

  contentWillChange: function() {
    var roomId = this.get('id');
    if (roomId) {
      this._publish(App.Message.create({type: 'p', from: this.get('nickname')}));
      CS.unsubscribe(this.get('id'));
    }
    if (this._timer) { clearInterval(this._timer); }
  }.observesBefore('content'),

  contentDidChange: function() {
    CS.subscribe(this.get('id'), this._update.bind(this));

    this._timer = setInterval(function() {
      this._publish(App.Message.create({type: 'j', from: this.get('nickname')}));
    }.bind(this), 10000);
  }.observes('content'),

  _publish: function(message) {
    CS.publish(this.get('id'), JSON.stringify(message.toJSON()), true);
  },

  _update: function(msgData) {
    var messages = this.get('messages'),
        users = this.get('users'),
        data;

    try {
      data = JSON.parse(msgData);
    } catch(e) {}

    if (!data) { return; }

    if (data.type === 'p') {
      users.removeObject(data.from);
    } else {
      if (!data.type || data.type === 'm') {
        messages.pushObject(App.Message.create({text: data.text, from: data.from, type: data.type}));
      }
      if (!users.contains(data.from)) {
        users.pushObject(data.from);
      }<commit_msg>Regenerate user list every 10s, remove users who we haven't heard from in 10m<commit_after>//= require jquery
//= require handlebars.runtime
//= require ember
//= require_tree .

var EMOJI_REGEXES = emojis.map(function(emoji) {
  // need to escape for regex
  emoji = emoji.replace("+", "\\+");
  return new RegExp(":(" + emoji + "):", "g");
});

Ember.Handlebars.registerBoundHelper("emojify", function(text) {
  var escape = Handlebars.Utils.escapeExpression;

  if (text) {
    text = escape(text);
  } else {
    return "";
  }

  EMOJI_REGEXES.forEach(function(regexp) {
    text = text.replace(regexp, '<img src="/assets/emoji/$1.png">');
  });

  return new Handlebars.SafeString(text);
});

var CATSOCKET_API_KEY = $("meta[name=catsocket-api-key]").attr("content");

var App = Ember.Application.create();

App.Router.map(function() {
  this.route("room", {path: "/rooms/:room_id"});
});

App.Room = Ember.Object.extend({
  id: null,

  messages: function() {
    var records = Ember.ArrayProxy.create({content: Ember.A(), isLoaded: false});

    // hack hack hack
    var params = {channel: this.get('id'), timestamp: 0, guid: "d8363d8e-2ccf-9e75-09d5-22c1912d2a6f", api_key: CATSOCKET_API_KEY};
    $.getJSON("http://catsocket.com:5000", params).then(function(json) {
      var messages = json.data.map(function(m) {
        var messageData = {};
        try {
          messageData = JSON.parse(m);
        } catch(e) {}

        if (messageData) {
          return App.Message.create(messageData);
        }
      }).filter(function(m) {
        return !!m;
      });
      records.set('content', messages);
      records.set('isLoaded', true);
    });
    return records;
  }.property()
});

App.Room.find = function(id) {
  return App.Room.create({id: id});
};

App.Message = Ember.Object.extend({
  from: null,
  text: null,
  type: 'm', // m for message, j for join, p for part
  timestamp: function() {
    return +new Date();
  }.property().volatile(),

  toJSON: function() {
    return this.getProperties('from', 'text', 'timestamp');
  }
});

App.IndexRoute = Ember.Route.extend({
  redirect: function() {
    this.transitionTo('room', App.Room.find("chat"));
  }
});

App.RoomRoute = Ember.Route.extend({
  model: function(params) {
    return App.Room.find(params.room_id);
  }
});

function getNickname() {
  var nickname = localStorage.getItem('nickname');
  if (!nickname) {
    nickname = prompt("Enter a nickname:");
    localStorage.setItem('nickname', nickname);
  }
  return nickname;
}

App.RoomController = Ember.ObjectController.extend({
  nickname: getNickname(),
  messageText: null,
  _timer: null,

  users: function() {
    return [];
  }.property(),

  regenerateUsers: function() {
    var messages = this.get('messages'),
        messagesLength = this.get('messages.length'),
        users = this.get('users'),
        newUsers = [],
        tenMinutesAgo = 60 * 10 * 1000;

    for (var i = messagesLength - 1; i >= 0; i--) {
      var message = messages.objectAt(i);

      if (message.get('timestamp') < (+new Date - tenMinutesAgo)) { break; }
      if (message.get('type') === 'p') { continue; }

      if (!newUsers.contains(message.get('from'))) {
        newUsers.pushObject(message.get('from'));
      }
    }

    this.set('users', newUsers);
  },

  contentWillChange: function() {
    var roomId = this.get('id');
    if (roomId) {
      this._publish(App.Message.create({type: 'p', from: this.get('nickname')}));
      CS.unsubscribe(this.get('id'));
    }
    if (this._timer) { clearInterval(this._timer); }
  }.observesBefore('content'),

  contentDidChange: function() {
    CS.subscribe(this.get('id'), this._update.bind(this));

    this._timer = setInterval(function() {
      this._publish(App.Message.create({type: 'j', from: this.get('nickname')}));
      this.regenerateUsers();
    }.bind(this), 10000);
  }.observes('content'),

  _publish: function(message) {
    CS.publish(this.get('id'), JSON.stringify(message.toJSON()), true);
  },

  _update: function(msgData) {
    var messages = this.get('messages'),
        users = this.get('users'),
        data;

    try {
      data = JSON.parse(msgData);
    } catch(e) {}

    if (!data) { return; }

    if (data.type === 'p') {
      users.removeObject(data.from);
    } else {
      if (!data.type || data.type === 'm') {
        messages.pushObject(App.Message.create({text: data.text, from: data.from, type: data.type}));
      }
      if (!users.contains(data.from)) {
        users.pushObject(data.from);
      }
{'fraction_non_alphanumeric': 0.1844903811685984, 'fraction_numerical': 0.0090811327518222, 'mean_word_length': 3.193386773547094, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 6, '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': '1350323', 'n_tokens_mistral': 2909, 'n_tokens_neox': 2791, 'n_words': 538}