refactor: async/await controllers/api
parent
ca7ba9be34
commit
4eaa630b1c
@ -1,227 +1,179 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const validator = require('validator');
|
||||||
var validator = require('validator');
|
const nconf = require('nconf');
|
||||||
var nconf = require('nconf');
|
|
||||||
|
const meta = require('../meta');
|
||||||
var meta = require('../meta');
|
const user = require('../user');
|
||||||
var user = require('../user');
|
const posts = require('../posts');
|
||||||
var posts = require('../posts');
|
const topics = require('../topics');
|
||||||
var topics = require('../topics');
|
const categories = require('../categories');
|
||||||
var categories = require('../categories');
|
const privileges = require('../privileges');
|
||||||
var privileges = require('../privileges');
|
const plugins = require('../plugins');
|
||||||
var plugins = require('../plugins');
|
const translator = require('../translator');
|
||||||
var translator = require('../translator');
|
const languages = require('../languages');
|
||||||
var languages = require('../languages');
|
|
||||||
|
const apiController = module.exports;
|
||||||
var apiController = module.exports;
|
|
||||||
|
apiController.loadConfig = async function (req) {
|
||||||
apiController.loadConfig = function (req, callback) {
|
let config = {
|
||||||
var config = {};
|
relative_path: nconf.get('relative_path'),
|
||||||
config.relative_path = nconf.get('relative_path');
|
upload_url: nconf.get('upload_url'),
|
||||||
config.upload_url = nconf.get('upload_url');
|
siteTitle: validator.escape(String(meta.config.title || meta.config.browserTitle || 'NodeBB')),
|
||||||
config.siteTitle = validator.escape(String(meta.config.title || meta.config.browserTitle || 'NodeBB'));
|
browserTitle: validator.escape(String(meta.config.browserTitle || meta.config.title || 'NodeBB')),
|
||||||
config.browserTitle = validator.escape(String(meta.config.browserTitle || meta.config.title || 'NodeBB'));
|
titleLayout: (meta.config.titleLayout || '{pageTitle} | {browserTitle}').replace(/{/g, '{').replace(/}/g, '}'),
|
||||||
config.titleLayout = (meta.config.titleLayout || '{pageTitle} | {browserTitle}').replace(/{/g, '{').replace(/}/g, '}');
|
showSiteTitle: meta.config.showSiteTitle === 1,
|
||||||
config.showSiteTitle = meta.config.showSiteTitle === 1;
|
minimumTitleLength: meta.config.minimumTitleLength,
|
||||||
config.minimumTitleLength = meta.config.minimumTitleLength;
|
maximumTitleLength: meta.config.maximumTitleLength,
|
||||||
config.maximumTitleLength = meta.config.maximumTitleLength;
|
minimumPostLength: meta.config.minimumPostLength,
|
||||||
config.minimumPostLength = meta.config.minimumPostLength;
|
maximumPostLength: meta.config.maximumPostLength,
|
||||||
config.maximumPostLength = meta.config.maximumPostLength;
|
minimumTagsPerTopic: meta.config.minimumTagsPerTopic || 0,
|
||||||
config.minimumTagsPerTopic = meta.config.minimumTagsPerTopic || 0;
|
maximumTagsPerTopic: meta.config.maximumTagsPerTopic || 5,
|
||||||
config.maximumTagsPerTopic = meta.config.maximumTagsPerTopic || 5;
|
minimumTagLength: meta.config.minimumTagLength || 3,
|
||||||
config.minimumTagLength = meta.config.minimumTagLength || 3;
|
maximumTagLength: meta.config.maximumTagLength || 15,
|
||||||
config.maximumTagLength = meta.config.maximumTagLength || 15;
|
useOutgoingLinksPage: meta.config.useOutgoingLinksPage === 1,
|
||||||
config.useOutgoingLinksPage = meta.config.useOutgoingLinksPage === 1;
|
outgoingLinksWhitelist: meta.config.useOutgoingLinksPage === 1 ? meta.config['outgoingLinks:whitelist'] : undefined,
|
||||||
config.allowGuestHandles = meta.config.allowGuestHandles === 1;
|
allowGuestHandles: meta.config.allowGuestHandles === 1,
|
||||||
config.allowFileUploads = meta.config.allowFileUploads === 1;
|
allowFileUploads: meta.config.allowFileUploads === 1,
|
||||||
config.allowTopicsThumbnail = meta.config.allowTopicsThumbnail === 1;
|
allowTopicsThumbnail: meta.config.allowTopicsThumbnail === 1,
|
||||||
config.usePagination = meta.config.usePagination === 1;
|
usePagination: meta.config.usePagination === 1,
|
||||||
config.disableChat = meta.config.disableChat === 1;
|
disableChat: meta.config.disableChat === 1,
|
||||||
config.disableChatMessageEditing = meta.config.disableChatMessageEditing === 1;
|
disableChatMessageEditing: meta.config.disableChatMessageEditing === 1,
|
||||||
config.maximumChatMessageLength = meta.config.maximumChatMessageLength || 1000;
|
maximumChatMessageLength: meta.config.maximumChatMessageLength || 1000,
|
||||||
config.socketioTransports = nconf.get('socket.io:transports') || ['polling', 'websocket'];
|
socketioTransports: nconf.get('socket.io:transports') || ['polling', 'websocket'],
|
||||||
config.socketioOrigins = nconf.get('socket.io:origins');
|
socketioOrigins: nconf.get('socket.io:origins'),
|
||||||
config.websocketAddress = nconf.get('socket.io:address') || '';
|
websocketAddress: nconf.get('socket.io:address') || '',
|
||||||
config.maxReconnectionAttempts = meta.config.maxReconnectionAttempts || 5;
|
maxReconnectionAttempts: meta.config.maxReconnectionAttempts || 5,
|
||||||
config.reconnectionDelay = meta.config.reconnectionDelay || 1500;
|
reconnectionDelay: meta.config.reconnectionDelay || 1500,
|
||||||
config.topicsPerPage = meta.config.topicsPerPage || 20;
|
topicsPerPage: meta.config.topicsPerPage || 20,
|
||||||
config.postsPerPage = meta.config.postsPerPage || 20;
|
postsPerPage: meta.config.postsPerPage || 20,
|
||||||
config.maximumFileSize = meta.config.maximumFileSize;
|
maximumFileSize: meta.config.maximumFileSize,
|
||||||
config['theme:id'] = meta.config['theme:id'];
|
'theme:id': meta.config['theme:id'],
|
||||||
config['theme:src'] = meta.config['theme:src'];
|
'theme:src': meta.config['theme:src'],
|
||||||
config.defaultLang = meta.config.defaultLang || 'en-GB';
|
defaultLang: meta.config.defaultLang || 'en-GB',
|
||||||
config.userLang = req.query.lang ? validator.escape(String(req.query.lang)) : config.defaultLang;
|
userLang: req.query.lang ? validator.escape(String(req.query.lang)) : (meta.config.defaultLang || 'en-GB'),
|
||||||
config.loggedIn = !!req.user;
|
loggedIn: !!req.user,
|
||||||
config.uid = req.uid;
|
uid: req.uid,
|
||||||
config['cache-buster'] = meta.config['cache-buster'] || '';
|
'cache-buster': meta.config['cache-buster'] || '',
|
||||||
config.requireEmailConfirmation = meta.config.requireEmailConfirmation === 1;
|
requireEmailConfirmation: meta.config.requireEmailConfirmation === 1,
|
||||||
config.topicPostSort = meta.config.topicPostSort || 'oldest_to_newest';
|
topicPostSort: meta.config.topicPostSort || 'oldest_to_newest',
|
||||||
config.categoryTopicSort = meta.config.categoryTopicSort || 'newest_to_oldest';
|
categoryTopicSort: meta.config.categoryTopicSort || 'newest_to_oldest',
|
||||||
config.csrf_token = req.uid >= 0 && req.csrfToken && req.csrfToken();
|
csrf_token: req.uid >= 0 && req.csrfToken && req.csrfToken(),
|
||||||
config.searchEnabled = plugins.hasListeners('filter:search.query');
|
searchEnabled: plugins.hasListeners('filter:search.query'),
|
||||||
config.bootswatchSkin = meta.config.bootswatchSkin || '';
|
bootswatchSkin: meta.config.bootswatchSkin || '',
|
||||||
config.enablePostHistory = (meta.config.enablePostHistory || 1) === 1;
|
enablePostHistory: meta.config.enablePostHistory === 1,
|
||||||
config.notificationAlertTimeout = meta.config.notificationAlertTimeout || 5000;
|
notificationAlertTimeout: meta.config.notificationAlertTimeout || 5000,
|
||||||
config.timeagoCodes = languages.timeagoCodes;
|
timeagoCutoff: meta.config.timeagoCutoff !== '' ? Math.max(0, parseInt(meta.config.timeagoCutoff, 10)) : meta.config.timeagoCutoff,
|
||||||
|
timeagoCodes: languages.timeagoCodes,
|
||||||
if (config.useOutgoingLinksPage) {
|
cookies: {
|
||||||
config.outgoingLinksWhitelist = meta.config['outgoingLinks:whitelist'];
|
enabled: meta.config.cookieConsentEnabled === 1,
|
||||||
}
|
message: translator.escape(validator.escape(meta.config.cookieConsentMessage || '[[global:cookies.message]]')).replace(/\\/g, '\\\\'),
|
||||||
|
dismiss: translator.escape(validator.escape(meta.config.cookieConsentDismiss || '[[global:cookies.accept]]')).replace(/\\/g, '\\\\'),
|
||||||
var timeagoCutoff = meta.config.timeagoCutoff === undefined ? 30 : meta.config.timeagoCutoff;
|
link: translator.escape(validator.escape(meta.config.cookieConsentLink || '[[global:cookies.learn_more]]')).replace(/\\/g, '\\\\'),
|
||||||
config.timeagoCutoff = timeagoCutoff !== '' ? Math.max(0, parseInt(timeagoCutoff, 10)) : timeagoCutoff;
|
link_url: translator.escape(validator.escape(meta.config.cookieConsentLinkUrl || 'https://www.cookiesandyou.com')).replace(/\\/g, '\\\\'),
|
||||||
|
},
|
||||||
config.cookies = {
|
|
||||||
enabled: meta.config.cookieConsentEnabled === 1,
|
|
||||||
message: translator.escape(validator.escape(meta.config.cookieConsentMessage || '[[global:cookies.message]]')).replace(/\\/g, '\\\\'),
|
|
||||||
dismiss: translator.escape(validator.escape(meta.config.cookieConsentDismiss || '[[global:cookies.accept]]')).replace(/\\/g, '\\\\'),
|
|
||||||
link: translator.escape(validator.escape(meta.config.cookieConsentLink || '[[global:cookies.learn_more]]')).replace(/\\/g, '\\\\'),
|
|
||||||
link_url: translator.escape(validator.escape(meta.config.cookieConsentLinkUrl || 'https://www.cookiesandyou.com')).replace(/\\/g, '\\\\'),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
async.waterfall([
|
let settings = config;
|
||||||
function (next) {
|
if (req.loggedIn) {
|
||||||
if (!req.loggedIn) {
|
settings = await user.getSettings(req.uid);
|
||||||
return next(null, config);
|
}
|
||||||
}
|
|
||||||
user.getSettings(req.uid, next);
|
|
||||||
},
|
|
||||||
function (settings, next) {
|
|
||||||
// Handle old skin configs
|
|
||||||
const oldSkins = ['noskin', 'default'];
|
|
||||||
settings.bootswatchSkin = oldSkins.includes(settings.bootswatchSkin) ? '' : settings.bootswatchSkin;
|
|
||||||
|
|
||||||
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.acpLang = (req.query.lang ? validator.escape(String(req.query.lang)) : null) || settings.acpLang;
|
|
||||||
config.openOutgoingLinksInNewTab = settings.openOutgoingLinksInNewTab;
|
|
||||||
config.topicPostSort = settings.topicPostSort || config.topicPostSort;
|
|
||||||
config.categoryTopicSort = settings.categoryTopicSort || config.categoryTopicSort;
|
|
||||||
config.topicSearchEnabled = settings.topicSearchEnabled || false;
|
|
||||||
config.bootswatchSkin = (meta.config.disableCustomUserSkins !== 1 && settings.bootswatchSkin && settings.bootswatchSkin !== '') ? settings.bootswatchSkin : '';
|
|
||||||
plugins.fireHook('filter:config.get', config, next);
|
|
||||||
},
|
|
||||||
function (config, next) {
|
|
||||||
req.res.locals.config = config;
|
|
||||||
process.nextTick(next, null, config);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
apiController.getConfig = function (req, res, next) {
|
// Handle old skin configs
|
||||||
async.waterfall([
|
const oldSkins = ['noskin', 'default'];
|
||||||
function (next) {
|
settings.bootswatchSkin = oldSkins.includes(settings.bootswatchSkin) ? '' : settings.bootswatchSkin;
|
||||||
apiController.loadConfig(req, next);
|
|
||||||
},
|
config.usePagination = settings.usePagination;
|
||||||
function (config, next) {
|
config.topicsPerPage = settings.topicsPerPage;
|
||||||
if (res.locals.isAPI) {
|
config.postsPerPage = settings.postsPerPage;
|
||||||
res.json(config);
|
config.userLang = (req.query.lang ? validator.escape(String(req.query.lang)) : null) || settings.userLang || config.defaultLang;
|
||||||
} else {
|
config.acpLang = (req.query.lang ? validator.escape(String(req.query.lang)) : null) || settings.acpLang;
|
||||||
next(null, config);
|
config.openOutgoingLinksInNewTab = settings.openOutgoingLinksInNewTab;
|
||||||
}
|
config.topicPostSort = settings.topicPostSort || config.topicPostSort;
|
||||||
},
|
config.categoryTopicSort = settings.categoryTopicSort || config.categoryTopicSort;
|
||||||
], next);
|
config.topicSearchEnabled = settings.topicSearchEnabled || false;
|
||||||
|
config.bootswatchSkin = (meta.config.disableCustomUserSkins !== 1 && settings.bootswatchSkin && settings.bootswatchSkin !== '') ? settings.bootswatchSkin : '';
|
||||||
|
config = await plugins.fireHook('filter:config.get', config);
|
||||||
|
req.res.locals.config = config;
|
||||||
|
return config;
|
||||||
};
|
};
|
||||||
|
|
||||||
apiController.getPostData = function (pid, uid, callback) {
|
apiController.getConfig = async function (req, res) {
|
||||||
async.parallel({
|
const config = await apiController.loadConfig(req);
|
||||||
privileges: function (next) {
|
res.json(config);
|
||||||
privileges.posts.get([pid], uid, next);
|
};
|
||||||
},
|
|
||||||
post: function (next) {
|
|
||||||
posts.getPostData(pid, next);
|
|
||||||
},
|
|
||||||
voted: async.apply(posts.hasVoted, pid, uid),
|
|
||||||
}, function (err, results) {
|
|
||||||
if (err || !results.post) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
var post = results.post;
|
apiController.getPostData = async function (pid, uid) {
|
||||||
Object.assign(post, results.voted);
|
const [userPrivileges, post, voted] = await Promise.all([
|
||||||
|
privileges.posts.get([pid], uid),
|
||||||
|
posts.getPostData(pid),
|
||||||
|
posts.hasVoted(pid, uid),
|
||||||
|
]);
|
||||||
|
if (!post) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Object.assign(post, voted);
|
||||||
|
|
||||||
var privileges = results.privileges[0];
|
const userPrivilege = userPrivileges[0];
|
||||||
if (!privileges.read || !privileges['topics:read']) {
|
if (!userPrivilege.read || !userPrivilege['topics:read']) {
|
||||||
return callback();
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
post.ip = privileges.isAdminOrMod ? post.ip : undefined;
|
post.ip = userPrivilege.isAdminOrMod ? post.ip : undefined;
|
||||||
var selfPost = uid && uid === parseInt(post.uid, 10);
|
const selfPost = uid && uid === parseInt(post.uid, 10);
|
||||||
if (post.deleted && !(privileges.isAdminOrMod || selfPost)) {
|
if (post.deleted && !(userPrivilege.isAdminOrMod || selfPost)) {
|
||||||
post.content = '[[topic:post_is_deleted]]';
|
post.content = '[[topic:post_is_deleted]]';
|
||||||
}
|
}
|
||||||
callback(null, post);
|
return post;
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
apiController.getTopicData = function (tid, uid, callback) {
|
apiController.getTopicData = async function (tid, uid) {
|
||||||
async.parallel({
|
const [userPrivileges, topic] = await Promise.all([
|
||||||
privileges: function (next) {
|
privileges.topics.get(tid, uid),
|
||||||
privileges.topics.get(tid, uid, next);
|
topics.getTopicData(tid),
|
||||||
},
|
]);
|
||||||
topic: function (next) {
|
if (!topic || !userPrivileges.read || !userPrivileges['topics:read'] || (topic.deleted && !userPrivileges.view_deleted)) {
|
||||||
topics.getTopicData(tid, next);
|
return null;
|
||||||
},
|
}
|
||||||
}, function (err, results) {
|
return topic;
|
||||||
if (err || !results.topic) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!results.privileges.read || !results.privileges['topics:read'] || (results.topic.deleted && !results.privileges.view_deleted)) {
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
callback(null, results.topic);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
apiController.getCategoryData = function (cid, uid, callback) {
|
apiController.getCategoryData = async function (cid, uid) {
|
||||||
async.parallel({
|
const [userPrivileges, category] = await Promise.all([
|
||||||
privileges: function (next) {
|
privileges.categories.get(cid, uid),
|
||||||
privileges.categories.get(cid, uid, next);
|
categories.getCategoryData(cid),
|
||||||
},
|
]);
|
||||||
category: function (next) {
|
if (!category || !userPrivileges.read) {
|
||||||
categories.getCategoryData(cid, next);
|
return null;
|
||||||
},
|
}
|
||||||
}, function (err, results) {
|
return category;
|
||||||
if (err || !results.category) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!results.privileges.read) {
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
callback(null, results.category);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
apiController.getObject = async function (req, res, next) {
|
||||||
apiController.getObject = function (req, res, next) {
|
const methods = {
|
||||||
var methods = {
|
|
||||||
post: apiController.getPostData,
|
post: apiController.getPostData,
|
||||||
topic: apiController.getTopicData,
|
topic: apiController.getTopicData,
|
||||||
category: apiController.getCategoryData,
|
category: apiController.getCategoryData,
|
||||||
};
|
};
|
||||||
var method = methods[req.params.type];
|
const method = methods[req.params.type];
|
||||||
if (!method) {
|
if (!method) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
method(req.params.id, req.uid, function (err, result) {
|
try {
|
||||||
if (err || !result) {
|
const result = await method(req.params.id, req.uid);
|
||||||
return next(err);
|
if (!result) {
|
||||||
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json(result);
|
res.json(result);
|
||||||
});
|
} catch (err) {
|
||||||
|
next(err);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
apiController.getModerators = function (req, res, next) {
|
apiController.getModerators = async function (req, res) {
|
||||||
categories.getModerators(req.params.cid, function (err, moderators) {
|
const moderators = await categories.getModerators(req.params.cid);
|
||||||
if (err) {
|
res.json({ moderators: moderators });
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
res.json({ moderators: moderators });
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
require('../promisify')(apiController, ['getConfig', 'getObject', 'getModerators']);
|
||||||
|
Loading…
Reference in New Issue