mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 11:05:54 +01:00 
			
		
		
		
	post tools test
This commit is contained in:
		| @@ -15,47 +15,47 @@ var social = require('../../social'); | |||||||
| module.exports = function (SocketPosts) { | module.exports = function (SocketPosts) { | ||||||
|  |  | ||||||
| 	SocketPosts.loadPostTools = function (socket, data, callback) { | 	SocketPosts.loadPostTools = function (socket, data, callback) { | ||||||
| 		if (!data) { | 		if (!data || !data.pid || !data.cid) { | ||||||
| 			return callback(new Error('[[error:invalid-data]]')); | 			return callback(new Error('[[error:invalid-data]]')); | ||||||
| 		} | 		} | ||||||
|  | 		async.waterfall([ | ||||||
| 		async.parallel({ | 			function (next) { | ||||||
| 			posts: function (next) { | 				async.parallel({ | ||||||
| 				posts.getPostFields(data.pid, ['deleted', 'bookmarks', 'uid'], next); | 					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); | ||||||
|  | 					} | ||||||
|  | 				}, next); | ||||||
| 			}, | 			}, | ||||||
| 			isAdminOrMod: function (next) { | 			function (results, next) { | ||||||
| 				privileges.categories.isAdminOrMod(data.cid, socket.uid, next); | 				results.posts.tools = results.tools.tools; | ||||||
| 			}, | 				results.posts.deleted = parseInt(results.posts.deleted, 10) === 1; | ||||||
| 			canEdit: function (next) { | 				results.posts.bookmarked = results.bookmarked; | ||||||
| 				privileges.posts.canEdit(data.pid, socket.uid, next); | 				results.posts.selfPost = socket.uid && socket.uid === parseInt(results.posts.uid, 10); | ||||||
| 			}, | 				results.posts.display_edit_tools = results.canEdit.flag; | ||||||
| 			canDelete: function (next) { | 				results.posts.display_delete_tools = results.canDelete.flag; | ||||||
| 				privileges.posts.canDelete(data.pid, socket.uid, next); | 				results.posts.display_moderator_tools = results.posts.display_edit_tools || results.posts.display_delete_tools; | ||||||
| 			}, | 				results.posts.display_move_tools = results.isAdminOrMod; | ||||||
| 			bookmarked: function (next) { | 				next(null, results); | ||||||
| 				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); |  | ||||||
| 			} | 			} | ||||||
| 		}, function (err, results) { | 		], callback); | ||||||
| 			if (err) { |  | ||||||
| 				return callback(err); |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			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; |  | ||||||
| 			callback(null, results); |  | ||||||
| 		}); |  | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	SocketPosts.delete = function (socket, data, callback) { | 	SocketPosts.delete = function (socket, data, callback) { | ||||||
| @@ -98,29 +98,31 @@ module.exports = function (SocketPosts) { | |||||||
| 			return callback(new Error('[[error:invalid-data]]')); | 			return callback(new Error('[[error:invalid-data]]')); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		posts.tools.restore(socket.uid, data.pid, function (err, postData) { | 		async.waterfall([ | ||||||
| 			if (err) { | 			function (next) { | ||||||
| 				return callback(err); | 				posts.tools.restore(socket.uid, data.pid, next); | ||||||
|  | 			}, | ||||||
|  | 			function (postData, next) { | ||||||
|  |  | ||||||
|  | 				websockets.in('topic_' + data.tid).emit('event:post_restored', postData); | ||||||
|  |  | ||||||
|  | 				events.log({ | ||||||
|  | 					type: 'post-restore', | ||||||
|  | 					uid: socket.uid, | ||||||
|  | 					pid: data.pid, | ||||||
|  | 					ip: socket.ip | ||||||
|  | 				}); | ||||||
|  |  | ||||||
|  | 				setImmediate(next); | ||||||
| 			} | 			} | ||||||
|  | 		], callback); | ||||||
| 			websockets.in('topic_' + data.tid).emit('event:post_restored', postData); |  | ||||||
|  |  | ||||||
| 			events.log({ |  | ||||||
| 				type: 'post-restore', |  | ||||||
| 				uid: socket.uid, |  | ||||||
| 				pid: data.pid, |  | ||||||
| 				ip: socket.ip |  | ||||||
| 			}); |  | ||||||
|  |  | ||||||
| 			callback(); |  | ||||||
| 		}); |  | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	SocketPosts.deletePosts = function (socket, data, callback) { | 	SocketPosts.deletePosts = function (socket, data, callback) { | ||||||
| 		if (!data || !Array.isArray(data.pids)) { | 		if (!data || !Array.isArray(data.pids)) { | ||||||
| 			return callback(new Error('[[error:invalid-data]]')); | 			return callback(new Error('[[error:invalid-data]]')); | ||||||
| 		} | 		} | ||||||
| 		async.each(data.pids, function (pid, next) { | 		async.eachSeries(data.pids, function (pid, next) { | ||||||
| 			SocketPosts.delete(socket, {pid: pid, tid: data.tid}, next); | 			SocketPosts.delete(socket, {pid: pid, tid: data.tid}, next); | ||||||
| 		}, callback); | 		}, callback); | ||||||
| 	}; | 	}; | ||||||
| @@ -129,66 +131,61 @@ module.exports = function (SocketPosts) { | |||||||
| 		if (!data || !Array.isArray(data.pids)) { | 		if (!data || !Array.isArray(data.pids)) { | ||||||
| 			return callback(new Error('[[error:invalid-data]]')); | 			return callback(new Error('[[error:invalid-data]]')); | ||||||
| 		} | 		} | ||||||
| 		async.each(data.pids, function (pid, next) { | 		async.eachSeries(data.pids, function (pid, next) { | ||||||
| 			SocketPosts.purge(socket, {pid: pid, tid: data.tid}, next); | 			SocketPosts.purge(socket, {pid: pid, tid: data.tid}, next); | ||||||
| 		}, callback); | 		}, callback); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	SocketPosts.purge = function (socket, data, callback) { | 	SocketPosts.purge = function (socket, data, callback) { | ||||||
| 		function purgePost() { |  | ||||||
| 			var postData; |  | ||||||
| 			async.waterfall([ |  | ||||||
| 				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, |  | ||||||
| 						title: validator.escape(String(title)) |  | ||||||
| 					}, next); |  | ||||||
| 				} |  | ||||||
| 			], callback); |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		if (!data || !parseInt(data.pid, 10)) { | 		if (!data || !parseInt(data.pid, 10)) { | ||||||
| 			return callback(new Error('[[error:invalid-data]]')); | 			return callback(new Error('[[error:invalid-data]]')); | ||||||
| 		} | 		} | ||||||
|  | 		var postData; | ||||||
| 		isMainAndLastPost(data.pid, function (err, results) { | 		async.waterfall([ | ||||||
| 			if (err) { | 			function (next) { | ||||||
| 				return callback(err); | 				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) { | ||||||
|  | 					deleteTopicOf(data.pid, socket, next); | ||||||
|  | 				} | ||||||
|  | 				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, | ||||||
|  | 					title: validator.escape(String(title)) | ||||||
|  | 				}, next); | ||||||
| 			} | 			} | ||||||
|  | 		], callback); | ||||||
| 			if (!results.isMain) { |  | ||||||
| 				return purgePost(); |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			if (!results.isLast) { |  | ||||||
| 				return callback(new Error('[[error:cant-purge-main-post]]')); |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			deleteTopicOf(data.pid, socket, callback); |  | ||||||
| 		}); |  | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	function deleteTopicOf(pid, socket, callback) { | 	function deleteTopicOf(pid, socket, callback) { | ||||||
| 		posts.getTopicFields(pid, ['tid', 'cid'], function (err, topic) { | 		async.waterfall([ | ||||||
| 			if (err) { | 			function (next) { | ||||||
| 				return callback(err); | 				posts.getTopicFields(pid, ['tid', 'cid'], next); | ||||||
|  | 			}, | ||||||
|  | 			function (topic, next) { | ||||||
|  | 				socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, {tids: [topic.tid], cid: topic.cid}, next); | ||||||
| 			} | 			} | ||||||
| 			socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, {tids: [topic.tid], cid: topic.cid}, callback); | 		], callback); | ||||||
| 		}); |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	function isMainAndLastPost(pid, callback) { | 	function isMainAndLastPost(pid, callback) { | ||||||
|   | |||||||
| @@ -1,7 +1,6 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
|  |  | ||||||
| var async = require('async'); | var async = require('async'); | ||||||
| var winston = require('winston'); |  | ||||||
| var validator = require('validator'); | var validator = require('validator'); | ||||||
|  |  | ||||||
| var topics = require('../../topics'); | var topics = require('../../topics'); | ||||||
| @@ -89,31 +88,37 @@ module.exports = function (SocketTopics) { | |||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		async.each(data.tids, function (tid, next) { | 		async.each(data.tids, function (tid, next) { | ||||||
| 			topics.tools[action](tid, socket.uid, function (err, data) { | 			async.waterfall([ | ||||||
| 				if (err) { | 				function (next) { | ||||||
| 					return next(err); | 					topics.tools[action](tid, socket.uid, next); | ||||||
|  | 				}, | ||||||
|  | 				function (data, next) { | ||||||
|  | 					socketHelpers.emitToTopicAndCategory(event, data); | ||||||
|  | 					logTopicAction(action, socket, tid, next); | ||||||
| 				} | 				} | ||||||
|  | 			], next); | ||||||
| 				socketHelpers.emitToTopicAndCategory(event, data); |  | ||||||
|  |  | ||||||
| 				if (action === 'delete' || action === 'restore' || action === 'purge') { |  | ||||||
| 					topics.getTopicField(tid, 'title', function (err, title) { |  | ||||||
| 						if (err) { |  | ||||||
| 							return winston.error(err); |  | ||||||
| 						} |  | ||||||
| 						events.log({ |  | ||||||
| 							type: 'topic-' + action, |  | ||||||
| 							uid: socket.uid, |  | ||||||
| 							ip: socket.ip, |  | ||||||
| 							tid: tid, |  | ||||||
| 							title: validator.escape(String(title)) |  | ||||||
| 						}); |  | ||||||
| 					}); |  | ||||||
| 				} |  | ||||||
|  |  | ||||||
| 				next(); |  | ||||||
| 			}); |  | ||||||
| 		}, callback); | 		}, callback); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
|  | 	function logTopicAction(action, socket, tid, callback) { | ||||||
|  | 		var actionsToLog = ['delete', 'restore', 'purge']; | ||||||
|  | 		if (actionsToLog.indexOf(action) === -1) { | ||||||
|  | 			return setImmediate(callback); | ||||||
|  | 		} | ||||||
|  | 		async.waterfall([ | ||||||
|  | 			function (next) { | ||||||
|  | 				topics.getTopicField(tid, 'title', next); | ||||||
|  | 			}, | ||||||
|  | 			function (title, next) { | ||||||
|  | 				events.log({ | ||||||
|  | 					type: 'topic-' + action, | ||||||
|  | 					uid: socket.uid, | ||||||
|  | 					ip: socket.ip, | ||||||
|  | 					tid: tid, | ||||||
|  | 					title: validator.escape(String(title)) | ||||||
|  | 				}, next); | ||||||
|  | 			} | ||||||
|  | 		], callback); | ||||||
|  | 	} | ||||||
|  |  | ||||||
| }; | }; | ||||||
| @@ -162,26 +162,68 @@ describe('Post\'s', function () { | |||||||
| 		}); | 		}); | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
|  | 	describe('post tools', function () { | ||||||
|  | 		var socketPosts = require('../src/socket.io/posts'); | ||||||
|  |  | ||||||
|  | 		it('should error if data is invalid', function (done) { | ||||||
|  | 			socketPosts.loadPostTools({uid: globalModUid}, null, function (err) { | ||||||
|  | 				assert.equal(err.message, '[[error:invalid-data]]'); | ||||||
|  | 				done(); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
|  |  | ||||||
|  | 		it('should load post tools', function (done) { | ||||||
|  | 			socketPosts.loadPostTools({uid: globalModUid}, {pid: postData.pid, cid: cid}, function (err, data) { | ||||||
|  | 				assert.ifError(err); | ||||||
|  | 				assert(data.posts.display_edit_tools); | ||||||
|  | 				assert(data.posts.display_delete_tools); | ||||||
|  | 				assert(data.posts.display_moderator_tools); | ||||||
|  | 				assert(data.posts.display_move_tools); | ||||||
|  | 				done(); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
| 	describe('delete/restore/purge', function () { | 	describe('delete/restore/purge', function () { | ||||||
| 		var pid; | 		var tid; | ||||||
|  | 		var mainPid; | ||||||
|  | 		var replyPid; | ||||||
|  |  | ||||||
| 		var socketPosts = require('../src/socket.io/posts'); | 		var socketPosts = require('../src/socket.io/posts'); | ||||||
| 		before(function (done) { | 		before(function (done) { | ||||||
| 			topics.reply({ | 			topics.post({ | ||||||
| 				uid: voterUid, | 				uid: voterUid, | ||||||
| 				tid: topicData.tid, | 				cid: cid, | ||||||
| 				timestamp: Date.now(), | 				title: 'topic to delete/restore/purge', | ||||||
| 				content: 'A post to delete/restore and purge' | 				content: 'A post to delete/restore/purge' | ||||||
| 			}, function (err, data) { | 			}, function (err, data) { | ||||||
| 				assert.ifError(err); | 				assert.ifError(err); | ||||||
| 				pid = data.pid; | 				tid = data.topicData.tid; | ||||||
| 				privileges.categories.give(['purge'], cid, 'registered-users', done); | 				mainPid = data.postData.pid; | ||||||
|  | 				topics.reply({ | ||||||
|  | 					uid: voterUid, | ||||||
|  | 					tid: topicData.tid, | ||||||
|  | 					timestamp: Date.now(), | ||||||
|  | 					content: 'A post to delete/restore and purge' | ||||||
|  | 				}, function (err, data) { | ||||||
|  | 					assert.ifError(err); | ||||||
|  | 					replyPid = data.pid; | ||||||
|  | 					privileges.categories.give(['purge'], cid, 'registered-users', done); | ||||||
|  | 				}); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
|  |  | ||||||
|  | 		it('should error with invalid data', function (done) { | ||||||
|  | 			socketPosts.delete({uid: voterUid}, null, function (err) { | ||||||
|  | 				assert.equal(err.message, '[[error:invalid-data]]'); | ||||||
|  | 				done(); | ||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		it('should delete a post', function (done) { | 		it('should delete a post', function (done) { | ||||||
| 			socketPosts.delete({uid: voterUid}, {pid: pid, tid: topicData.tid}, function (err) { | 			socketPosts.delete({uid: voterUid}, {pid: replyPid, tid: tid}, function (err) { | ||||||
| 				assert.ifError(err); | 				assert.ifError(err); | ||||||
| 				posts.getPostField(pid, 'deleted', function (err, isDeleted) { | 				posts.getPostField(replyPid, 'deleted', function (err, isDeleted) { | ||||||
| 					assert.ifError(err); | 					assert.ifError(err); | ||||||
| 					assert.equal(parseInt(isDeleted, 10), 1); | 					assert.equal(parseInt(isDeleted, 10), 1); | ||||||
| 					done(); | 					done(); | ||||||
| @@ -190,9 +232,9 @@ describe('Post\'s', function () { | |||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		it('should restore a post', function (done) { | 		it('should restore a post', function (done) { | ||||||
| 			socketPosts.restore({uid: voterUid}, {pid: pid, tid: topicData.tid}, function (err) { | 			socketPosts.restore({uid: voterUid}, {pid: replyPid, tid: tid}, function (err) { | ||||||
| 				assert.ifError(err); | 				assert.ifError(err); | ||||||
| 				posts.getPostField(pid, 'deleted', function (err, isDeleted) { | 				posts.getPostField(replyPid, 'deleted', function (err, isDeleted) { | ||||||
| 					assert.ifError(err); | 					assert.ifError(err); | ||||||
| 					assert.equal(parseInt(isDeleted, 10), 0); | 					assert.equal(parseInt(isDeleted, 10), 0); | ||||||
| 					done(); | 					done(); | ||||||
| @@ -200,16 +242,31 @@ describe('Post\'s', function () { | |||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		it('should purge a post', function (done) { | 		it('should delete posts and topic', function (done) { | ||||||
| 			socketPosts.purge({uid: voterUid}, {pid: pid}, function (err) { | 			socketPosts.deletePosts({uid: globalModUid}, {pids: [replyPid, mainPid], tid: tid}, function (err) { | ||||||
| 				assert.ifError(err); | 				assert.ifError(err); | ||||||
| 				posts.exists('post:' + pid, function (err, exists) { | 				topics.getTopicField(tid, 'deleted', function (err, deleted) { | ||||||
| 					assert.ifError(err); | 					assert.ifError(err); | ||||||
| 					assert.equal(exists, false); | 					assert.equal(parseInt(deleted, 10), 1); | ||||||
| 					done(); | 					done(); | ||||||
| 				}); | 				}); | ||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
|  | 		it('should purge posts', function (done) { | ||||||
|  | 			var socketTopics = require('../src/socket.io/topics'); | ||||||
|  | 			socketTopics.restore({uid: globalModUid}, {tids: [tid], cid: cid}, function (err) { | ||||||
|  | 				assert.ifError(err); | ||||||
|  | 				socketPosts.purgePosts({uid: voterUid}, {pids: [replyPid, mainPid], tid: tid}, function (err) { | ||||||
|  | 					assert.ifError(err); | ||||||
|  | 					posts.exists('post:' + replyPid, function (err, exists) { | ||||||
|  | 						assert.ifError(err); | ||||||
|  | 						assert.equal(exists, false); | ||||||
|  | 						done(); | ||||||
|  | 					}); | ||||||
|  | 				}); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	describe('edit', function () { | 	describe('edit', function () { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user