| 
									
										
										
										
											2014-02-28 00:14:11 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-29 20:35:49 +03:00
										 |  |  | var async = require('async'); | 
					
						
							| 
									
										
										
										
											2017-05-26 01:39:40 -06:00
										 |  |  | var _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2014-06-12 16:45:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 15:03:32 -04:00
										 |  |  | var db = require('../database'); | 
					
						
							|  |  |  | var utils = require('../utils'); | 
					
						
							|  |  |  | var user = require('../user'); | 
					
						
							|  |  |  | var topics = require('../topics'); | 
					
						
							|  |  |  | var privileges = require('../privileges'); | 
					
						
							|  |  |  | var plugins = require('../plugins'); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | var Posts = module.exports; | 
					
						
							| 
									
										
										
										
											2014-06-09 12:51:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 15:03:32 -04:00
										 |  |  | require('./data')(Posts); | 
					
						
							|  |  |  | require('./create')(Posts); | 
					
						
							|  |  |  | require('./delete')(Posts); | 
					
						
							|  |  |  | require('./edit')(Posts); | 
					
						
							|  |  |  | require('./parse')(Posts); | 
					
						
							|  |  |  | require('./user')(Posts); | 
					
						
							|  |  |  | require('./topics')(Posts); | 
					
						
							|  |  |  | require('./category')(Posts); | 
					
						
							|  |  |  | require('./summary')(Posts); | 
					
						
							|  |  |  | require('./recent')(Posts); | 
					
						
							|  |  |  | require('./tools')(Posts); | 
					
						
							|  |  |  | require('./votes')(Posts); | 
					
						
							|  |  |  | require('./bookmarks')(Posts); | 
					
						
							|  |  |  | require('./queue')(Posts); | 
					
						
							|  |  |  | require('./diffs')(Posts); | 
					
						
							|  |  |  | require('./uploads')(Posts); | 
					
						
							| 
									
										
										
										
											2014-10-31 22:04:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | Posts.exists = function (pid, callback) { | 
					
						
							| 
									
										
										
										
											2018-10-26 14:31:30 -04:00
										 |  |  | 	db.exists('post:' + pid, callback); | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-05-11 15:38:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | Posts.getPidsFromSet = function (set, start, stop, reverse, callback) { | 
					
						
							|  |  |  | 	if (isNaN(start) || isNaN(stop)) { | 
					
						
							| 
									
										
										
										
											2018-11-12 00:20:44 -05:00
										 |  |  | 		return setImmediate(callback, null, []); | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop, callback); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-20 21:00:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | Posts.getPostsByPids = function (pids, uid, callback) { | 
					
						
							|  |  |  | 	if (!Array.isArray(pids) || !pids.length) { | 
					
						
							|  |  |  | 		return callback(null, []); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-20 21:00:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2018-10-21 19:33:46 -04:00
										 |  |  | 			Posts.getPostsData(pids, next); | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function (posts, next) { | 
					
						
							| 
									
										
										
										
											2018-10-21 19:33:46 -04:00
										 |  |  | 			async.map(posts, Posts.parsePost, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (posts, next) { | 
					
						
							|  |  |  | 			user.blocks.filter(uid, posts, next); | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function (posts, next) { | 
					
						
							|  |  |  | 			plugins.fireHook('filter:post.getPosts', { posts: posts, uid: uid }, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (data, next) { | 
					
						
							|  |  |  | 			if (!data || !Array.isArray(data.posts)) { | 
					
						
							|  |  |  | 				return next(null, []); | 
					
						
							| 
									
										
										
										
											2013-11-03 11:53:44 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 			data.posts = data.posts.filter(Boolean); | 
					
						
							|  |  |  | 			next(null, data.posts); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-05-24 12:46:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | Posts.getPostSummariesFromSet = function (set, uid, start, stop, callback) { | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			db.getSortedSetRevRange(set, start, stop, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (pids, next) { | 
					
						
							|  |  |  | 			privileges.posts.filter('read', pids, uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (pids, next) { | 
					
						
							|  |  |  | 			Posts.getPostSummaryByPids(pids, uid, { stripTags: false }, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (posts, next) { | 
					
						
							|  |  |  | 			next(null, { posts: posts, nextStart: stop + 1 }); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-11-11 18:08:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | Posts.getPidIndex = function (pid, tid, topicPostSort, callback) { | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			var set = topicPostSort === 'most_votes' ? 'tid:' + tid + ':posts:votes' : 'tid:' + tid + ':posts'; | 
					
						
							|  |  |  | 			db.sortedSetRank(set, pid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (index, next) { | 
					
						
							| 
									
										
										
										
											2015-09-14 21:04:56 -04:00
										 |  |  | 			if (!utils.isNumber(index)) { | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 				return next(null, 0); | 
					
						
							| 
									
										
										
										
											2014-02-17 20:57:12 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 			next(null, parseInt(index, 10) + 1); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-25 19:19:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | Posts.getPostIndices = function (posts, uid, callback) { | 
					
						
							|  |  |  | 	if (!Array.isArray(posts) || !posts.length) { | 
					
						
							|  |  |  | 		return callback(null, []); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-09-26 22:19:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			user.getSettings(uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (settings, next) { | 
					
						
							|  |  |  | 			var byVotes = settings.topicPostSort === 'most_votes'; | 
					
						
							|  |  |  | 			var sets = posts.map(function (post) { | 
					
						
							|  |  |  | 				return byVotes ? 'tid:' + post.tid + ':posts:votes' : 'tid:' + post.tid + ':posts'; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2016-12-02 16:10:07 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 			var uniqueSets = _.uniq(sets); | 
					
						
							|  |  |  | 			var method = 'sortedSetsRanks'; | 
					
						
							|  |  |  | 			if (uniqueSets.length === 1) { | 
					
						
							|  |  |  | 				method = 'sortedSetRanks'; | 
					
						
							|  |  |  | 				sets = uniqueSets[0]; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-08-16 21:33:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 			var pids = posts.map(function (post) { | 
					
						
							|  |  |  | 				return post.pid; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2016-12-02 16:10:07 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 			db[method](sets, pids, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (indices, next) { | 
					
						
							|  |  |  | 			for (var i = 0; i < indices.length; i += 1) { | 
					
						
							|  |  |  | 				indices[i] = utils.isNumber(indices[i]) ? parseInt(indices[i], 10) + 1 : 0; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-08-16 21:33:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 			next(null, indices); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-08-16 21:33:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | Posts.updatePostVoteCount = function (postData, callback) { | 
					
						
							|  |  |  | 	if (!postData || !postData.pid || !postData.tid) { | 
					
						
							|  |  |  | 		return callback(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	async.parallel([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			if (postData.uid) { | 
					
						
							|  |  |  | 				if (postData.votes > 0) { | 
					
						
							|  |  |  | 					db.sortedSetAdd('uid:' + postData.uid + ':posts:votes', postData.votes, postData.pid, next); | 
					
						
							| 
									
										
										
										
											2016-01-14 13:47:26 +02:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 					db.sortedSetRemove('uid:' + postData.uid + ':posts:votes', postData.pid, next); | 
					
						
							| 
									
										
										
										
											2016-01-14 13:47:26 +02:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				next(); | 
					
						
							| 
									
										
										
										
											2016-11-02 13:21:51 +03:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2018-12-12 14:35:51 -05:00
										 |  |  | 					topics.getTopicFields(postData.tid, ['mainPid', 'cid', 'pinned'], next); | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-12-08 19:58:12 -05:00
										 |  |  | 				function (topicData, next) { | 
					
						
							|  |  |  | 					if (parseInt(topicData.mainPid, 10) === parseInt(postData.pid, 10)) { | 
					
						
							|  |  |  | 						async.parallel([ | 
					
						
							|  |  |  | 							function (next) { | 
					
						
							|  |  |  | 								topics.setTopicFields(postData.tid, { | 
					
						
							|  |  |  | 									upvotes: postData.upvotes, | 
					
						
							|  |  |  | 									downvotes: postData.downvotes, | 
					
						
							|  |  |  | 								}, next); | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							function (next) { | 
					
						
							|  |  |  | 								db.sortedSetAdd('topics:votes', postData.votes, postData.tid, next); | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							function (next) { | 
					
						
							| 
									
										
										
										
											2018-12-12 14:35:51 -05:00
										 |  |  | 								if (!topicData.pinned) { | 
					
						
							|  |  |  | 									db.sortedSetAdd('cid:' + topicData.cid + ':tids:votes', postData.votes, postData.tid, next); | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									next(); | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2017-12-08 19:58:12 -05:00
										 |  |  | 							}, | 
					
						
							|  |  |  | 						], function (err) { | 
					
						
							|  |  |  | 							next(err); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 						return; | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 					} | 
					
						
							|  |  |  | 					db.sortedSetAdd('tid:' + postData.tid + ':posts:votes', postData.votes, postData.pid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			db.sortedSetAdd('posts:votes', postData.votes, postData.pid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2017-12-08 19:58:12 -05:00
										 |  |  | 			Posts.setPostFields(postData.pid, { | 
					
						
							|  |  |  | 				upvotes: postData.upvotes, | 
					
						
							|  |  |  | 				downvotes: postData.downvotes, | 
					
						
							|  |  |  | 			}, next); | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	], function (err) { | 
					
						
							|  |  |  | 		callback(err); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-08 16:01:11 -04:00
										 |  |  | Posts.modifyPostByPrivilege = function (post, privileges) { | 
					
						
							|  |  |  | 	if (post.deleted && !(post.selfPost || privileges['posts:view_deleted'])) { | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 		post.content = '[[topic:post_is_deleted]]'; | 
					
						
							|  |  |  | 		if (post.user) { | 
					
						
							|  |  |  | 			post.user.signature = ''; | 
					
						
							| 
									
										
										
										
											2016-11-02 13:21:51 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-04-19 12:33:53 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-08-31 11:04:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 15:03:32 -04:00
										 |  |  | Posts.async = require('../promisify')(Posts); |