moved api-only routes into routes/api.js, cleanup & linting
parent
d75bc9578b
commit
fe9d4efa98
@ -1,357 +1,224 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
var path = require('path'),
|
var path = require('path'),
|
||||||
nconf = require('nconf'),
|
|
||||||
async = require('async'),
|
async = require('async'),
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
|
|
||||||
db = require('../database'),
|
db = require('../database'),
|
||||||
user = require('../user'),
|
user = require('../user'),
|
||||||
groups = require('../groups'),
|
|
||||||
auth = require('./authentication'),
|
|
||||||
topics = require('../topics'),
|
topics = require('../topics'),
|
||||||
ThreadTools = require('../threadTools'),
|
|
||||||
posts = require('../posts'),
|
posts = require('../posts'),
|
||||||
categories = require('../categories'),
|
categories = require('../categories'),
|
||||||
categoryTools = require('../categoryTools'),
|
|
||||||
meta = require('../meta'),
|
meta = require('../meta'),
|
||||||
Plugins = require('../plugins'),
|
plugins = require('../plugins'),
|
||||||
utils = require('../../public/src/utils'),
|
utils = require('../../public/src/utils'),
|
||||||
translator = require('../../public/src/translator'),
|
|
||||||
pkg = require('../../package.json');
|
pkg = require('../../package.json');
|
||||||
|
|
||||||
|
|
||||||
(function (Api) {
|
module.exports = function(app, middleware, controllers) {
|
||||||
Api.createRoutes = function (app) {
|
app.namespace('/api', function () {
|
||||||
|
app.all('*', function(req, res, next) {
|
||||||
app.namespace('/api', function () {
|
if(req.user) {
|
||||||
app.all('*', function(req, res, next) {
|
user.updateLastOnlineTime(req.user.uid);
|
||||||
if(req.user) {
|
}
|
||||||
user.updateLastOnlineTime(req.user.uid);
|
|
||||||
}
|
|
||||||
|
|
||||||
db.sortedSetAdd('ip:recent', Date.now(), req.ip || 'Unknown');
|
|
||||||
res.locals.isAPI = true;
|
|
||||||
|
|
||||||
next();
|
db.sortedSetAdd('ip:recent', Date.now(), req.ip || 'Unknown');
|
||||||
});
|
res.locals.isAPI = true;
|
||||||
|
|
||||||
app.get('/get_templates_listing', function (req, res) {
|
next();
|
||||||
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;
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get('/config', function (req, res, next) {
|
|
||||||
var config = require('../../public/config.json');
|
|
||||||
|
|
||||||
config.version = pkg.version;
|
|
||||||
config.postDelay = meta.config.postDelay;
|
|
||||||
config.minimumTitleLength = meta.config.minimumTitleLength;
|
|
||||||
config.maximumTitleLength = meta.config.maximumTitleLength;
|
|
||||||
config.minimumPostLength = meta.config.minimumPostLength;
|
|
||||||
config.hasImageUploadPlugin = Plugins.hasListeners('filter:uploadImage');
|
|
||||||
config.maximumProfileImageSize = meta.config.maximumProfileImageSize;
|
|
||||||
config.minimumUsernameLength = meta.config.minimumUsernameLength;
|
|
||||||
config.maximumUsernameLength = meta.config.maximumUsernameLength;
|
|
||||||
config.minimumPasswordLength = meta.config.minimumPasswordLength;
|
|
||||||
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;
|
|
||||||
config.allowTopicsThumbnail = parseInt(meta.config.allowTopicsThumbnail, 10) === 1;
|
|
||||||
config.usePagination = parseInt(meta.config.usePagination, 10) === 1;
|
|
||||||
config.disableSocialButtons = parseInt(meta.config.disableSocialButtons, 10) === 1;
|
|
||||||
config.topicsPerPage = meta.config.topicsPerPage || 20;
|
|
||||||
config.postsPerPage = meta.config.postsPerPage || 20;
|
|
||||||
config.maximumFileSize = meta.config.maximumFileSize;
|
|
||||||
config.defaultLang = meta.config.defaultLang || 'en_GB';
|
|
||||||
config.environment = process.env.NODE_ENV;
|
|
||||||
|
|
||||||
if (!req.user) {
|
|
||||||
return res.json(200, config);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(req.user) {
|
app.get('/user/uid/:uid', middleware.checkGlobalPrivacySettings, controllers.accounts.getUserByUID);
|
||||||
user.getSettings(req.user.uid, function(err, settings) {
|
|
||||||
if(err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
config.usePagination = settings.usePagination;
|
app.get('/get_templates_listing', function (req, res) {
|
||||||
config.topicsPerPage = settings.topicsPerPage;
|
utils.walk(path.join(__dirname, '../../', 'public/templates'), function (err, data) {
|
||||||
config.postsPerPage = settings.postsPerPage;
|
res.json(data.concat(app.get_custom_templates()).filter(function(value, index, self) {
|
||||||
res.json(200, config);
|
return self.indexOf(value) === index;
|
||||||
});
|
}));
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
app.get('/topic/:id/:slug?', function (req, res, next) {
|
app.get('/config', function (req, res, next) {
|
||||||
var uid = req.user? parseInt(req.user.uid, 10) : 0;
|
var config = require('../../public/config.json');
|
||||||
var tid = req.params.id;
|
|
||||||
var page = 1;
|
config.version = pkg.version;
|
||||||
if(req.query && req.query.page) {
|
config.postDelay = meta.config.postDelay;
|
||||||
page = req.query.page;
|
config.minimumTitleLength = meta.config.minimumTitleLength;
|
||||||
}
|
config.maximumTitleLength = meta.config.maximumTitleLength;
|
||||||
|
config.minimumPostLength = meta.config.minimumPostLength;
|
||||||
if(!utils.isNumber(page) || parseInt(page, 10) < 1) {
|
config.hasImageUploadPlugin = plugins.hasListeners('filter:uploadImage');
|
||||||
return res.send(404);
|
config.maximumProfileImageSize = meta.config.maximumProfileImageSize;
|
||||||
}
|
config.minimumUsernameLength = meta.config.minimumUsernameLength;
|
||||||
|
config.maximumUsernameLength = meta.config.maximumUsernameLength;
|
||||||
|
config.minimumPasswordLength = meta.config.minimumPasswordLength;
|
||||||
|
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;
|
||||||
|
config.allowTopicsThumbnail = parseInt(meta.config.allowTopicsThumbnail, 10) === 1;
|
||||||
|
config.usePagination = parseInt(meta.config.usePagination, 10) === 1;
|
||||||
|
config.disableSocialButtons = parseInt(meta.config.disableSocialButtons, 10) === 1;
|
||||||
|
config.topicsPerPage = meta.config.topicsPerPage || 20;
|
||||||
|
config.postsPerPage = meta.config.postsPerPage || 20;
|
||||||
|
config.maximumFileSize = meta.config.maximumFileSize;
|
||||||
|
config.defaultLang = meta.config.defaultLang || 'en_GB';
|
||||||
|
config.environment = process.env.NODE_ENV;
|
||||||
|
|
||||||
|
if (!req.user) {
|
||||||
|
return res.json(200, config);
|
||||||
|
}
|
||||||
|
|
||||||
user.getSettings(uid, function(err, settings) {
|
if(req.user) {
|
||||||
|
user.getSettings(req.user.uid, function(err, settings) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
var start = (page - 1) * settings.postsPerPage;
|
config.usePagination = settings.usePagination;
|
||||||
var end = start + settings.postsPerPage - 1;
|
config.topicsPerPage = settings.topicsPerPage;
|
||||||
|
config.postsPerPage = settings.postsPerPage;
|
||||||
ThreadTools.privileges(tid, uid, function(err, privileges) {
|
res.json(200, config);
|
||||||
if(err) {
|
});
|
||||||
return next(err);
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
if(!privileges.read) {
|
|
||||||
res.send(403);
|
|
||||||
}
|
|
||||||
|
|
||||||
topics.getTopicWithPosts(tid, uid, start, end, function (err, data) {
|
|
||||||
if(err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(page > data.pageCount) {
|
|
||||||
return res.send(404);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parseInt(data.deleted, 10) === 1 && parseInt(data.expose_tools, 10) === 0) {
|
|
||||||
return res.json(404, {});
|
|
||||||
}
|
|
||||||
|
|
||||||
data.currentPage = page;
|
|
||||||
data.privileges = privileges;
|
|
||||||
|
|
||||||
if (uid) {
|
|
||||||
topics.markAsRead(tid, uid, function(err) {
|
|
||||||
topics.pushUnreadCount(uid);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
topics.increaseViewCount(tid);
|
|
||||||
|
|
||||||
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
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
} else {
|
||||||
|
res.send(403);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!utils.isNumber(page) || parseInt(page, 10) < 1) {
|
app.get('/search/:term', function (req, res, next) {
|
||||||
return res.send(404);
|
if (!plugins.hasListeners('filter:search.query')) {
|
||||||
}
|
return res.redirect('/404');
|
||||||
|
}
|
||||||
|
|
||||||
user.getSettings(uid, function(err, settings) {
|
function searchPosts(callback) {
|
||||||
if(err) {
|
plugins.fireHook('filter:search.query', {
|
||||||
return next(err);
|
index: 'post',
|
||||||
|
query: req.params.term
|
||||||
|
}, function(err, pids) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
var start = (page - 1) * settings.topicsPerPage,
|
posts.getPostSummaryByPids(pids, false, callback);
|
||||||
end = start + settings.topicsPerPage - 1;
|
});
|
||||||
|
}
|
||||||
|
|
||||||
categoryTools.privileges(req.params.id, uid, function(err, privileges) {
|
function searchTopics(callback) {
|
||||||
if (err) {
|
plugins.fireHook('filter:search.query', {
|
||||||
return next(err);
|
index: 'topic',
|
||||||
}
|
query: req.params.term
|
||||||
|
}, function(err, tids) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
if (!privileges.read) {
|
topics.getTopicsByTids(tids, 0, callback);
|
||||||
return res.send(403);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
categories.getCategoryById(req.params.id, start, end, uid, function (err, data) {
|
if ((req.user && req.user.uid) || meta.config.allowGuestSearching === '1') {
|
||||||
if(err) {
|
async.parallel([searchPosts, searchTopics], function (err, results) {
|
||||||
return next(err);
|
if (err) {
|
||||||
}
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
data.currentPage = page;
|
if(!results) {
|
||||||
data.privileges = privileges;
|
results = [];
|
||||||
|
results[0] = results[1] = [];
|
||||||
|
}
|
||||||
|
|
||||||
if (data && !data.disabled) {
|
return res.json({
|
||||||
res.json(data);
|
show_no_topics: results[1].length ? 'hide' : '',
|
||||||
} else {
|
show_no_posts: results[0].length ? 'hide' : '',
|
||||||
next();
|
show_results: '',
|
||||||
}
|
search_query: req.params.term,
|
||||||
});
|
posts: results[0],
|
||||||
|
topics: results[1],
|
||||||
|
post_matches : results[0].length,
|
||||||
|
topic_matches : results[1].length
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
} else {
|
||||||
|
res.send(403);
|
||||||
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
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
res.send(403);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get('/search', function (req, res) {
|
function upload(req, res, filesIterator, next) {
|
||||||
if ((req.user && req.user.uid) || meta.config.allowGuestSearching === '1') {
|
if(!req.user) {
|
||||||
return res.json({
|
return res.json(403, {message:'not allowed'});
|
||||||
show_no_topics: 'hide',
|
}
|
||||||
show_no_posts: 'hide',
|
var files = req.files.files;
|
||||||
show_results: 'hide',
|
|
||||||
search_query: '',
|
|
||||||
posts: [],
|
|
||||||
topics: []
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
res.send(403);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get('/search/:term', function (req, res, next) {
|
if(!Array.isArray(files)) {
|
||||||
if (!Plugins.hasListeners('filter:search.query')) {
|
return res.json(500, {message: 'invalid files'});
|
||||||
return res.redirect('/404');
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function searchPosts(callback) {
|
// multiple files
|
||||||
Plugins.fireHook('filter:search.query', {
|
if(Array.isArray(files[0])) {
|
||||||
index: 'post',
|
files = files[0];
|
||||||
query: req.params.term
|
}
|
||||||
}, function(err, pids) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
posts.getPostSummaryByPids(pids, false, callback);
|
function deleteTempFiles() {
|
||||||
});
|
for(var i=0; i<files.length; ++i) {
|
||||||
|
fs.unlink(files[i].path);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function searchTopics(callback) {
|
async.map(files, filesIterator, function(err, images) {
|
||||||
Plugins.fireHook('filter:search.query', {
|
deleteTempFiles();
|
||||||
index: 'topic',
|
|
||||||
query: req.params.term
|
|
||||||
}, function(err, tids) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
topics.getTopicsByTids(tids, 0, callback);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((req.user && req.user.uid) || meta.config.allowGuestSearching === '1') {
|
if(err) {
|
||||||
async.parallel([searchPosts, searchTopics], function (err, results) {
|
return res.send(500, err.message);
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!results) {
|
|
||||||
results = [];
|
|
||||||
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],
|
|
||||||
topics: results[1],
|
|
||||||
post_matches : results[0].length,
|
|
||||||
topic_matches : results[1].length
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
res.send(403);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
function upload(req, res, filesIterator, next) {
|
// if this was not a XMLHttpRequest (hence the req.xhr check http://expressjs.com/api.html#req.xhr)
|
||||||
if(!req.user) {
|
// then most likely it's submit via the iFrame workaround, via the jquery.form plugin's ajaxSubmit()
|
||||||
return res.json(403, {message:'not allowed'});
|
// we need to send it as text/html so IE8 won't trigger a file download for the json response
|
||||||
}
|
// malsup.com/jquery/form/#file-upload
|
||||||
var files = req.files.files;
|
|
||||||
|
|
||||||
if(!Array.isArray(files)) {
|
// Also, req.send is safe for both types, if the response was an object, res.send will automatically submit as application/json
|
||||||
return res.json(500, {message: 'invalid files'});
|
// expressjs.com/api.html#res.send
|
||||||
}
|
res.send(200, req.xhr ? images : JSON.stringify(images));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// multiple files
|
app.post('/post/upload', function(req, res, next) {
|
||||||
if(Array.isArray(files[0])) {
|
upload(req, res, function(file, next) {
|
||||||
files = files[0];
|
if(file.type.match(/image./)) {
|
||||||
|
posts.uploadPostImage(file, next);
|
||||||
|
} else {
|
||||||
|
posts.uploadPostFile(file, next);
|
||||||
}
|
}
|
||||||
|
}, next);
|
||||||
|
});
|
||||||
|
|
||||||
function deleteTempFiles() {
|
app.post('/topic/thumb/upload', function(req, res, next) {
|
||||||
for(var i=0; i<files.length; ++i) {
|
upload(req, res, function(file, next) {
|
||||||
fs.unlink(files[i].path);
|
if(file.type.match(/image./)) {
|
||||||
}
|
topics.uploadTopicThumb(file, next);
|
||||||
|
} else {
|
||||||
|
res.json(500, {message: 'Invalid File'});
|
||||||
}
|
}
|
||||||
|
}, next);
|
||||||
|
});
|
||||||
|
|
||||||
async.map(files, filesIterator, function(err, images) {
|
app.get('/categories/:cid/moderators', function(req, res) {
|
||||||
deleteTempFiles();
|
categories.getModerators(req.params.cid, function(err, moderators) {
|
||||||
|
res.json({moderators: moderators});
|
||||||
if(err) {
|
|
||||||
return res.send(500, err.message);
|
|
||||||
}
|
|
||||||
|
|
||||||
// if this was not a XMLHttpRequest (hence the req.xhr check http://expressjs.com/api.html#req.xhr)
|
|
||||||
// then most likely it's submit via the iFrame workaround, via the jquery.form plugin's ajaxSubmit()
|
|
||||||
// we need to send it as text/html so IE8 won't trigger a file download for the json response
|
|
||||||
// malsup.com/jquery/form/#file-upload
|
|
||||||
|
|
||||||
// Also, req.send is safe for both types, if the response was an object, res.send will automatically submit as application/json
|
|
||||||
// expressjs.com/api.html#res.send
|
|
||||||
res.send(200, req.xhr ? images : JSON.stringify(images));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
app.post('/post/upload', function(req, res, next) {
|
|
||||||
upload(req, res, function(file, next) {
|
|
||||||
if(file.type.match(/image./)) {
|
|
||||||
posts.uploadPostImage(file, next);
|
|
||||||
} else {
|
|
||||||
posts.uploadPostFile(file, next);
|
|
||||||
}
|
|
||||||
}, next)
|
|
||||||
});
|
|
||||||
|
|
||||||
app.post('/topic/thumb/upload', function(req, res, next) {
|
|
||||||
upload(req, res, function(file, next) {
|
|
||||||
if(file.type.match(/image./)) {
|
|
||||||
topics.uploadTopicThumb(file, next);
|
|
||||||
} else {
|
|
||||||
res.json(500, {message: 'Invalid File'});
|
|
||||||
}
|
|
||||||
}, next);
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get('/reset', function (req, res) {
|
|
||||||
res.json({});
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get('/reset/:code', function (req, res) {
|
|
||||||
res.json({
|
|
||||||
reset_code: req.params.code
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
app.namespace('/categories', function() {
|
|
||||||
app.get(':cid/moderators', function(req, res) {
|
|
||||||
categories.getModerators(req.params.cid, function(err, moderators) {
|
|
||||||
res.json({moderators: moderators});
|
|
||||||
})
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
}(exports));
|
|
||||||
|
// this should have been in the API namespace
|
||||||
|
// also, perhaps pass in :userslug so we can use checkAccountPermissions middleware - in future will allow admins to upload a picture for a user
|
||||||
|
app.post('/user/uploadpicture', middleware.checkGlobalPrivacySettings, /*middleware.checkAccountPermissions,*/ controllers.accounts.uploadPicture);
|
||||||
|
};
|
Loading…
Reference in New Issue