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) {
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/');
@ -229,12 +230,10 @@ exports.upvote = function(req, res, next) {
);
story.markModified('rank');
story.save();
User.find({'_id': story.author.userId}, function(err, user) {
'use strict';
User.findOne({'_id': story.author.userId}, function(err, user) {
if (err) {
return next(err);
}
user = user.pop();
user.progressTimestamps.push(Date.now() || 0);
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) {
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) {
return res.status(500);
}
@ -513,7 +512,8 @@ exports.storySubmission = function(req, res, next) {
return next(err);
}
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) {
if (err) {
return next(err);
@ -533,8 +533,15 @@ exports.storySubmission = function(req, res, next) {
if (err) {
return next(err);
}
// If the emails of both authors differ, only then proceed with email notification
if (typeof data.author !== 'undefined' && data.author.email && typeof recipient !== 'undefined' && recipient.email && (data.author.email !== recipient.email)) {
// If the emails of both authors differ,
// 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({
service: 'Mandrill',
auth: {
@ -546,11 +553,16 @@ exports.storySubmission = function(req, res, next) {
var mailOptions = {
to: recipient.email,
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: [
'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.',
'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'
].join('\n')
};