This commit is contained in:
Michael Q Larson
2015-03-31 13:43:35 -07:00

View File

@ -119,13 +119,13 @@ 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.findOne({'storyLink' : new RegExp(storyName, 'i')}, function(err, story) {
if (err) { if (err) {
next(err); next(err);
} }
if (story.length < 1) { if (story == null) {
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."
}); });
@ -133,13 +133,12 @@ exports.returnIndividualStory = function(req, res, next) {
return res.redirect('/stories/'); return res.redirect('/stories/');
} }
story = story.pop();
var dashedNameFull = story.storyLink.toLowerCase().replace(/\s/g, '-'); var dashedNameFull = story.storyLink.toLowerCase().replace(/\s/g, '-');
if (dashedNameFull !== dashedName) { if (dashedNameFull !== dashedName) {
return res.redirect('../stories/' + dashedNameFull); return res.redirect('../stories/' + dashedNameFull);
} }
var userVoted = false; var userVoted = false;
try { try {
var votedObj = story.upVotes.filter(function(a){ var votedObj = story.upVotes.filter(function(a){
return a['upVotedByUsername'] === req.user['profile']['username']; return a['upVotedByUsername'] === req.user['profile']['username'];
@ -150,7 +149,7 @@ exports.returnIndividualStory = function(req, res, next) {
} catch(err){ } catch(err){
userVoted = false; userVoted = false;
} }
res.render('stories/index', { res.render('stories/index', {
title: story.headline, title: story.headline,
link: story.link, link: story.link,
author: story.author, author: story.author,
@ -163,7 +162,7 @@ exports.returnIndividualStory = function(req, res, next) {
image: story.image, image: story.image,
page: 'show', page: 'show',
storyMetaDescription: story.metaDescription, storyMetaDescription: story.metaDescription,
hasUserVoted: userVoted hasUserVoted: userVoted
}); });
}); });
}; };
@ -308,37 +307,47 @@ exports.storySubmission = function(req, res) {
.replace(/[^a-z0-9]/gi, ' ') .replace(/[^a-z0-9]/gi, ' ')
.replace(/\s+/g, ' ') .replace(/\s+/g, ' ')
.toLowerCase(); .toLowerCase();
var link = data.link;
if (link.search(/^https?:\/\//g) === -1) {
link = 'http://' + link;
}
var story = new Story({
headline: sanitizeHtml(data.headline, {
allowedTags: [],
allowedAttributes: []
}).replace(/&quot;/g, '"'),
timePosted: Date.now(),
link: link,
description: sanitizeHtml(data.description, {
allowedTags: [],
allowedAttributes: []
}).replace(/&quot;/g, '"'),
rank: 1,
upVotes: data.upVotes,
author: data.author,
comments: [],
image: data.image,
storyLink: storyLink,
metaDescription: data.storyMetaDescription
});
story.save(function(err) { Story.count({'storyLink': storyLink}, function(err, storyCount) {
if (err) { if (err) {
return res.status(500); return res.status(500);
} }
res.send(JSON.stringify({
storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase() // 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(/&quot;/g, '"'),
timePosted: Date.now(),
link: link,
description: sanitizeHtml(data.description, {
allowedTags: [],
allowedAttributes: []
}).replace(/&quot;/g, '"'),
rank: 1,
upVotes: data.upVotes,
author: data.author,
comments: [],
image: data.image,
storyLink: storyLink,
metaDescription: data.storyMetaDescription
});
story.save(function(err) {
if (err) {
return res.status(500);
}
res.send(JSON.stringify({
storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase()
}));
});
}); });
}; };