|
|
@ -1,32 +1,21 @@
|
|
|
|
"use strict";
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
var async = require('async'),
|
|
|
|
var async = require('async');
|
|
|
|
validator = require('validator'),
|
|
|
|
var validator = require('validator');
|
|
|
|
nconf = require('nconf'),
|
|
|
|
var nconf = require('nconf');
|
|
|
|
|
|
|
|
|
|
|
|
meta = require('../meta'),
|
|
|
|
var meta = require('../meta');
|
|
|
|
user = require('../user'),
|
|
|
|
var user = require('../user');
|
|
|
|
posts = require('../posts'),
|
|
|
|
var posts = require('../posts');
|
|
|
|
topics = require('../topics'),
|
|
|
|
var topics = require('../topics');
|
|
|
|
categories = require('../categories'),
|
|
|
|
var categories = require('../categories');
|
|
|
|
privileges = require('../privileges'),
|
|
|
|
var privileges = require('../privileges');
|
|
|
|
plugins = require('../plugins'),
|
|
|
|
var plugins = require('../plugins');
|
|
|
|
helpers = require('./helpers'),
|
|
|
|
var widgets = require('../widgets');
|
|
|
|
widgets = require('../widgets');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var apiController = {};
|
|
|
|
var apiController = {};
|
|
|
|
|
|
|
|
|
|
|
|
apiController.getConfig = function(req, res, next) {
|
|
|
|
apiController.getConfig = function(req, res, next) {
|
|
|
|
function filterConfig() {
|
|
|
|
|
|
|
|
plugins.fireHook('filter:config.get', config, function(err, config) {
|
|
|
|
|
|
|
|
if (res.locals.isAPI) {
|
|
|
|
|
|
|
|
res.status(200).json(config);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
next(err, config);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var config = {};
|
|
|
|
var config = {};
|
|
|
|
config.environment = process.env.NODE_ENV;
|
|
|
|
config.environment = process.env.NODE_ENV;
|
|
|
|
config.relative_path = nconf.get('relative_path');
|
|
|
|
config.relative_path = nconf.get('relative_path');
|
|
|
@ -51,7 +40,6 @@ apiController.getConfig = function(req, res, next) {
|
|
|
|
config.allowFileUploads = parseInt(meta.config.allowFileUploads, 10) === 1;
|
|
|
|
config.allowFileUploads = parseInt(meta.config.allowFileUploads, 10) === 1;
|
|
|
|
config.allowTopicsThumbnail = parseInt(meta.config.allowTopicsThumbnail, 10) === 1;
|
|
|
|
config.allowTopicsThumbnail = parseInt(meta.config.allowTopicsThumbnail, 10) === 1;
|
|
|
|
config.usePagination = parseInt(meta.config.usePagination, 10) === 1;
|
|
|
|
config.usePagination = parseInt(meta.config.usePagination, 10) === 1;
|
|
|
|
config.disableSocialButtons = parseInt(meta.config.disableSocialButtons, 10) === 1;
|
|
|
|
|
|
|
|
config.disableChat = parseInt(meta.config.disableChat, 10) === 1;
|
|
|
|
config.disableChat = parseInt(meta.config.disableChat, 10) === 1;
|
|
|
|
config.socketioTransports = nconf.get('socket.io:transports') || ['polling', 'websocket'];
|
|
|
|
config.socketioTransports = nconf.get('socket.io:transports') || ['polling', 'websocket'];
|
|
|
|
config.websocketAddress = nconf.get('socket.io:address') || '';
|
|
|
|
config.websocketAddress = nconf.get('socket.io:address') || '';
|
|
|
@ -73,27 +61,41 @@ apiController.getConfig = function(req, res, next) {
|
|
|
|
config.searchEnabled = plugins.hasListeners('filter:search.query');
|
|
|
|
config.searchEnabled = plugins.hasListeners('filter:search.query');
|
|
|
|
config.bootswatchSkin = 'default';
|
|
|
|
config.bootswatchSkin = 'default';
|
|
|
|
|
|
|
|
|
|
|
|
if (!req.user) {
|
|
|
|
async.waterfall([
|
|
|
|
return filterConfig();
|
|
|
|
function (next) {
|
|
|
|
}
|
|
|
|
if (!req.user) {
|
|
|
|
|
|
|
|
return next(null, config);
|
|
|
|
user.getSettings(req.user.uid, function(err, settings) {
|
|
|
|
}
|
|
|
|
|
|
|
|
user.getSettings(req.uid, function(err, settings) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
return next(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
config.usePagination = settings.usePagination;
|
|
|
|
|
|
|
|
config.topicsPerPage = settings.topicsPerPage;
|
|
|
|
|
|
|
|
config.postsPerPage = settings.postsPerPage;
|
|
|
|
|
|
|
|
config.notificationSounds = settings.notificationSounds;
|
|
|
|
|
|
|
|
config.userLang = req.query.lang || settings.userLang || config.defaultLang;
|
|
|
|
|
|
|
|
config.openOutgoingLinksInNewTab = settings.openOutgoingLinksInNewTab;
|
|
|
|
|
|
|
|
config.topicPostSort = settings.topicPostSort || config.topicPostSort;
|
|
|
|
|
|
|
|
config.categoryTopicSort = settings.categoryTopicSort || config.categoryTopicSort;
|
|
|
|
|
|
|
|
config.topicSearchEnabled = settings.topicSearchEnabled || false;
|
|
|
|
|
|
|
|
config.bootswatchSkin = settings.bootswatchSkin || config.bootswatchSkin;
|
|
|
|
|
|
|
|
next(null, config);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
function (config, next) {
|
|
|
|
|
|
|
|
plugins.fireHook('filter:config.get', config, next);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
], function(err, config) {
|
|
|
|
if (err) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
config.usePagination = settings.usePagination;
|
|
|
|
if (res.locals.isAPI) {
|
|
|
|
config.topicsPerPage = settings.topicsPerPage;
|
|
|
|
res.json(config);
|
|
|
|
config.postsPerPage = settings.postsPerPage;
|
|
|
|
} else {
|
|
|
|
config.notificationSounds = settings.notificationSounds;
|
|
|
|
next(null, config);
|
|
|
|
config.userLang = req.query.lang || settings.userLang || config.defaultLang;
|
|
|
|
}
|
|
|
|
config.openOutgoingLinksInNewTab = settings.openOutgoingLinksInNewTab;
|
|
|
|
|
|
|
|
config.topicPostSort = settings.topicPostSort || config.topicPostSort;
|
|
|
|
|
|
|
|
config.categoryTopicSort = settings.categoryTopicSort || config.categoryTopicSort;
|
|
|
|
|
|
|
|
config.topicSearchEnabled = settings.topicSearchEnabled || false;
|
|
|
|
|
|
|
|
config.bootswatchSkin = settings.bootswatchSkin || config.bootswatchSkin;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
filterConfig();
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -126,6 +128,16 @@ apiController.renderWidgets = function(req, res, next) {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
apiController.getObject = function(req, res, next) {
|
|
|
|
apiController.getObject = function(req, res, next) {
|
|
|
|
|
|
|
|
apiController.getObjectByType(req.uid, req.params.type, req.params.id, function(err, results) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
return next(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.json(results);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apiController.getObjectByType = function(uid, type, id, callback) {
|
|
|
|
var methods = {
|
|
|
|
var methods = {
|
|
|
|
post: {
|
|
|
|
post: {
|
|
|
|
canRead: privileges.posts.can,
|
|
|
|
canRead: privileges.posts.can,
|
|
|
@ -141,74 +153,101 @@ apiController.getObject = function(req, res, next) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (!methods[req.params.type]) {
|
|
|
|
if (!methods[type]) {
|
|
|
|
return next();
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
async.waterfall([
|
|
|
|
canRead: async.apply(methods[req.params.type].canRead, 'read', req.params.id, req.uid),
|
|
|
|
function (next) {
|
|
|
|
data: async.apply(methods[req.params.type].data, req.params.id)
|
|
|
|
methods[type].canRead('read', id, uid, next);
|
|
|
|
}, function(err, results) {
|
|
|
|
},
|
|
|
|
if (err || !results.data) {
|
|
|
|
function (canRead, next) {
|
|
|
|
return next(err);
|
|
|
|
if (!canRead) {
|
|
|
|
}
|
|
|
|
return next(new Error('[[error:no-privileges]]'));
|
|
|
|
|
|
|
|
}
|
|
|
|
if (!results.canRead) {
|
|
|
|
methods[type].data(id, next);
|
|
|
|
return helpers.notAllowed(req, res);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
], callback);
|
|
|
|
res.json(results.data);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apiController.getUserByUID = function(req, res, next) {
|
|
|
|
apiController.getUserByUID = function(req, res, next) {
|
|
|
|
var uid = req.params.uid ? req.params.uid : 0;
|
|
|
|
var uid = req.params.uid ? req.params.uid : 0;
|
|
|
|
|
|
|
|
|
|
|
|
getUserByUID(uid, res, next);
|
|
|
|
apiController.getUserDataByUID(req.uid, uid, function(err, data) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
return next(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json(data);
|
|
|
|
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
apiController.getUserByUsername = function(req, res, next) {
|
|
|
|
apiController.getUserByUsername = function(req, res, next) {
|
|
|
|
var username = req.params.username ? req.params.username : 0;
|
|
|
|
var username = req.params.username ? req.params.username : 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apiController.getUserDataByUsername(req.uid, username, function(err, data) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
return next(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json(data);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apiController.getUserByEmail = function(req, res, next) {
|
|
|
|
|
|
|
|
var email = req.params.email ? req.params.email : 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apiController.getUserDataByEmail(req.uid, email, function(err, data) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
return next(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json(data);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apiController.getUserDataByUsername = function(callerUid, username, callback) {
|
|
|
|
async.waterfall([
|
|
|
|
async.waterfall([
|
|
|
|
function(next) {
|
|
|
|
function(next) {
|
|
|
|
user.getUidByUsername(username, next);
|
|
|
|
user.getUidByUsername(username, next);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
function(uid, next) {
|
|
|
|
function(uid, next) {
|
|
|
|
getUserByUID(uid, res, next);
|
|
|
|
apiController.getUserDataByUID(callerUid, uid, next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
], next);
|
|
|
|
], callback);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
apiController.getUserByEmail = function(req, res, next) {
|
|
|
|
apiController.getUserDataByEmail = function(callerUid, email, callback) {
|
|
|
|
var email = req.params.email ? req.params.email : 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
async.waterfall([
|
|
|
|
function(next) {
|
|
|
|
function(next) {
|
|
|
|
user.getUidByEmail(email, next);
|
|
|
|
user.getUidByEmail(email, next);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
function(uid, next) {
|
|
|
|
function(uid, next) {
|
|
|
|
getUserByUID(uid, res, next);
|
|
|
|
apiController.getUserDataByUID(callerUid, uid, next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
], next);
|
|
|
|
], callback);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function getUserByUID(uid, res, next) {
|
|
|
|
apiController.getUserDataByUID = function(callerUid, uid, callback) {
|
|
|
|
|
|
|
|
if (!parseInt(callerUid, 10) && parseInt(meta.config.privateUserInfo, 10) === 1) {
|
|
|
|
|
|
|
|
return callback(new Error('[[error:no-privileges]]'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!parseInt(uid, 10)) {
|
|
|
|
|
|
|
|
return callback(new Error('[[error:no-user]]'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
async.parallel({
|
|
|
|
userData: async.apply(user.getUserData, uid),
|
|
|
|
userData: async.apply(user.getUserData, uid),
|
|
|
|
settings: async.apply(user.getSettings, uid)
|
|
|
|
settings: async.apply(user.getSettings, uid)
|
|
|
|
}, function(err, results) {
|
|
|
|
}, function(err, results) {
|
|
|
|
if (err || !results.userData) {
|
|
|
|
if (err || !results.userData) {
|
|
|
|
return next(err);
|
|
|
|
return callback(err || new Error('[[error:no-user]]'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
results.userData.email = results.settings.showemail ? results.userData.email : undefined;
|
|
|
|
results.userData.email = results.settings.showemail ? results.userData.email : undefined;
|
|
|
|
results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined;
|
|
|
|
results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined;
|
|
|
|
|
|
|
|
|
|
|
|
res.json(results.userData);
|
|
|
|
callback(null, results.userData);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
apiController.getModerators = function(req, res, next) {
|
|
|
|
apiController.getModerators = function(req, res, next) {
|
|
|
|
categories.getModerators(req.params.cid, function(err, moderators) {
|
|
|
|
categories.getModerators(req.params.cid, function(err, moderators) {
|
|
|
|