<commit_before>"use strict";

var request = require('request');
var async = require('async')
var Blog = require('../models/blog').Blog;
var BlogController = require('./blog');
var Blogger = require('../models/blogger').Blogger;

// Callback of form done(profile, error);
exports.getUserProfile = function(passportUser, req, done) {
	if (passportUser.userProvider && passportUser.userId) {
		// This user is registered (so their profile is actually already in req.user, but we need to attach some blogs)
        getBlogs({userId: passportUser.userId, userProvider: passportUser.userProvider}, req, function(page, error) {
            if (error) {
                console.log("[ERROR] %j", error);
                done(null, null, error);
            }
            else {
                done(passportUser, page, null);
            }
        });
	}
	else {
		// Not a registered user
		done(null, null, null);
	}
};

exports.getProfileByVanityUrl = function(vanityUrl, req, done) {
	Blogger.findOne({vanityUrl: vanityUrl}, {emailAddress: 0, __v: 0}, function(error, profile) {
		if (error) {
			console.log("[ERROR] %j", error);
			done(null, null, error);
		}
		else if (profile) {
            // Attach blogs to user to create full profile
            getBlogs({userId: profile.userId, userProvider: profile.userProvider}, req, function(page, error) {
                if (error) {
                    console.log("[ERROR] %j", error);
                    done(null, null, error);
                }
                else {
                    done(profile, page, null);
                }
            });
		}
        else {
            // Not a registered user
            done(null, null, null);
        }
	});
};

exports.getAllProfiles = function(validatedOnly, done) {
	var options = {};
	if (validatedOnly) {
		options.validated = true;
	}
	
    Blogger.find(options, {emailAddress: 0, __v: 0}, function(error, allBloggers) {
        allBloggers.sort(function(a,b) {
           return a.firstName - b.firstName;
        });
		done(allBloggers, error);
	});
};

function isVanityUrlTaken(vanityUrl, done) {
    Blogger.findOne({vanityUrl: vanityUrl}, function(error, profile) {
        if (error) {
            console.log("[ERROR] %j", error);
            done(true, error);
        }
        else if (profile) {
            done(true, null);
        }
        else {
            done(false, null);
        }
    });
};

function getBlogs(bloggerQuery, req, done) {
    BlogController.getPaginatedBlogs(bloggerQuery, req, function(blogs, pageNumber, showBack, showNext, error) {
        var page = {
            blogs: blogs,
            pageNumber: pageNumber,
            hasLess: showBack,
            hasMore: showNext<commit_msg>Moved sort to find function.<commit_after>"use strict";

var request = require('request');
var async = require('async')
var Blog = require('../models/blog').Blog;
var BlogController = require('./blog');
var Blogger = require('../models/blogger').Blogger;

// Callback of form done(profile, error);
exports.getUserProfile = function(passportUser, req, done) {
	if (passportUser.userProvider && passportUser.userId) {
		// This user is registered (so their profile is actually already in req.user, but we need to attach some blogs)
        getBlogs({userId: passportUser.userId, userProvider: passportUser.userProvider}, req, function(page, error) {
            if (error) {
                console.log("[ERROR] %j", error);
                done(null, null, error);
            }
            else {
                done(passportUser, page, null);
            }
        });
	}
	else {
		// Not a registered user
		done(null, null, null);
	}
};

exports.getProfileByVanityUrl = function(vanityUrl, req, done) {
	Blogger.findOne({vanityUrl: vanityUrl}, {emailAddress: 0, __v: 0}, function(error, profile) {
		if (error) {
			console.log("[ERROR] %j", error);
			done(null, null, error);
		}
		else if (profile) {
            // Attach blogs to user to create full profile
            getBlogs({userId: profile.userId, userProvider: profile.userProvider}, req, function(page, error) {
                if (error) {
                    console.log("[ERROR] %j", error);
                    done(null, null, error);
                }
                else {
                    done(profile, page, null);
                }
            });
		}
        else {
            // Not a registered user
            done(null, null, null);
        }
	});
};

exports.getAllProfiles = function(validatedOnly, done) {
	var options = {};
	if (validatedOnly) {
		options.validated = true;
	}
	
    Blogger.find(options, {emailAddress: 0, __v: 0}, {sort:{firstName: 'asc'}}, function(error, allBloggers) {
		done(allBloggers, error);
	});
};

function isVanityUrlTaken(vanityUrl, done) {
    Blogger.findOne({vanityUrl: vanityUrl}, function(error, profile) {
        if (error) {
            console.log("[ERROR] %j", error);
            done(true, error);
        }
        else if (profile) {
            done(true, null);
        }
        else {
            done(false, null);
        }
    });
};

function getBlogs(bloggerQuery, req, done) {
    BlogController.getPaginatedBlogs(bloggerQuery, req, function(blogs, pageNumber, showBack, showNext, error) {
        var page = {
            blogs: blogs,
            pageNumber: pageNumber,
            hasLess: showBack,
            hasMore: showNext
{'fraction_non_alphanumeric': 0.13275480059084194, 'fraction_numerical': 0.0014771048744460858, 'mean_word_length': 2.2091232227488153, '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': '2604903', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1580, 'n_words': 397}