pull/298/head
Michael Q Larson 2015-03-31 13:43:35 -07:00
commit 22507de01f
1 changed files with 42 additions and 33 deletions

View File

@ -119,13 +119,13 @@ exports.returnIndividualStory = function(req, res, next) {
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) {
next(err);
}
if (story.length < 1) {
if (story == null) {
req.flash('errors', {
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/');
}
story = story.pop();
var dashedNameFull = story.storyLink.toLowerCase().replace(/\s/g, '-');
if (dashedNameFull !== dashedName) {
return res.redirect('../stories/' + dashedNameFull);
}
var userVoted = false;
var userVoted = false;
try {
var votedObj = story.upVotes.filter(function(a){
return a['upVotedByUsername'] === req.user['profile']['username'];
@ -150,7 +149,7 @@ exports.returnIndividualStory = function(req, res, next) {
} catch(err){
userVoted = false;
}
res.render('stories/index', {
res.render('stories/index', {
title: story.headline,
link: story.link,
author: story.author,
@ -163,7 +162,7 @@ exports.returnIndividualStory = function(req, res, next) {
image: story.image,
page: 'show',
storyMetaDescription: story.metaDescription,
hasUserVoted: userVoted
hasUserVoted: userVoted
});
});
};
@ -308,37 +307,47 @@ exports.storySubmission = function(req, res) {
.replace(/[^a-z0-9]/gi, ' ')
.replace(/\s+/g, ' ')
.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) {
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()
}));
});
});
};