Files
NodeBB/src/controllers/api.js

323 lines
9.7 KiB
JavaScript
Raw Normal View History

"use strict";
2016-03-08 11:24:32 +02:00
var async = require('async');
var validator = require('validator');
var nconf = require('nconf');
var meta = require('../meta');
var user = require('../user');
var posts = require('../posts');
var topics = require('../topics');
var categories = require('../categories');
var privileges = require('../privileges');
var plugins = require('../plugins');
var widgets = require('../widgets');
2016-07-11 12:56:23 +03:00
var accountHelpers = require('../controllers/accounts/helpers');
var apiController = {};
apiController.getConfig = function (req, res, next) {
var config = {};
2016-02-10 12:53:39 +02:00
config.environment = process.env.NODE_ENV;
2014-11-29 23:38:36 -05:00
config.relative_path = nconf.get('relative_path');
config.version = nconf.get('version');
config.siteTitle = validator.escape(String(meta.config.title || meta.config.browserTitle || 'NodeBB'));
config.browserTitle = validator.escape(String(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;
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;
2016-02-10 12:53:39 +02:00
config.minimumTagsPerTopic = meta.config.minimumTagsPerTopic || 0;
config.maximumTagsPerTopic = meta.config.maximumTagsPerTopic || 5;
config.minimumTagLength = meta.config.minimumTagLength || 3;
config.maximumTagLength = meta.config.maximumTagLength || 15;
config.hasImageUploadPlugin = plugins.hasListeners('filter:uploadImage');
config.useOutgoingLinksPage = parseInt(meta.config.useOutgoingLinksPage, 10) === 1;
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;
config.allowFileUploads = parseInt(meta.config.allowFileUploads, 10) === 1;
config.allowTopicsThumbnail = parseInt(meta.config.allowTopicsThumbnail, 10) === 1;
config.usePagination = parseInt(meta.config.usePagination, 10) === 1;
2014-11-13 15:47:25 -05:00
config.disableChat = parseInt(meta.config.disableChat, 10) === 1;
2016-02-10 12:53:39 +02:00
config.socketioTransports = nconf.get('socket.io:transports') || ['polling', 'websocket'];
config.websocketAddress = nconf.get('socket.io:address') || '';
2014-04-03 17:36:27 -04:00
config.maxReconnectionAttempts = meta.config.maxReconnectionAttempts || 5;
config.reconnectionDelay = meta.config.reconnectionDelay || 1500;
config.topicsPerPage = meta.config.topicsPerPage || 20;
config.postsPerPage = meta.config.postsPerPage || 20;
config.maximumFileSize = meta.config.maximumFileSize;
config['theme:id'] = meta.config['theme:id'];
config['theme:src'] = meta.config['theme:src'];
config.defaultLang = meta.config.defaultLang || 'en_GB';
config.userLang = req.query.lang ? validator.escape(String(req.query.lang)) : config.defaultLang;
2014-07-24 21:11:46 -04:00
config.loggedIn = !!req.user;
config['cache-buster'] = meta.config['cache-buster'] || '';
config.requireEmailConfirmation = parseInt(meta.config.requireEmailConfirmation, 10) === 1;
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();
config.searchEnabled = plugins.hasListeners('filter:search.query');
config.bootswatchSkin = 'default';
2016-03-08 11:24:32 +02:00
async.waterfall([
function (next) {
if (!req.user) {
return next(null, config);
}
2016-08-29 12:43:32 +03:00
user.getSettings(req.uid, next);
2016-03-08 11:24:32 +02:00
},
2016-08-29 12:43:32 +03:00
function (settings, next) {
config.usePagination = settings.usePagination;
config.topicsPerPage = settings.topicsPerPage;
config.postsPerPage = settings.postsPerPage;
config.userLang = (req.query.lang ? validator.escape(String(req.query.lang)) : null) || 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.delayImageLoading = settings.delayImageLoading !== undefined ? settings.delayImageLoading : true;
config.bootswatchSkin = settings.bootswatchSkin || config.bootswatchSkin;
2016-03-08 11:24:32 +02:00
plugins.fireHook('filter:config.get', config, next);
}
], function (err, config) {
if (err) {
return next(err);
}
2016-03-08 11:24:32 +02:00
if (res.locals.isAPI) {
res.json(config);
} else {
next(null, config);
}
});
};
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
if (!areas.template || !areas.locations) {
2014-10-04 18:47:56 -04:00
return res.status(200).json({});
}
widgets.render(req.uid,
{
template: areas.template,
url: areas.url,
locations: areas.locations,
2016-05-03 17:17:38 +03:00
isMobile: req.query.isMobile === 'true'
2015-09-16 00:46:11 -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
});
};
apiController.getPostData = function (pid, uid, callback) {
2016-07-01 13:01:09 +03:00
async.parallel({
privileges: function (next) {
2016-07-01 13:01:09 +03:00
privileges.posts.get([pid], uid, next);
},
post: function (next) {
2016-07-01 13:01:09 +03:00
posts.getPostData(pid, next);
}
}, function (err, results) {
2016-07-01 13:01:09 +03:00
if (err || !results.post) {
return callback(err);
2016-03-08 11:24:32 +02:00
}
2016-07-01 13:01:09 +03:00
var post = results.post;
var privileges = results.privileges[0];
if (!privileges.read || !privileges['topics:read']) {
return callback();
}
post.ip = privileges.isAdminOrMod ? post.ip : undefined;
var selfPost = uid && uid === parseInt(post.uid, 10);
if (post.deleted && !(privileges.isAdminOrMod || selfPost)) {
post.content = '[[topic:post_is_deleted]]';
}
callback(null, post);
2016-03-08 11:24:32 +02:00
});
};
apiController.getTopicData = function (tid, uid, callback) {
2016-07-01 13:01:09 +03:00
async.parallel({
privileges: function (next) {
2016-07-01 13:01:09 +03:00
privileges.topics.get(tid, uid, next);
2015-09-21 12:59:41 -04:00
},
topic: function (next) {
2016-07-01 13:01:09 +03:00
topics.getTopicData(tid, next);
}
}, function (err, results) {
2016-07-01 13:01:09 +03:00
if (err || !results.topic) {
return callback(err);
2015-09-21 12:59:41 -04:00
}
2016-07-01 13:01:09 +03:00
if (!results.privileges.read || !results.privileges['topics:read'] || (parseInt(results.topic.deleted, 10) && !results.privileges.view_deleted)) {
return callback();
}
callback(null, results.topic);
});
};
2015-09-21 12:59:41 -04:00
apiController.getCategoryData = function (cid, uid, callback) {
2016-07-01 13:01:09 +03:00
async.parallel({
privileges: function (next) {
2016-07-01 13:01:09 +03:00
privileges.categories.get(cid, uid, next);
2016-03-08 11:24:32 +02:00
},
category: function (next) {
2016-07-01 13:01:09 +03:00
categories.getCategoryData(cid, next);
}
}, function (err, results) {
2016-07-01 13:01:09 +03:00
if (err || !results.category) {
return callback(err);
}
if (!results.privileges.read) {
return callback();
2015-09-21 12:59:41 -04:00
}
2016-07-01 13:01:09 +03:00
callback(null, results.category);
});
};
apiController.getObject = function (req, res, next) {
2016-07-01 13:01:09 +03:00
var methods = {
post: apiController.getPostData,
topic: apiController.getTopicData,
category: apiController.getCategoryData
};
var method = methods[req.params.type];
if (!method) {
return next();
}
method(req.params.id, req.uid, function (err, result) {
2016-07-01 13:01:09 +03:00
if (err || !result) {
return next(err);
}
res.json(result);
});
2015-09-21 12:59:41 -04:00
};
apiController.getCurrentUser = function (req, res, next) {
2016-07-11 13:03:47 +03:00
if (!req.uid) {
2016-08-04 21:12:03 +03:00
return res.status(401).json('not-authorized');
2016-07-11 13:03:47 +03:00
}
2016-07-11 12:56:23 +03:00
async.waterfall([
function (next) {
2016-07-11 12:56:23 +03:00
user.getUserField(req.uid, 'userslug', next);
},
function (userslug, next) {
2016-07-11 12:56:23 +03:00
accountHelpers.getUserDataByUserSlug(userslug, req.uid, next);
}
], function (err, userData) {
2016-07-11 12:56:23 +03:00
if (err) {
return next(err);
}
res.json(userData);
});
};
apiController.getUserByUID = function (req, res, next) {
2016-04-30 21:08:47 +03:00
byType('uid', req, res, next);
};
apiController.getUserByUsername = function (req, res, next) {
2016-04-30 21:08:47 +03:00
byType('username', req, res, next);
2016-03-08 11:24:32 +02:00
};
apiController.getUserByEmail = function (req, res, next) {
2016-04-30 21:08:47 +03:00
byType('email', req, res, next);
};
2016-03-08 11:24:32 +02:00
2016-04-30 21:08:47 +03:00
function byType(type, req, res, next) {
apiController.getUserDataByField(req.uid, type, req.params[type], function (err, data) {
2016-04-30 21:08:47 +03:00
if (err || !data) {
2016-03-08 11:24:32 +02:00
return next(err);
}
res.json(data);
});
2016-04-30 21:08:47 +03:00
}
apiController.getUserDataByField = function (callerUid, field, fieldValue, callback) {
async.waterfall([
2016-04-30 21:08:47 +03:00
function (next) {
if (field === 'uid') {
next(null, fieldValue);
} else if (field === 'username') {
user.getUidByUsername(fieldValue, next);
} else if (field === 'email') {
user.getUidByEmail(fieldValue, next);
} else {
next();
}
},
2016-04-30 21:08:47 +03:00
function (uid, next) {
if (!uid) {
return next();
}
2016-03-08 11:24:32 +02:00
apiController.getUserDataByUID(callerUid, uid, next);
}
2016-03-08 11:24:32 +02:00
], callback);
};
apiController.getUserDataByUID = function (callerUid, uid, callback) {
2016-03-08 11:24:32 +02:00
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]]'));
}
2015-12-09 17:43:28 +02:00
async.parallel({
userData: async.apply(user.getUserData, uid),
settings: async.apply(user.getSettings, uid)
}, function (err, results) {
2015-12-09 17:43:28 +02:00
if (err || !results.userData) {
2016-03-08 11:24:32 +02:00
return callback(err || new Error('[[error:no-user]]'));
2015-12-09 17:43:28 +02:00
}
results.userData.email = results.settings.showemail ? results.userData.email : undefined;
results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined;
2016-03-08 11:24:32 +02:00
callback(null, results.userData);
2015-12-09 17:43:28 +02:00
});
2016-03-08 11:24:32 +02:00
};
2015-12-09 17:43:28 +02:00
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);
}
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;