Merge pull request #473 from terakilobyte/staging
E-mail signin from camper news
This commit is contained in:
@ -9,7 +9,7 @@ var async = require('async'),
|
|||||||
_ = require('lodash'),
|
_ = require('lodash'),
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
|
|
||||||
constantStrings = require('constantStrings.json'),
|
constantStrings = require('./constantStrings.json'),
|
||||||
User = require('../models/User'),
|
User = require('../models/User'),
|
||||||
Challenge = require('./../models/Challenge'),
|
Challenge = require('./../models/Challenge'),
|
||||||
Story = require('./../models/Story'),
|
Story = require('./../models/Story'),
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
var moment = require('moment'),
|
/* eslint-disable no-catch-shadow, no-unused-vars */
|
||||||
mongodb = require('mongodb'),
|
var R = require('ramda'),
|
||||||
nodemailer = require('nodemailer'),
|
debug = require('debug')('freecc:cntr:story'),
|
||||||
sanitizeHtml = require('sanitize-html'),
|
|
||||||
MongoClient = mongodb.MongoClient,
|
|
||||||
resources = require('./resources'),
|
|
||||||
secrets = require('../config/secrets'),
|
|
||||||
Story = require('./../models/Story'),
|
Story = require('./../models/Story'),
|
||||||
Comment = require('./../models/Comment'),
|
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) {
|
function hotRank(timeValue, rank) {
|
||||||
/*
|
/*
|
||||||
@ -227,14 +230,13 @@ 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) {
|
||||||
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.save(function (err, user) {
|
||||||
req.user.save(function (err) {
|
req.user.save(function (err, user) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
@ -336,64 +338,61 @@ 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(
|
Story.count({ storyLink: new RegExp('^' + storyLink + '(?: [0-9]+)?$', 'i')}, function (err, storyCount) {
|
||||||
{ storyLink: new RegExp('^' + storyLink + '(?: [0-9]+)?$', 'i') },
|
if (err) {
|
||||||
function (err, storyCount) {
|
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) {
|
if (err) {
|
||||||
return res.status(500);
|
return res.status(500);
|
||||||
}
|
}
|
||||||
|
req.user.progressTimestamps.push(Date.now() || 0);
|
||||||
// if duplicate storyLink add unique number
|
req.user.save(function (err, user) {
|
||||||
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) {
|
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) {
|
exports.commentSubmit = function(req, res, next) {
|
||||||
@ -515,74 +514,69 @@ exports.storySubmission = function(req, res, next) {
|
|||||||
try {
|
try {
|
||||||
// Based on the context retrieve the parent
|
// Based on the context retrieve the parent
|
||||||
// object of the comment (Story/Comment)
|
// object of the comment (Story/Comment)
|
||||||
Context.find(
|
Context.find({'_id': data.associatedPost}, function (err, associatedContext) {
|
||||||
{ '_id': data.associatedPost },
|
if (err) {
|
||||||
function (err, associatedContext) {
|
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) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
associatedContext = associatedContext.pop();
|
// If the emails of both authors differ,
|
||||||
if (associatedContext) {
|
// only then proceed with email notification
|
||||||
associatedContext.comments.push(data._id);
|
if (
|
||||||
associatedContext.save(function (err) {
|
typeof data.author !== 'undefined' &&
|
||||||
if (err) {
|
data.author.email &&
|
||||||
return next(err);
|
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) {
|
} catch (e) {
|
||||||
|
// delete comment
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -53,6 +53,9 @@ exports.postSignin = function(req, res, next) {
|
|||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
req.flash('success', { msg: 'Success! You are logged in.' });
|
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 || '/');
|
return res.redirect(req.session.returnTo || '/');
|
||||||
});
|
});
|
||||||
})(req, res, next);
|
})(req, res, next);
|
||||||
|
Reference in New Issue
Block a user