Files
NodeBB/src/routes/api.js

475 lines
13 KiB
JavaScript
Raw Normal View History

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'),
groups = require('../groups'),
auth = require('./authentication'),
topics = require('../topics'),
ThreadTools = require('../threadTools'),
posts = require('../posts'),
categories = require('../categories'),
2013-11-30 13:35:42 -05:00
categoryTools = require('../categoryTools')
meta = require('../meta'),
Plugins = require('../plugins'),
utils = require('../../public/src/utils'),
2014-01-17 08:53:23 -05:00
translator = require('../../public/src/translator'),
pkg = require('../../package.json');
2013-08-20 12:11:17 -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.all('*', function(req, res, next) {
if(req.user) {
user.updateLastOnlineTime(req.user.uid);
}
next();
});
2013-09-24 14:18:41 -04:00
app.get('/get_templates_listing', function (req, res) {
utils.walk(path.join(__dirname, '../../', 'public/templates'), function (err, data) {
res.json(data.concat(app.get_custom_templates()).filter(function(value, index, self) {
return self.indexOf(value) === index;
}));
2013-09-24 14:18:41 -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;
2013-12-31 14:25:26 -05:00
config.maximumTitleLength = meta.config.maximumTitleLength;
2013-09-24 14:18:41 -04:00
config.minimumPostLength = meta.config.minimumPostLength;
config.hasImageUploadPlugin = Plugins.hasListeners('filter:uploadImage');
2014-02-11 17:16:17 -05:00
config.maximumProfileImageSize = meta.config.maximumProfileImageSize;
2013-09-24 14:18:41 -04:00
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;
config.useOutgoingLinksPage = parseInt(meta.config.useOutgoingLinksPage, 10) === 1;
config.allowGuestPosting = parseInt(meta.config.allowGuestPosting, 10) === 1;
config.allowFileUploads = parseInt(meta.config.allowFileUploads, 10) === 1;
2014-01-24 13:27:36 -05:00
config.usePagination = parseInt(meta.config.usePagination, 10) === 1;
config.topicsPerPage = meta.config.topicsPerPage || 20;
config.postsPerPage = meta.config.postsPerPage || 20;
2013-12-31 17:01:51 -05:00
config.maximumFileSize = meta.config.maximumFileSize;
config.defaultLang = meta.config.defaultLang || 'en_GB';
config.environment = process.env.NODE_ENV;
2013-12-19 15:43:15 -05:00
2014-02-10 14:15:54 -05:00
if (!req.user) {
return res.json(200, config);
}
if(req.user) {
user.getSettings(req.user.uid, function(err, settings) {
if(err) {
return next(err);
}
config.usePagination = settings.usePagination;
config.topicsPerPage = settings.topicsPerPage;
config.postsPerPage = settings.postsPerPage;
res.json(200, config);
});
}
2013-09-24 14:18:41 -04:00
});
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) {
2014-01-31 22:25:59 -05:00
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
2014-01-31 22:25:59 -05:00
function canSee(category, next) {
categoryTools.privileges(category.cid, ((req.user) ? req.user.uid || 0 : 0), function(err, privileges) {
next(!err && privileges.read);
});
}
function getRecentReplies(category, callback) {
2014-01-08 21:50:19 -05:00
categories.getRecentReplies(category.cid, uid, parseInt(category.numRecentReplies, 10), function (err, posts) {
2013-09-24 14:18:41 -04:00
category.posts = posts;
category.post_count = posts.length > 2 ? 2 : posts.length; // this was a hack to make metro work back in the day, post_count should just = length
2013-09-24 14:18:41 -04:00
callback(null);
});
}
2014-01-31 22:25:59 -05:00
async.filter(data.categories, canSee, function(visibleCategories) {
data.categories = visibleCategories;
async.each(data.categories, getRecentReplies, function (err) {
var motdString,
assemble = function() {
data.motd_class = (parseInt(meta.config.show_motd, 10) === 1 || meta.config.show_motd === undefined) ? '' : ' none';
data.motd_class += (meta.config.motd && meta.config.motd.length > 0) ? '' : ' default';
2014-01-31 22:25:59 -05:00
data.motd_class += meta.config.motd_class ? ' ' + meta.config.motd_class : '';
2014-02-10 19:14:50 -05:00
data.motd = motdString;
2014-01-31 22:25:59 -05:00
res.json(data);
};
if (!meta.config.motd) {
2014-02-10 19:14:50 -05:00
translator.translate('\n\n<h1>NodeBB</h1> <small><span>v' + pkg.version + '</span></small>\n\n<h5>[[global:motd.welcome]]</h5>\
2014-01-31 22:25:59 -05:00
<div class="btn-group">\
<a target="_blank" href="https://www.nodebb.org" class="btn btn-link btn-md">\
<i class="fa fa-comment"></i>\
<span>&nbsp;[[global:motd.get]]</span>\
</a>\
<a target="_blank" href="https://github.com/designcreateplay/NodeBB" class="btn btn-link btn-md">\
<i class="fa fa-github"></i>\
<span>&nbsp;[[global:motd.fork]]</span>\
</a>\
<a target="_blank" href="https://facebook.com/NodeBB" class="btn btn-link btn-md">\
<i class="fa fa-facebook"></i>\
<span>&nbsp;[[global:motd.like]]</span>\
</a>\
<a target="_blank" href="https://twitter.com/NodeBB" class="btn btn-link btn-md">\
<i class="fa fa-twitter"></i>\
<span>&nbsp;[[global:motd.follow]]</span>\
</a>\
</div>\
', function(motd) {
motdString = motd;
assemble();
});
} else {
motdString = meta.config.motd;
2014-01-20 19:05:19 -05:00
assemble();
2014-01-31 22:25:59 -05: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,
emailersPresent = Plugins.hasListeners('action:email.send');
2013-09-24 14:18:41 -04:00
if (num_strategies == 0) {
data = {
'login_window:spansize': 'col-md-12',
'alternate_logins': false
2013-09-24 14:18:41 -04:00
};
} else {
data = {
'login_window:spansize': 'col-md-6',
'alternate_logins': true
2013-09-24 14:18:41 -04:00
}
}
data.authentication = login_strategies;
2013-09-24 14:18:41 -04:00
data.token = res.locals.csrf_token;
data.showResetLink = emailersPresent;
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',
'alternate_logins': false
2013-09-24 14:18:41 -04:00
};
} else {
data = {
'register_window:spansize': 'col-md-6',
'alternate_logins': true
2013-09-24 14:18:41 -04: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;
data.termsOfUse = meta.config.termsOfUse;
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('/topic/:id/:slug?', function (req, res, next) {
2013-09-24 14:18:41 -04:00
var uid = (req.user) ? req.user.uid : 0;
var page = 1;
if(req.query && req.query.page) {
page = req.query.page;
}
2014-01-25 19:50:50 -05:00
if(!utils.isNumber(page) || parseInt(page, 10) < 1) {
return res.send(404);
}
2014-02-10 14:15:54 -05:00
user.getSettings(uid, function(err, settings) {
if(err) {
return next(err);
}
var start = (page - 1) * settings.postsPerPage;
var end = start + settings.postsPerPage - 1;
2014-01-24 20:00:56 -05:00
2014-02-10 14:15:54 -05:00
ThreadTools.privileges(req.params.id, uid, function(err, privileges) {
if (privileges.read) {
topics.getTopicWithPosts(req.params.id, uid, start, end, false, function (err, data) {
if(err) {
return next(err);
}
2014-02-10 14:15:54 -05:00
if(page > data.pageCount) {
return res.send(404);
}
2014-02-10 14:15:54 -05:00
data.currentPage = page;
data.privileges = privileges;
2014-02-10 14:15:54 -05:00
if (parseInt(data.deleted, 10) === 1 && parseInt(data.expose_tools, 10) === 0) {
return res.json(404, {});
}
2014-01-24 13:27:36 -05:00
2014-02-10 14:15:54 -05:00
res.json(data);
});
} else {
res.send(403);
}
});
2013-09-24 14:18:41 -04:00
});
});
app.get('/category/:id/:slug?', function (req, res, next) {
var uid = (req.user) ? req.user.uid : 0;
var page = 1;
if(req.query && req.query.page) {
page = req.query.page;
}
2014-01-25 19:57:25 -05:00
if(!utils.isNumber(page) || parseInt(page, 10) < 1) {
return res.send(404);
}
2014-02-10 14:15:54 -05:00
user.getSettings(uid, function(err, settings) {
if(err) {
return next(err);
}
2013-11-13 19:54:46 -05:00
2014-02-10 14:15:54 -05:00
var start = (page - 1) * settings.topicsPerPage;
var end = start + settings.topicsPerPage - 1;
2013-12-19 14:57:59 -05:00
2014-02-10 14:15:54 -05:00
categoryTools.privileges(req.params.id, uid, function(err, privileges) {
if (!err && privileges.read) {
categories.getCategoryById(req.params.id, start, end, uid, function (err, data) {
if(err) {
return next(err);
}
2014-01-21 15:17:41 -05:00
2014-02-10 14:15:54 -05:00
data.currentPage = page;
data.privileges = privileges;
if (data && parseInt(data.disabled, 10) === 0) {
res.json(data);
} else {
next();
}
}, req.params.id, uid);
} else {
res.send(403);
}
});
2013-11-13 19:54:46 -05:00
});
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;
2014-01-26 17:25:18 -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
});
});
2014-01-30 19:46:25 -05:00
app.get('/popular/:set?', function (req, res, next) {
var uid = (req.user) ? req.user.uid : 0;
var set = 'topics:' + req.params.set;
if(!req.params.set) {
set = 'topics:posts';
}
topics.getTopicsFromSet(uid, set, 0, 19, function(err, data) {
if(err) {
return next(err);
}
res.json(data);
});
});
2014-01-16 20:53:32 -05:00
app.get('/unread', function (req, res, next) {
2013-09-24 14:18:41 -04:00
var uid = (req.user) ? req.user.uid : 0;
2014-01-16 20:53:32 -05:00
topics.getUnreadTopics(uid, 0, 19, function (err, data) {
if(err) {
return next(err);
}
2013-09-24 14:18:41 -04:00
res.json(data);
});
});
2014-01-16 20:53:32 -05:00
app.get('/unread/total', function (req, res, next) {
2013-09-24 14:18:41 -04:00
var uid = (req.user) ? req.user.uid : 0;
2014-01-16 20:53:32 -05:00
topics.getTotalUnread(uid, function (err, data) {
if(err) {
return next(err);
}
2013-09-24 14:18:41 -04:00
res.json(data);
});
});
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-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-danger',
2013-09-24 14:18:41 -04:00
title: 'An error occurred...',
text: 'There was a problem validating your email address. Perhaps the code was invalid or has expired.'
});
}
});
});
2013-09-24 14:18:41 -04:00
app.get('/outgoing', function (req, res) {
var url = req.query.url;
if (url) {
res.json({
2013-11-21 22:15:04 -05:00
url: url,
title: meta.config.title
});
} else {
2013-09-24 14:18:41 -04:00
res.status(404);
res.redirect(nconf.get('relative_path') + '/404');
}
});
2013-09-24 14:18:41 -04:00
app.get('/search', function (req, res) {
if ((req.user && req.user.uid) || meta.config.allowGuestSearching === '1') {
return res.json({
show_no_topics: 'hide',
show_no_posts: 'hide',
show_results: 'hide',
search_query: '',
posts: [],
topics: []
});
} else {
res.send(403);
}
});
2013-09-24 14:18:41 -04:00
app.get('/search/:term', function (req, res, next) {
2014-01-14 18:04:54 -05:00
var limit = 50;
2013-09-24 14:18:41 -04:00
function searchPosts(callback) {
2014-01-14 18:04:54 -05:00
db.search('post', req.params.term, limit, 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
2014-01-17 18:12:56 -05:00
posts.getPostSummaryByPids(pids, false, callback);
});
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) {
2014-01-14 18:04:54 -05:00
db.search('topic', req.params.term, limit, 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
}
2014-01-17 18:12:56 -05:00
topics.getTopicsByTids(tids, 0, 0, callback);
});
2013-09-24 14:18:41 -04:00
}
2013-08-08 11:40:31 -04:00
if ((req.user && req.user.uid) || meta.config.allowGuestSearching === '1') {
async.parallel([searchPosts, searchTopics], function (err, results) {
if (err) {
2014-01-14 18:04:54 -05:00
return next(err);
}
2013-09-24 14:18:41 -04:00
2014-01-17 18:12:56 -05:00
if(!results) {
2014-01-17 18:14:00 -05:00
results = [];
2014-01-17 18:12:56 -05:00
results[0] = results[1] = [];
}
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-09-24 14:18:41 -04: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-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
});
}
}(exports));