<commit_before>var UserModel    = require('../model/people/user.js').UserModel
  , FBTokenModel = require('../model/auth/fbToken.js').FBTokenModel
  , error        = require('./error.js')
  , fb           = require('../fb.js')
  , paginate     = require('../paginate.js');

var logError = function(functionName, failure, err) {
  error.log('user', functionName, failure, err);
};

var getUser = function(req, res) {
  UserModel.findOne({
    facebookId: req.param('facebook_id')
  }, function(err, user) {
    if (err) {
      logError('getUser', 'UserModel.findOne', err);
      return error.server(res);
    }

    return res.json(user);
  });
};

var getFriends = function(req, res) {
  var facebookId = req.param('facebook_id');

  var conditions = {
    facebookId: facebookId
  };

  if (!paginate(req, res, conditions)) {
    return;
  }

  FBTokenModel.findOne({
    facebookId: facebookId
  }, function(err, fbToken) {
    if (err) {
      logError('getFriends', 'FBTokenModel.findOne', err);
      return error.server(res);
    }

    if (fb.expired(fbToken)) {
      return error.unauthorized(res, 'Access token has expired, please log in again', 2);
    }

    if (fb.needPermissions(fbToken, 'user_friends')) {
      return error.unauthorized(res, 'Permission required to access user friends', 1);
    }

    fb.friends(fbToken, function(err, friends, totalCount, errMessageObj) {
      if (err) {
        logError('getFriends', 'fb.friends', err);
        return error.server(res);
      }

      if (!friends) {
        return error.server(res);
      }

      var name = req.query.name;

      if (name) {
        friends = friends.filter(function(element) {
          var fullFriendName = element['name'];
          return fullFriendName.indexOf(name) > -1;
        });
      }

      friends = friends.sort(function(a, b) {
        if (a['name'] < b['name']) {
          return -1;
        } else if (a['name'] > b['name']) {
          return 1;
        }
        return 0;
      }).slice(req.query.offset, req.query.offset + req.query.limit);

      var JSON = {
        friends: friends
      }
      if (totalCount) {
        JSON['total_count'] = totalCount;
      }
      return res.json(JSON);
    });<commit_msg>Extract ascending sort function for reuse<commit_after>var UserModel    = require('../model/people/user.js').UserModel
  , FBTokenModel = require('../model/auth/fbToken.js').FBTokenModel
  , error        = require('./error.js')
  , fb           = require('../fb.js')
  , paginate     = require('../paginate.js');

var ascending = function(key) {
  return function(a, b) {
    if (a[key] < b[key]) {
      return -1;
    } else if (a[key] > b[key]) {
      return 1;
    }
    return 0;
  };
};

var logError = function(functionName, failure, err) {
  error.log('user', functionName, failure, err);
};

var getUser = function(req, res) {
  UserModel.findOne({
    facebookId: req.param('facebook_id')
  }, function(err, user) {
    if (err) {
      logError('getUser', 'UserModel.findOne', err);
      return error.server(res);
    }

    return res.json(user);
  });
};

var getFriends = function(req, res) {
  var facebookId = req.param('facebook_id');

  var conditions = {
    facebookId: facebookId
  };

  if (!paginate(req, res, conditions)) {
    return;
  }

  FBTokenModel.findOne({
    facebookId: facebookId
  }, function(err, fbToken) {
    if (err) {
      logError('getFriends', 'FBTokenModel.findOne', err);
      return error.server(res);
    }

    if (fb.expired(fbToken)) {
      return error.unauthorized(res, 'Access token has expired, please log in again', 2);
    }

    if (fb.needPermissions(fbToken, 'user_friends')) {
      return error.unauthorized(res, 'Permission required to access user friends', 1);
    }

    fb.friends(fbToken, function(err, friends, totalCount, errMessageObj) {
      if (err) {
        logError('getFriends', 'fb.friends', err);
        return error.server(res);
      }

      if (!friends) {
        return error.server(res);
      }

      var name = req.query.name;

      if (name) {
        friends = friends.filter(function(element) {
          var fullFriendName = element['name'];
          return fullFriendName.indexOf(name) > -1;
        });
      }

      friends = friends.sort(ascending('name'))
                       .slice(req.query.offset, req.query.offset + req.query.limit);

      var JSON = {
        friends: friends
      };

      if (totalCount) {
        JSON['total_count'] = totalCount;
      }

      return res.json(JSON);
    });
{'fraction_non_alphanumeric': 0.15374478365912586, 'fraction_numerical': 0.002635624862727872, 'mean_word_length': 2.473684210526316, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4919929', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1433, 'n_words': 308}