| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-05 13:07:13 -05:00
										 |  |  | 	meta = require('../meta'), | 
					
						
							| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | 	db = require('../database'), | 
					
						
							|  |  |  | 	plugins = require('../plugins'), | 
					
						
							|  |  |  | 	user = require('../user'), | 
					
						
							|  |  |  | 	topics = require('../topics'), | 
					
						
							|  |  |  | 	categories = require('../categories'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = function(Posts) { | 
					
						
							|  |  |  | 	Posts.create = function(data, callback) { | 
					
						
							|  |  |  | 		var uid = data.uid, | 
					
						
							|  |  |  | 			tid = data.tid, | 
					
						
							|  |  |  | 			content = data.content, | 
					
						
							|  |  |  | 			timestamp = data.timestamp || Date.now(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!uid && parseInt(uid, 10) !== 0) { | 
					
						
							|  |  |  | 			return callback(new Error('[[error:invalid-uid]]')); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var postData; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				db.incrObjectField('global', 'nextPid', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(pid, next) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				postData = { | 
					
						
							|  |  |  | 					'pid': pid, | 
					
						
							|  |  |  | 					'uid': uid, | 
					
						
							|  |  |  | 					'tid': tid, | 
					
						
							|  |  |  | 					'content': content, | 
					
						
							|  |  |  | 					'timestamp': timestamp, | 
					
						
							|  |  |  | 					'reputation': 0, | 
					
						
							|  |  |  | 					'votes': 0, | 
					
						
							|  |  |  | 					'editor': '', | 
					
						
							|  |  |  | 					'edited': 0, | 
					
						
							|  |  |  | 					'deleted': 0 | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (data.toPid) { | 
					
						
							|  |  |  | 					postData.toPid = data.toPid; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-05 13:07:13 -05:00
										 |  |  | 				if (data.ip && parseInt(meta.config.trackIpPerPost, 10) === 1) { | 
					
						
							|  |  |  | 					postData.ip = data.ip; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-01 13:05:58 -05:00
										 |  |  | 				if (parseInt(uid, 10) === 0 && data.handle) { | 
					
						
							|  |  |  | 					postData.handle = data.handle; | 
					
						
							| 
									
										
										
										
											2014-12-31 16:27:35 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | 				plugins.fireHook('filter:post.save', postData, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(postData, next) { | 
					
						
							|  |  |  | 				db.setObject('post:' + postData.pid, postData, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				async.parallel([ | 
					
						
							|  |  |  | 					function(next) { | 
					
						
							|  |  |  | 						user.onNewPostMade(postData, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					function(next) { | 
					
						
							|  |  |  | 						topics.onNewPostMade(postData, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					function(next) { | 
					
						
							| 
									
										
										
										
											2015-03-15 01:12:13 -04:00
										 |  |  | 						topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) { | 
					
						
							|  |  |  | 							if (err) { | 
					
						
							|  |  |  | 								return next(err); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							postData.cid = topicData.cid; | 
					
						
							|  |  |  | 							categories.onNewPostMade(topicData.cid, topicData.pinned, postData, next); | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2014-11-09 01:12:24 -05:00
										 |  |  | 					}, | 
					
						
							|  |  |  | 					function(next) { | 
					
						
							|  |  |  | 						db.sortedSetAdd('posts:pid', timestamp, postData.pid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					function(next) { | 
					
						
							|  |  |  | 						db.incrObjectField('global', 'postCount', next); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				], function(err) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return next(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					plugins.fireHook('filter:post.get', postData, next); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(postData, next) { | 
					
						
							|  |  |  | 				plugins.fireHook('action:post.save', postData); | 
					
						
							|  |  |  | 				next(null, postData); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |