<commit_before>"use strict";

var mongoose = require('mongoose');

var User = mongoose.model('User');
const crypto = require('crypto');
const words = require('../../wordJson/wordData');

// Controller logic for searching a user based on a query string.
module.exports.findUserByQuery = function(req, res) {
    // Check if the user was authenticated
    if (req.user) {
        // set queryString variable to be from the requested query string.
        var queryString = req.query.queryString;
        // Check if queryString was provided
        if (!queryString) {
          res.status(400).json({'error': 'queryString required in query'});
          return;
        }
        // search based on the queryString provided
        User.search(queryString, function(err, result) {
            if (err) {
              // if there was an error respond with status 500
              // and the err information.
              res.status(500).json(err);
            } else if (!result || result.length === 0) {
              // if the result wasn't found respond with status 404
              // and the information stating results were not found.
              res.status(404).json({'error' : 'No Results Found.'});
            } else {
              // the results were found and responding with it as a list of
              // json object.
              res.status(200).json(result);
            }
          });
        } else {
        // Respond with Unauthorized access.
        res.status(401).json({'error': 'Access Not Authorized.'});
    }
};

// Controller logic for finding a user by their facebook Id
module.exports.findUserById = function(req, res) {
    // Check if the user was authenticated
    if (req.user) {
        var facebookID = req.params.id;

        // check if the id was passed in the parameter.
        if (!facebookID) {
            res.status(404).json({'error': 'ID is required to find user.'});
            return;
        }

        // Find the user based on their facebookID and return the data
        // stored in the database.
        User.findOne({'facebook.id' : facebookID}, function(err, user) {
            if (!user) {
                // if the user wasn't found respond with status 404
                // and the information stating User was not found.
                res.status(404).json({'error': 'User was not found.'});
            } else if (err) {
                // if there was an error respond with status 500
                // and the err information.
                res.status(500).json(err);
            } else {
                // if there wasn't an error and the user was found.
                // respond with status 200 and user's information
                res.status(200).json(user);
            }
        });
    } else {
        // Respond with Unauthorized access.
        res.status(401).json({'error': 'Access Not Authorized.'});
    }
};

module.exports.getFriend = function(req, res) {
    if (req.user) {
        // Create a new promise to find all friends.
        var findFriend = new Promise((resolve, reject) => {
            var friends = [];
            if (req.user.friends.length === 0) {
                // return empty list since no friends :(|
                resolve(friends);
            }
            req.user.friends.forEach((friendId) => {
                // Find friends and add them to the list.
                User.findOne({'facebook.id' : friendId})
                .select('name profilePhotoURL')
                .exec(function(err, friend) {
                    if (err) {
                        // error finding friend.
                        res.status(500).json(err);
                        reject("Error");
                    } else if (friend) {
                        // add friend to list.
                        friends.push(friend);
                        if (friends.length === req.user.friends.length) {
                            // return the list of friends.
                            resolve(friends);
                        }
                    }
                });
            });
        });
        // Create a new promise to find pending friends.
        var findPending = new Promise((resolve, reject) => {
            var friends = [];
            if (req.user.pendingFriends.length === 0) {
                // return empty list since no pending friends :(|
                resolve(friends);
            }
            req.user.pendingFriends.forEach((friendId) => {
                // Find pending friends add them to the list.
                User.findOne({'facebook.id' : friendId})
                .select('name profilePhotoURL')
                .exec(function(err, friend) {
                    if (err) {
                        // error finding friend.
                        res.status(500).json(err);
                        reject("Error");
                    } else if (friend) {
                        // add pending friend to list.
                        friends.push(friend);
                        if (friends.length === req.user.pendingFriends.length) {
                            // return the list of pending friends.
                            resolve(friends);
                        }
                    }
                });
            });
        });
        // Run all promises first then respond with the data.
        Promise.all([findFriend, findPending]).then((data) => {
            res.status(200).json({
                'friends' : data[0],
                'pending friends' : data[1]
            });
        });

    } else {
        // Respond with Unauthorized access.
        res.status(401).json({'error': 'Access Not Authorized.'});
    }
};

module.exports.putPublicKey = function(req, res) {
    // Check if the user was authenticated
    if (req.user) {
        var thisUser = req.user;
        var publicKey = req.body.publicKey;
        // check if the publicKey was sent through the body.
        if (!publicKey) {
            res.status(400).json({'error' : 'publicKey required in body.'});
            return;
        }
        // update the hash with the given publicKey
        // Can't recylcle hash objects, declaring here
        var hash = crypto.createHash('sha1');
        hash.update(publicKey);
        // splits every 1-2 characters into a hex digest.
        var digests = hash.digest('hex').toUpperCase().match(/.{1,2}/g);
        // creates the readable key.
        for (var i = 0; i < digests.length; i++) {
            digests[i] = words[digests[i]];
        }
        // sets the public and readable key.
        thisUser.publicKey.keys.pgp = publicKey;
        thisUser.publicKey.keys.readable = digests.join(" ");
        thisUser.save(function(err, user) {
            if (err) {
                res.status(500).json(err);
            } else {
                res.status(201).json(user);
            }
        });
    } else {
        // Respond with Unauthorized access.
        res.status(401).json({'error': 'Access Not Authorized.'});
    }
};

module.exports.getPublicKey = function(req, res) {
    //  Check if the user was authenticated
    if (req.user) {
        var otherUserId = req.params.id;
        if (!otherUserId) {
            res.status(400).json({'error': 'OtherUserID required in parameter.'});
            return;
        }
        // Find a user based on the queried id.
        User.findOne({'facebook.id': otherUserId}, function(err, user) {
            var publicKey = user.publicKey.keys.pgp;
            if (err) {
                res.status(500).json(err);
            } else if (!publicKey) {
                res.status(404).json({'error' : 'Public key not set for the queried user.'});
            } else {
                // update the hash with their publickey
                // Can't recylcle hash objects, declaring here
                var hash = crypto.createHash('sha1');
                hash.update(publicKey);
                var digests = hash.digest('hex').toUpperCase().match(/.{1,2}/g);
                for (var i = 0; i < digests.length; i++) {
                    digests[i] = words[digests[i]];
                }
                // respond with the readable and pgp key
                res.status(200).json(
                    {
                        'publicKey' : publicKey,
                        'readableKey' : digests.join(" ")
                    }
                );
            }
        });
    } else {
        // Respond with Unauthorized access.
        res.status(401).json({'error': 'Access Not Authorized.'});
    }
}
<commit_msg>Added facebook.id and _id to friends<commit_after>"use strict";

var mongoose = require('mongoose');

var User = mongoose.model('User');
const crypto = require('crypto');
const words = require('../../wordJson/wordData');

// Controller logic for searching a user based on a query string.
module.exports.findUserByQuery = function(req, res) {
    // Check if the user was authenticated
    if (req.user) {
        // set queryString variable to be from the requested query string.
        var queryString = req.query.queryString;
        // Check if queryString was provided
        if (!queryString) {
          res.status(400).json({'error': 'queryString required in query'});
          return;
        }
        // search based on the queryString provided
        User.search(queryString, function(err, result) {
            if (err) {
              // if there was an error respond with status 500
              // and the err information.
              res.status(500).json(err);
            } else if (!result || result.length === 0) {
              // if the result wasn't found respond with status 404
              // and the information stating results were not found.
              res.status(404).json({'error' : 'No Results Found.'});
            } else {
              // the results were found and responding with it as a list of
              // json object.
              res.status(200).json(result);
            }
          });
        } else {
        // Respond with Unauthorized access.
        res.status(401).json({'error': 'Access Not Authorized.'});
    }
};

// Controller logic for finding a user by their facebook Id
module.exports.findUserById = function(req, res) {
    // Check if the user was authenticated
    if (req.user) {
        var facebookID = req.params.id;

        // check if the id was passed in the parameter.
        if (!facebookID) {
            res.status(404).json({'error': 'ID is required to find user.'});
            return;
        }

        // Find the user based on their facebookID and return the data
        // stored in the database.
        User.findOne({'facebook.id' : facebookID}, function(err, user) {
            if (!user) {
                // if the user wasn't found respond with status 404
                // and the information stating User was not found.
                res.status(404).json({'error': 'User was not found.'});
            } else if (err) {
                // if there was an error respond with status 500
                // and the err information.
                res.status(500).json(err);
            } else {
                // if there wasn't an error and the user was found.
                // respond with status 200 and user's information
                res.status(200).json(user);
            }
        });
    } else {
        // Respond with Unauthorized access.
        res.status(401).json({'error': 'Access Not Authorized.'});
    }
};

module.exports.getFriend = function(req, res) {
    if (req.user) {
        // Create a new promise to find all friends.
        var findFriend = new Promise((resolve, reject) => {
            var friends = [];
            if (req.user.friends.length === 0) {
                // return empty list since no friends :(|
                resolve(friends);
            }
            req.user.friends.forEach((friendId) => {
                // Find friends and add them to the list.
                User.findOne({'facebook.id' : friendId})
                .select('name profilePhotoURL _id facebook.id')
                .exec(function(err, friend) {
                    if (err) {
                        // error finding friend.
                        res.status(500).json(err);
                        reject("Error");
                    } else if (friend) {
                        // add friend to list.
                        friends.push(friend);
                        if (friends.length === req.user.friends.length) {
                            // return the list of friends.
                            resolve(friends);
                        }
                    }
                });
            });
        });
        // Create a new promise to find pending friends.
        var findPending = new Promise((resolve, reject) => {
            var friends = [];
            if (req.user.pendingFriends.length === 0) {
                // return empty list since no pending friends :(|
                resolve(friends);
            }
            req.user.pendingFriends.forEach((friendId) => {
                // Find pending friends add them to the list.
                User.findOne({'facebook.id' : friendId})
                .select('name profilePhotoURL id facebook.id')
                .exec(function(err, friend) {
                    if (err) {
                        // error finding friend.
                        res.status(500).json(err);
                        reject("Error");
                    } else if (friend) {
                        // add pending friend to list.
                        friends.push(friend);
                        if (friends.length === req.user.pendingFriends.length) {
                            // return the list of pending friends.
                            resolve(friends);
                        }
                    }
                });
            });
        });
        // Run all promises first then respond with the data.
        Promise.all([findFriend, findPending]).then((data) => {
            res.status(200).json({
                'friends' : data[0],
                'pending friends' : data[1]
            });
        });

    } else {
        // Respond with Unauthorized access.
        res.status(401).json({'error': 'Access Not Authorized.'});
    }
};

module.exports.putPublicKey = function(req, res) {
    // Check if the user was authenticated
    if (req.user) {
        var thisUser = req.user;
        var publicKey = req.body.publicKey;
        // check if the publicKey was sent through the body.
        if (!publicKey) {
            res.status(400).json({'error' : 'publicKey required in body.'});
            return;
        }
        // update the hash with the given publicKey
        // Can't recylcle hash objects, declaring here
        var hash = crypto.createHash('sha1');
        hash.update(publicKey);
        // splits every 1-2 characters into a hex digest.
        var digests = hash.digest('hex').toUpperCase().match(/.{1,2}/g);
        // creates the readable key.
        for (var i = 0; i < digests.length; i++) {
            digests[i] = words[digests[i]];
        }
        // sets the public and readable key.
        thisUser.publicKey.keys.pgp = publicKey;
        thisUser.publicKey.keys.readable = digests.join(" ");
        thisUser.save(function(err, user) {
            if (err) {
                res.status(500).json(err);
            } else {
                res.status(201).json(user);
            }
        });
    } else {
        // Respond with Unauthorized access.
        res.status(401).json({'error': 'Access Not Authorized.'});
    }
};

module.exports.getPublicKey = function(req, res) {
    //  Check if the user was authenticated
    if (req.user) {
        var otherUserId = req.params.id;
        if (!otherUserId) {
            res.status(400).json({'error': 'OtherUserID required in parameter.'});
            return;
        }
        // Find a user based on the queried id.
        User.findOne({'facebook.id': otherUserId}, function(err, user) {
            var publicKey = user.publicKey.keys.pgp;
            if (err) {
                res.status(500).json(err);
            } else if (!publicKey) {
                res.status(404).json({'error' : 'Public key not set for the queried user.'});
            } else {
                // update the hash with their publickey
                // Can't recylcle hash objects, declaring here
                var hash = crypto.createHash('sha1');
                hash.update(publicKey);
                var digests = hash.digest('hex').toUpperCase().match(/.{1,2}/g);
                for (var i = 0; i < digests.length; i++) {
                    digests[i] = words[digests[i]];
                }
                // respond with the readable and pgp key
                res.status(200).json(
                    {
                        'publicKey' : publicKey,
                        'readableKey' : digests.join(" ")
                    }
                );
            }
        });
    } else {
        // Respond with Unauthorized access.
        res.status(401).json({'error': 'Access Not Authorized.'});
    }
}

{'fraction_non_alphanumeric': 0.11733688724069996, 'fraction_numerical': 0.01147294008575733, 'mean_word_length': 1.4047652222377038, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '132966', 'n_tokens_mistral': 4625, 'n_tokens_neox': 4289, 'n_words': 1277}