some cleanup to socket posts

v1.18.x
barisusakli 11 years ago
parent 92267de9d8
commit 2befa06a33

@ -79,34 +79,30 @@ SocketPosts.reply = function(socket, data, callback) {
}; };
SocketPosts.upvote = function(socket, data) { SocketPosts.upvote = function(socket, data) {
if(data && data.pid && data.room_id) { favouriteCommand('upvote', socket, data);
favourites.upvote(data.pid, data.room_id, socket.uid, socket);
}
}; };
SocketPosts.downvote = function(socket, data) { SocketPosts.downvote = function(socket, data) {
if(data && data.pid && data.room_id) { favouriteCommand('downvote', socket, data);
favourites.downvote(data.pid, data.room_id, socket.uid, socket);
}
}; };
SocketPosts.unvote = function(socket, data) { SocketPosts.unvote = function(socket, data) {
if(data && data.pid && data.room_id) { favouriteCommand('unvote', socket, data);
favourites.unvote(data.pid, data.room_id, socket.uid, socket);
}
}; };
SocketPosts.favourite = function(socket, data) { SocketPosts.favourite = function(socket, data) {
if(data && data.pid && data.room_id) { favouriteCommand('favourite', socket, data);
favourites.favourite(data.pid, data.room_id, socket.uid, socket);
}
}; };
SocketPosts.unfavourite = function(socket, data) { SocketPosts.unfavourite = function(socket, data) {
favouriteCommand('unfavourite', socket, data);
};
function favouriteCommand(command, socket, data) {
if(data && data.pid && data.room_id) { if(data && data.pid && data.room_id) {
favourites.unfavourite(data.pid, data.room_id, socket.uid, socket); favourites[command](data.pid, data.room_id, socket.uid, socket);
} }
}; }
SocketPosts.uploadImage = function(socket, data, callback) { SocketPosts.uploadImage = function(socket, data, callback) {
if(data) { if(data) {
@ -170,51 +166,40 @@ SocketPosts.edit = function(socket, data, callback) {
}; };
SocketPosts.delete = function(socket, data, callback) { SocketPosts.delete = function(socket, data, callback) {
if(!data) { deleteOrRestore('delete', socket, data, callback);
return callback(new Error('invalid data'));
}
postTools.delete(socket.uid, data.pid, function(err) {
if(err) {
return callback(err);
}
module.parent.exports.emitTopicPostStats();
index.server.sockets.in('topic_' + data.tid).emit('event:post_deleted', {
pid: data.pid
});
callback();
});
}; };
SocketPosts.restore = function(socket, data, callback) { SocketPosts.restore = function(socket, data, callback) {
deleteOrRestore('restore', socket, data, callback);
};
function deleteOrRestore(command, socket, data, callback) {
if(!data) { if(!data) {
return callback(new Error('invalid data')); return callback(new Error('invalid data'));
} }
postTools.restore(socket.uid, data.pid, function(err) { postTools[command](socket.uid, data.pid, function(err) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
module.parent.exports.emitTopicPostStats(); module.parent.exports.emitTopicPostStats();
index.server.sockets.in('topic_' + data.tid).emit('event:post_restored', { var eventName = command === 'restore' ? 'event:post_restored' : 'event:post_deleted';
index.server.sockets.in('topic_' + data.tid).emit(eventName, {
pid: data.pid pid: data.pid
}); });
callback(); callback();
}); });
}; }
SocketPosts.getPrivileges = function(socket, pid, callback) { SocketPosts.getPrivileges = function(socket, pid, callback) {
postTools.privileges(pid, socket.uid, function(err, privileges) { postTools.privileges(pid, socket.uid, function(err, privileges) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
privileges.pid = parseInt(pid); privileges.pid = parseInt(pid, 10);
callback(null, privileges); callback(null, privileges);
}); });
}; };
@ -247,9 +232,8 @@ SocketPosts.getFavouritedUsers = function(socket, pid, callback) {
return callback(err); return callback(err);
} }
finalText = usernames.join(', ') + (rest_amount > 0 finalText = usernames.join(', ') + (rest_amount > 0 ?
? " and " + rest_amount + (rest_amount > 1 ? " others" : " other") (" and " + rest_amount + (rest_amount > 1 ? " others" : " other")) : "");
: "");
callback(null, finalText); callback(null, finalText);
}); });
}); });
@ -323,6 +307,7 @@ SocketPosts.loadMoreUserPosts = function(socket, data, callback) {
posts.getPostsByUid(socket.uid, data.uid, start, end, callback); posts.getPostsByUid(socket.uid, data.uid, start, end, callback);
}; };
SocketPosts.getRecentPosts = function(socket, term, callback) { SocketPosts.getRecentPosts = function(socket, term, callback) {
posts.getRecentPosts(socket.uid, 0, 19, term, callback); posts.getRecentPosts(socket.uid, 0, 19, term, callback);
}; };

Loading…
Cancel
Save