| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'); | 
					
						
							| 
									
										
										
										
											2016-07-25 15:23:50 +03:00
										 |  |  | var validator = require('validator'); | 
					
						
							| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | var posts = require('../../posts'); | 
					
						
							| 
									
										
										
										
											2016-07-25 15:23:50 +03:00
										 |  |  | var topics = require('../../topics'); | 
					
						
							| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | var events = require('../../events'); | 
					
						
							|  |  |  | var websockets = require('../index'); | 
					
						
							|  |  |  | var socketTopics = require('../topics'); | 
					
						
							| 
									
										
										
										
											2015-10-24 20:50:43 -04:00
										 |  |  | var privileges = require('../../privileges'); | 
					
						
							| 
									
										
										
										
											2015-10-28 20:13:18 -04:00
										 |  |  | var plugins = require('../../plugins'); | 
					
						
							| 
									
										
										
										
											2016-02-25 14:05:48 -05:00
										 |  |  | var social = require('../../social'); | 
					
						
							| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | module.exports = function (SocketPosts) { | 
					
						
							|  |  |  | 	SocketPosts.loadPostTools = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 		if (!data || !data.pid || !data.cid) { | 
					
						
							| 
									
										
										
										
											2015-10-28 20:13:18 -04:00
										 |  |  | 			return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2015-10-24 20:50:43 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				async.parallel({ | 
					
						
							|  |  |  | 					posts: function (next) { | 
					
						
							|  |  |  | 						posts.getPostFields(data.pid, ['deleted', 'bookmarks', 'uid'], next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					isAdminOrMod: function (next) { | 
					
						
							|  |  |  | 						privileges.categories.isAdminOrMod(data.cid, socket.uid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					canEdit: function (next) { | 
					
						
							|  |  |  | 						privileges.posts.canEdit(data.pid, socket.uid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					canDelete: function (next) { | 
					
						
							|  |  |  | 						privileges.posts.canDelete(data.pid, socket.uid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					bookmarked: function (next) { | 
					
						
							|  |  |  | 						posts.hasBookmarked(data.pid, socket.uid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					tools: function (next) { | 
					
						
							|  |  |  | 						plugins.fireHook('filter:post.tools', {pid: data.pid, uid: socket.uid, tools: []}, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					postSharing: function (next) { | 
					
						
							|  |  |  | 						social.getActivePostSharing(next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 				}, next); | 
					
						
							| 
									
										
										
										
											2016-02-25 14:05:48 -05:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 			function (results, next) { | 
					
						
							|  |  |  | 				results.posts.tools = results.tools.tools; | 
					
						
							|  |  |  | 				results.posts.deleted = parseInt(results.posts.deleted, 10) === 1; | 
					
						
							|  |  |  | 				results.posts.bookmarked = results.bookmarked; | 
					
						
							|  |  |  | 				results.posts.selfPost = socket.uid && socket.uid === parseInt(results.posts.uid, 10); | 
					
						
							|  |  |  | 				results.posts.display_edit_tools = results.canEdit.flag; | 
					
						
							|  |  |  | 				results.posts.display_delete_tools = results.canDelete.flag; | 
					
						
							|  |  |  | 				results.posts.display_moderator_tools = results.posts.display_edit_tools || results.posts.display_delete_tools; | 
					
						
							|  |  |  | 				results.posts.display_move_tools = results.isAdminOrMod; | 
					
						
							|  |  |  | 				next(null, results); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2015-10-24 20:50:43 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	SocketPosts.delete = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2016-10-12 14:46:04 +03:00
										 |  |  | 		if (!data || !data.pid) { | 
					
						
							|  |  |  | 			return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		var postData; | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2016-10-12 14:46:04 +03:00
										 |  |  | 				posts.tools.delete(socket.uid, data.pid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (_postData, next) { | 
					
						
							| 
									
										
										
										
											2016-10-12 14:46:04 +03:00
										 |  |  | 				postData = _postData; | 
					
						
							|  |  |  | 				isMainAndLastPost(data.pid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (results, next) { | 
					
						
							| 
									
										
										
										
											2016-10-12 14:46:04 +03:00
										 |  |  | 				if (results.isMain && results.isLast) { | 
					
						
							|  |  |  | 					deleteTopicOf(data.pid, socket, next); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					next(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2016-10-12 14:46:04 +03:00
										 |  |  | 				websockets.in('topic_' + data.tid).emit('event:post_deleted', postData); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				events.log({ | 
					
						
							|  |  |  | 					type: 'post-delete', | 
					
						
							|  |  |  | 					uid: socket.uid, | 
					
						
							|  |  |  | 					pid: data.pid, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					ip: socket.ip, | 
					
						
							| 
									
										
										
										
											2016-10-12 14:46:04 +03:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				next(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-12 14:46:04 +03:00
										 |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	SocketPosts.restore = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2016-10-12 14:46:04 +03:00
										 |  |  | 		if (!data || !data.pid) { | 
					
						
							|  |  |  | 			return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				posts.tools.restore(socket.uid, data.pid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (postData, next) { | 
					
						
							|  |  |  | 				websockets.in('topic_' + data.tid).emit('event:post_restored', postData); | 
					
						
							| 
									
										
										
										
											2016-10-12 14:46:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 				events.log({ | 
					
						
							|  |  |  | 					type: 'post-restore', | 
					
						
							|  |  |  | 					uid: socket.uid, | 
					
						
							|  |  |  | 					pid: data.pid, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					ip: socket.ip, | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2016-10-12 14:46:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 				setImmediate(next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	SocketPosts.deletePosts = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-24 12:08:10 +02:00
										 |  |  | 		if (!data || !Array.isArray(data.pids)) { | 
					
						
							|  |  |  | 			return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 		async.eachSeries(data.pids, function (pid, next) { | 
					
						
							| 
									
										
										
										
											2015-12-24 12:08:10 +02:00
										 |  |  | 			SocketPosts.delete(socket, {pid: pid, tid: data.tid}, next); | 
					
						
							|  |  |  | 		}, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	SocketPosts.purgePosts = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-24 12:08:10 +02:00
										 |  |  | 		if (!data || !Array.isArray(data.pids)) { | 
					
						
							|  |  |  | 			return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 		async.eachSeries(data.pids, function (pid, next) { | 
					
						
							| 
									
										
										
										
											2015-12-24 12:08:10 +02:00
										 |  |  | 			SocketPosts.purge(socket, {pid: pid, tid: data.tid}, next); | 
					
						
							|  |  |  | 		}, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	SocketPosts.purge = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | 		if (!data || !parseInt(data.pid, 10)) { | 
					
						
							|  |  |  | 			return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 		var postData; | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				isMainAndLastPost(data.pid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (results, next) { | 
					
						
							|  |  |  | 				if (results.isMain && !results.isLast) { | 
					
						
							|  |  |  | 					return callback(new Error('[[error:cant-purge-main-post]]')); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (results.isMain && results.isLast) { | 
					
						
							| 
									
										
										
										
											2016-12-21 00:31:15 +03:00
										 |  |  | 					return deleteTopicOf(data.pid, socket, next); | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				setImmediate(next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				posts.getPostField(data.pid, 'toPid', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (toPid, next) { | 
					
						
							|  |  |  | 				postData = {pid: data.pid, toPid: toPid}; | 
					
						
							|  |  |  | 				posts.tools.purge(socket.uid, data.pid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				websockets.in('topic_' + data.tid).emit('event:post_purged', postData); | 
					
						
							|  |  |  | 				topics.getTopicField(data.tid, 'title', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (title, next) { | 
					
						
							|  |  |  | 				events.log({ | 
					
						
							|  |  |  | 					type: 'post-purge', | 
					
						
							|  |  |  | 					uid: socket.uid, | 
					
						
							|  |  |  | 					pid: data.pid, | 
					
						
							|  |  |  | 					ip: socket.ip, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					title: validator.escape(String(title)), | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 				}, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-12 14:46:04 +03:00
										 |  |  | 	function deleteTopicOf(pid, socket, callback) { | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				posts.getTopicFields(pid, ['tid', 'cid'], next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (topic, next) { | 
					
						
							|  |  |  | 				socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, {tids: [topic.tid], cid: topic.cid}, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-12-20 16:03:01 +03:00
										 |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2016-10-12 14:46:04 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | 	function isMainAndLastPost(pid, callback) { | 
					
						
							|  |  |  | 		async.parallel({ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			isMain: function (next) { | 
					
						
							| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | 				posts.isMain(pid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			isLast: function (next) { | 
					
						
							|  |  |  | 				posts.getTopicFields(pid, ['postcount'], function (err, topic) { | 
					
						
							| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | 					next(err, topic ? parseInt(topic.postcount, 10) === 1 : false); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-09-25 15:09:25 -04:00
										 |  |  | 		}, callback); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-06 20:28:55 -05:00
										 |  |  | }; |