| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-31 12:39:15 -04:00
										 |  |  | const nconf = require('nconf'); | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-31 12:39:15 -04:00
										 |  |  | const user = require('../user'); | 
					
						
							|  |  |  | const plugins = require('../plugins'); | 
					
						
							|  |  |  | const topics = require('../topics'); | 
					
						
							|  |  |  | const posts = require('../posts'); | 
					
						
							|  |  |  | const helpers = require('./helpers'); | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-31 12:39:15 -04:00
										 |  |  | exports.get = async function (req, res, callback) { | 
					
						
							| 
									
										
										
										
											2019-03-27 17:10:56 -04:00
										 |  |  | 	res.locals.metaTags = { | 
					
						
							|  |  |  | 		...res.locals.metaTags, | 
					
						
							|  |  |  | 		name: 'robots', | 
					
						
							|  |  |  | 		content: 'noindex', | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 	const data = await plugins.hooks.fire('filter:composer.build', { | 
					
						
							| 
									
										
										
										
											2019-07-31 12:39:15 -04:00
										 |  |  | 		req: req, | 
					
						
							|  |  |  | 		res: res, | 
					
						
							|  |  |  | 		next: callback, | 
					
						
							|  |  |  | 		templateData: {}, | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 21:01:19 -05:00
										 |  |  | 	if (res.headersSent) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-01-15 11:36:27 -05:00
										 |  |  | 	if (!data || !data.templateData) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-31 12:39:15 -04:00
										 |  |  | 	if (data.templateData.disabled) { | 
					
						
							|  |  |  | 		res.render('', { | 
					
						
							|  |  |  | 			title: '[[modules:composer.compose]]', | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		data.templateData.title = '[[modules:composer.compose]]'; | 
					
						
							|  |  |  | 		res.render('compose', data.templateData); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-31 12:39:15 -04:00
										 |  |  | exports.post = async function (req, res) { | 
					
						
							|  |  |  | 	const body = req.body; | 
					
						
							|  |  |  | 	const data = { | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | 		uid: req.uid, | 
					
						
							|  |  |  | 		req: req, | 
					
						
							|  |  |  | 		timestamp: Date.now(), | 
					
						
							|  |  |  | 		content: body.content, | 
					
						
							| 
									
										
										
										
											2019-07-30 13:19:50 -04:00
										 |  |  | 		fromQueue: false, | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 	req.body.noscript = 'true'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!data.content) { | 
					
						
							|  |  |  | 		return helpers.noScriptErrors(req, res, '[[error:invalid-data]]', 400); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-07-31 12:39:15 -04:00
										 |  |  | 	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); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-31 12:39:15 -04:00
										 |  |  | 	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) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			return res.redirect(`${nconf.get('relative_path') || '/'}?noScriptMessage=[[success:post-queued]]`); | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-31 12:39:15 -04:00
										 |  |  | 		const uid = result.uid ? result.uid : result.topicData.uid; | 
					
						
							|  |  |  | 		user.updateOnlineUsers(uid); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		const path = result.pid ? `/post/${result.pid}` : `/topic/${result.topicData.slug}`; | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | 		res.redirect(nconf.get('relative_path') + path); | 
					
						
							| 
									
										
										
										
											2019-07-31 12:39:15 -04:00
										 |  |  | 	} catch (err) { | 
					
						
							|  |  |  | 		helpers.noScriptErrors(req, res, err.message, 400); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-11-05 14:05:21 -05:00
										 |  |  | }; |