diff --git a/controllers/resources.js b/controllers/resources.js index 9f80434b59..1f0235697f 100644 --- a/controllers/resources.js +++ b/controllers/resources.js @@ -9,7 +9,7 @@ var async = require('async'), _ = require('lodash'), fs = require('fs'), - constantStrings = require('constantStrings.json'), + constantStrings = require('./constantStrings.json'), User = require('../models/User'), Challenge = require('./../models/Challenge'), Story = require('./../models/Story'), diff --git a/controllers/story.js b/controllers/story.js index 8c01bedd2c..f8b6f88acb 100755 --- a/controllers/story.js +++ b/controllers/story.js @@ -1,13 +1,16 @@ -var moment = require('moment'), - mongodb = require('mongodb'), - nodemailer = require('nodemailer'), - sanitizeHtml = require('sanitize-html'), - MongoClient = mongodb.MongoClient, - resources = require('./resources'), - secrets = require('../config/secrets'), +/* eslint-disable no-catch-shadow, no-unused-vars */ +var R = require('ramda'), + debug = require('debug')('freecc:cntr:story'), Story = require('./../models/Story'), Comment = require('./../models/Comment'), - User = require('./../models/User'); + User = require('./../models/User'), + moment = require('moment'), + resources = require('./resources'), + mongodb = require('mongodb'), + MongoClient = mongodb.MongoClient, + secrets = require('../config/secrets'), + nodemailer = require('nodemailer'), + sanitizeHtml = require('sanitize-html'); function hotRank(timeValue, rank) { /* @@ -227,14 +230,13 @@ exports.upvote = function(req, res, next) { ); story.markModified('rank'); story.save(); - User.find({ '_id': story.author.userId }, function(err, user) { + 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) { - req.user.save(function (err) { + user.save(function (err, user) { + req.user.save(function (err, user) { if (err) { return next(err); } @@ -336,64 +338,61 @@ 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); + } + + // if duplicate storyLink add unique number + storyLink = (storyCount === 0) ? storyLink : storyLink + ' ' + storyCount; + + var link = data.link; + if (link.search(/^https?:\/\//g) === -1) { + link = 'http://' + link; + } + var story = new Story({ + headline: sanitizeHtml(data.headline, { + allowedTags: [], + allowedAttributes: [] + }).replace(/"/g, '"'), + timePosted: Date.now(), + link: link, + description: sanitizeHtml(data.description, { + allowedTags: [], + allowedAttributes: [] + }).replace(/"/g, '"'), + rank: 1, + upVotes: [({ + upVotedBy: req.user._id, + upVotedByUsername: req.user.profile.username + })], + author: { + picture: req.user.profile.picture, + userId: req.user._id, + username: req.user.profile.username, + email: req.user.email + }, + comments: [], + image: data.image, + storyLink: storyLink, + metaDescription: data.storyMetaDescription, + originalStoryAuthorEmail: req.user.email + }); + story.save(function (err) { if (err) { return res.status(500); } - - // if duplicate storyLink add unique number - storyLink = (storyCount === 0) ? storyLink : storyLink + ' ' + storyCount; - - var link = data.link; - if (link.search(/^https?:\/\//g) === -1) { - link = 'http://' + link; - } - var story = new Story({ - headline: sanitizeHtml(data.headline, { - allowedTags: [], - allowedAttributes: [] - }).replace(/"/g, '"'), - timePosted: Date.now(), - link: link, - description: sanitizeHtml(data.description, { - allowedTags: [], - allowedAttributes: [] - }).replace(/"/g, '"'), - rank: 1, - upVotes: [({ - upVotedBy: req.user._id, - upVotedByUsername: req.user.profile.username - })], - author: { - picture: req.user.profile.picture, - userId: req.user._id, - username: req.user.profile.username, - email: req.user.email - }, - comments: [], - image: data.image, - storyLink: storyLink, - metaDescription: data.storyMetaDescription, - originalStoryAuthorEmail: req.user.email - }); - story.save(function (err) { + req.user.progressTimestamps.push(Date.now() || 0); + req.user.save(function (err, user) { if (err) { - return res.status(500); + return next(err); } - req.user.progressTimestamps.push(Date.now() || 0); - req.user.save(function (err) { - if (err) { - return next(err); - } - }); - res.send(JSON.stringify({ - storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase() - })); }); - } - ); + res.send(JSON.stringify({ + storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase() + })); + }); + }); }; exports.commentSubmit = function(req, res, next) { @@ -515,74 +514,69 @@ exports.storySubmission = function(req, res, next) { try { // 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) { + return next(err); + } + associatedContext = associatedContext.pop(); + if (associatedContext) { + associatedContext.comments.push(data._id); + associatedContext.save(function (err) { + if (err) { + return next(err); + } + res.send(true); + }); + } + // Find the author of the parent object + User.findOne({'profile.username': associatedContext.author.username}, function(err, recipient) { if (err) { return next(err); } - associatedContext = associatedContext.pop(); - if (associatedContext) { - associatedContext.comments.push(data._id); - associatedContext.save(function (err) { - 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) + ) { + var transporter = nodemailer.createTransport({ + service: 'Mandrill', + auth: { + user: secrets.mandrill.user, + pass: secrets.mandrill.password + } + }); + + var mailOptions = { + to: recipient.email, + from: 'Team@freecodecamp.com', + 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.', + 'You can keep this conversation going.', + 'Just head back to the discussion here: ', + 'http://freecodecamp.com/news/', + data.originalStoryLink, + '- the Free Code Camp Volunteer Team' + ].join('\n') + }; + + transporter.sendMail(mailOptions, function (err) { + if (err) { + return err; } - res.send(true); }); } - // Find the author of the parent object - User.findOne( - { 'profile.username': associatedContext.author.username }, - function(err, recipient) { - 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) - ) { - var transporter = nodemailer.createTransport({ - service: 'Mandrill', - auth: { - user: secrets.mandrill.user, - pass: secrets.mandrill.password - } - }); - - var mailOptions = { - to: recipient.email, - from: 'Team@freecodecamp.com', - 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.', - 'You can keep this conversation going.', - 'Just head back to the discussion here: ', - 'http://freecodecamp.com/news/', - data.originalStoryLink, - '- the Free Code Camp Volunteer Team' - ].join('\n') - }; - - transporter.sendMail(mailOptions, function (err) { - if (err) { - return err; - } - }); - } - } - ); - } - ); + }); + }); } catch (e) { + // delete comment return next(err); } }); diff --git a/controllers/user.js b/controllers/user.js index ae434c4a22..cd961db05a 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -53,6 +53,9 @@ exports.postSignin = function(req, res, next) { return next(err); } req.flash('success', { msg: 'Success! You are logged in.' }); + if (/hotStories/.test(req.session.returnTo)) { + return res.redirect('../news'); + } return res.redirect(req.session.returnTo || '/'); }); })(req, res, next);