| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | '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'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 19:13:43 -05:00
										 |  |  | exports.get = function (req, res, callback) { | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | 	async.waterfall([ | 
					
						
							| 
									
										
										
										
											2017-11-23 19:13:43 -05:00
										 |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | 			plugins.fireHook('filter:composer.build', { | 
					
						
							|  |  |  | 				req: req, | 
					
						
							|  |  |  | 				res: res, | 
					
						
							| 
									
										
										
										
											2017-11-23 19:13:43 -05:00
										 |  |  | 				next: callback, | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | 				templateData: {}, | 
					
						
							| 
									
										
										
										
											2017-11-23 19:13:43 -05:00
										 |  |  | 			}, next); | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function (data) { | 
					
						
							|  |  |  | 			if (data.templateData.disabled) { | 
					
						
							|  |  |  | 				res.render('', { | 
					
						
							|  |  |  | 					title: '[[modules:composer.compose]]', | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				data.templateData.title = '[[modules:composer.compose]]'; | 
					
						
							|  |  |  | 				res.render('compose', data.templateData); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-11-23 19:13:43 -05:00
										 |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; |