<commit_before>var moment = require('moment');
var geolib = require('geolib');
var shortid = require('shortid');
var fs = require('fs');
var themes = require('./themes');

// Use moment.js to calculate how much time has elapsed since some prior time
exports.getTimeElapsedSince = function(time) {
  return moment(time).fromNow();
};

// Use geolib.js to calculate distance from the mark to the user
exports.getDistanceFrom = function(mark, user) {
  var dist = geolib.getDistance(
      {latitude: user.x, longitude: user.y},
      {latitude: mark.x, longitude: mark.y}
      );
  return dist + 'm';
};

exports.decorateMarksWithVoteStatus = function(marks, votes) {
  var voted = {};
  for (var k = 0;k < votes.length;k++) {
    voted[votes[k].messageId] = true;
  }
  for (var i = 0;i < marks.length;i++) {
    var markMessageId = marks[i].messageId;
    if (voted[markMessageId]) {
      marks[i].voted = true;
    } else {
      marks[i].voted = false;
    }
  }
  return marks;
};

exports.decorateCommentsWithVoteStatus = function(comments, votes) {
  var voted = {};
  for (var k = 0;k < votes.length;k++) {
    voted[votes[k].commentId] = true;
  }
  for (var i = 0;i < comments.length;i++) {
    var commentId = comments[i].commentId;
    if (voted[commentId]) {
      comments[i].voted = true;
    } else {
      comments[i].voted = false;
    }
  }
  return comments;
};

// The object we pull from the database has specific data (time/location)
// Here we create a new object that has data that is relevant to the user
exports.createMessageResponseObjects = function(marks, user) {
  var responseObjects = [];
  var responseObject;

  marks.forEach(function(mark) {
    responseObject = {
      timestamp: exports.getTimeElapsedSince(mark.timestamp),
      distance: exports.getDistanceFrom(mark, user),
      messageId: mark.messageId,
      messageString: mark.messageString,
      image: mark.image,
      imageH: mark.imageH,
      imageW: mark.imageW,
      score: mark.score,
      votes: mark['COUNT(distinct votes.id)'],
      comments: mark['COUNT(distinct comments.id)']
    };
    responseObjects.push(responseObject);
  });
  return responseObjects;
};

exports.createCommentResponseObjects = function(marks, user) {
  var responseObjects = [];
  var responseObject;

  marks.forEach(function(mark) {
    if (mark.id !== null) {
      responseObject = {
        timestamp: exports.getTimeElapsedSince(mark.timestamp),
        distance: exports.getDistanceFrom(mark, user),
        commentId: mark.commentId,
        commentString: mark.commentString,
        name: createIdentity(user.userToken, user.messageId),
        votes: mark['COUNT(votes.id)']
      };
      responseObjects.push(responseObject);
    }
  });
  
  return responseObjects;
};

var hashCode = function(string) {
  var hash = 0;
  if (string.length == 0) return hash;
  for (i = 0; i < string.length; i++) {
    char = string.charCodeAt(i);
    hash = ((hash<<5)-hash)+char;
    hash = hash & hash;
  }
  return hash;
};

var createIdentity = function(userToken, messageId) {
  var identities = themes[0];
  var i = Math.abs(hashCode(userToken + messageId)) % 20;
  return identities[i];
};

exports.saveImage = function(img64, callback) {
  if (img64) {
    var base64Data = img64.replace(/^data:image\/jpg;base64,/, '');
    var imgName = shortid.generate();
    var filename = 'server/images/' + imgName + '.jpg';

    fs.writeFile(filename, base64Data, 'base64', function(err) {
      if (err) console.log(err);
      if (callback) callback(imgName);
    });

    return imgName;
  } else {
    return null;
  }
};

exports.getImage = function(imgName) {
  return new Promise(function(resolve, reject) {
    var filename = 'server/images/' + imgName + '.jpg';
    fs.readFile(filename, function(err, img) {
      if (err) {
        reject(err);
      } else {
        resolve(img);
      }
    });
  });
};

exports.getLogStream = function() {
  console.log(__dirname);
  var accessLogStream = fs.createWriteStream(__dirname + '/../log/access.log', {flags: 'a'});
  return {stream: accessLogStream};
};

exports.createQueryParams = function(obj) {
  var params = [];

  for (var k in obj) {
    params.push(k);
    params.push(obj[k]);
  }

  return params;
};

exports.rejectPOST = exports.rejectGET = function(req, res, err) {
  res.sendStatus(400);
};

exports.resolveGET = function(req, res, success) {
  res.status(200);
  res.send(success);
};

exports.resolvePOST = function(req, res, success) {
  res.sendStatus(201);
};
<commit_msg>Fix identity creation to hash correct userToken<commit_after>var moment = require('moment');
var geolib = require('geolib');
var shortid = require('shortid');
var fs = require('fs');
var themes = require('./themes');

// Use moment.js to calculate how much time has elapsed since some prior time
exports.getTimeElapsedSince = function(time) {
  return moment(time).fromNow();
};

// Use geolib.js to calculate distance from the mark to the user
exports.getDistanceFrom = function(mark, user) {
  var dist = geolib.getDistance(
      {latitude: user.x, longitude: user.y},
      {latitude: mark.x, longitude: mark.y}
      );
  return dist + 'm';
};

exports.decorateMarksWithVoteStatus = function(marks, votes) {
  var voted = {};
  for (var k = 0;k < votes.length;k++) {
    voted[votes[k].messageId] = true;
  }
  for (var i = 0;i < marks.length;i++) {
    var markMessageId = marks[i].messageId;
    if (voted[markMessageId]) {
      marks[i].voted = true;
    } else {
      marks[i].voted = false;
    }
  }
  return marks;
};

exports.decorateCommentsWithVoteStatus = function(comments, votes) {
  var voted = {};
  for (var k = 0;k < votes.length;k++) {
    voted[votes[k].commentId] = true;
  }
  for (var i = 0;i < comments.length;i++) {
    var commentId = comments[i].commentId;
    if (voted[commentId]) {
      comments[i].voted = true;
    } else {
      comments[i].voted = false;
    }
  }
  return comments;
};

// The object we pull from the database has specific data (time/location)
// Here we create a new object that has data that is relevant to the user
exports.createMessageResponseObjects = function(marks, user) {
  var responseObjects = [];
  var responseObject;

  marks.forEach(function(mark) {
    responseObject = {
      timestamp: exports.getTimeElapsedSince(mark.timestamp),
      distance: exports.getDistanceFrom(mark, user),
      messageId: mark.messageId,
      messageString: mark.messageString,
      image: mark.image,
      imageH: mark.imageH,
      imageW: mark.imageW,
      score: mark.score,
      votes: mark['COUNT(distinct votes.id)'],
      comments: mark['COUNT(distinct comments.id)']
    };
    responseObjects.push(responseObject);
  });
  return responseObjects;
};

exports.createCommentResponseObjects = function(marks, user) {
  var responseObjects = [];
  var responseObject;

  marks.forEach(function(mark) {
    if (mark.id !== null) {
      responseObject = {
        timestamp: exports.getTimeElapsedSince(mark.timestamp),
        distance: exports.getDistanceFrom(mark, user),
        commentId: mark.commentId,
        commentString: mark.commentString,
        name: createIdentity(mark.userToken, user.messageId),
        votes: mark['COUNT(votes.id)']
      };
      responseObjects.push(responseObject);
    }
  });

  return responseObjects;
};

var hashCode = function(string) {
  var hash = 0;
  if (string.length == 0) return hash;
  for (i = 0; i < string.length; i++) {
    char = string.charCodeAt(i);
    hash = ((hash<<5)-hash)+char;
    hash = hash & hash;
  }
  return hash;
};

var createIdentity = function(userToken, messageId) {
  var identities = themes[0];
  var i = Math.abs(hashCode(userToken + messageId)) % (identities.length-1);
  return identities[i];
};

exports.saveImage = function(img64, callback) {
  if (img64) {
    var base64Data = img64.replace(/^data:image\/jpg;base64,/, '');
    var imgName = shortid.generate();
    var filename = 'server/images/' + imgName + '.jpg';

    fs.writeFile(filename, base64Data, 'base64', function(err) {
      if (err) console.log(err);
      if (callback) callback(imgName);
    });

    return imgName;
  } else {
    return null;
  }
};

exports.getImage = function(imgName) {
  return new Promise(function(resolve, reject) {
    var filename = 'server/images/' + imgName + '.jpg';
    fs.readFile(filename, function(err, img) {
      if (err) {
        reject(err);
      } else {
        resolve(img);
      }
    });
  });
};

exports.getLogStream = function() {
  console.log(__dirname);
  var accessLogStream = fs.createWriteStream(__dirname + '/../log/access.log', {flags: 'a'});
  return {stream: accessLogStream};
};

exports.createQueryParams = function(obj) {
  var params = [];

  for (var k in obj) {
    params.push(k);
    params.push(obj[k]);
  }

  return params;
};

exports.rejectPOST = exports.rejectGET = function(req, res, err) {
  res.sendStatus(400);
};

exports.resolveGET = function(req, res, success) {
  res.status(200);
  res.send(success);
};

exports.resolvePOST = function(req, res, success) {
  res.sendStatus(201);
};

{'fraction_non_alphanumeric': 0.1458766838243347, 'fraction_numerical': 0.007337641003175994, 'mean_word_length': 3.3218173213440605, 'pattern_counts': {'":': 0, '<': 17, '<?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': '3067261', 'n_tokens_mistral': 2997, 'n_tokens_neox': 2935, 'n_words': 686}