2014-03-03 11:45:23 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
2015-09-21 12:59:41 -04:00
|
|
|
var async = require('async'),
|
2015-01-04 11:37:20 -05:00
|
|
|
validator = require('validator'),
|
2015-09-21 12:59:41 -04:00
|
|
|
nconf = require('nconf'),
|
|
|
|
|
|
|
|
|
|
meta = require('../meta'),
|
|
|
|
|
user = require('../user'),
|
|
|
|
|
posts = require('../posts'),
|
|
|
|
|
topics = require('../topics'),
|
|
|
|
|
categories = require('../categories'),
|
|
|
|
|
privileges = require('../privileges'),
|
|
|
|
|
plugins = require('../plugins'),
|
|
|
|
|
helpers = require('./helpers'),
|
|
|
|
|
widgets = require('../widgets');
|
2014-03-03 11:45:23 -05:00
|
|
|
|
|
|
|
|
var apiController = {};
|
|
|
|
|
|
2014-03-03 12:16:46 -05:00
|
|
|
apiController.getConfig = function(req, res, next) {
|
2015-02-27 15:30:36 -05:00
|
|
|
function filterConfig() {
|
|
|
|
|
plugins.fireHook('filter:config.get', config, function(err, config) {
|
|
|
|
|
if (res.locals.isAPI) {
|
|
|
|
|
res.status(200).json(config);
|
|
|
|
|
} else {
|
|
|
|
|
next(err, config);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-19 02:07:14 -04:00
|
|
|
var config = {};
|
2014-11-29 23:38:36 -05:00
|
|
|
config.relative_path = nconf.get('relative_path');
|
2014-12-25 14:13:54 -05:00
|
|
|
config.socketioTransports = nconf.get('socket.io:transports') || ['polling', 'websocket'];
|
2014-12-19 18:58:28 -05:00
|
|
|
config.websocketAddress = nconf.get('socket.io:address') || '';
|
2015-04-16 11:30:53 +02:00
|
|
|
config.version = nconf.get('version');
|
2015-01-04 11:37:20 -05:00
|
|
|
config.siteTitle = validator.escape(meta.config.title || meta.config.browserTitle || 'NodeBB');
|
2015-08-26 15:54:54 -04:00
|
|
|
config.browserTitle = validator.escape(meta.config.browserTitle || meta.config.title || 'NodeBB');
|
2015-09-23 01:59:13 -04:00
|
|
|
config.titleLayout = (meta.config.titleLayout || '{pageTitle} | {browserTitle}').replace(/{/g, '{').replace(/}/g, '}');
|
2014-10-22 17:22:47 -04:00
|
|
|
config.showSiteTitle = parseInt(meta.config.showSiteTitle, 10) === 1;
|
2014-03-03 11:45:23 -05:00
|
|
|
config.postDelay = meta.config.postDelay;
|
|
|
|
|
config.minimumTitleLength = meta.config.minimumTitleLength;
|
|
|
|
|
config.maximumTitleLength = meta.config.maximumTitleLength;
|
|
|
|
|
config.minimumPostLength = meta.config.minimumPostLength;
|
2015-02-03 19:55:14 -05:00
|
|
|
config.maximumPostLength = meta.config.maximumPostLength;
|
2015-10-06 05:28:05 -04:00
|
|
|
config.topicStaleDays = parseInt(meta.config.topicStaleDays, 10) || 60;
|
2014-03-03 11:45:23 -05:00
|
|
|
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;
|
2015-05-04 13:54:29 -04:00
|
|
|
config.maximumAboutMeLength = meta.config.maximumAboutMeLength || 1000;
|
2014-03-03 11:45:23 -05:00
|
|
|
config.useOutgoingLinksPage = parseInt(meta.config.useOutgoingLinksPage, 10) === 1;
|
2014-05-16 16:28:35 -04:00
|
|
|
config.allowGuestSearching = parseInt(meta.config.allowGuestSearching, 10) === 1;
|
2015-06-17 14:57:10 -04:00
|
|
|
config.allowGuestUserSearching = parseInt(meta.config.allowGuestUserSearching, 10) === 1;
|
2014-12-31 12:36:25 -05:00
|
|
|
config.allowGuestHandles = parseInt(meta.config.allowGuestHandles, 10) === 1;
|
2014-03-03 11:45:23 -05:00
|
|
|
config.allowFileUploads = parseInt(meta.config.allowFileUploads, 10) === 1;
|
2015-04-14 14:27:42 -04:00
|
|
|
config.allowProfileImageUploads = parseInt(meta.config.allowProfileImageUploads) === 1;
|
2014-03-03 11:45:23 -05:00
|
|
|
config.allowTopicsThumbnail = parseInt(meta.config.allowTopicsThumbnail, 10) === 1;
|
2014-09-17 17:16:03 -04:00
|
|
|
config.allowAccountDelete = parseInt(meta.config.allowAccountDelete, 10) === 1;
|
2015-09-17 15:31:05 -04:00
|
|
|
config.allowUserHomePage = parseInt(meta.config.allowUserHomePage, 10) === 1;
|
2014-03-26 11:57:42 -04:00
|
|
|
config.privateUserInfo = parseInt(meta.config.privateUserInfo, 10) === 1;
|
2015-04-16 14:31:49 -04:00
|
|
|
config.privateTagListing = parseInt(meta.config.privateTagListing, 10) === 1;
|
2014-03-03 11:45:23 -05:00
|
|
|
config.usePagination = parseInt(meta.config.usePagination, 10) === 1;
|
|
|
|
|
config.disableSocialButtons = parseInt(meta.config.disableSocialButtons, 10) === 1;
|
2014-11-13 15:47:25 -05:00
|
|
|
config.disableChat = parseInt(meta.config.disableChat, 10) === 1;
|
2015-09-15 12:29:24 -04:00
|
|
|
config.maximumChatMessageLength = parseInt(meta.config.maximumChatMessageLength, 10) || 1000;
|
2014-04-03 17:36:27 -04:00
|
|
|
config.maxReconnectionAttempts = meta.config.maxReconnectionAttempts || 5;
|
2015-03-10 11:52:32 -04:00
|
|
|
config.reconnectionDelay = meta.config.reconnectionDelay || 1500;
|
2015-06-23 10:23:56 -04:00
|
|
|
config.minimumTagsPerTopic = meta.config.minimumTagsPerTopic || 0;
|
2015-06-23 13:25:26 -04:00
|
|
|
config.maximumTagsPerTopic = meta.config.maximumTagsPerTopic || 5;
|
2015-02-12 13:04:49 -05:00
|
|
|
config.minimumTagLength = meta.config.minimumTagLength || 3;
|
|
|
|
|
config.maximumTagLength = meta.config.maximumTagLength || 15;
|
2014-03-03 11:45:23 -05:00
|
|
|
config.topicsPerPage = meta.config.topicsPerPage || 20;
|
|
|
|
|
config.postsPerPage = meta.config.postsPerPage || 20;
|
|
|
|
|
config.maximumFileSize = meta.config.maximumFileSize;
|
2014-03-09 19:19:13 -04:00
|
|
|
config['theme:id'] = meta.config['theme:id'];
|
2015-08-17 16:11:26 -04:00
|
|
|
config['theme:src'] = meta.config['theme:src'];
|
2014-03-03 11:45:23 -05:00
|
|
|
config.defaultLang = meta.config.defaultLang || 'en_GB';
|
2015-07-22 14:17:29 -04:00
|
|
|
config.userLang = req.query.lang || config.defaultLang;
|
2014-03-03 11:45:23 -05:00
|
|
|
config.environment = process.env.NODE_ENV;
|
2014-07-24 21:11:46 -04:00
|
|
|
config.loggedIn = !!req.user;
|
2014-04-16 20:19:26 -04:00
|
|
|
config['cache-buster'] = meta.config['cache-buster'] || '';
|
2014-06-03 18:39:54 -04:00
|
|
|
config.requireEmailConfirmation = parseInt(meta.config.requireEmailConfirmation, 10) === 1;
|
2014-06-06 22:12:14 -04:00
|
|
|
config.topicPostSort = meta.config.topicPostSort || 'oldest_to_newest';
|
2015-01-08 13:47:15 -05:00
|
|
|
config.categoryTopicSort = meta.config.categoryTopicSort || 'newest_to_oldest';
|
2014-11-18 14:54:54 -05:00
|
|
|
config.csrf_token = req.csrfToken();
|
2014-11-24 12:38:44 -05:00
|
|
|
config.searchEnabled = plugins.hasListeners('filter:search.query');
|
2015-10-23 14:07:09 -04:00
|
|
|
config.bootswatchSkin = 'default';
|
2014-03-03 11:45:23 -05:00
|
|
|
|
|
|
|
|
if (!req.user) {
|
2015-02-27 15:30:36 -05:00
|
|
|
return filterConfig();
|
2014-03-03 11:45:23 -05:00
|
|
|
}
|
|
|
|
|
|
2014-03-26 11:57:42 -04:00
|
|
|
user.getSettings(req.user.uid, function(err, settings) {
|
2014-04-13 15:05:17 -04:00
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-26 11:57:42 -04:00
|
|
|
config.usePagination = settings.usePagination;
|
|
|
|
|
config.topicsPerPage = settings.topicsPerPage;
|
|
|
|
|
config.postsPerPage = settings.postsPerPage;
|
|
|
|
|
config.notificationSounds = settings.notificationSounds;
|
2015-07-22 14:17:29 -04:00
|
|
|
config.userLang = req.query.lang || settings.userLang || config.defaultLang;
|
2014-05-08 14:16:40 -04:00
|
|
|
config.openOutgoingLinksInNewTab = settings.openOutgoingLinksInNewTab;
|
2014-06-06 22:12:14 -04:00
|
|
|
config.topicPostSort = settings.topicPostSort || config.topicPostSort;
|
2015-01-08 13:47:15 -05:00
|
|
|
config.categoryTopicSort = settings.categoryTopicSort || config.categoryTopicSort;
|
2014-11-24 12:48:21 -05:00
|
|
|
config.topicSearchEnabled = settings.topicSearchEnabled || false;
|
2015-10-23 14:07:09 -04:00
|
|
|
config.bootswatchSkin = settings.bootswatchSkin || config.bootswatchSkin;
|
2014-03-26 11:57:42 -04:00
|
|
|
|
2015-02-27 15:30:36 -05:00
|
|
|
filterConfig();
|
2014-03-26 11:57:42 -04:00
|
|
|
});
|
2014-03-03 11:45:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2014-07-09 15:41:03 -04:00
|
|
|
apiController.renderWidgets = function(req, res, next) {
|
2015-10-28 16:17:27 -04:00
|
|
|
var areas = {
|
|
|
|
|
template: req.query.template,
|
|
|
|
|
locations: req.query.locations,
|
|
|
|
|
url: req.query.url
|
|
|
|
|
};
|
2014-07-09 15:41:03 -04:00
|
|
|
|
2014-09-16 10:12:12 -04:00
|
|
|
if (!areas.template || !areas.locations) {
|
2014-10-04 18:47:56 -04:00
|
|
|
return res.status(200).json({});
|
2014-07-09 19:23:03 -04:00
|
|
|
}
|
|
|
|
|
|
2015-09-16 00:36:13 -07:00
|
|
|
widgets.render(req.uid,
|
|
|
|
|
{
|
|
|
|
|
template: areas.template,
|
|
|
|
|
url: areas.url,
|
|
|
|
|
locations: areas.locations,
|
2015-09-16 00:46:11 -07:00
|
|
|
},
|
2015-09-16 00:36:13 -07:00
|
|
|
req,
|
|
|
|
|
res,
|
|
|
|
|
function(err, widgets) {
|
2014-09-21 18:56:36 -04:00
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
2014-10-04 18:47:56 -04:00
|
|
|
res.status(200).json(widgets);
|
2014-07-09 15:41:03 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-21 12:59:41 -04:00
|
|
|
apiController.getObject = function(req, res, next) {
|
|
|
|
|
var methods = {
|
|
|
|
|
post: {
|
|
|
|
|
canRead: privileges.posts.can,
|
|
|
|
|
data: posts.getPostData
|
|
|
|
|
},
|
|
|
|
|
topic: {
|
|
|
|
|
canRead: privileges.topics.can,
|
|
|
|
|
data: topics.getTopicData
|
|
|
|
|
},
|
|
|
|
|
category: {
|
|
|
|
|
canRead: privileges.categories.can,
|
|
|
|
|
data: categories.getCategoryData
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!methods[req.params.type]) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
|
canRead: async.apply(methods[req.params.type].canRead, 'read', req.params.id, req.uid),
|
|
|
|
|
data: async.apply(methods[req.params.type].data, req.params.id)
|
|
|
|
|
}, function(err, results) {
|
|
|
|
|
if (err || !results.data) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!results.canRead) {
|
|
|
|
|
return helpers.notAllowed(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json(results.data);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2015-09-25 13:11:11 -04:00
|
|
|
apiController.getUserByUID = function(req, res, next) {
|
|
|
|
|
var uid = req.params.uid ? req.params.uid : 0;
|
|
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
|
userData: async.apply(user.getUserData, uid),
|
|
|
|
|
settings: async.apply(user.getSettings, uid)
|
|
|
|
|
}, function(err, results) {
|
|
|
|
|
if (err || !results.userData) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results.userData.email = results.settings.showemail ? results.userData.email : undefined;
|
|
|
|
|
results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined;
|
|
|
|
|
|
|
|
|
|
res.json(results.userData);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apiController.getModerators = function(req, res, next) {
|
|
|
|
|
categories.getModerators(req.params.cid, function(err, moderators) {
|
2015-09-25 18:29:02 -04:00
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
2015-09-25 13:11:11 -04:00
|
|
|
res.json({moderators: moderators});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apiController.getRecentPosts = function(req, res, next) {
|
|
|
|
|
posts.getRecentPosts(req.uid, 0, 19, req.params.term, function (err, data) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json(data);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-10 20:31:57 +01:00
|
|
|
module.exports = apiController;
|