<commit_before>'use strict';

/**
 * @file User management functions
 * @module users
 */
const _ = require('underscore');
const dates = require(__dirname + '/dates.js');
const moment = require('moment');

/**
 * Get data about user
 * @function getUser
 *
 * @param {Object} db - Database connection
 * @param {string} user - Username
 * @param {getUserCallback} callback - Callback
 */

/**
 * Callback after user id is retrieved
 * @callback getUserCallback
 *
 * @param {Object} err - Null if success, error object if failure
 * @param {Boolean} isUser - True if there is a valid user, false if not. Null if error.
 * @param {Object} userDoc - Everything in the user's document. Null if error or no valid user.
 */

function getUser(db, user, callback) {
	if(typeof callback !== 'function') return;

	if(typeof db !== 'object') {
		callback(new Error('Invalid database connection!'), null, null);
		return;
	}
	if(typeof user !== 'string') {
		callback(new Error('Invalid username!'), null, null);
		return;
	}

	const userdata = db.collection('users');
	// Query database to find possible user
	userdata.find({ user }).toArray((err, docs) => {
		if(err) {
			callback(new Error('There was a problem querying the database!'), null, null);
			return;
		}
		if(docs.length === 0) {
			callback(null, false, null);
		} else {
			callback(null, true, docs[0]);
		}

	});
}

/**
 * Retrieves basic information about a specific user
 * @function getInfo
 *
 * @param {Object} db - Database connection
 * @param {string} user - Username
 * @param {Boolean} privateInfo - Whether to include sensitive information such as canvasURL and portalURL. Set to true if only the user is viewing it. Defaults to false.
 * @param {getInfoCallback} callback - Callback
 */

/**
 * Returns basic information about the user
 * @callback getInfoCallback
 *
 * @param {Object} err - Null if success, error object if failure.
 * @param {Object} userInfo - Object containing information about the user. Null if error.
 */

function getInfo(db, user, privateInfo, callback) {
	if(typeof callback !== 'function') return;

	if(typeof db !== 'object') {
		callback(new Error('Invalid database connection!'), null);
		return;
	}
	if(typeof privateInfo !== 'boolean') {
		privateInfo = false;
	}

	getUser(db, user, (err, isUser, userDoc) => {
		if(err) {
			callback(err, null);
			return;
		}
		if(!isUser) {
			callback(new Error('User doesn\'t exist!'), null);
			return;
		}

		// Create userInfo object and manually move values from database.
		// We don't want something accidentally being released to user.
		const userInfo = {};
		userInfo.user      = userDoc['user'];
		userInfo.password  = '<PASSWORD>'; /** @TODO: Fix glitch? Shows up as ******* for me. */
		userInfo.firstName = userDoc['firstName'];
		userInfo.lastName  = userDoc['lastName'];
		userInfo.gradYear  = userDoc['gradYear'];
		userInfo.grade     = gradYearToGrade(userInfo['gradYear']);
		userInfo.school    = gradeToSchool(userInfo['grade']);

		if(privateInfo) {
			if(typeof userDoc['canvasURL'] === 'string') {
				userInfo.canvasURL = userDoc['canvasURL'];
			} else {
				userInfo.canvasURL = null;
			}

			// Legacy Blackbaud portal URL
			if(typeof userDoc['portalURL'] === 'string') {
				userInfo.portalURL = userDoc['portalURL'];
			} else {
				userInfo.portalURL = null;
			}
<commit_msg>Add comment for new urls<commit_after>'use strict';

/**
 * @file User management functions
 * @module users
 */
const _ = require('underscore');
const dates = require(__dirname + '/dates.js');
const moment = require('moment');

/**
 * Get data about user
 * @function getUser
 *
 * @param {Object} db - Database connection
 * @param {string} user - Username
 * @param {getUserCallback} callback - Callback
 */

/**
 * Callback after user id is retrieved
 * @callback getUserCallback
 *
 * @param {Object} err - Null if success, error object if failure
 * @param {Boolean} isUser - True if there is a valid user, false if not. Null if error.
 * @param {Object} userDoc - Everything in the user's document. Null if error or no valid user.
 */

function getUser(db, user, callback) {
	if(typeof callback !== 'function') return;

	if(typeof db !== 'object') {
		callback(new Error('Invalid database connection!'), null, null);
		return;
	}
	if(typeof user !== 'string') {
		callback(new Error('Invalid username!'), null, null);
		return;
	}

	const userdata = db.collection('users');
	// Query database to find possible user
	userdata.find({ user }).toArray((err, docs) => {
		if(err) {
			callback(new Error('There was a problem querying the database!'), null, null);
			return;
		}
		if(docs.length === 0) {
			callback(null, false, null);
		} else {
			callback(null, true, docs[0]);
		}

	});
}

/**
 * Retrieves basic information about a specific user
 * @function getInfo
 *
 * @param {Object} db - Database connection
 * @param {string} user - Username
 * @param {Boolean} privateInfo - Whether to include sensitive information such as canvasURL and portalURL. Set to true if only the user is viewing it. Defaults to false.
 * @param {getInfoCallback} callback - Callback
 */

/**
 * Returns basic information about the user
 * @callback getInfoCallback
 *
 * @param {Object} err - Null if success, error object if failure.
 * @param {Object} userInfo - Object containing information about the user. Null if error.
 */

function getInfo(db, user, privateInfo, callback) {
	if(typeof callback !== 'function') return;

	if(typeof db !== 'object') {
		callback(new Error('Invalid database connection!'), null);
		return;
	}
	if(typeof privateInfo !== 'boolean') {
		privateInfo = false;
	}

	getUser(db, user, (err, isUser, userDoc) => {
		if(err) {
			callback(err, null);
			return;
		}
		if(!isUser) {
			callback(new Error('User doesn\'t exist!'), null);
			return;
		}

		// Create userInfo object and manually move values from database.
		// We don't want something accidentally being released to user.
		const userInfo = {};
		userInfo.user      = userDoc['user'];
		userInfo.password  = '<PASSWORD>'; /** @TODO: Fix glitch? Shows up as ******* for me. */
		userInfo.firstName = userDoc['firstName'];
		userInfo.lastName  = userDoc['lastName'];
		userInfo.gradYear  = userDoc['gradYear'];
		userInfo.grade     = gradYearToGrade(userInfo['gradYear']);
		userInfo.school    = gradeToSchool(userInfo['grade']);

		if(privateInfo) {
			if(typeof userDoc['canvasURL'] === 'string') {
				userInfo.canvasURL = userDoc['canvasURL'];
			} else {
				userInfo.canvasURL = null;
			}

			// Legacy Blackbaud portal URL
			if(typeof userDoc['portalURL'] === 'string') {
				userInfo.portalURL = userDoc['portalURL'];
			} else {
				userInfo.portalURL = null;
			}

			// New Veracross portal URLs
{'fraction_non_alphanumeric': 0.14173461823573016, 'fraction_numerical': 0.0005930318754633062, 'mean_word_length': 3.7742392073602264, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '462840', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2025, 'n_words': 660}