2015-03-03 09:41:32 +00:00
|
|
|
var R = require('ramda'),
|
2015-03-04 03:55:04 +00:00
|
|
|
debug = require('debug')('freecc:cntr:story'),
|
2015-03-03 10:23:56 +00:00
|
|
|
Story = require('./../models/Story'),
|
2015-03-03 09:41:32 +00:00
|
|
|
Comment = require('./../models/Comment'),
|
|
|
|
User = require('./../models/User'),
|
2015-03-04 03:55:04 +00:00
|
|
|
moment = require('../public/js/lib/moment/moment.js'),
|
2015-03-06 18:06:38 +00:00
|
|
|
resources = require('./resources'),
|
|
|
|
mongodb = require('mongodb'),
|
|
|
|
MongoClient = mongodb.MongoClient,
|
2015-03-08 07:50:51 +00:00
|
|
|
secrets = require('../config/secrets'),
|
2015-03-09 09:30:37 +00:00
|
|
|
sanitizeHtml = require('sanitize-html');
|
2015-03-03 09:41:32 +00:00
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
function hotRank(timeValue, rank) {
|
2015-03-08 21:09:51 +00:00
|
|
|
/*
|
|
|
|
* Hotness ranking algorithm: http://amix.dk/blog/post/19588
|
|
|
|
* tMS = postedOnDate - foundationTime;
|
|
|
|
* Ranking...
|
|
|
|
* f(ts, 1, rank) = log(10)z + (ts)/45000;
|
|
|
|
*/
|
|
|
|
var hotness;
|
|
|
|
var z = Math.log(rank) / Math.log(10);
|
2015-03-21 07:47:21 +00:00
|
|
|
hotness = z + (timeValue / 115200000);
|
2015-03-08 21:09:51 +00:00
|
|
|
return hotness;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
exports.hotJSON = function(req, res) {
|
2015-03-08 21:09:51 +00:00
|
|
|
var story = Story.find({}).sort({'timePosted': -1}).limit(1000);
|
2015-03-03 10:23:56 +00:00
|
|
|
story.exec(function(err, stories) {
|
|
|
|
if (err) {
|
2015-03-09 23:15:43 +00:00
|
|
|
res.send(500);
|
|
|
|
return next(err);
|
2015-03-03 10:23:56 +00:00
|
|
|
}
|
2015-03-08 21:09:51 +00:00
|
|
|
|
|
|
|
var foundationDate = 1413298800000;
|
|
|
|
|
|
|
|
var sliceVal = stories.length >= 100 ? 100 : stories.length;
|
2015-03-09 09:30:37 +00:00
|
|
|
return res.json(stories.map(function(elem) {
|
2015-03-08 21:09:51 +00:00
|
|
|
return elem;
|
|
|
|
}).sort(function(a, b) {
|
|
|
|
return hotRank(b.timePosted - foundationDate, b.rank, b.headline) - hotRank(a.timePosted - foundationDate, a.rank, a.headline);
|
|
|
|
}).slice(0, sliceVal));
|
|
|
|
|
2015-03-03 10:23:56 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-03-09 23:18:17 +00:00
|
|
|
exports.recentJSON = function(req, res, next) {
|
2015-03-08 11:36:13 +00:00
|
|
|
var story = Story.find({}).sort({'timePosted': -1}).limit(100);
|
2015-03-03 10:23:56 +00:00
|
|
|
story.exec(function(err, stories) {
|
2015-03-03 09:41:32 +00:00
|
|
|
if (err) {
|
2015-03-09 23:15:43 +00:00
|
|
|
res.status(500);
|
|
|
|
return next(err);
|
2015-03-03 09:41:32 +00:00
|
|
|
}
|
2015-03-05 23:11:18 +00:00
|
|
|
res.json(stories);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
exports.hot = function(req, res) {
|
2015-03-05 23:11:18 +00:00
|
|
|
res.render('stories/index', {
|
2015-03-10 17:22:20 +00:00
|
|
|
title: 'Hot stories currently trending on Camper News',
|
2015-03-05 23:11:18 +00:00
|
|
|
page: 'hot'
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
exports.submitNew = function(req, res) {
|
2015-03-05 23:20:49 +00:00
|
|
|
res.render('stories/index', {
|
2015-03-10 17:22:20 +00:00
|
|
|
title: 'Submit a new story to Camper News',
|
2015-03-05 23:11:18 +00:00
|
|
|
page: 'submit'
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
exports.search = function(req, res) {
|
2015-03-05 23:20:49 +00:00
|
|
|
res.render('stories/index', {
|
2015-03-10 17:22:20 +00:00
|
|
|
title: 'Search the archives of Camper News',
|
2015-03-05 23:11:18 +00:00
|
|
|
page: 'search'
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
exports.recent = function(req, res) {
|
2015-03-05 23:11:18 +00:00
|
|
|
res.render('stories/index', {
|
2015-03-10 17:22:20 +00:00
|
|
|
title: 'Recently submitted stories on Camper News',
|
2015-03-05 23:11:18 +00:00
|
|
|
page: 'recent'
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
exports.preSubmit = function(req, res) {
|
2015-03-08 01:38:47 +00:00
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
var data = req.query;
|
2015-03-11 23:20:23 +00:00
|
|
|
var cleanData = sanitizeHtml(data.url, {
|
|
|
|
allowedTags: [],
|
|
|
|
allowedAttributes: []
|
|
|
|
}).replace(/";/g, '"');
|
2015-03-09 09:30:37 +00:00
|
|
|
if (data.url.replace(/&/g, '&') !== cleanData) {
|
2015-03-07 09:26:49 +00:00
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
req.flash('errors', {
|
|
|
|
msg: 'The data for this post is malformed'
|
|
|
|
});
|
|
|
|
return res.render('stories/index', {
|
|
|
|
page: 'stories/submit'
|
|
|
|
});
|
|
|
|
}
|
2015-03-08 01:38:47 +00:00
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
var title = data.title || '';
|
|
|
|
var image = data.image || '';
|
|
|
|
var description = data.description || '';
|
|
|
|
return res.render('stories/index', {
|
2015-03-10 17:22:20 +00:00
|
|
|
title: "Confirm your Camper News story submission",
|
2015-03-07 08:42:22 +00:00
|
|
|
page: 'storySubmission',
|
2015-03-09 09:30:37 +00:00
|
|
|
storyURL: data.url,
|
2015-03-09 04:08:39 +00:00
|
|
|
storyTitle: title,
|
2015-03-09 09:30:37 +00:00
|
|
|
storyImage: image,
|
|
|
|
storyMetaDescription: description
|
2015-03-07 08:42:22 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-03-03 10:50:16 +00:00
|
|
|
|
|
|
|
exports.returnIndividualStory = function(req, res, next) {
|
|
|
|
var dashedName = req.params.storyName;
|
|
|
|
|
2015-03-05 10:40:44 +00:00
|
|
|
var storyName = dashedName.replace(/\-/g, ' ');
|
2015-03-03 10:50:16 +00:00
|
|
|
|
2015-03-05 10:40:44 +00:00
|
|
|
Story.find({'storyLink' : new RegExp(storyName, 'i')}, function(err, story) {
|
2015-03-03 10:50:16 +00:00
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (story.length < 1) {
|
|
|
|
req.flash('errors', {
|
|
|
|
msg: "404: We couldn't find a story with that name. Please double check the name."
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.redirect('/stories/');
|
|
|
|
}
|
|
|
|
|
|
|
|
story = story.pop();
|
2015-03-06 18:55:59 +00:00
|
|
|
var dashedNameFull = story.storyLink.toLowerCase().replace(/\s/g, '-');
|
2015-03-03 13:03:33 +00:00
|
|
|
if (dashedNameFull !== dashedName) {
|
2015-03-03 10:50:16 +00:00
|
|
|
return res.redirect('../stories/' + dashedNameFull);
|
|
|
|
}
|
|
|
|
|
2015-03-17 06:06:58 +00:00
|
|
|
var userVoted = false;
|
2015-03-17 07:15:00 +00:00
|
|
|
try {
|
2015-03-17 07:20:07 +00:00
|
|
|
var votedObj = story.upVotes.filter(function(a){
|
|
|
|
return a['upVotedByUsername'] === req.user['profile']['username'];
|
|
|
|
})
|
|
|
|
if (votedObj.length > 0){
|
|
|
|
userVoted = true;
|
|
|
|
}
|
|
|
|
} catch(err){
|
|
|
|
userVoted = false;
|
|
|
|
}
|
2015-03-17 07:32:41 +00:00
|
|
|
res.render('stories/index', {
|
2015-03-03 10:50:16 +00:00
|
|
|
title: story.headline,
|
2015-03-04 03:55:04 +00:00
|
|
|
link: story.link,
|
2015-03-03 10:50:16 +00:00
|
|
|
author: story.author,
|
2015-03-05 10:21:26 +00:00
|
|
|
description: story.description,
|
2015-03-06 00:20:30 +00:00
|
|
|
rank: story.upVotes.length,
|
2015-03-03 10:50:16 +00:00
|
|
|
upVotes: story.upVotes,
|
2015-03-03 13:03:33 +00:00
|
|
|
comments: story.comments,
|
2015-03-03 22:15:00 +00:00
|
|
|
id: story._id,
|
2015-03-05 10:21:26 +00:00
|
|
|
timeAgo: moment(story.timePosted).fromNow(),
|
2015-03-07 09:26:49 +00:00
|
|
|
image: story.image,
|
2015-03-09 09:30:37 +00:00
|
|
|
page: 'show',
|
2015-03-17 06:06:58 +00:00
|
|
|
storyMetaDescription: story.metaDescription,
|
|
|
|
hasUserVoted: userVoted
|
2015-03-03 10:50:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2015-03-03 13:03:33 +00:00
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
exports.getStories = function(req, res) {
|
2015-03-06 18:06:38 +00:00
|
|
|
MongoClient.connect(secrets.db, function(err, database) {
|
2015-03-09 09:30:37 +00:00
|
|
|
database.collection('stories').find({
|
2015-03-06 18:06:38 +00:00
|
|
|
"$text": {
|
|
|
|
"$search": req.body.data.searchValue
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
headline: 1,
|
|
|
|
timePosted: 1,
|
|
|
|
link: 1,
|
|
|
|
description: 1,
|
|
|
|
rank: 1,
|
|
|
|
upVotes: 1,
|
|
|
|
author: 1,
|
|
|
|
comments: 1,
|
|
|
|
image: 1,
|
|
|
|
storyLink: 1,
|
2015-03-09 09:30:37 +00:00
|
|
|
metaDescription: 1,
|
2015-03-06 18:06:38 +00:00
|
|
|
textScore: {
|
|
|
|
$meta: "textScore"
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
sort: {
|
|
|
|
textScore: {
|
|
|
|
$meta: "textScore"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).toArray(function(err, items) {
|
2015-03-08 21:09:51 +00:00
|
|
|
if (items !== null && items.length !== 0) {
|
2015-03-06 19:51:00 +00:00
|
|
|
return res.json(items);
|
|
|
|
}
|
|
|
|
return res.status(404);
|
2015-03-06 18:06:38 +00:00
|
|
|
});
|
|
|
|
});
|
2015-03-06 00:20:30 +00:00
|
|
|
};
|
|
|
|
|
2015-03-09 23:15:43 +00:00
|
|
|
exports.upvote = function(req, res, next) {
|
2015-03-03 22:15:00 +00:00
|
|
|
var data = req.body.data;
|
|
|
|
Story.find({'_id': data.id}, function(err, story) {
|
2015-03-03 13:03:33 +00:00
|
|
|
if (err) {
|
2015-03-09 23:15:43 +00:00
|
|
|
res.status(500);
|
|
|
|
return next(err);
|
2015-03-03 13:03:33 +00:00
|
|
|
}
|
|
|
|
story = story.pop();
|
|
|
|
story.rank++;
|
2015-03-03 22:15:00 +00:00
|
|
|
story.upVotes.push(
|
|
|
|
{
|
|
|
|
upVotedBy: data.upVoter._id,
|
|
|
|
upVotedByUsername: data.upVoter.profile.username
|
|
|
|
}
|
|
|
|
);
|
2015-03-08 21:09:51 +00:00
|
|
|
story.markModified('rank');
|
2015-03-03 13:03:33 +00:00
|
|
|
story.save();
|
|
|
|
return res.send(story);
|
|
|
|
});
|
|
|
|
};
|
2015-03-03 22:15:00 +00:00
|
|
|
|
2015-03-09 23:15:43 +00:00
|
|
|
exports.comments = function(req, res, next) {
|
2015-03-03 22:15:00 +00:00
|
|
|
var data = req.params.id;
|
|
|
|
Comment.find({'_id': data}, function(err, comment) {
|
|
|
|
if (err) {
|
2015-03-09 23:15:43 +00:00
|
|
|
res.status(500);
|
|
|
|
return next(err);
|
2015-03-03 22:15:00 +00:00
|
|
|
}
|
|
|
|
comment = comment.pop();
|
|
|
|
return res.send(comment);
|
|
|
|
});
|
|
|
|
};
|
2015-03-05 10:21:26 +00:00
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
exports.newStory = function(req, res) {
|
2015-03-17 08:44:52 +00:00
|
|
|
if (!req.user) {
|
2015-03-17 20:01:14 +00:00
|
|
|
return res.status(500);
|
2015-03-17 08:44:52 +00:00
|
|
|
}
|
2015-03-07 08:42:22 +00:00
|
|
|
var url = req.body.data.url;
|
2015-03-11 23:20:23 +00:00
|
|
|
var cleanURL = sanitizeHtml(url, {
|
|
|
|
allowedTags: [],
|
|
|
|
allowedAttributes: []
|
|
|
|
}).replace(/"/g, '"');
|
2015-03-09 09:30:37 +00:00
|
|
|
if (cleanURL !== url) {
|
|
|
|
req.flash('errors', {
|
|
|
|
msg: "The URL you submitted doesn't appear valid"
|
|
|
|
});
|
|
|
|
return res.json({
|
|
|
|
alreadyPosted: true,
|
|
|
|
storyURL: '/stories/submit'
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2015-03-08 02:53:03 +00:00
|
|
|
if (url.search(/^https?:\/\//g) === -1) {
|
|
|
|
url = 'http://' + url;
|
|
|
|
}
|
2015-03-08 01:38:47 +00:00
|
|
|
Story.find({'link': url}, function(err, story) {
|
|
|
|
if (err) {
|
|
|
|
return res.status(500);
|
|
|
|
}
|
|
|
|
if (story.length) {
|
|
|
|
req.flash('errors', {
|
|
|
|
msg: "Someone's already posted that link. Here's the discussion."
|
|
|
|
});
|
2015-03-08 02:53:03 +00:00
|
|
|
return res.json({
|
2015-03-08 01:38:47 +00:00
|
|
|
alreadyPosted: true,
|
2015-03-09 09:30:37 +00:00
|
|
|
storyURL: '/stories/' + story.pop().storyLink
|
2015-03-08 01:38:47 +00:00
|
|
|
});
|
|
|
|
}
|
2015-03-08 02:53:03 +00:00
|
|
|
resources.getURLTitle(url, processResponse);
|
2015-03-08 01:38:47 +00:00
|
|
|
});
|
2015-03-08 02:53:03 +00:00
|
|
|
|
2015-03-09 04:08:39 +00:00
|
|
|
function processResponse(err, story) {
|
2015-03-07 08:42:22 +00:00
|
|
|
if (err) {
|
|
|
|
res.json({
|
2015-03-08 01:38:47 +00:00
|
|
|
alreadyPosted: false,
|
2015-03-07 08:42:22 +00:00
|
|
|
storyURL: url,
|
2015-03-09 04:08:39 +00:00
|
|
|
storyTitle: '',
|
2015-03-09 09:30:37 +00:00
|
|
|
storyImage: '',
|
|
|
|
storyMetaDescription: ''
|
2015-03-07 08:42:22 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.json({
|
2015-03-08 01:38:47 +00:00
|
|
|
alreadyPosted: false,
|
2015-03-07 08:42:22 +00:00
|
|
|
storyURL: url,
|
2015-03-09 04:08:39 +00:00
|
|
|
storyTitle: story.title,
|
2015-03-09 09:30:37 +00:00
|
|
|
storyImage: story.image,
|
|
|
|
storyMetaDescription: story.description
|
2015-03-07 08:42:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
exports.storySubmission = function(req, res) {
|
2015-03-05 10:21:26 +00:00
|
|
|
var data = req.body.data;
|
2015-03-17 20:01:14 +00:00
|
|
|
if (req.user._id.toString() !== data.author.userId.toString()) {
|
|
|
|
return res.status(500);
|
2015-03-17 08:44:52 +00:00
|
|
|
}
|
2015-03-05 10:21:26 +00:00
|
|
|
var storyLink = data.headline
|
|
|
|
.replace(/\'/g, '')
|
|
|
|
.replace(/\"/g, '')
|
|
|
|
.replace(/,/g, '')
|
|
|
|
.replace(/[^a-z0-9]/gi, ' ')
|
|
|
|
.replace(/\s+/g, ' ')
|
|
|
|
.toLowerCase();
|
2015-03-05 10:51:56 +00:00
|
|
|
var link = data.link;
|
|
|
|
if (link.search(/^https?:\/\//g) === -1) {
|
|
|
|
link = 'http://' + link;
|
|
|
|
}
|
2015-03-05 10:21:26 +00:00
|
|
|
var story = new Story({
|
2015-03-11 23:20:23 +00:00
|
|
|
headline: sanitizeHtml(data.headline, {
|
|
|
|
allowedTags: [],
|
|
|
|
allowedAttributes: []
|
|
|
|
}).replace(/"/g, '"'),
|
2015-03-05 10:21:26 +00:00
|
|
|
timePosted: Date.now(),
|
2015-03-05 10:51:56 +00:00
|
|
|
link: link,
|
2015-03-11 23:20:23 +00:00
|
|
|
description: sanitizeHtml(data.description, {
|
|
|
|
allowedTags: [],
|
|
|
|
allowedAttributes: []
|
|
|
|
}).replace(/"/g, '"'),
|
2015-03-06 00:20:30 +00:00
|
|
|
rank: 1,
|
|
|
|
upVotes: data.upVotes,
|
2015-03-05 10:21:26 +00:00
|
|
|
author: data.author,
|
|
|
|
comments: [],
|
|
|
|
image: data.image,
|
2015-03-09 09:30:37 +00:00
|
|
|
storyLink: storyLink,
|
|
|
|
metaDescription: data.storyMetaDescription
|
2015-03-05 10:21:26 +00:00
|
|
|
});
|
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
story.save(function(err) {
|
2015-03-05 10:21:26 +00:00
|
|
|
if (err) {
|
2015-03-05 10:40:44 +00:00
|
|
|
return res.status(500);
|
2015-03-05 10:21:26 +00:00
|
|
|
}
|
|
|
|
res.send(JSON.stringify({
|
|
|
|
storyLink: story.storyLink.replace(/\s/g, '-').toLowerCase()
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
};
|
2015-03-05 21:08:40 +00:00
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
exports.commentSubmit = function(req, res) {
|
2015-03-05 21:08:40 +00:00
|
|
|
var data = req.body.data;
|
2015-03-17 20:01:14 +00:00
|
|
|
if (req.user._id.toString() !== data.author.userId.toString()) {
|
|
|
|
return res.status(500);
|
2015-03-17 08:44:52 +00:00
|
|
|
}
|
2015-03-09 09:30:37 +00:00
|
|
|
var sanitizedBody = sanitizeHtml(data.body,
|
|
|
|
{
|
|
|
|
allowedTags: [],
|
|
|
|
allowedAttributes: []
|
2015-03-11 23:20:23 +00:00
|
|
|
}).replace(/"/g, '"');
|
2015-03-09 09:30:37 +00:00
|
|
|
if (data.body !== sanitizedBody) {
|
|
|
|
req.flash('errors', {
|
|
|
|
msg: 'HTML is not allowed'
|
|
|
|
});
|
|
|
|
return res.send(true);
|
|
|
|
}
|
2015-03-05 21:08:40 +00:00
|
|
|
var comment = new Comment({
|
|
|
|
associatedPost: data.associatedPost,
|
2015-03-09 09:30:37 +00:00
|
|
|
body: sanitizedBody,
|
2015-03-05 21:08:40 +00:00
|
|
|
rank: 0,
|
|
|
|
upvotes: 0,
|
|
|
|
author: data.author,
|
2015-03-06 16:57:09 +00:00
|
|
|
comments: [],
|
2015-03-08 09:48:20 +00:00
|
|
|
topLevel: true,
|
|
|
|
commentOn: Date.now()
|
2015-03-06 16:57:09 +00:00
|
|
|
});
|
|
|
|
commentSave(comment, Story, res);
|
|
|
|
};
|
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
exports.commentOnCommentSubmit = function(req, res) {
|
2015-03-06 16:57:09 +00:00
|
|
|
var data = req.body.data;
|
2015-03-17 20:01:14 +00:00
|
|
|
|
|
|
|
if (req.user._id.toString() !== data.author.userId.toString()) {
|
|
|
|
return res.status(500);
|
2015-03-17 08:44:52 +00:00
|
|
|
}
|
2015-03-17 20:01:14 +00:00
|
|
|
|
2015-03-09 09:30:37 +00:00
|
|
|
var sanitizedBody = sanitizeHtml(data.body,
|
|
|
|
{
|
|
|
|
allowedTags: [],
|
|
|
|
allowedAttributes: []
|
2015-03-11 23:20:23 +00:00
|
|
|
}).replace(/"/g, '"');
|
2015-03-09 09:30:37 +00:00
|
|
|
if (data.body !== sanitizedBody) {
|
|
|
|
req.flash('errors', {
|
|
|
|
msg: 'HTML is not allowed'
|
|
|
|
});
|
|
|
|
return res.send(true);
|
|
|
|
}
|
2015-03-06 16:57:09 +00:00
|
|
|
var comment = new Comment({
|
|
|
|
associatedPost: data.associatedPost,
|
2015-03-09 09:30:37 +00:00
|
|
|
body: sanitizedBody,
|
2015-03-06 16:57:09 +00:00
|
|
|
rank: 0,
|
|
|
|
upvotes: 0,
|
|
|
|
author: data.author,
|
|
|
|
comments: [],
|
2015-03-08 09:48:20 +00:00
|
|
|
topLevel: false,
|
|
|
|
commentOn: Date.now()
|
2015-03-05 21:08:40 +00:00
|
|
|
});
|
2015-03-06 16:57:09 +00:00
|
|
|
commentSave(comment, Comment, res);
|
|
|
|
};
|
2015-03-05 21:08:40 +00:00
|
|
|
|
2015-03-06 16:57:09 +00:00
|
|
|
function commentSave(comment, Context, res) {
|
2015-03-05 21:08:40 +00:00
|
|
|
comment.save(function(err, data) {
|
|
|
|
if (err) {
|
|
|
|
return res.status(500);
|
|
|
|
}
|
2015-03-07 11:51:20 +00:00
|
|
|
try {
|
2015-03-06 16:57:09 +00:00
|
|
|
Context.find({'_id': comment.associatedPost}, function (err, associatedStory) {
|
|
|
|
if (err) {
|
|
|
|
return res.status(500);
|
|
|
|
}
|
|
|
|
associatedStory = associatedStory.pop();
|
|
|
|
if (associatedStory) {
|
|
|
|
associatedStory.comments.push(data._id);
|
2015-03-09 09:30:37 +00:00
|
|
|
associatedStory.save(function (err) {
|
2015-03-06 16:57:09 +00:00
|
|
|
if (err) {
|
|
|
|
res.status(500);
|
|
|
|
}
|
|
|
|
res.send(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// delete comment
|
|
|
|
return res.status(500);
|
|
|
|
}
|
2015-03-05 21:08:40 +00:00
|
|
|
});
|
2015-03-06 16:57:09 +00:00
|
|
|
}
|