<commit_before>// Public
Parse.Cloud.beforeSave("Memory", function(request, response) {
	_incrementPosition(request.object, response);
});

Parse.Cloud.define("randomMemory", function(request, response) {
	_lastMemoryQuery().find({
		success: function(objects) {
			var lastPosition = _positionForMemory(objects[0]);

			if (lastPosition == 1) {
				response.success();
			}

			var randomPosition = _randomPosition(lastPosition);
			
			_memoryAtPositionQuery(randomPosition).find({
				success: function(objects) {
					response.success(objects[0]);
				},

				error: function(error) {
					response.error(error);
				}
			});
		},<commit_msg>Send mail on save.<commit_after>// Public
Parse.Cloud.beforeSave("Memory", function(request, response) {
	_incrementPosition(request.object, response);
});

Parse.Cloud.afterSave("Memory", function(request) {
	_mailMemory(request.object);
});

function _mailMemory(memory, response) {
	var Mandrill = require('mandrill');
	Mandrill.initialize('cyYOeRQuFBaaEo5s8bSCSw');

	Mandrill.sendEmail({
	  message: _mailMessage(memory)
	}, {
	  success: function(httpResponse) {
	  	console.log(httpResponse);
	  },
	  error: function(httpResponse) {
	  	console.log(httpResponse);
	    response.error(httpResponse);
	  }
	});
}

function _mailMessage(memory) {
	var user = Parse.User.current();
	return {
    text: memory.get("thoughts"),
    subject: "Butterfly Daily Memories",
    from_email: "<EMAIL>",
    from_name: "Butterly Memories App",
    to: [{email: user.get("username")}]
  }
}


Parse.Cloud.define("randomMemory", function(request, response) {
	_lastMemoryQuery().find({
		success: function(objects) {
			var lastPosition = _positionForMemory(objects[0]);

			if (lastPosition == 1) {
				response.success();
			}

			var randomPosition = _randomPosition(lastPosition);
			
			_memoryAtPositionQuery(randomPosition).find({
				success: function(objects) {
					response.success(objects[0]);
				},

				error: function(error) {
					response.error(error);
				}
			});
		},
{'fraction_non_alphanumeric': 0.14758144126357353, 'fraction_numerical': 0.003948667324777887, 'mean_word_length': 4.419786096256685, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '2721725', 'n_tokens_mistral': 731, 'n_tokens_neox': 627, 'n_words': 104}