v1.18.x
barisusakli 11 years ago
parent fa221437dc
commit 68fd997efd

@ -317,8 +317,6 @@ var db = require('./database'),
if(parseInt(topicData.pinned, 10) === 0) {
db.sortedSetAdd('categories:' + cid + ':tid', postData.timestamp, postData.tid);
}
Categories.addActiveUser(cid, postData.uid, postData.timestamp);
});
};

@ -1,83 +1,31 @@
'use strict';
var async = require('async'),
db = require('./../database'),
posts = require('./../posts'),
topics = require('./../topics');
module.exports = function(Categories) {
Categories.isUserActiveIn = function(cid, uid, callback) {
db.getSortedSetRange('uid:' + uid + ':posts', 0, -1, function(err, pids) {
if (err) {
return callback(err);
}
var index = 0,
active = false;
Categories.getActiveUsers = function(cid, callback) {
db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, 99, function(err, pids) {
var keys = pids.map(function(pid) {
return 'post:' + pid;
});
async.whilst(
function() {
return active === false && index < pids.length;
},
function(callback) {
posts.getCidByPid(pids[index], function(err, postCid) {
db.getObjectsFields(keys, ['uid'], function(err, users) {
if (err) {
return callback(err);
}
if (postCid === cid) {
active = true;
}
++index;
callback();
});
},
function(err) {
callback(err, active);
}
);
});
};
Categories.addActiveUser = function(cid, uid, timestamp) {
if(parseInt(uid, 10)) {
db.sortedSetAdd('cid:' + cid + ':active_users', timestamp, uid);
}
};
Categories.removeActiveUser = function(cid, uid, callback) {
db.sortedSetRemove('cid:' + cid + ':active_users', uid, callback);
};
Categories.getActiveUsers = function(cid, callback) {
db.getSortedSetRevRange('cid:' + cid + ':active_users', 0, 23, callback);
};
Categories.moveActiveUsers = function(tid, oldCid, cid) {
function updateUser(uid, timestamp) {
Categories.addActiveUser(cid, uid, timestamp);
Categories.isUserActiveIn(oldCid, uid, function(err, active) {
var uids = users.map(function(user) {
return user.uid;
}).filter(function(value, index, array) {
return parseInt(value, 10) !== 0 && array.indexOf(value) === index;
}).slice(0, 24);
if (!err && !active) {
Categories.removeActiveUser(oldCid, uid);
}
callback(null, uids);
});
}
topics.getTopicField(tid, 'timestamp', function(err, timestamp) {
if(!err) {
topics.getUids(tid, function(err, uids) {
if (!err && uids) {
for (var i = 0; i < uids.length; ++i) {
updateUser(uids[i], timestamp);
}
}
});
}
});
};
};

@ -2,6 +2,8 @@
'use strict';
var async = require('async'),
winston = require('winston'),
db = require('./../database'),
posts = require('./../posts'),
topics = require('./../topics'),
@ -23,26 +25,40 @@ module.exports = function(Categories) {
};
Categories.moveRecentReplies = function(tid, oldCid, cid) {
function movePost(pid, next) {
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
if(err) {
return next(err);
function movePost(postData, next) {
async.parallel([
function(next) {
db.sortedSetRemove('categories:recent_posts:cid:' + oldCid, postData.pid, next);
},
function(next) {
db.sortedSetAdd('categories:recent_posts:cid:' + cid, postData.timestamp, postData.pid, next);
}
], next);
}
topics.getPids(tid, function(err, pids) {
if (err) {
return winston.error(err.message);
}
if (pids && !pids.length) {
return;
}
db.sortedSetRemove('categories:recent_posts:cid:' + oldCid, pid);
db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
next();
var keys = pids.map(function(pid) {
return 'post:' + pid;
});
db.getObjectsFields(keys, ['pid', 'timestamp'], function(err, postData) {
if (err) {
return winston.error(err.message);
}
topics.getPids(tid, function(err, pids) {
if(!err && pids) {
async.each(pids, movePost, function(err) {
async.each(postData, movePost, function(err) {
if (err) {
winston.error(err.message);
}
});
}
});
});
};
};

@ -201,8 +201,6 @@ var winston = require('winston'),
categories.incrementCategoryFieldBy(cid, 'topic_count', 1);
}
categories.moveActiveUsers(tid, oldCid, cid);
categories.moveRecentReplies(tid, oldCid, cid);
topics.setTopicField(tid, 'cid', cid, callback);

@ -266,9 +266,6 @@ module.exports = function(User) {
function(next) {
db.delete('uid:' + uid + ':downvote', next);
},
function(next) {
deleteUserFromCategoryActiveUsers(uid, next);
},
function(next) {
deleteUserFromFollowers(uid, next);
},
@ -298,18 +295,6 @@ module.exports = function(User) {
});
}
function deleteUserFromCategoryActiveUsers(uid, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
if (err) {
return callback(err);
}
async.each(cids, function(cid, next) {
categories.removeActiveUser(cid, uid, next);
}, callback);
});
}
function deleteUserFromFollowers(uid, callback) {
db.getSetMembers('followers:' + uid, function(err, uids) {
if (err) {

Loading…
Cancel
Save