diff --git a/controllers/story.js b/controllers/story.js index a69c9c9182..e44c4d7dad 100644 --- a/controllers/story.js +++ b/controllers/story.js @@ -54,8 +54,10 @@ exports.recent = function(req, res, next) { }; exports.preSubmit = function(req, res, next) { + var data = req.params.newStory; + data = data.replace(/url=/gi, '').replace(/&title=/gi, ',').split(','); var url = data[0]; var title = data[1]; @@ -177,16 +179,38 @@ exports.comments = function(req, res, next) { exports.newStory = function(req, res, next) { var url = req.body.data.url; + debug('In pre submit with a url', url); + + Story.find({'link': url}, function(err, story) { + debug('Attempting to find a story'); + if (err) { + debug('oops'); + return res.status(500); + } + if (story.length) { + debug('Found a story already, here\'s the return from find', story); + req.flash('errors', { + msg: "Someone's already posted that link. Here's the discussion." + }); + debug('Redirecting the user with', story[0].storyLink); + res.json({ + alreadyPosted: true, + storyURL: story.pop().storyLink + }); + } + }); resources.getURLTitle(url, processResponse); function processResponse(err, storyTitle) { if (err) { res.json({ + alreadyPosted: false, storyURL: url, storyTitle: '' }); } else { storyTitle = storyTitle ? storyTitle : ''; res.json({ + alreadyPosted: false, storyURL: url, storyTitle: storyTitle.title }); diff --git a/views/stories/preliminary-submit.jade b/views/stories/preliminary-submit.jade index 3937b6a800..f8485ff26c 100644 --- a/views/stories/preliminary-submit.jade +++ b/views/stories/preliminary-submit.jade @@ -42,6 +42,9 @@ $('#preliminary-story-submit').attr('disabled', false); }) .done(function (data, textStatus, xhr) { + if (data.alreadyPosted) { + window.location = '/stories/' + data.storyURL; + } window.location = '/stories/submit/url=' + encodeURIComponent(data.storyURL) + '&title=' + encodeURIComponent(data.storyTitle); diff --git a/views/stories/show.jade b/views/stories/show.jade index 6f77bf608a..59764476c1 100644 --- a/views/stories/show.jade +++ b/views/stories/show.jade @@ -26,8 +26,7 @@ .col-xs-12.negative-15 span Posted #{timeAgo} span  by  - a(href="/" + author.username) - @#{author.username} + a(href="/" + author.username) @#{author.username} .col-xs-10.col-xs-offset-1 p= description diff --git a/views/stories/submit-story.jade b/views/stories/submit-story.jade index df601fcbfd..cd67e38cd3 100644 --- a/views/stories/submit-story.jade +++ b/views/stories/submit-story.jade @@ -14,7 +14,7 @@ .col-xs-12.col-md-1 label.control-label.control-label-story-submission(for='name') Title .col-xs-12.col-md-11 - input#story-title.form-control(placeholder='Type a headline for your link here', name='Title') + input#story-title.form-control(placeholder='Type a headline for your link here', name='Title', autofocus) .form-group .col-xs-12.col-md-1 label.control-label.control-label-story-submission(for='name') Description