From a4de1b247fbaccf87a4dd25612c76c3c542b803a Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Fri, 19 Jul 2013 10:59:24 -0400 Subject: [PATCH] decrease/increase post count on post deletion/restore --- src/postTools.js | 23 +++++++++++++++-------- src/posts.js | 3 ++- src/user.js | 4 ++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/postTools.js b/src/postTools.js index 7759f7ef60..736613cc35 100644 --- a/src/postTools.js +++ b/src/postTools.js @@ -83,21 +83,25 @@ marked.setOptions({ PostTools.delete = function(uid, pid) { var success = function() { posts.setPostField(pid, 'deleted', 1); + - posts.getPostField(pid, 'tid', function(tid) { - io.sockets.in('topic_' + tid).emit('event:post_deleted', { + posts.getPostFields(pid, ['tid', 'uid'], function(postData) { + + user.decrementUserFieldBypostData.uid, 'postcount', 1); + + io.sockets.in('topic_' + postData.tid).emit('event:post_deleted', { pid: pid }); // Delete the thread if it is the last undeleted post - threadTools.get_latest_undeleted_pid(tid, function(err, pid) { + threadTools.get_latest_undeleted_pid(postData.tid, function(err, pid) { if (err && err.message === 'no-undeleted-pids-found') { - threadTools.delete(tid, -1, function(err) { - if (err) console.log('Error: Could not delete topic (tid: ' + tid + ')'); + threadTools.delete(postData.tid, -1, function(err) { + if (err) console.log('Error: Could not delete topic (tid: ' + postData.tid + ')'); }); } else { posts.getPostField(pid, 'timestamp', function(timestamp) { - topics.updateTimestamp(tid, timestamp); + topics.updateTimestamp(postData.tid, timestamp); }); } }); @@ -115,8 +119,11 @@ marked.setOptions({ var success = function() { posts.setPostField(pid, 'deleted', 0); - posts.getPostField(pid, 'tid', function(tid) { - io.sockets.in('topic_' + tid).emit('event:post_restored', { + posts.getPostFields(pid, ['tid', 'uid', function(postData) { + + user.incrementUserFieldBy(postData.uid, 'postcount', 1); + + io.sockets.in('topic_' + postData.tid).emit('event:post_restored', { pid: pid }); }); diff --git a/src/posts.js b/src/posts.js index dd28e6c64e..3c9714b5bc 100644 --- a/src/posts.js +++ b/src/posts.js @@ -82,8 +82,9 @@ marked.setOptions({ Posts.getPostData = function(pid, callback) { RDB.hgetall('post:' + pid, function(err, data) { - if(err === null) + if(err === null) { callback(data); + } else console.log(err); }); diff --git a/src/user.js b/src/user.js index 0e8e84f1fc..e32214f483 100644 --- a/src/user.js +++ b/src/user.js @@ -316,6 +316,10 @@ var utils = require('./../public/src/utils.js'), RDB.hincrby('user:' + uid, field, value); } + User.decrementUserFieldBy = function(uid, field, value) { + RDB.hincrby('user:' + uid, field, -value); + } + User.getUserList = function(callback) { var data = [];