Use findone in controllers/story

also fixes some linting issues
closes #469
This commit is contained in:
Berkeley Martinez
2015-05-21 09:15:16 -07:00
parent 0bd4d350f4
commit 92f4c7a41a

View File

@ -128,7 +128,8 @@ exports.returnIndividualStory = function(req, res, next) {
if (story.length < 1) { if (story.length < 1) {
req.flash('errors', { req.flash('errors', {
msg: "404: We couldn't find a story with that name. Please double check the name." msg: "404: We couldn't find a story with that name. " +
"Please double check the name."
}); });
return res.redirect('/news/'); return res.redirect('/news/');
@ -229,12 +230,10 @@ exports.upvote = function(req, res, next) {
); );
story.markModified('rank'); story.markModified('rank');
story.save(); story.save();
User.find({'_id': story.author.userId}, function(err, user) { User.findOne({'_id': story.author.userId}, function(err, user) {
'use strict';
if (err) { if (err) {
return next(err); return next(err);
} }
user = user.pop();
user.progressTimestamps.push(Date.now() || 0); user.progressTimestamps.push(Date.now() || 0);
user.save(function (err, user) { user.save(function (err, user) {
req.user.save(function (err, user) { req.user.save(function (err, user) {
@ -339,7 +338,7 @@ exports.storySubmission = function(req, res, next) {
if (link.search(/^https?:\/\//g) === -1) { if (link.search(/^https?:\/\//g) === -1) {
link = 'http://' + link; link = 'http://' + link;
} }
Story.count({'storyLink': new RegExp('^' + storyLink + '(?: [0-9]+)?$', 'i')}, function (err, storyCount) { Story.count({ storyLink: new RegExp('^' + storyLink + '(?: [0-9]+)?$', 'i')}, function (err, storyCount) {
if (err) { if (err) {
return res.status(500); return res.status(500);
} }
@ -513,7 +512,8 @@ exports.storySubmission = function(req, res, next) {
return next(err); return next(err);
} }
try { try {
// Based on the context retrieve the parent object of the comment (Story/Comment) // Based on the context retrieve the parent
// object of the comment (Story/Comment)
Context.find({'_id': data.associatedPost}, function (err, associatedContext) { Context.find({'_id': data.associatedPost}, function (err, associatedContext) {
if (err) { if (err) {
return next(err); return next(err);
@ -533,8 +533,15 @@ exports.storySubmission = function(req, res, next) {
if (err) { if (err) {
return next(err); return next(err);
} }
// If the emails of both authors differ, only then proceed with email notification // If the emails of both authors differ,
if (typeof data.author !== 'undefined' && data.author.email && typeof recipient !== 'undefined' && recipient.email && (data.author.email !== recipient.email)) { // only then proceed with email notification
if (
typeof data.author !== 'undefined' &&
data.author.email &&
typeof recipient !== 'undefined' &&
recipient.email &&
(data.author.email !== recipient.email)
) {
var transporter = nodemailer.createTransport({ var transporter = nodemailer.createTransport({
service: 'Mandrill', service: 'Mandrill',
auth: { auth: {
@ -546,11 +553,16 @@ exports.storySubmission = function(req, res, next) {
var mailOptions = { var mailOptions = {
to: recipient.email, to: recipient.email,
from: 'Team@freecodecamp.com', from: 'Team@freecodecamp.com',
subject: data.author.username + ' replied to your post on Camper News', subject: data.author.username +
' replied to your post on Camper News',
text: [ text: [
'Just a quick heads-up: ' + data.author.username + ' replied to you on Camper News.', 'Just a quick heads-up: ',
data.author.username,
' replied to you on Camper News.',
'You can keep this conversation going.', 'You can keep this conversation going.',
'Just head back to the discussion here: http://freecodecamp.com/news/' + data.originalStoryLink, 'Just head back to the discussion here: ',
'http://freecodecamp.com/news/',
data.originalStoryLink,
'- the Free Code Camp Volunteer Team' '- the Free Code Camp Volunteer Team'
].join('\n') ].join('\n')
}; };