diff --git a/app.js b/app.js index 3c253644a01..fc625717dad 100644 --- a/app.js +++ b/app.js @@ -317,11 +317,15 @@ app.get( storyController.submitNew ); -app.post( - '/stories/', - storyController.storySubmission +app.get( + '/stories/submit/:newStory', + storyController.preSubmit ); +app.post( + '/stories/preliminary', + storyController.newStory +); app.get( '/stories/hot', @@ -354,11 +358,6 @@ app.post( storyController.upvote ); -app.post( - '/stories/getURLMetaData', - resourcesController.getMetaData -); - /** * Challenge related routes */ diff --git a/controllers/resources.js b/controllers/resources.js index b2f271e6273..104b4bd90da 100644 --- a/controllers/resources.js +++ b/controllers/resources.js @@ -264,7 +264,7 @@ module.exports = { return { _id: elem._id, difficulty: elem.difficulty - } + }; }) .sort(function(a, b) { return a.difficulty - b.difficulty; @@ -278,7 +278,7 @@ module.exports = { return { name: elem.name, difficulty: elem.difficulty - } + }; }) .sort(function(a, b) { return a.difficulty - b.difficulty; @@ -290,19 +290,25 @@ module.exports = { whichEnvironment: function() { return process.env.NODE_ENV; }, - getURLTitle: function(req, res, next) { - var url = req.body.data.url; + getURLTitle: function(url, callback) { + debug('getURL called initialled'); - var result = {title: ''}; - request(url, function (error, response, body) { - if (!error && response.statusCode === 200) { - var $ = cheerio.load(body); - var title = $('title').text(); - result.title = title; - debug(result); - res.json(result); - } - }); + (function () { + var result = {title: ''}; + request(url, function (error, response, body) { + debug('request fired'); + if (!error && response.statusCode === 200) { + debug('fetching data'); + var $ = cheerio.load(body); + var title = $('title').text(); + result.title = title; + debug('calling callback with', result); + callback(null, result); + } else { + callback('failed'); + } + }); + })(); } }; diff --git a/controllers/story.js b/controllers/story.js index 3f2ea24bcc6..03d033885e0 100644 --- a/controllers/story.js +++ b/controllers/story.js @@ -53,6 +53,16 @@ exports.recent = function(req, res, next) { }); }; +exports.preSubmit = function(req, res, next) { + var data = req.params.newStory; + debug('got presubmission with info', data.url, data.title); + res.render('stories/index', { + page: 'storySubmission', + storyURL: data.url, + storyTitle: data.title + }); +}; + exports.returnIndividualStory = function(req, res, next) { var dashedName = req.params.storyName; @@ -166,6 +176,26 @@ exports.comments = function(req, res, next) { }); }; +exports.newStory = function(req, res, next) { + var url = req.body.data.url; + debug('Got new story submission, calling resources with', url); + resources.getURLTitle(url, processResponse); + function processResponse(err, storyTitle) { + if (err) { + res.json({ + storyURL: url, + storyTitle: '' + }); + } else { + storyTitle = storyTitle ? storyTitle : ''; + res.json({ + storyURL: url, + storyTitle: storyTitle.title + }); + } + } +}; + exports.storySubmission = function(req, res, next) { var data = req.body.data; var storyLink = data.headline diff --git a/views/stories/index.jade b/views/stories/index.jade index 9e71245a010..ae2eb90ba24 100644 --- a/views/stories/index.jade +++ b/views/stories/index.jade @@ -15,6 +15,10 @@ block content if (page === 'recent') include ./new-stories if (page === 'submit') - include ./submit-story + include ./preliminary-submit if (page === 'search') - include ./search-stories \ No newline at end of file + include ./search-stories + if (page === 'storySubmission') + include ./submit-story + if (page === 'storyShow') + include ./show \ No newline at end of file diff --git a/views/stories/preliminary-submit.jade b/views/stories/preliminary-submit.jade new file mode 100644 index 00000000000..704501a92e5 --- /dev/null +++ b/views/stories/preliminary-submit.jade @@ -0,0 +1,37 @@ +.spacer +.col-xs-12 + form.form-horizontal.control-label-story-submission#story-submission-form + .col-xs-2.img-story-post + img.img-responsive(src='#{user.profile.picture}') + .col-xs-10 + .form-group + .col-xs-1 + label.control-label.control-label-story-submission(for='name') Link + .col-xs-11 + input#story-url.form-control(placeholder='Paste your link here', name='Link') + .spacer + .form-group + .btn.btn-big.btn-block.btn-primary#preliminary-story-submit Submit + script. + var preliminaryStorySubmit = function preliminaryStorySubmit() { + + var storyURL = $('#story-url').val(); + console.log(storyURL); + $('#preliminary-story-submit').attr('disabled', 'disabled'); + + $.post('/stories/preliminary', + { + data: { + url: storyURL + } + }) + .fail(function (xhr, textStatus, errorThrown) { + $('#preliminary-story-submit').attr('disabled', false); + }) + .done(function (data, textStatus, xhr) { + window.location = '/stories/submit/url=' + + encodeURIComponent(data.storyURL) + + '&title=' + encodeURIComponent(data.storyTitle); + }); + } + $('#preliminary-story-submit').on('click', preliminaryStorySubmit); \ No newline at end of file diff --git a/views/stories/submit-story.jade b/views/stories/submit-story.jade index a1bba32ae6b..6e0dd1e6f20 100644 --- a/views/stories/submit-story.jade +++ b/views/stories/submit-story.jade @@ -1,5 +1,8 @@ .spacer .col-xs-12 + script. + var storyURL = !{JSON.stringify(storyURL)}; + var storyTitle = !{JSON.stringify(storyTitle)}; form.form-horizontal.control-label-story-submission#story-submission-form .col-xs-2.img-story-post img.img-responsive(src='#{user.profile.picture}') @@ -8,12 +11,15 @@ .col-xs-1 label.control-label.control-label-story-submission(for='name') Link .col-xs-11 - input.form-control(placeholder='Paste your link here', name='Link') + input#story-url.form-control(placeholder='Paste your link here', name='Link') .form-group .col-xs-1 label.control-label.control-label-story-submission(for='name') Title .col-xs-11 - input.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') .spacer .form-group .btn.btn-big.btn-block.btn-primary#story-submit Submit + script. + $('#story-url').val(storyURL).attr('disabled', 'disabled'); + $('#story-title').val(storyTitle); \ No newline at end of file