mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 03:55:55 +01:00
move composer routes to its own file
This commit is contained in:
75
src/controllers/composer.js
Normal file
75
src/controllers/composer.js
Normal file
@@ -0,0 +1,75 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var nconf = require('nconf');
|
||||
|
||||
var user = require('../user');
|
||||
var plugins = require('../plugins');
|
||||
var topics = require('../topics');
|
||||
var helpers = require('./helpers');
|
||||
|
||||
exports.get = function (req, res, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
plugins.fireHook('filter:composer.build', {
|
||||
req: req,
|
||||
res: res,
|
||||
next: next,
|
||||
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);
|
||||
}
|
||||
},
|
||||
], next);
|
||||
};
|
||||
|
||||
exports.post = function (req, res) {
|
||||
var body = req.body;
|
||||
var data = {
|
||||
uid: req.uid,
|
||||
req: req,
|
||||
timestamp: Date.now(),
|
||||
content: body.content,
|
||||
};
|
||||
req.body.noscript = 'true';
|
||||
|
||||
if (!data.content) {
|
||||
return helpers.noScriptErrors(req, res, '[[error:invalid-data]]', 400);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (body.tid) {
|
||||
data.tid = body.tid;
|
||||
topics.reply(data, next);
|
||||
} else if (body.cid) {
|
||||
data.cid = body.cid;
|
||||
data.title = body.title;
|
||||
data.tags = [];
|
||||
data.thumb = '';
|
||||
|
||||
topics.post(data, next);
|
||||
} else {
|
||||
next(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
},
|
||||
function (result, next) {
|
||||
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);
|
||||
}
|
||||
res.redirect(nconf.get('relative_path') + path);
|
||||
});
|
||||
};
|
||||
@@ -7,7 +7,6 @@ var validator = require('validator');
|
||||
var meta = require('../meta');
|
||||
var user = require('../user');
|
||||
var plugins = require('../plugins');
|
||||
var topics = require('../topics');
|
||||
var helpers = require('./helpers');
|
||||
|
||||
var Controllers = module.exports;
|
||||
@@ -35,6 +34,7 @@ Controllers.sitemap = require('./sitemap');
|
||||
Controllers.osd = require('./osd');
|
||||
Controllers['404'] = require('./404');
|
||||
Controllers.errors = require('./errors');
|
||||
Controllers.composer = require('./composer');
|
||||
|
||||
Controllers.reset = function (req, res, next) {
|
||||
if (req.params.code) {
|
||||
@@ -152,10 +152,7 @@ Controllers.register = function (req, res, next) {
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
], function (err, termsOfUse) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
function (termsOfUse) {
|
||||
var loginStrategies = require('../routes/authentication').getLoginStrategies();
|
||||
var data = {
|
||||
'register_window:spansize': loginStrategies.length ? 'col-md-6' : 'col-md-12',
|
||||
@@ -177,7 +174,8 @@ Controllers.register = function (req, res, next) {
|
||||
data.title = '[[pages:register]]';
|
||||
|
||||
res.render('register', data);
|
||||
});
|
||||
},
|
||||
], next);
|
||||
};
|
||||
|
||||
Controllers.registerInterstitial = function (req, res, next) {
|
||||
@@ -216,69 +214,6 @@ Controllers.registerInterstitial = function (req, res, next) {
|
||||
], next);
|
||||
};
|
||||
|
||||
Controllers.compose = function (req, res, next) {
|
||||
plugins.fireHook('filter:composer.build', {
|
||||
req: req,
|
||||
res: res,
|
||||
next: next,
|
||||
templateData: {},
|
||||
}, function (err, data) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
if (data.templateData.disabled) {
|
||||
res.render('', {
|
||||
title: '[[modules:composer.compose]]',
|
||||
});
|
||||
} else {
|
||||
data.templateData.title = '[[modules:composer.compose]]';
|
||||
res.render('compose', data.templateData);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Controllers.composePost = function (req, res) {
|
||||
var body = req.body;
|
||||
var data = {
|
||||
uid: req.uid,
|
||||
req: req,
|
||||
timestamp: Date.now(),
|
||||
content: body.content,
|
||||
};
|
||||
req.body.noscript = 'true';
|
||||
|
||||
if (!data.content) {
|
||||
return helpers.noScriptErrors(req, res, '[[error:invalid-data]]', 400);
|
||||
}
|
||||
|
||||
if (body.tid) {
|
||||
data.tid = body.tid;
|
||||
|
||||
topics.reply(data, function (err, result) {
|
||||
if (err) {
|
||||
return helpers.noScriptErrors(req, res, err.message, 400);
|
||||
}
|
||||
user.updateOnlineUsers(result.uid);
|
||||
|
||||
res.redirect(nconf.get('relative_path') + '/post/' + result.pid);
|
||||
});
|
||||
} else if (body.cid) {
|
||||
data.cid = body.cid;
|
||||
data.title = body.title;
|
||||
data.tags = [];
|
||||
data.thumb = '';
|
||||
|
||||
topics.post(data, function (err, result) {
|
||||
if (err) {
|
||||
return helpers.noScriptErrors(req, res, err.message, 400);
|
||||
}
|
||||
|
||||
res.redirect(nconf.get('relative_path') + '/topic/' + result.topicData.slug);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Controllers.confirmEmail = function (req, res) {
|
||||
user.email.confirm(req.params.code, function (err) {
|
||||
res.render('confirm', {
|
||||
|
||||
@@ -27,14 +27,14 @@ function mainRoutes(app, middleware, controllers) {
|
||||
setupPageRoute(app, '/login', middleware, loginRegisterMiddleware, controllers.login);
|
||||
setupPageRoute(app, '/register', middleware, loginRegisterMiddleware, controllers.register);
|
||||
setupPageRoute(app, '/register/complete', middleware, [], controllers.registerInterstitial);
|
||||
setupPageRoute(app, '/compose', middleware, [], controllers.compose);
|
||||
setupPageRoute(app, '/compose', middleware, [], controllers.composer.get);
|
||||
setupPageRoute(app, '/confirm/:code', middleware, [], controllers.confirmEmail);
|
||||
setupPageRoute(app, '/outgoing', middleware, [], controllers.outgoing);
|
||||
setupPageRoute(app, '/search', middleware, [], controllers.search.search);
|
||||
setupPageRoute(app, '/reset/:code?', middleware, [middleware.delayLoading], controllers.reset);
|
||||
setupPageRoute(app, '/tos', middleware, [], controllers.termsOfUse);
|
||||
|
||||
app.post('/compose', middleware.applyCSRF, controllers.composePost);
|
||||
app.post('/compose', middleware.applyCSRF, controllers.composer.post);
|
||||
}
|
||||
|
||||
function modRoutes(app, middleware, controllers) {
|
||||
|
||||
@@ -1904,6 +1904,110 @@ describe('Controllers', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('composer', function () {
|
||||
var csrf_token;
|
||||
var jar;
|
||||
|
||||
before(function (done) {
|
||||
helpers.loginUser('foo', 'barbar', function (err, _jar) {
|
||||
assert.ifError(err);
|
||||
jar = _jar;
|
||||
|
||||
request({
|
||||
url: nconf.get('url') + '/api/config',
|
||||
json: true,
|
||||
jar: jar,
|
||||
}, function (err, response, body) {
|
||||
assert.ifError(err);
|
||||
csrf_token = body.csrf_token;
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should load the composer route', function (done) {
|
||||
request(nconf.get('url') + '/api/compose', { json: true }, function (err, res, body) {
|
||||
assert.ifError(err);
|
||||
assert.equal(res.statusCode, 200);
|
||||
assert(body.title);
|
||||
assert(body.template);
|
||||
assert.equal(body.url, '/compose');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should load the composer route if disabled by plugin', function (done) {
|
||||
function hookMethod(hookData, callback) {
|
||||
hookData.templateData.disabled = true;
|
||||
callback(null, hookData);
|
||||
}
|
||||
|
||||
plugins.registerHook('myTestPlugin', {
|
||||
hook: 'filter:composer.build',
|
||||
method: hookMethod,
|
||||
});
|
||||
|
||||
request(nconf.get('url') + '/api/compose', { json: true }, function (err, res, body) {
|
||||
assert.ifError(err);
|
||||
assert.equal(res.statusCode, 200);
|
||||
assert(body.title);
|
||||
assert.strictEqual(body.template.name, '');
|
||||
assert.strictEqual(body.url, '/compose');
|
||||
|
||||
plugins.unregisterHook('myTestPlugin', 'filter:composer.build', hookMethod);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should error with invalid data', function (done) {
|
||||
request.post(nconf.get('url') + '/compose', {
|
||||
form: {
|
||||
content: 'a new reply',
|
||||
},
|
||||
jar: jar,
|
||||
headers: {
|
||||
'x-csrf-token': csrf_token,
|
||||
},
|
||||
}, function (err, res, body) {
|
||||
assert.ifError(err);
|
||||
assert.equal(res.statusCode, 400);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should create a new topic and reply by composer route', function (done) {
|
||||
var data = {
|
||||
cid: cid,
|
||||
title: 'no js is good',
|
||||
content: 'a topic with noscript',
|
||||
};
|
||||
request.post(nconf.get('url') + '/compose', {
|
||||
form: data,
|
||||
jar: jar,
|
||||
headers: {
|
||||
'x-csrf-token': csrf_token,
|
||||
},
|
||||
}, function (err, res) {
|
||||
assert.ifError(err);
|
||||
assert.equal(res.statusCode, 302);
|
||||
request.post(nconf.get('url') + '/compose', {
|
||||
form: {
|
||||
tid: tid,
|
||||
content: 'a new reply',
|
||||
},
|
||||
jar: jar,
|
||||
headers: {
|
||||
'x-csrf-token': csrf_token,
|
||||
},
|
||||
}, function (err, res, body) {
|
||||
assert.ifError(err);
|
||||
assert.equal(res.statusCode, 302);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
after(function (done) {
|
||||
var analytics = require('../src/analytics');
|
||||
analytics.writeData(done);
|
||||
|
||||
Reference in New Issue
Block a user