categories refactor

v1.18.x
barisusakli 11 years ago
parent 970639274e
commit cc0ac29b36

@ -17,6 +17,9 @@ var db = require('./database'),
(function(Categories) { (function(Categories) {
require('./categories/activeusers')(Categories);
require('./categories/recentreplies')(Categories);
Categories.create = function(data, callback) { Categories.create = function(data, callback) {
db.incrObjectField('global', 'nextCid', function(err, cid) { db.incrObjectField('global', 'nextCid', function(err, cid) {
if (err) { if (err) {
@ -56,7 +59,6 @@ var db = require('./database'),
}; };
Categories.getCategoryById = function(cid, start, end, uid, callback) { Categories.getCategoryById = function(cid, start, end, uid, callback) {
CategoryTools.exists(cid, function(err, exists) { CategoryTools.exists(cid, function(err, exists) {
if(err || !exists) { if(err || !exists) {
return callback(err || new Error('category-not-found [' + cid + ']')); return callback(err || new Error('category-not-found [' + cid + ']'));
@ -226,56 +228,6 @@ var db = require('./database'),
db.isSetMember('cid:' + cid + ':read_by_uid', uid, callback); db.isSetMember('cid:' + cid + ':read_by_uid', uid, callback);
}; };
Categories.getRecentReplies = function(cid, uid, count, callback) {
if(count === 0 || !count) {
return callback(null, []);
}
CategoryTools.privileges(cid, uid, function(err, privileges) {
if(err) {
return callback(err);
}
if (!privileges.read) {
return callback(null, []);
}
db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, count - 1, function(err, pids) {
if (err) {
return callback(err, []);
}
if (!pids || !pids.length) {
return callback(null, []);
}
posts.getPostSummaryByPids(pids, true, callback);
});
});
};
Categories.moveRecentReplies = function(tid, oldCid, cid, callback) {
function movePost(pid, callback) {
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
if(err) {
return callback(err);
}
db.sortedSetRemove('categories:recent_posts:cid:' + oldCid, pid);
db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
callback();
});
}
topics.getPids(tid, function(err, pids) {
if(err) {
return callback(err);
}
async.each(pids, movePost, callback);
});
};
Categories.getCategoryData = function(cid, callback) { Categories.getCategoryData = function(cid, callback) {
Categories.getCategoriesData([cid], function(err, categories) { Categories.getCategoriesData([cid], function(err, categories) {
callback(err, categories ? categories[0] : null); callback(err, categories ? categories[0] : null);
@ -351,79 +303,6 @@ var db = require('./database'),
}); });
}; };
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;
async.whilst(
function() {
return active === false && index < pids.length;
},
function(callback) {
posts.getCidByPid(pids[index], function(err, postCid) {
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) {
db.sortedSetRemove('cid:' + cid + ':active_users', uid);
};
Categories.getActiveUsers = function(cid, callback) {
db.getSortedSetRevRange('cid:' + cid + ':active_users', 0, 23, callback);
};
Categories.moveActiveUsers = function(tid, oldCid, cid, callback) {
function updateUser(uid, timestamp) {
Categories.addActiveUser(cid, uid, timestamp);
Categories.isUserActiveIn(oldCid, uid, function(err, active) {
if (!err && !active) {
Categories.removeActiveUser(oldCid, uid);
}
});
}
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);
}
}
});
}
});
};
Categories.onNewPostMade = function(uid, tid, pid, timestamp) { Categories.onNewPostMade = function(uid, tid, pid, timestamp) {
topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) { topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) {

@ -0,0 +1,83 @@
'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;
async.whilst(
function() {
return active === false && index < pids.length;
},
function(callback) {
posts.getCidByPid(pids[index], function(err, postCid) {
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) {
db.sortedSetRemove('cid:' + cid + ':active_users', uid);
};
Categories.getActiveUsers = function(cid, callback) {
db.getSortedSetRevRange('cid:' + cid + ':active_users', 0, 23, callback);
};
Categories.moveActiveUsers = function(tid, oldCid, cid, callback) {
function updateUser(uid, timestamp) {
Categories.addActiveUser(cid, uid, timestamp);
Categories.isUserActiveIn(oldCid, uid, function(err, active) {
if (!err && !active) {
Categories.removeActiveUser(oldCid, uid);
}
});
}
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);
}
}
});
}
});
};
};

@ -0,0 +1,62 @@
'use strict';
var async = require('async'),
db = require('./../database'),
posts = require('./../posts'),
topics = require('./../topics'),
CategoryTools = require('./../categoryTools');
module.exports = function(Categories) {
Categories.getRecentReplies = function(cid, uid, count, callback) {
if(count === 0 || !count) {
return callback(null, []);
}
CategoryTools.privileges(cid, uid, function(err, privileges) {
if(err) {
return callback(err);
}
if (!privileges.read) {
return callback(null, []);
}
db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, count - 1, function(err, pids) {
if (err) {
return callback(err, []);
}
if (!pids || !pids.length) {
return callback(null, []);
}
posts.getPostSummaryByPids(pids, true, callback);
});
});
};
Categories.moveRecentReplies = function(tid, oldCid, cid, callback) {
function movePost(pid, callback) {
posts.getPostField(pid, 'timestamp', function(err, timestamp) {
if(err) {
return callback(err);
}
db.sortedSetRemove('categories:recent_posts:cid:' + oldCid, pid);
db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
callback();
});
}
topics.getPids(tid, function(err, pids) {
if(err) {
return callback(err);
}
async.each(pids, movePost, callback);
});
};
};
Loading…
Cancel
Save