| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const meta = require('../meta'); | 
					
						
							|  |  |  | const db = require('../database'); | 
					
						
							|  |  |  | const plugins = require('../plugins'); | 
					
						
							|  |  |  | const user = require('../user'); | 
					
						
							|  |  |  | const topics = require('../topics'); | 
					
						
							|  |  |  | const categories = require('../categories'); | 
					
						
							|  |  |  | const groups = require('../groups'); | 
					
						
							|  |  |  | const utils = require('../utils'); | 
					
						
							| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | module.exports = function (Posts) { | 
					
						
							| 
									
										
										
										
											2019-07-17 00:17:21 -04:00
										 |  |  | 	Posts.create = async function (data) { | 
					
						
							| 
									
										
										
										
											2015-05-15 09:47:39 -04:00
										 |  |  | 		// This is an internal method, consider using Topics.reply instead
 | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 		const { uid } = data; | 
					
						
							|  |  |  | 		const { tid } = data; | 
					
						
							| 
									
										
										
										
											2019-07-17 00:17:21 -04:00
										 |  |  | 		const content = data.content.toString(); | 
					
						
							|  |  |  | 		const timestamp = data.timestamp || Date.now(); | 
					
						
							|  |  |  | 		const isMain = data.isMain || false; | 
					
						
							| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (!uid && parseInt(uid, 10) !== 0) { | 
					
						
							| 
									
										
										
										
											2019-07-17 00:17:21 -04:00
										 |  |  | 			throw new Error('[[error:invalid-uid]]'); | 
					
						
							| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 15:34:05 -04:00
										 |  |  | 		if (data.hasOwnProperty('toPid') && !utils.isNumber(data.toPid)) { | 
					
						
							| 
									
										
										
										
											2019-07-17 00:17:21 -04:00
										 |  |  | 			throw new Error('[[error:invalid-pid]]'); | 
					
						
							| 
									
										
										
										
											2016-12-17 16:00:39 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-17 00:17:21 -04:00
										 |  |  | 		const pid = await db.incrObjectField('global', 'nextPid'); | 
					
						
							|  |  |  | 		let postData = { | 
					
						
							|  |  |  | 			pid: pid, | 
					
						
							|  |  |  | 			uid: uid, | 
					
						
							|  |  |  | 			tid: tid, | 
					
						
							|  |  |  | 			content: content, | 
					
						
							|  |  |  | 			timestamp: timestamp, | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-17 00:17:21 -04:00
										 |  |  | 		if (data.toPid) { | 
					
						
							|  |  |  | 			postData.toPid = data.toPid; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (data.ip && meta.config.trackIpPerPost) { | 
					
						
							|  |  |  | 			postData.ip = data.ip; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (data.handle && !parseInt(uid, 10)) { | 
					
						
							|  |  |  | 			postData.handle = data.handle; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 		let result = await plugins.hooks.fire('filter:post.create', { post: postData, data: data }); | 
					
						
							| 
									
										
										
										
											2019-07-17 00:17:21 -04:00
										 |  |  | 		postData = result.post; | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 		await db.setObject(`post:${postData.pid}`, postData); | 
					
						
							| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-17 00:17:21 -04:00
										 |  |  | 		const topicData = await topics.getTopicFields(tid, ['cid', 'pinned']); | 
					
						
							|  |  |  | 		postData.cid = topicData.cid; | 
					
						
							| 
									
										
										
										
											2014-12-05 13:07:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-17 00:17:21 -04:00
										 |  |  | 		await Promise.all([ | 
					
						
							|  |  |  | 			db.sortedSetAdd('posts:pid', timestamp, postData.pid), | 
					
						
							|  |  |  | 			db.incrObjectField('global', 'postCount'), | 
					
						
							|  |  |  | 			user.onNewPostMade(postData), | 
					
						
							|  |  |  | 			topics.onNewPostMade(postData), | 
					
						
							|  |  |  | 			categories.onNewPostMade(topicData.cid, topicData.pinned, postData), | 
					
						
							|  |  |  | 			groups.onNewPostMade(postData), | 
					
						
							|  |  |  | 			addReplyTo(postData, timestamp), | 
					
						
							|  |  |  | 			Posts.uploads.sync(postData.pid), | 
					
						
							|  |  |  | 		]); | 
					
						
							| 
									
										
										
										
											2014-12-31 16:27:35 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 		result = await plugins.hooks.fire('filter:post.get', { post: postData, uid: data.uid }); | 
					
						
							| 
									
										
										
										
											2019-07-17 00:17:21 -04:00
										 |  |  | 		result.post.isMain = isMain; | 
					
						
							| 
									
										
										
										
											2020-11-20 16:06:26 -05:00
										 |  |  | 		plugins.hooks.fire('action:post.save', { post: _.clone(result.post) }); | 
					
						
							| 
									
										
										
										
											2019-07-17 00:17:21 -04:00
										 |  |  | 		return result.post; | 
					
						
							| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2019-07-17 00:17:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	async function addReplyTo(postData, timestamp) { | 
					
						
							|  |  |  | 		if (!postData.toPid) { | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		await Promise.all([ | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			db.sortedSetAdd(`pid:${postData.toPid}:replies`, timestamp, postData.pid), | 
					
						
							|  |  |  | 			db.incrObjectField(`post:${postData.toPid}`, 'replies'), | 
					
						
							| 
									
										
										
										
											2019-07-17 00:17:21 -04:00
										 |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | }; |