<commit_before>'use strict';
var _ = require('underscore');
var userUtils = require('cloud/v2/userUtils.js');

exports.examples = _examples;
exports.getCardsForSet = _getCardsForSet;
exports.getCardById = _getCardById;
exports.getCardsForSuggestion = _getCardsForSuggestion;

// Use Parse.Cloud.define to define as many cloud functions as you want.
function _examples(request, response) {
	Parse.Cloud.useMasterKey();
	var setId = request.params.id;
	_loadSetItems();

	function _loadSetItems() {
		var query = _buildCardsQuery();
		var SetObject = Parse.Object.extend('Set');
		var mockSet = new SetObject();
		mockSet.id = setId;
		query.equalTo('owner', mockSet);
		query.include('card.owner');
		query.find({
			success: _onSuggestionsLoaded,
			error: _onError
		});
	}

	function _onSuggestionsLoaded(setItems) {
		var card;
		var zeroSuggestions = [];
		var oneSuggestions = [];

		_.each(setItems, function stripData(item) {
			card = item.attributes.card;
			userUtils.stripPrivateData(card.attributes.owner);
			if (card.attributes.type === 0) {
				zeroSuggestions.push(card);
			} else {
				oneSuggestions.push(card);
			}
		});

		response.success({
			zero: zeroSuggestions,
			one: oneSuggestions
		});
	}

	function _onError(error) {
		console.log('examples Error');
		response.error(error);
	}

}

function _getCardsForSet(request, response) {
	// console.log('getCardsForSet');
		Parse.Cloud.useMasterKey();
	var setId = request.params.id;

	if(setId !== undefined) {
		// console.log('getCardsForSet fetchData');
		var query = _buildCardsQuery(setId);
		var SetObject = Parse.Object.extend('Set');
		var mockSet = new SetObject();
		mockSet.id = setId;
		query.equalTo('owner', mockSet);
		query.find({
			success: _onSuccess,
			error: _onError
		});
	} else {
		response.error('you must pass a set id to get');
	}

	function _onSuccess(setItems) {
		// console.log('setItems found:' + setItems.length);
		var card;
		_.each(setItems, function(item) {
			card = item.attributes.card;
			userUtils.stripPrivateData(card.attributes.owner);
		});
		response.success(setItems);
	}

	function _onError(error) {
		console.log('getAllSets Error');
		response.error(error);
	}

}

function _getCardById(request, response) {
	Parse.Cloud.useMasterKey();
	var cardId = request.params.id;
	if(cardId !== undefined) {
		// console.log('getCardsForSet fetchData');
		var SuggestionObject = Parse.Object.extend('Suggestion');
		var query = new Parse.Query(SuggestionObject);
		query.include('owner');
		query.get(request.params.id, {
			success: _onSuccess,
			error: _onError
		});
	} else {
		response.error('you must pass a set id to get');
	}

	function _onSuccess(card) {
		userUtils.stripPrivateData(card.attributes.owner);
		response.success(card);
	}

	function _onError(error) {
		console.log('getCard Error');
		response.error(error);
	}

}

function _getCardsForSuggestion(request, response) {
	// console.log('getCardsForSet');
		Parse.Cloud.useMasterKey();
	var suggestionId = request.params.id;
	var includeOwner = request.params.includeOwner;

	if(suggestionId !== undefined) {
		// console.log('getCardsForSet fetchData');
		var SuggestionObject = Parse.Object.extend('Suggestion');
		var mockSuggestion = new SuggestionObject();
		mockSuggestion.id = suggestionId;

		// actor query
		var query = _buildCardsQuery();
		query.equalTo('card', mockSuggestion);

		query.find({
			success: _onSuccess,
			error: _onError
		});
	} else {
		response.error('you must pass a suggestion id to get');
	}

	function _onSuccess(setItems) {
		// console.log('setItems found:' + setItems.length);
		var card;
		_.each(setItems, function(item) {
			card = item.attributes.card;
			userUtils.stripPrivateData(card.attributes.owner);
		});
		response.success(setItems);
	}<commit_msg>Revert "removed owner from set fetch"<commit_after>'use strict';
var _ = require('underscore');
var userUtils = require('cloud/v2/userUtils.js');

exports.examples = _examples;
exports.getCardsForSet = _getCardsForSet;
exports.getCardById = _getCardById;
exports.getCardsForSuggestion = _getCardsForSuggestion;

// Use Parse.Cloud.define to define as many cloud functions as you want.
function _examples(request, response) {
	Parse.Cloud.useMasterKey();
	var setId = request.params.id;
	_loadSetItems();

	function _loadSetItems() {
		var query = _buildCardsQuery();
		var SetObject = Parse.Object.extend('Set');
		var mockSet = new SetObject();
		mockSet.id = setId;
		query.equalTo('owner', mockSet);
		query.include('card.owner');
		query.find({
			success: _onSuggestionsLoaded,
			error: _onError
		});
	}

	function _onSuggestionsLoaded(setItems) {
		var card;
		var zeroSuggestions = [];
		var oneSuggestions = [];

		_.each(setItems, function stripData(item) {
			card = item.attributes.card;
			userUtils.stripPrivateData(card.attributes.owner);
			if (card.attributes.type === 0) {
				zeroSuggestions.push(card);
			} else {
				oneSuggestions.push(card);
			}
		});

		response.success({
			zero: zeroSuggestions,
			one: oneSuggestions
		});
	}

	function _onError(error) {
		console.log('examples Error');
		response.error(error);
	}

}

function _getCardsForSet(request, response) {
	// console.log('getCardsForSet');
		Parse.Cloud.useMasterKey();
	var setId = request.params.id;
	var includeOwner = request.params.includeOwner;

	if(setId !== undefined) {
		// console.log('getCardsForSet fetchData');
		var query = _buildCardsQuery(setId);
		var SetObject = Parse.Object.extend('Set');
		var mockSet = new SetObject();
		mockSet.id = setId;
		query.equalTo('owner', mockSet);
		if(includeOwner === true) {
			query.include('card.owner');
		} else {
			query.include('card');
		}
		query.find({
			success: _onSuccess,
			error: _onError
		});
	} else {
		response.error('you must pass a set id to get');
	}

	function _onSuccess(setItems) {
		// console.log('setItems found:' + setItems.length);
		var card;
		_.each(setItems, function(item) {
			card = item.attributes.card;
			userUtils.stripPrivateData(card.attributes.owner);
		});
		response.success(setItems);
	}

	function _onError(error) {
		console.log('getAllSets Error');
		response.error(error);
	}

}

function _getCardById(request, response) {
	Parse.Cloud.useMasterKey();
	var cardId = request.params.id;
	if(cardId !== undefined) {
		// console.log('getCardsForSet fetchData');
		var SuggestionObject = Parse.Object.extend('Suggestion');
		var query = new Parse.Query(SuggestionObject);
		query.include('owner');
		query.get(request.params.id, {
			success: _onSuccess,
			error: _onError
		});
	} else {
		response.error('you must pass a set id to get');
	}

	function _onSuccess(card) {
		userUtils.stripPrivateData(card.attributes.owner);
		response.success(card);
	}

	function _onError(error) {
		console.log('getCard Error');
		response.error(error);
	}

}

function _getCardsForSuggestion(request, response) {
	// console.log('getCardsForSet');
		Parse.Cloud.useMasterKey();
	var suggestionId = request.params.id;
	var includeOwner = request.params.includeOwner;

	if(suggestionId !== undefined) {
		// console.log('getCardsForSet fetchData');
		var SuggestionObject = Parse.Object.extend('Suggestion');
		var mockSuggestion = new SuggestionObject();
		mockSuggestion.id = suggestionId;

		// actor query
		var query = _buildCardsQuery();
		query.equalTo('card', mockSuggestion);
		includeOwner ? query.include('card.owner') : query.include('card');

		query.find({
			success: _onSuccess,
			error: _onError
		});
	} else {
		response.error('you must pass a suggestion id to get');
	}

	function _onSuccess(setItems) {
		// console.log('setItems found:' + setItems.length);
		var card;
		_.each(setItems, function(item) {
			card = item.attributes.card;
			userUtils.stripPrivateData(card.attributes.owner);
		});
		response.success(setItems);
	}
{'fraction_non_alphanumeric': 0.1402657128257537, 'fraction_numerical': 0.000510986203372509, 'mean_word_length': 4.384456671251719, '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': '5972416', 'n_tokens_mistral': 2855, 'n_tokens_neox': 2582, 'n_words': 504}