Skip to content

Node.js based forum software built for the modern web

move composer routes to its own file

Barış Soner Uşaklıe4ea4d51348f
diff --git a/src/controllers/composer.js b/src/controllers/composer.js
new file mode 100644
index 0000000000..c0b0da2236
--- /dev/null
+++ b/src/controllers/composer.js
@@ -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);
+ });
+};
diff --git a/src/controllers/index.js b/src/controllers/index.js
index 7144f1a740..1861422db9 100644
--- a/src/controllers/index.js
+++ b/src/controllers/index.js
@@ -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,32 +152,30 @@ Controllers.register = function (req, res, next) {
},
}, next);
},
- ], function (err, termsOfUse) {
- if (err) {
- return next(err);
- }
- var loginStrategies = require('../routes/authentication').getLoginStrategies();
- var data = {
- 'register_window:spansize': loginStrategies.length ? 'col-md-6' : 'col-md-12',
- alternate_logins: !!loginStrategies.length,
- };
-
- data.authentication = loginStrategies;
-
- data.minimumUsernameLength = parseInt(meta.config.minimumUsernameLength, 10);
- data.maximumUsernameLength = parseInt(meta.config.maximumUsernameLength, 10);
- data.minimumPasswordLength = parseInt(meta.config.minimumPasswordLength, 10);
- data.minimumPasswordStrength = parseInt(meta.config.minimumPasswordStrength || 0, 10);
- data.termsOfUse = termsOfUse.postData.content;
- data.breadcrumbs = helpers.buildBreadcrumbs([{
- text: '[[register:register]]',
- }]);
- data.regFormEntry = [];
- data.error = req.flash('error')[0] || errorText;
- data.title = '[[pages:register]]';
-
- res.render('register', data);
- });
+ function (termsOfUse) {
+ var loginStrategies = require('../routes/authentication').getLoginStrategies();
+ var data = {
+ 'register_window:spansize': loginStrategies.length ? 'col-md-6' : 'col-md-12',
+ alternate_logins: !!loginStrategies.length,
+ };
+
+ data.authentication = loginStrategies;
+
+ data.minimumUsernameLength = parseInt(meta.config.minimumUsernameLength, 10);
+ data.maximumUsernameLength = parseInt(meta.config.maximumUsernameLength, 10);
+ data.minimumPasswordLength = parseInt(meta.config.minimumPasswordLength, 10);
+ data.minimumPasswordStrength = parseInt(meta.config.minimumPasswordStrength || 0, 10);
+ data.termsOfUse = termsOfUse.postData.content;
+ data.breadcrumbs = helpers.buildBreadcrumbs([{
+ text: '[[register:register]]',
+ }]);
+ data.regFormEntry = [];
+ data.error = req.flash('error')[0] || errorText;
+ 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', {
diff --git a/src/routes/index.js b/src/routes/index.js
index e2afb836ab..9feaa0ff60 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -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) {
diff --git a/test/controllers.js b/test/controllers.js
index fb3fda8830..6aa5f482c1 100644
--- a/test/controllers.js
+++ b/test/controllers.js
@@ -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);