mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-30 02:25:55 +01:00
feat: async refactor
This commit is contained in:
@@ -1,46 +1,40 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var nconf = require('nconf');
|
||||
const nconf = require('nconf');
|
||||
|
||||
var user = require('../user');
|
||||
var plugins = require('../plugins');
|
||||
var topics = require('../topics');
|
||||
var posts = require('../posts');
|
||||
var helpers = require('./helpers');
|
||||
const user = require('../user');
|
||||
const plugins = require('../plugins');
|
||||
const topics = require('../topics');
|
||||
const posts = require('../posts');
|
||||
const helpers = require('./helpers');
|
||||
|
||||
exports.get = function (req, res, callback) {
|
||||
exports.get = async function (req, res, callback) {
|
||||
res.locals.metaTags = {
|
||||
...res.locals.metaTags,
|
||||
name: 'robots',
|
||||
content: 'noindex',
|
||||
};
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
plugins.fireHook('filter:composer.build', {
|
||||
req: req,
|
||||
res: res,
|
||||
next: callback,
|
||||
templateData: {},
|
||||
}, next);
|
||||
},
|
||||
function (data) {
|
||||
if (data.templateData.disabled) {
|
||||
res.render('', {
|
||||
title: '[[modules:composer.compose]]',
|
||||
});
|
||||
} else {
|
||||
data.templateData.title = '[[modules:composer.compose]]';
|
||||
res.render('compose', data.templateData);
|
||||
}
|
||||
},
|
||||
], callback);
|
||||
const data = await plugins.fireHook('filter:composer.build', {
|
||||
req: req,
|
||||
res: res,
|
||||
next: callback,
|
||||
templateData: {},
|
||||
});
|
||||
|
||||
if (data.templateData.disabled) {
|
||||
res.render('', {
|
||||
title: '[[modules:composer.compose]]',
|
||||
});
|
||||
} else {
|
||||
data.templateData.title = '[[modules:composer.compose]]';
|
||||
res.render('compose', data.templateData);
|
||||
}
|
||||
};
|
||||
|
||||
exports.post = function (req, res) {
|
||||
var body = req.body;
|
||||
var data = {
|
||||
exports.post = async function (req, res) {
|
||||
const body = req.body;
|
||||
const data = {
|
||||
uid: req.uid,
|
||||
req: req,
|
||||
timestamp: Date.now(),
|
||||
@@ -52,49 +46,37 @@ exports.post = function (req, res) {
|
||||
if (!data.content) {
|
||||
return helpers.noScriptErrors(req, res, '[[error:invalid-data]]', 400);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
function queueOrPost(postFn, data, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts.shouldQueue(req.uid, data, next);
|
||||
},
|
||||
function (shouldQueue, next) {
|
||||
if (shouldQueue) {
|
||||
delete data.req;
|
||||
posts.addToQueue(data, next);
|
||||
} else {
|
||||
postFn(data, next);
|
||||
}
|
||||
},
|
||||
], next);
|
||||
}
|
||||
if (body.tid) {
|
||||
data.tid = body.tid;
|
||||
queueOrPost(topics.reply, data, next);
|
||||
} else if (body.cid) {
|
||||
data.cid = body.cid;
|
||||
data.title = body.title;
|
||||
data.tags = [];
|
||||
data.thumb = '';
|
||||
queueOrPost(topics.post, data, next);
|
||||
} else {
|
||||
next(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
},
|
||||
function (result, next) {
|
||||
if (result.queued) {
|
||||
return res.redirect((nconf.get('relative_path') || '/'));
|
||||
}
|
||||
var uid = result.uid ? result.uid : result.topicData.uid;
|
||||
user.updateOnlineUsers(uid);
|
||||
next(null, result.pid ? '/post/' + result.pid : '/topic/' + result.topicData.slug);
|
||||
},
|
||||
], function (err, path) {
|
||||
if (err) {
|
||||
return helpers.noScriptErrors(req, res, err.message, 400);
|
||||
async function queueOrPost(postFn, data) {
|
||||
const shouldQueue = await posts.shouldQueue(req.uid, data);
|
||||
if (shouldQueue) {
|
||||
delete data.req;
|
||||
return await posts.addToQueue(data);
|
||||
}
|
||||
return await postFn(data);
|
||||
}
|
||||
|
||||
try {
|
||||
let result;
|
||||
if (body.tid) {
|
||||
data.tid = body.tid;
|
||||
result = await queueOrPost(topics.reply, data);
|
||||
} else if (body.cid) {
|
||||
data.cid = body.cid;
|
||||
data.title = body.title;
|
||||
data.tags = [];
|
||||
data.thumb = '';
|
||||
result = await queueOrPost(topics.post, data);
|
||||
} else {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
if (result.queued) {
|
||||
return res.redirect((nconf.get('relative_path') || '/'));
|
||||
}
|
||||
const uid = result.uid ? result.uid : result.topicData.uid;
|
||||
user.updateOnlineUsers(uid);
|
||||
const path = result.pid ? '/post/' + result.pid : '/topic/' + result.topicData.slug;
|
||||
res.redirect(nconf.get('relative_path') + path);
|
||||
});
|
||||
} catch (err) {
|
||||
helpers.noScriptErrors(req, res, err.message, 400);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user