2013-11-30 13:35:42 -05:00
|
|
|
var path = require('path'),
|
|
|
|
|
nconf = require('nconf'),
|
|
|
|
|
async = require('async'),
|
|
|
|
|
|
2013-12-06 21:08:21 -05:00
|
|
|
db = require('../database'),
|
2013-11-30 13:35:42 -05:00
|
|
|
user = require('../user'),
|
2013-12-17 20:34:21 +00:00
|
|
|
groups = require('../groups'),
|
2013-11-27 12:47:00 -05:00
|
|
|
auth = require('./authentication'),
|
|
|
|
|
topics = require('../topics'),
|
|
|
|
|
posts = require('../posts'),
|
|
|
|
|
categories = require('../categories'),
|
2013-11-30 13:35:42 -05:00
|
|
|
categoryTools = require('../categoryTools')
|
2013-11-27 12:47:00 -05:00
|
|
|
utils = require('../../public/src/utils'),
|
2013-08-13 11:25:10 -04:00
|
|
|
pkg = require('../../package.json'),
|
2013-11-30 13:35:42 -05:00
|
|
|
meta = require('../meta');
|
2013-08-20 12:11:17 -04:00
|
|
|
|
2013-07-31 15:17:03 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
(function (Api) {
|
2013-11-12 12:41:16 -05:00
|
|
|
Api.createRoutes = function (app) {
|
2013-09-24 14:18:41 -04:00
|
|
|
app.namespace('/api', function () {
|
|
|
|
|
app.get('/get_templates_listing', function (req, res) {
|
|
|
|
|
utils.walk(path.join(__dirname, '../../', 'public/templates'), function (err, data) {
|
|
|
|
|
res.json(data);
|
|
|
|
|
});
|
2013-07-31 15:17:03 -04:00
|
|
|
});
|
2013-08-13 11:25:10 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/config', function (req, res, next) {
|
|
|
|
|
var config = require('../../public/config.json');
|
2013-08-20 12:11:17 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
config.postDelay = meta.config.postDelay;
|
|
|
|
|
config.minimumTitleLength = meta.config.minimumTitleLength;
|
|
|
|
|
config.minimumPostLength = meta.config.minimumPostLength;
|
|
|
|
|
config.imgurClientIDSet = !! meta.config.imgurClientID;
|
|
|
|
|
config.minimumUsernameLength = meta.config.minimumUsernameLength;
|
|
|
|
|
config.maximumUsernameLength = meta.config.maximumUsernameLength;
|
|
|
|
|
config.minimumPasswordLength = meta.config.minimumPasswordLength;
|
2013-12-19 14:57:59 -05:00
|
|
|
config.maximumSignatureLength = meta.config.maximumSignatureLength;
|
2013-10-01 12:07:58 -04:00
|
|
|
config.useOutgoingLinksPage = meta.config.useOutgoingLinksPage;
|
2013-12-22 15:15:59 -05:00
|
|
|
config.allowGuestPosting = meta.config.allowGuestPosting;
|
2013-12-23 12:22:50 -05:00
|
|
|
config.allowRegistration = meta.config.allowRegistration || '1';
|
2013-11-18 15:44:32 -05:00
|
|
|
config.emailSetup = !!meta.config['email:from'];
|
2013-12-19 15:43:15 -05:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
res.json(200, config);
|
|
|
|
|
});
|
2013-08-15 17:03:43 -04:00
|
|
|
|
2013-12-12 16:02:12 -05:00
|
|
|
app.get('/home', function (req, res) {
|
2013-09-24 14:18:41 -04:00
|
|
|
var uid = (req.user) ? req.user.uid : 0;
|
2013-11-26 14:25:46 -05:00
|
|
|
categories.getAllCategories(uid, function (err, data) {
|
2013-09-24 14:18:41 -04:00
|
|
|
data.categories = data.categories.filter(function (category) {
|
2013-12-05 13:11:27 -05:00
|
|
|
return (!category.disabled || parseInt(category.disabled, 10) === 0);
|
2013-09-24 14:18:41 -04:00
|
|
|
});
|
2013-08-08 12:49:01 -04:00
|
|
|
|
2013-12-12 16:02:12 -05:00
|
|
|
function iterator(category, callback) {
|
2013-12-12 16:07:15 -05:00
|
|
|
categories.getRecentReplies(category.cid, 2, function (err, posts) {
|
2013-09-24 14:18:41 -04:00
|
|
|
category.posts = posts;
|
|
|
|
|
category.post_count = posts.length > 2 ? 2 : posts.length;
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 16:02:12 -05:00
|
|
|
async.each(data.categories, iterator, function (err) {
|
2013-12-05 13:11:27 -05:00
|
|
|
data.motd_class = (parseInt(meta.config.show_motd, 10) === 1 || meta.config.show_motd === undefined) ? '' : ' none';
|
2013-11-07 15:54:19 -05:00
|
|
|
data.motd_class += (meta.config.motd && meta.config.motd.length > 0 ? '' : ' default');
|
2013-11-07 15:52:17 -05:00
|
|
|
|
2013-12-13 15:55:24 -05:00
|
|
|
data.motd = require('marked')(meta.config.motd || "<div class=\"pull-right btn-group\"><a target=\"_blank\" href=\"http://www.nodebb.org\" class=\"btn btn-default btn-lg\"><i class=\"fa fa-comment\"></i><span class='hidden-mobile'> Get NodeBB</span></a> <a target=\"_blank\" href=\"https://github.com/designcreateplay/NodeBB\" class=\"btn btn-default btn-lg\"><i class=\"fa fa-github\"></i><span class='hidden-mobile'> Fork us on Github</span></a> <a target=\"_blank\" href=\"https://twitter.com/dcplabs\" class=\"btn btn-default btn-lg\"><i class=\"fa fa-twitter\"></i><span class='hidden-mobile'> @NodeBB</span></a></div>\n\n# NodeBB <span>v" + pkg.version + "</span>\nWelcome to NodeBB, the discussion platform of the future.");
|
2013-09-24 14:18:41 -04:00
|
|
|
res.json(data);
|
2013-07-31 15:17:03 -04:00
|
|
|
});
|
2013-11-26 14:25:46 -05:00
|
|
|
});
|
2013-09-24 14:18:41 -04:00
|
|
|
});
|
2013-08-08 14:30:42 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/login', function (req, res) {
|
|
|
|
|
var data = {},
|
|
|
|
|
login_strategies = auth.get_login_strategies(),
|
|
|
|
|
num_strategies = login_strategies.length;
|
2013-07-31 15:17:03 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
if (num_strategies == 0) {
|
|
|
|
|
data = {
|
|
|
|
|
'login_window:spansize': 'col-md-12',
|
2013-11-27 16:47:40 -05:00
|
|
|
'alternate_logins': false
|
2013-09-24 14:18:41 -04:00
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
data = {
|
|
|
|
|
'login_window:spansize': 'col-md-6',
|
2013-11-27 16:47:40 -05:00
|
|
|
'alternate_logins': true
|
2013-09-24 14:18:41 -04:00
|
|
|
}
|
2013-07-31 15:17:03 -04:00
|
|
|
}
|
|
|
|
|
|
2013-11-27 16:47:40 -05:00
|
|
|
data.authentication = login_strategies;
|
|
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
data.token = res.locals.csrf_token;
|
2013-07-31 15:17:03 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
res.json(data);
|
|
|
|
|
});
|
2013-08-20 12:11:17 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/register', function (req, res) {
|
|
|
|
|
var data = {},
|
|
|
|
|
login_strategies = auth.get_login_strategies(),
|
|
|
|
|
num_strategies = login_strategies.length;
|
|
|
|
|
|
|
|
|
|
if (num_strategies == 0) {
|
|
|
|
|
data = {
|
|
|
|
|
'register_window:spansize': 'col-md-12',
|
2013-11-27 16:47:40 -05:00
|
|
|
'alternate_logins': false
|
2013-09-24 14:18:41 -04:00
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
data = {
|
|
|
|
|
'register_window:spansize': 'col-md-6',
|
2013-11-27 16:47:40 -05:00
|
|
|
'alternate_logins': true
|
2013-09-24 14:18:41 -04:00
|
|
|
}
|
2013-07-31 15:17:03 -04:00
|
|
|
}
|
|
|
|
|
|
2013-11-27 16:47:40 -05:00
|
|
|
data.authentication = login_strategies;
|
|
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
data.token = res.locals.csrf_token;
|
|
|
|
|
data.minimumUsernameLength = meta.config['minimumUsernameLength'];
|
|
|
|
|
data.maximumUsernameLength = meta.config['maximumUsernameLength'];
|
|
|
|
|
data.minimumPasswordLength = meta.config['minimumPasswordLength'];
|
|
|
|
|
res.json(data);
|
|
|
|
|
});
|
2013-08-20 12:11:17 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/topic/:id/:slug?', function (req, res, next) {
|
|
|
|
|
var uid = (req.user) ? req.user.uid : 0;
|
2013-12-05 13:59:16 -05:00
|
|
|
topics.getTopicWithPosts(req.params.id, uid, 0, 10, false, function (err, data) {
|
2013-09-24 14:18:41 -04:00
|
|
|
if (!err) {
|
2013-12-05 13:11:27 -05:00
|
|
|
if (parseInt(data.deleted, 10) === 1 && parseInt(data.expose_tools, 10) === 0) {
|
2013-09-24 14:18:41 -04:00
|
|
|
return res.json(404, {});
|
|
|
|
|
}
|
2013-12-17 20:34:21 +00:00
|
|
|
// get the category this post belongs to and check category access
|
|
|
|
|
var cid = data.category_slug.split("/")[0];
|
|
|
|
|
groups.getCategoryAccess(cid, uid, function(err, access){
|
|
|
|
|
if (access){
|
|
|
|
|
res.json(data);
|
|
|
|
|
} else {
|
|
|
|
|
res.send(403);
|
|
|
|
|
}
|
|
|
|
|
})
|
2013-09-24 14:18:41 -04:00
|
|
|
} else next();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/category/:id/:slug?', function (req, res, next) {
|
|
|
|
|
var uid = (req.user) ? req.user.uid : 0;
|
2013-11-13 19:54:46 -05:00
|
|
|
|
2013-11-27 12:47:00 -05:00
|
|
|
// Category Whitelisting
|
2013-11-30 13:35:42 -05:00
|
|
|
categoryTools.privileges(req.params.id, uid, function(err, privileges) {
|
2013-11-27 12:47:00 -05:00
|
|
|
if (!err && privileges.read) {
|
2013-12-17 20:34:21 +00:00
|
|
|
groups.getCategoryAccess(req.params.id, uid, function(err, access){
|
|
|
|
|
if (access){
|
|
|
|
|
categories.getCategoryById(req.params.id, uid, function (err, data) {
|
|
|
|
|
if (!err && data && parseInt(data.disabled, 10) === 0) {
|
|
|
|
|
res.json(data);
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
2013-12-19 14:57:59 -05:00
|
|
|
}, req.params.id, uid);
|
2013-12-05 13:11:27 -05:00
|
|
|
} else {
|
2013-12-17 20:34:21 +00:00
|
|
|
res.send(403);
|
2013-12-05 13:11:27 -05:00
|
|
|
}
|
2013-12-19 14:57:59 -05:00
|
|
|
|
2013-12-17 20:34:21 +00:00
|
|
|
});
|
2013-11-13 19:54:46 -05:00
|
|
|
} else {
|
|
|
|
|
res.send(403);
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-08-20 12:11:17 -04:00
|
|
|
});
|
|
|
|
|
|
2013-12-02 13:28:46 -05:00
|
|
|
app.get('/recent/:term?', function (req, res, next) {
|
2013-09-24 14:18:41 -04:00
|
|
|
var uid = (req.user) ? req.user.uid : 0;
|
2013-11-22 11:42:42 -05:00
|
|
|
topics.getLatestTopics(uid, 0, 19, req.params.term, function (err, data) {
|
2013-12-02 13:28:46 -05:00
|
|
|
if(err) {
|
|
|
|
|
return next(err);
|
2013-11-22 11:42:42 -05:00
|
|
|
}
|
2013-12-02 13:28:46 -05:00
|
|
|
|
|
|
|
|
res.json(data);
|
2013-09-24 14:18:41 -04:00
|
|
|
});
|
|
|
|
|
});
|
2013-07-31 15:17:03 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/unread', function (req, res) {
|
|
|
|
|
var uid = (req.user) ? req.user.uid : 0;
|
|
|
|
|
topics.getUnreadTopics(uid, 0, 19, function (data) {
|
|
|
|
|
res.json(data);
|
|
|
|
|
});
|
2013-07-31 15:17:03 -04:00
|
|
|
});
|
|
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/unread/total', function (req, res) {
|
|
|
|
|
var uid = (req.user) ? req.user.uid : 0;
|
|
|
|
|
topics.getTotalUnread(uid, function (data) {
|
|
|
|
|
res.json(data);
|
|
|
|
|
});
|
2013-07-31 15:17:03 -04:00
|
|
|
});
|
|
|
|
|
|
2013-10-22 14:35:20 -04:00
|
|
|
app.get('/notifications', function(req, res) {
|
|
|
|
|
if (req.user && req.user.uid) {
|
|
|
|
|
user.notifications.getAll(req.user.uid, null, null, function(err, notifications) {
|
|
|
|
|
res.json({
|
|
|
|
|
notifications: notifications
|
|
|
|
|
});
|
|
|
|
|
});
|
2013-11-30 13:35:42 -05:00
|
|
|
} else {
|
|
|
|
|
res.send(403);
|
|
|
|
|
}
|
2013-10-22 14:35:20 -04:00
|
|
|
});
|
|
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/confirm/:id', function (req, res) {
|
|
|
|
|
user.email.confirm(req.params.id, function (data) {
|
|
|
|
|
if (data.status === 'ok') {
|
|
|
|
|
res.json({
|
|
|
|
|
'alert-class': 'alert-success',
|
|
|
|
|
title: 'Email Confirmed',
|
|
|
|
|
text: 'Thank you for vaidating your email. Your account is now fully activated.'
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.json({
|
|
|
|
|
'alert-class': 'alert-error',
|
|
|
|
|
title: 'An error occurred...',
|
|
|
|
|
text: 'There was a problem validating your email address. Perhaps the code was invalid or has expired.'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-08-21 23:34:35 +08:00
|
|
|
});
|
|
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/outgoing', function (req, res) {
|
|
|
|
|
var url = req.query.url;
|
|
|
|
|
|
|
|
|
|
if (url) {
|
2013-07-31 15:17:03 -04:00
|
|
|
res.json({
|
2013-11-21 22:15:04 -05:00
|
|
|
url: url,
|
|
|
|
|
title: meta.config.title
|
2013-07-31 15:17:03 -04:00
|
|
|
});
|
|
|
|
|
} else {
|
2013-09-24 14:18:41 -04:00
|
|
|
res.status(404);
|
|
|
|
|
res.redirect(nconf.get('relative_path') + '/404');
|
2013-07-31 15:17:03 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/search', function (req, res) {
|
2013-12-11 22:50:36 -05:00
|
|
|
if ((req.user && req.user.uid) || meta.config.allowGuestSearching === '1') {
|
2013-12-07 17:45:51 -05:00
|
|
|
return res.json({
|
|
|
|
|
show_no_topics: 'hide',
|
|
|
|
|
show_no_posts: 'hide',
|
|
|
|
|
show_results: 'hide',
|
|
|
|
|
search_query: '',
|
|
|
|
|
posts: [],
|
|
|
|
|
topics: []
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
res.send(403);
|
|
|
|
|
}
|
2013-08-03 20:54:16 -04:00
|
|
|
});
|
|
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/search/:term', function (req, res, next) {
|
2013-08-03 20:54:16 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
function searchPosts(callback) {
|
2013-12-05 20:06:36 -05:00
|
|
|
db.search('post', req.params.term, function(err, pids) {
|
2013-11-30 13:35:42 -05:00
|
|
|
if (err) {
|
2013-09-24 14:18:41 -04:00
|
|
|
return callback(err, null);
|
2013-11-30 13:35:42 -05:00
|
|
|
}
|
2013-09-24 14:18:41 -04:00
|
|
|
|
2013-12-06 21:08:21 -05:00
|
|
|
posts.getPostSummaryByPids(pids, false, function (err, posts) {
|
2013-11-30 13:35:42 -05:00
|
|
|
if (err){
|
2013-09-24 14:18:41 -04:00
|
|
|
return callback(err, null);
|
2013-11-30 13:35:42 -05:00
|
|
|
}
|
2013-09-24 14:18:41 -04:00
|
|
|
callback(null, posts);
|
|
|
|
|
});
|
2013-12-05 20:06:36 -05:00
|
|
|
});
|
2013-09-24 14:18:41 -04:00
|
|
|
}
|
2013-08-08 11:40:31 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
function searchTopics(callback) {
|
2013-12-05 20:06:36 -05:00
|
|
|
db.search('topic', req.params.term, function(err, tids) {
|
2013-11-30 13:35:42 -05:00
|
|
|
if (err) {
|
2013-08-20 12:11:17 -04:00
|
|
|
return callback(err, null);
|
2013-11-30 13:35:42 -05:00
|
|
|
}
|
2013-12-10 15:02:22 -05:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
topics.getTopicsByTids(tids, 0, function (topics) {
|
|
|
|
|
callback(null, topics);
|
|
|
|
|
}, 0);
|
2013-08-03 20:54:16 -04:00
|
|
|
});
|
2013-09-24 14:18:41 -04:00
|
|
|
}
|
2013-08-08 11:40:31 -04:00
|
|
|
|
2013-12-11 22:50:36 -05:00
|
|
|
if ((req.user && req.user.uid) || meta.config.allowGuestSearching === '1') {
|
2013-12-07 17:45:51 -05:00
|
|
|
async.parallel([searchPosts, searchTopics], function (err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
2013-09-24 14:18:41 -04:00
|
|
|
|
2013-12-07 17:45:51 -05:00
|
|
|
return res.json({
|
|
|
|
|
show_no_topics: results[1].length ? 'hide' : '',
|
|
|
|
|
show_no_posts: results[0].length ? 'hide' : '',
|
|
|
|
|
show_results: '',
|
|
|
|
|
search_query: req.params.term,
|
|
|
|
|
posts: results[0],
|
2013-12-13 16:36:29 -05:00
|
|
|
topics: results[1],
|
|
|
|
|
post_matches : results[0].length,
|
|
|
|
|
topic_matches : results[1].length
|
2013-12-07 17:45:51 -05:00
|
|
|
});
|
2013-09-24 14:18:41 -04:00
|
|
|
});
|
2013-12-07 17:45:51 -05:00
|
|
|
} else {
|
|
|
|
|
res.send(403);
|
|
|
|
|
}
|
2013-09-24 14:18:41 -04:00
|
|
|
});
|
2013-08-08 11:40:31 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/reset', function (req, res) {
|
|
|
|
|
res.json({});
|
|
|
|
|
});
|
2013-08-28 14:46:18 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/reset/:code', function (req, res) {
|
|
|
|
|
res.json({
|
|
|
|
|
reset_code: req.params.code
|
2013-08-03 20:54:16 -04:00
|
|
|
});
|
2013-08-08 11:40:31 -04:00
|
|
|
});
|
2013-08-19 13:31:04 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/404', function (req, res) {
|
|
|
|
|
res.json({});
|
2013-09-17 13:10:14 -04:00
|
|
|
});
|
2013-08-19 13:31:04 -04:00
|
|
|
|
2013-09-24 14:18:41 -04:00
|
|
|
app.get('/403', function (req, res) {
|
|
|
|
|
res.json({});
|
|
|
|
|
});
|
2013-12-02 13:28:46 -05:00
|
|
|
|
|
|
|
|
app.get('/500', function(req, res) {
|
|
|
|
|
res.json({errorMessage: 'testing'});
|
2013-12-12 16:02:12 -05:00
|
|
|
});
|
2013-08-11 14:50:12 -04:00
|
|
|
});
|
2013-07-31 15:17:03 -04:00
|
|
|
}
|
2013-12-17 20:34:21 +00:00
|
|
|
}(exports));
|