| 
									
										
										
										
											2014-06-10 14:24:50 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'), | 
					
						
							|  |  |  | 	db = require('../database'), | 
					
						
							| 
									
										
										
										
											2014-06-17 12:34:09 +07:00
										 |  |  | 	topics = require('../topics'), | 
					
						
							| 
									
										
										
										
											2014-06-10 14:24:50 -04:00
										 |  |  | 	plugins = require('../plugins'); | 
					
						
							| 
									
										
										
										
											2014-06-09 12:51:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = function(Posts) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-10 16:56:55 -04:00
										 |  |  | 	Posts.purge = function(pid, callback) { | 
					
						
							| 
									
										
										
										
											2014-06-10 14:24:50 -04:00
										 |  |  | 		async.parallel([ | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				deletePostFromTopicAndUser(pid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				deletePostFromCategoryRecentPosts(pid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				deletePostFromUsersFavourites(pid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				deletePostFromUsersVotes(pid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				db.sortedSetRemove('posts:pid', pid, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], function(err) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-06-09 12:51:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-10 14:24:50 -04:00
										 |  |  | 			plugins.fireHook('action:post.delete', pid); | 
					
						
							|  |  |  | 			db.delete('post:' + pid, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-06-09 12:51:49 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-10 14:24:50 -04:00
										 |  |  | 	function deletePostFromTopicAndUser(pid, callback) { | 
					
						
							|  |  |  | 		Posts.getPostFields(pid, ['tid', 'uid', 'deleted'], function(err, postData) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-16 21:43:11 -04:00
										 |  |  | 			db.sortedSetsRemove([ | 
					
						
							|  |  |  | 				'tid:' + postData.tid + ':posts', | 
					
						
							|  |  |  | 				'tid:' + postData.tid + ':posts:votes', | 
					
						
							|  |  |  | 				'uid:' + postData.uid + ':posts' | 
					
						
							|  |  |  | 			], pid, function(err) { | 
					
						
							| 
									
										
										
										
											2014-06-10 14:24:50 -04:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-17 13:11:09 -04:00
										 |  |  | 				topics.getTopicFields(postData.tid, ['cid', 'deleted'], function(err, topicData) { | 
					
						
							| 
									
										
										
										
											2014-06-17 12:34:09 +07:00
										 |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return callback(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (parseInt(postData.deleted, 10) === 0 && parseInt(topicData.deleted, 10) !== 1) { | 
					
						
							| 
									
										
										
										
											2014-06-17 13:11:09 -04:00
										 |  |  | 						async.parallel([ | 
					
						
							|  |  |  | 							function (next) { | 
					
						
							|  |  |  | 								db.decrObjectField('global', 'postCount', next); | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							function (next) { | 
					
						
							|  |  |  | 								db.decrObjectField('category:' + topicData.cid, 'post_count', next); | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							function (next) { | 
					
						
							|  |  |  | 								db.decrObjectField('topic:' + postData.tid, 'postcount', next); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						], callback); | 
					
						
							| 
									
										
										
										
											2014-06-17 12:34:09 +07:00
										 |  |  | 					} else { | 
					
						
							|  |  |  | 						callback(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-06-10 14:24:50 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function deletePostFromCategoryRecentPosts(pid, callback) { | 
					
						
							|  |  |  | 		db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 22:37:46 -04:00
										 |  |  | 			var sets = cids.map(function(cid) { | 
					
						
							|  |  |  | 				return 'categories:recent_posts:cid:' + cid; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			db.sortedSetsRemove(sets, pid, callback); | 
					
						
							| 
									
										
										
										
											2014-06-10 14:24:50 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function deletePostFromUsersFavourites(pid, callback) { | 
					
						
							|  |  |  | 		db.getSetMembers('pid:' + pid + ':users_favourited', function(err, uids) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 22:37:46 -04:00
										 |  |  | 			var sets = uids.map(function(uid) { | 
					
						
							| 
									
										
										
										
											2014-09-16 21:43:11 -04:00
										 |  |  | 				return 'uid:' + uid + ':favourites'; | 
					
						
							| 
									
										
										
										
											2014-06-21 22:37:46 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			db.sortedSetsRemove(sets, pid, function(err) { | 
					
						
							| 
									
										
										
										
											2014-06-10 14:24:50 -04:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				db.delete('pid:' + pid + ':users_favourited', callback); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function deletePostFromUsersVotes(pid, callback) { | 
					
						
							|  |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			upvoters: function(next) { | 
					
						
							|  |  |  | 				db.getSetMembers('pid:' + pid + ':upvote', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			downvoters: function(next) { | 
					
						
							|  |  |  | 				db.getSetMembers('pid:' + pid + ':downvote', next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, function(err, results) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 22:37:46 -04:00
										 |  |  | 			var upvoterSets = results.upvoters.map(function(uid) { | 
					
						
							|  |  |  | 				return 'uid:' + uid + ':upvote'; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var downvoterSets = results.downvoters.map(function(uid) { | 
					
						
							|  |  |  | 				return 'uid:' + uid + ':downvote'; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-10 14:24:50 -04:00
										 |  |  | 			async.parallel([ | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							| 
									
										
										
										
											2014-06-21 22:37:46 -04:00
										 |  |  | 					db.sortedSetsRemove(upvoterSets, pid, next); | 
					
						
							| 
									
										
										
										
											2014-06-10 14:24:50 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							| 
									
										
										
										
											2014-06-21 22:37:46 -04:00
										 |  |  | 					db.sortedSetsRemove(downvoterSets, pid, next); | 
					
						
							| 
									
										
										
										
											2014-06-21 22:11:44 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							| 
									
										
										
										
											2014-09-16 21:43:11 -04:00
										 |  |  | 					db.deleteAll(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], next); | 
					
						
							| 
									
										
										
										
											2014-06-10 14:24:50 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			], callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-17 12:34:09 +07:00
										 |  |  | }; |