<commit_before>'use strict';

var database = require('./database');

var addUser = function(username) {
 database.addUser(username);
};

var setupSocket = function(user) {
  var callbackUserList = function(userList) {
    user.socket.emit('userList', {userList: userList});
    user.socket.broadcast.emit('userList', {userList: userList});
  }
  database.getUserListSetup(callbackUserList);

  user.socket.on('getConversations', function(lastestTime) {
    if (typeof lastestTime == 'number') {
      getConversations(user, lastestTime);
    }
  });
  user.socket.on('getMessages', function(request) {
    var verifiedRequest = verifyRequest(request);
    if (verifiedRequest) {
      getMessages(user, verifiedRequest);
    }
  });
};

var verifyRequest = function(request) {
  var verifiedRequest = {};
  if (typeof request.conversationId == 'string') {
    verifiedRequest.conversationId = request.conversationId;
  } else {
    return false;
  }
  if (typeof request.lastestTime == 'number') {
    verifiedRequest.lastestTime = request.lastestTime;
  } else {
    return false;
  }
  return verifiedRequest;
};

var getConversations = function(user, lastestTime) {
  var conversationCallback = function(conversations) {
    user.socket.emit(conversations);
  }
  var messageCallback = function(messages) {
    user.socket.emit(messages);
  }
  var callbacks = {conversation: conversationCallback,
                   message: messageCallback
                  };
  database.getConversations(user.username, lastestTime, callbacks);
};

var getMessages = function(user, request) {
  var callback = function(messages) {
    user.socket.emit(messages);
  }
  database.getMessages(request, callback);
};

var storeConversation = function(conversation, messages) {
  database.storeConversation(conversation, messages);
};

var storeMessage = function(message, conversationId) {
  database.storeMessage(message, conversationId);
};

var sendInitialData = function(user) {
  getConversations(user, Date.now());
};

module.exports = {
  addUser: addUser,<commit_msg>fixed emitting of new conversations and messages from server side<commit_after>'use strict';

var database = require('./database');

var addUser = function(username) {
 database.addUser(username);
};

var setupSocket = function(user) {
  var callbackUserList = function(userList) {
    user.socket.emit('userList', {userList: userList});
    user.socket.broadcast.emit('userList', {userList: userList});
  }
  database.getUserListSetup(callbackUserList);

  user.socket.on('getConversations', function(lastestTime) {
    if (typeof lastestTime == 'number') {
      getConversations(user, lastestTime);
    }
  });
  user.socket.on('getMessages', function(request) {
    var verifiedRequest = verifyRequest(request);
    if (verifiedRequest) {
      getMessages(user, verifiedRequest);
    }
  });
};

var verifyRequest = function(request) {
  var verifiedRequest = {};
  if (typeof request.conversationId == 'string') {
    verifiedRequest.conversationId = request.conversationId;
  } else {
    return false;
  }
  if (typeof request.lastestTime == 'number') {
    verifiedRequest.lastestTime = request.lastestTime;
  } else {
    return false;
  }
  return verifiedRequest;
};

var getConversations = function(user, lastestTime) {
  var conversationCallback = function(conversation) {
    user.socket.emit('newConversation', conversation);
  }
  var messageCallback = function(message) {
    user.socket.emit('newMessage', message);
  }
  var callbacks = {conversation: conversationCallback,
                   message: messageCallback
                  };
  database.getConversations(user.username, lastestTime, callbacks);
};

var getMessages = function(user, request) {
  var callback = function(message) {
    user.socket.emit('newMessage', message);
  }
  database.getMessages(request, callback);
};

var storeConversation = function(conversation, messages) {
  database.storeConversation(conversation, messages);
};

var storeMessage = function(message, conversationId) {
  database.storeMessage(message, conversationId);
};

var sendInitialData = function(user) {
  getConversations(user, Date.now());
};

module.exports = {
  addUser: addUser,
{'fraction_non_alphanumeric': 0.12461428910515072, 'fraction_numerical': 0.0, 'mean_word_length': 4.395646606914212, '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': '7422437', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1220, 'n_words': 241}