merge pr for unique story slugs

This commit is contained in:
terakilobyte
2015-04-20 00:28:55 -04:00
parent 66bf61cfdf
commit 7cbd8e1c04
3 changed files with 185 additions and 172 deletions

View File

@ -109,8 +109,8 @@ exports.returnIndividualBonfire = function(req, res, next) {
dashedName: dashedName, dashedName: dashedName,
name: bonfire.name, name: bonfire.name,
difficulty: Math.floor(+bonfire.difficulty), difficulty: Math.floor(+bonfire.difficulty),
brief: bonfire.description[0], brief: bonfire.description.shift(),
details: bonfire.description.slice(1), details: bonfire.description,
tests: bonfire.tests, tests: bonfire.tests,
challengeSeed: bonfire.challengeSeed, challengeSeed: bonfire.challengeSeed,
points: req.user ? req.user.points : undefined, points: req.user ? req.user.points : undefined,

View File

@ -57,7 +57,7 @@ exports.recentJSON = function(req, res, next) {
}; };
exports.hot = function(req, res) { exports.hot = function(req, res) {
return res.render('stories/index', { return res.render('stories/index', {
title: 'Hot stories currently trending on Camper News', title: 'Hot stories currently trending on Camper News',
page: 'hot' page: 'hot'
}); });
@ -120,7 +120,7 @@ exports.returnIndividualStory = function(req, res, next) {
var storyName = dashedName.replace(/\-/g, ' '); var storyName = dashedName.replace(/\-/g, ' ');
Story.find({'storyLink': new RegExp(storyName, 'i')}, function(err, story) { Story.find({'storyLink': storyName}, function(err, story) {
if (err) { if (err) {
return next(err); return next(err);
} }
@ -328,186 +328,198 @@ exports.storySubmission = function(req, res, next) {
if (link.search(/^https?:\/\//g) === -1) { if (link.search(/^https?:\/\//g) === -1) {
link = 'http://' + link; link = 'http://' + link;
} }
var story = new Story({ Story.count({'storyLink': new RegExp('^' + storyLink + '(?: [0-9]+)?$', 'i')}, function (err, storyCount) {
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);
}
res.send(JSON.stringify({
storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase()
}));
});
};
exports.commentSubmit = function(req, res, next) {
var data = req.body.data;
if (!req.user) {
return next(new Error('Not authorized'));
}
var sanitizedBody = sanitizeHtml(data.body,
{
allowedTags: [],
allowedAttributes: []
}).replace(/"/g, '"');
if (data.body !== sanitizedBody) {
req.flash('errors', {
msg: 'HTML is not allowed'
});
return res.send(true);
}
var comment = new Comment({
associatedPost: data.associatedPost,
originalStoryLink: data.originalStoryLink,
originalStoryAuthorEmail: req.user.email,
body: sanitizedBody,
rank: 0,
upvotes: 0,
author: {
picture: req.user.profile.picture,
userId: req.user._id,
username: req.user.profile.username,
email: req.user.email
},
comments: [],
topLevel: true,
commentOn: Date.now()
});
commentSave(comment, Story, res, next);
};
exports.commentOnCommentSubmit = function(req, res, next) {
var data = req.body.data;
if (!req.user) {
return next(new Error('Not authorized'));
}
var sanitizedBody = sanitizeHtml(data.body,
{
allowedTags: [],
allowedAttributes: []
}).replace(/"/g, '"');
if (data.body !== sanitizedBody) {
req.flash('errors', {
msg: 'HTML is not allowed'
});
return res.send(true);
}
var comment = new Comment({
associatedPost: data.associatedPost,
body: sanitizedBody,
rank: 0,
upvotes: 0,
originalStoryLink: data.originalStoryLink,
originalStoryAuthorEmail: data.originalStoryAuthorEmail,
author: {
picture: req.user.profile.picture,
userId: req.user._id,
username: req.user.profile.username,
email: req.user.email
},
comments: [],
topLevel: false,
commentOn: Date.now()
});
commentSave(comment, Comment, res, next);
};
exports.commentEdit = function(req, res, next) {
Comment.find({'_id': req.params.id}, function(err, cmt) {
if (err) { if (err) {
return next(err); return res.status(500);
} }
cmt = cmt.pop();
if (!req.user && cmt.author.userId !== req.user._id) { // 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);
}
res.send(JSON.stringify({
storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase()
}));
});
});
};
exports.commentSubmit = function(req, res, next) {
var data = req.body.data;
if (!req.user) {
return next(new Error('Not authorized')); return next(new Error('Not authorized'));
} }
var sanitizedBody = sanitizeHtml(data.body,
{
var sanitizedBody = sanitizeHtml(req.body.body, { allowedTags: [],
allowedTags: [], allowedAttributes: []
allowedAttributes: [] }).replace(/"/g, '"');
}).replace(/"/g, '"'); if (data.body !== sanitizedBody) {
if (req.body.body !== sanitizedBody) {
req.flash('errors', { req.flash('errors', {
msg: 'HTML is not allowed' msg: 'HTML is not allowed'
}); });
return res.send(true); return res.send(true);
} }
var comment = new Comment({
cmt.body = sanitizedBody; associatedPost: data.associatedPost,
cmt.commentOn = Date.now(); originalStoryLink: data.originalStoryLink,
cmt.save(function (err) { originalStoryAuthorEmail: req.user.email,
if (err) { body: sanitizedBody,
return next(err); rank: 0,
} upvotes: 0,
res.send(true); author: {
picture: req.user.profile.picture,
userId: req.user._id,
username: req.user.profile.username,
email: req.user.email
},
comments: [],
topLevel: true,
commentOn: Date.now()
}); });
}); commentSave(comment, Story, res, next);
};
}; exports.commentOnCommentSubmit = function(req, res, next) {
var data = req.body.data;
function commentSave(comment, Context, res, next) { if (!req.user) {
comment.save(function(err, data) { return next(new Error('Not authorized'));
if (err) {
return next(err);
} }
try {
Context.find({'_id': comment.associatedPost}, function (err, associatedStory) { var sanitizedBody = sanitizeHtml(data.body,
{
allowedTags: [],
allowedAttributes: []
}).replace(/"/g, '"');
if (data.body !== sanitizedBody) {
req.flash('errors', {
msg: 'HTML is not allowed'
});
return res.send(true);
}
var comment = new Comment({
associatedPost: data.associatedPost,
body: sanitizedBody,
rank: 0,
upvotes: 0,
originalStoryLink: data.originalStoryLink,
originalStoryAuthorEmail: data.originalStoryAuthorEmail,
author: {
picture: req.user.profile.picture,
userId: req.user._id,
username: req.user.profile.username,
email: req.user.email
},
comments: [],
topLevel: false,
commentOn: Date.now()
});
commentSave(comment, Comment, res, next);
};
exports.commentEdit = function(req, res, next) {
Comment.find({'_id': req.params.id}, function(err, cmt) {
if (err) {
return next(err);
}
cmt = cmt.pop();
if (!req.user && cmt.author.userId !== req.user._id) {
return next(new Error('Not authorized'));
}
var sanitizedBody = sanitizeHtml(req.body.body, {
allowedTags: [],
allowedAttributes: []
}).replace(/"/g, '"');
if (req.body.body !== sanitizedBody) {
req.flash('errors', {
msg: 'HTML is not allowed'
});
return res.send(true);
}
cmt.body = sanitizedBody;
cmt.commentOn = Date.now();
cmt.save(function (err) {
if (err) { if (err) {
return next(err); return next(err);
} }
associatedStory = associatedStory.pop(); res.send(true);
if (associatedStory) { });
associatedStory.comments.push(data._id);
associatedStory.save(function (err) { });
if (err) {
return next(err); };
}
res.send(true); function commentSave(comment, Context, res, next) {
}); comment.save(function(err, data) {
} if (err) {
User.findOne({'profile.username': associatedStory.author.username}, function(err, recipient) { return next(err);
}
try {
Context.find({'_id': comment.associatedPost}, function (err, associatedStory) {
if (err) { if (err) {
return next(err); return next(err);
} }
var recipients = ''; associatedStory = associatedStory.pop();
if (data.originalStoryAuthorEmail && (data.originalStoryAuthorEmail !== recipient.email)) { if (associatedStory) {
recipients = data.originalStoryAuthorEmail + ',' + recipient.email; associatedStory.comments.push(data._id);
} else { associatedStory.save(function (err) {
recipients = recipient.email; if (err) {
} return next(err);
}
res.send(true);
});
}
User.findOne({'profile.username': associatedStory.author.username}, function(err, recipient) {
if (err) {
return next(err);
}
var recipients = '';
if (data.originalStoryAuthorEmail && (data.originalStoryAuthorEmail !== recipient.email)) {
recipients = data.originalStoryAuthorEmail + ',' + recipient.email;
} else {
recipients = recipient.email;
}
var transporter = nodemailer.createTransport({ var transporter = nodemailer.createTransport({
service: 'Mandrill', service: 'Mandrill',
auth: { auth: {
@ -531,11 +543,11 @@ function commentSave(comment, Context, res, next) {
return err; return err;
} }
}); });
});
}); });
}); } catch (e) {
} catch (e) { // delete comment
// delete comment return next(err);
return next(err); }
} });
}); }
}

View File

@ -728,8 +728,9 @@
"Create a function that takes two or more arrays and returns an array of the symmetric difference of the provided arrays.", "Create a function that takes two or more arrays and returns an array of the symmetric difference of the provided arrays.",
"The mathematical term symmetric difference refers to the elements in two sets that are in either the first or second set, but not in both." "The mathematical term symmetric difference refers to the elements in two sets that are in either the first or second set, but not in both."
], ],
"challengeSeed": "function sym(args) {\n return arr;\r\n}\n\nsym([1, 2, 3], [5, 2, 1, 4]);", "challengeSeed": "function sym(args) {\n return arguments;\r\n}\n\nsym([1, 2, 3], [5, 2, 1, 4]);",
"tests": [ "tests": [
"expect(sym([1, 2, 3], [5, 2, 1, 4])).to.eqls([3, 5, 4])",
"assert.deepEqual(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5], 'should return the symmetric difference of the given arrays');", "assert.deepEqual(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5], 'should return the symmetric difference of the given arrays');",
"assert.deepEqual(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5], 'should return an array of unique values');", "assert.deepEqual(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5], 'should return an array of unique values');",
"assert.deepEqual(sym([1, 1]), [1], 'should return an array of unique values');" "assert.deepEqual(sym([1, 1]), [1], 'should return an array of unique values');"