<commit_before>var models = require('../db/models');
var util = require('../core/utilities');
var connections = {};
var privateSessions = {};

var events = {

  //input: {userToken: string}
  init: function(data) {
    connections[data.userToken] = this;
    models.retrieveUserScore(data.userToken)
    .then(sendRetrievedUser);
  },

  //input: {userToken: string, messageId: string}
  upvote: function(data) {
    models.createVote(data)
    .then(function(success) {
      return models.retrieveUserScore(data.userToken);
    }).then(sendRetrievedUser);
  },

  //input: {userToken: string, messageId: string, commentId: string}
  pmInit: function(data) {
    var id;

    Object.keys(privateSessions).forEach(function(session, shortid) {
      session.users.forEach(function(user) {
        if (data.userToken === user && data.messageId === session.messageId) {
          id = shortid;
        }
      });
    });

    if (id === undefined) {
      models.retrieveUserByContentId(data)
      .then(function(content) {
        var userA = data.userToken;
        var userB = content[0].userToken;

        id = util.createId();
        privateSessions[id] = {
          users: [userA, userB],
          messageId: data.messageId,
          messages: []
        };

        privateSessions[id].users.forEach(function(user) {
          exports.sendUserData(user, 'pmInit', {sessionId: id, messages: []});
        });
      });
    } else {
      privateSessions[id].users.forEach(function(user) {
        exports.sendUserData(user, 'pmInit', {
          sessionId: id,
          messages: privateSessions[id].messages
        });
      });
    }

  },

  //input: {content: string, sessionId: string}
  pmContent: function(data) {
    privateSessions[data.sessionId].users.forEach(function(user) {
      data.from = connections[user] === this ? 'you' : 'them';
      privateSessions[data.sessionId].messages.push(data);
      exports.sendUserData(user, 'pmContent', data);
    }.bind(this));
  },

  //input: {userToken: string}
  pmList: function(data) {
    var chats = [];
    Object.keys(privateSessions).forEach(function(session) {
      session.users.forEach(function(user) {
        if (data.userToken === user) {
          chats.push(session.messageId);
        }
      });
    });
    exports.sendUserData(data.userToken, 'pmList', {chats: chats});
  },
<commit_msg>Fix session loop to get correct messages<commit_after>var models = require('../db/models');
var util = require('../core/utilities');
var connections = {};
var privateSessions = {};

var events = {

  //input: {userToken: string}
  init: function(data) {
    connections[data.userToken] = this;
    models.retrieveUserScore(data.userToken)
    .then(sendRetrievedUser);
  },

  //input: {userToken: string, messageId: string}
  upvote: function(data) {
    models.createVote(data)
    .then(function(success) {
      return models.retrieveUserScore(data.userToken);
    }).then(sendRetrievedUser);
  },

  //input: {userToken: string, messageId: string, commentId: string}
  pmInit: function(data) {
    var id;

    // Check to see if a chat session already exists
    Object.keys(privateSessions).forEach(function(shortid) {
      var session = privateSessions[shortid];
      session.users.forEach(function(user) {
        if (data.userToken === user && data.messageId === session.messageId) {
          id = shortid;
        }
      });
    });

    if (id === undefined) {
      // Create a new session, since one doesn't already exist
      models.retrieveUserByContentId(data)
      .then(function(content) {
        var userA = data.userToken;
        var userB = content[0].userToken;

        id = util.createId();
        privateSessions[id] = {
          users: [userA, userB],
          messageId: data.messageId,
          messages: []
        };

        privateSessions[id].users.forEach(function(user) {
          exports.sendUserData(user, 'pmInit', {sessionId: id, messages: []});
        });
      });
    } else {
      // Send users the old session messages, since it exists
      privateSessions[id].users.forEach(function(user) {
        exports.sendUserData(user, 'pmInit', {
          sessionId: id,
          messages: privateSessions[id].messages
        });
      });
    }

  },

  //input: {content: string, sessionId: string}
  pmContent: function(data) {
    privateSessions[data.sessionId].users.forEach(function(user) {
      data.from = connections[user] === this ? 'you' : 'them';
      privateSessions[data.sessionId].messages.push(data);
      exports.sendUserData(user, 'pmContent', data);
    }.bind(this));
  },

  //input: {userToken: string}
  pmList: function(data) {
    var chats = [];
    Object.keys(privateSessions).forEach(function(id) {
      session = privateSessions[id];
      session.users.forEach(function(user) {
        if (data.userToken === user) {
          chats.push({messageId: session.messageId});
        }
      });
    });
    exports.sendUserData(data.userToken, 'pmList', {chats: chats});
  },

{'fraction_non_alphanumeric': 0.1501686173378298, 'fraction_numerical': 0.00039674667724657806, 'mean_word_length': 2.7796101949025487, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3592513', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1534, 'n_words': 284}