all files / src/socket.io/posts/ tools.js

24.68% Statements 19/77
0% Branches 0/36
4% Functions 1/25
24.68% Lines 19/77
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159                                                                                                                                                                                                                                                                                       
'use strict';
 
var async = require('async');
 
var posts = require('../../posts');
var events = require('../../events');
var websockets = require('../index');
var socketTopics = require('../topics');
var privileges = require('../../privileges');
var plugins = require('../../plugins');
var social = require('../../social');
var favourites = require('../../favourites');
 
module.exports = function(SocketPosts) {
 
	SocketPosts.loadPostTools = function(socket, data, callback) {
		if (!data) {
			return callback(new Error('[[error:invalid-data]]'));
		}
 
		async.parallel({
			posts: function(next) {
				posts.getPostFields(data.pid, ['deleted', 'reputation', 'uid'], next);
			},
			isAdminOrMod: function(next) {
				privileges.categories.isAdminOrMod(data.cid, socket.uid, next);
			},
			favourited: function(next) {
				favourites.getFavouritesByPostIDs([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) {
			if (err) {
				return callback(err);
			}
			results.posts.tools = results.tools.tools;
			results.posts.deleted = parseInt(results.posts.deleted, 10) === 1;
			results.posts.favourited = results.favourited[0];
			results.posts.selfPost = socket.uid && socket.uid === parseInt(results.posts.uid, 10);
			results.posts.display_moderator_tools = results.isAdminOrMod || results.posts.selfPost;
			results.posts.display_move_tools = results.isAdminOrMod;
			callback(null, results);
		});
	};
 
	SocketPosts.delete = function(socket, data, callback) {
		doPostAction('delete', 'event:post_deleted', socket, data, callback);
	};
 
	SocketPosts.restore = function(socket, data, callback) {
		doPostAction('restore', 'event:post_restored', socket, data, callback);
	};
 
	SocketPosts.deletePosts = function(socket, data, callback) {
		if (!data || !Array.isArray(data.pids)) {
			return callback(new Error('[[error:invalid-data]]'));
		}
		async.each(data.pids, function(pid, next) {
			SocketPosts.delete(socket, {pid: pid, tid: data.tid}, next);
		}, callback);
	};
 
	SocketPosts.purgePosts = function(socket, data, callback) {
		if (!data || !Array.isArray(data.pids)) {
			return callback(new Error('[[error:invalid-data]]'));
		}
		async.each(data.pids, function(pid, next) {
			SocketPosts.purge(socket, {pid: pid, tid: data.tid}, next);
		}, callback);
	};
 
	function doPostAction(command, eventName, socket, data, callback) {
		if (!data) {
			return callback(new Error('[[error:invalid-data]]'));
		}
 
		posts.tools[command](socket.uid, data.pid, function(err, postData) {
			if (err) {
				return callback(err);
			}
 
			websockets.in('topic_' + data.tid).emit(eventName, postData);
 
			events.log({
				type: 'post-' + command,
				uid: socket.uid,
				pid: data.pid,
				ip: socket.ip
			});
 
			callback();
		});
	}
 
	SocketPosts.purge = function(socket, data, callback) {
		function purgePost() {
			posts.tools.purge(socket.uid, data.pid, function(err) {
				if (err) {
					return callback(err);
				}
 
				websockets.in('topic_' + data.tid).emit('event:post_purged', data.pid);
 
				events.log({
					type: 'post-purge',
					uid: socket.uid,
					pid: data.pid,
					ip: socket.ip
				});
 
				callback();
			});
		}
 
		if (!data || !parseInt(data.pid, 10)) {
			return callback(new Error('[[error:invalid-data]]'));
		}
 
		isMainAndLastPost(data.pid, function(err, results) {
			if (err) {
				return callback(err);
			}
 
			if (!results.isMain) {
				return purgePost();
			}
 
			if (!results.isLast) {
				return callback(new Error('[[error:cant-purge-main-post]]'));
			}
 
			posts.getTopicFields(data.pid, ['tid', 'cid'], function(err, topic) {
				if (err) {
					return callback(err);
				}
				socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, {tids: [topic.tid], cid: topic.cid}, callback);
			});
		});
	};
 
	function isMainAndLastPost(pid, callback) {
		async.parallel({
			isMain: function(next) {
				posts.isMain(pid, next);
			},
			isLast: function(next) {
				posts.getTopicFields(pid, ['postcount'], function(err, topic) {
					next(err, topic ? parseInt(topic.postcount, 10) === 1 : false);
				});
			}
		}, callback);
	}
 
};