tons more changes

v1.18.x
Baris Soner Usakli 11 years ago
parent 3775c8e50a
commit 32d5118266

@ -1,5 +1,4 @@
var RDB = require('../redis'), var utils = require('../../public/src/utils'),
utils = require('../../public/src/utils'),
user = require('../user'), user = require('../user'),
groups = require('../groups'); groups = require('../groups');

@ -1,4 +1,4 @@
var RDB = require('./redis.js'), var db = require('./database.js'),
posts = require('./posts.js'), posts = require('./posts.js'),
utils = require('./../public/src/utils.js'), utils = require('./../public/src/utils.js'),
user = require('./user.js'), user = require('./user.js'),
@ -12,13 +12,13 @@ var RDB = require('./redis.js'),
"use strict"; "use strict";
Categories.create = function(data, callback) { Categories.create = function(data, callback) {
RDB.incr('global:next_category_id', function(err, cid) { db.incrObjectField('global', 'nextCid', function(err, cid) {
if (err) { if (err) {
return callback(err, null); return callback(err, null);
} }
var slug = cid + '/' + utils.slugify(data.name); var slug = cid + '/' + utils.slugify(data.name);
RDB.rpush('categories:cid', cid); db.listAppend('categories:cid', cid);
var category = { var category = {
cid: cid, cid: cid,
@ -33,7 +33,7 @@ var RDB = require('./redis.js'),
order: data.order order: data.order
}; };
RDB.hmset('category:' + cid, category, function(err, data) { db.setObject('category:' + cid, category, function(err, data) {
callback(err, category); callback(err, category);
}); });
}); });
@ -134,15 +134,15 @@ var RDB = require('./redis.js'),
}; };
Categories.getTopicIds = function(cid, start, stop, callback) { Categories.getTopicIds = function(cid, start, stop, callback) {
RDB.zrevrange('categories:' + cid + ':tid', start, stop, callback); db.getSortedSetRevRange('categories:' + cid + ':tid', start, stop, callback);
}; };
Categories.getActiveUsers = function(cid, callback) { Categories.getActiveUsers = function(cid, callback) {
RDB.smembers('cid:' + cid + ':active_users', callback); db.getSetMembers('cid:' + cid + ':active_users', callback);
}; };
Categories.getAllCategories = function(current_user, callback) { Categories.getAllCategories = function(current_user, callback) {
RDB.lrange('categories:cid', 0, -1, function(err, cids) { db.getListRange('categories:cid', 0, -1, function(err, cids) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
@ -155,7 +155,7 @@ var RDB = require('./redis.js'),
}; };
Categories.getModerators = function(cid, callback) { Categories.getModerators = function(cid, callback) {
RDB.smembers('cid:' + cid + ':moderators', function(err, mods) { db.getSetMembers('cid:' + cid + ':moderators', function(err, mods) {
if (!err) { if (!err) {
if (mods && mods.length) { if (mods && mods.length) {
user.getMultipleUserFields(mods, ['username'], function(err, moderators) { user.getMultipleUserFields(mods, ['username'], function(err, moderators) {
@ -172,7 +172,7 @@ var RDB = require('./redis.js'),
}; };
Categories.isTopicsRead = function(cid, uid, callback) { Categories.isTopicsRead = function(cid, uid, callback) {
RDB.zrange('categories:' + cid + ':tid', 0, -1, function(err, tids) { db.getSortedSetRange('categories:' + cid + ':tid', 0, -1, function(err, tids) {
topics.hasReadTopics(tids, uid, function(hasRead) { topics.hasReadTopics(tids, uid, function(hasRead) {
@ -189,7 +189,7 @@ var RDB = require('./redis.js'),
}; };
Categories.markAsRead = function(cid, uid) { Categories.markAsRead = function(cid, uid) {
RDB.sadd('cid:' + cid + ':read_by_uid', uid); db.setAdd('cid:' + cid + ':read_by_uid', uid);
}; };
Categories.hasReadCategories = function(cids, uid, callback) { Categories.hasReadCategories = function(cids, uid, callback) {
@ -205,15 +205,17 @@ var RDB = require('./redis.js'),
}; };
Categories.hasReadCategory = function(cid, uid, callback) { Categories.hasReadCategory = function(cid, uid, callback) {
RDB.sismember('cid:' + cid + ':read_by_uid', uid, function(err, hasRead) { db.isSetMember('cid:' + cid + ':read_by_uid', uid, function(err, hasRead) {
RDB.handle(err); if(err) {
return callback(false);
}
callback(hasRead); callback(hasRead);
}); });
}; };
Categories.getRecentReplies = function(cid, count, callback) { Categories.getRecentReplies = function(cid, count, callback) {
RDB.zrevrange('categories:recent_posts:cid:' + cid, 0, (count < 10) ? 10 : count, function(err, pids) { db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, (count < 10) ? 10 : count, function(err, pids) {
if (err) { if (err) {
winston.err(err); winston.err(err);
@ -242,8 +244,8 @@ var RDB = require('./redis.js'),
return callback(err); return callback(err);
} }
RDB.zrem('categories:recent_posts:cid:' + oldCid, pid); db.sortedSetRemove('categories:recent_posts:cid:' + oldCid, pid);
RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid); db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
callback(null); callback(null);
}); });
} }
@ -283,9 +285,9 @@ var RDB = require('./redis.js'),
}; };
Categories.getCategoryData = function(cid, callback) { Categories.getCategoryData = function(cid, callback) {
RDB.exists('category:' + cid, function(err, exists) { db.exists('category:' + cid, function(err, exists) {
if (exists) { if (exists) {
RDB.hgetall('category:' + cid, callback); db.getObject('category:' + cid, callback);
} else { } else {
callback(new Error('No category found!')); callback(new Error('No category found!'));
} }
@ -293,19 +295,19 @@ var RDB = require('./redis.js'),
}; };
Categories.getCategoryField = function(cid, field, callback) { Categories.getCategoryField = function(cid, field, callback) {
RDB.hget('category:' + cid, field, callback); db.getObjectField('category:' + cid, field, callback);
}; };
Categories.getCategoryFields = function(cid, fields, callback) { Categories.getCategoryFields = function(cid, fields, callback) {
RDB.hmgetObject('category:' + cid, fields, callback); db.getObjectFields('category:' + cid, fields, callback);
}; };
Categories.setCategoryField = function(cid, field, value, callback) { Categories.setCategoryField = function(cid, field, value, callback) {
RDB.hset('category:' + cid, field, value, callback); db.setObjectField('category:' + cid, field, value, callback);
}; };
Categories.incrementCategoryFieldBy = function(cid, field, value, callback) { Categories.incrementCategoryFieldBy = function(cid, field, value, callback) {
RDB.hincrby('category:' + cid, field, value, callback); db.incrObjectFieldBy('category:' + cid, field, value, callback);
}; };
Categories.getCategories = function(cids, uid, callback) { Categories.getCategories = function(cids, uid, callback) {
@ -349,7 +351,7 @@ var RDB = require('./redis.js'),
Categories.isUserActiveIn = function(cid, uid, callback) { Categories.isUserActiveIn = function(cid, uid, callback) {
RDB.lrange('uid:' + uid + ':posts', 0, -1, function(err, pids) { db.getListRange('uid:' + uid + ':posts', 0, -1, function(err, pids) {
if (err) { if (err) {
return callback(err, null); return callback(err, null);
} }
@ -387,12 +389,13 @@ var RDB = require('./redis.js'),
}; };
Categories.addActiveUser = function(cid, uid) { Categories.addActiveUser = function(cid, uid) {
if(parseInt(uid, 10)) if(parseInt(uid, 10)) {
RDB.sadd('cid:' + cid + ':active_users', uid); db.setAdd('cid:' + cid + ':active_users', uid);
}
}; };
Categories.removeActiveUser = function(cid, uid) { Categories.removeActiveUser = function(cid, uid) {
RDB.srem('cid:' + cid + ':active_users', uid); db.setRemove('cid:' + cid + ':active_users', uid);
}; };
}(exports)); }(exports));

@ -17,6 +17,9 @@
redisClient = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host')); redisClient = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
} }
module.client = redisClient;
module.type = 'redis';
if (nconf.get('redis:password')) { if (nconf.get('redis:password')) {
redisClient.auth(nconf.get('redis:password')); redisClient.auth(nconf.get('redis:password'));
} }
@ -68,6 +71,38 @@
}); });
} }
module.info = function(callback) {
redisClient.info(function (err, data) {
if(err) {
return callback(err);
}
data = data.split("\r\n");
var finalData = {};
for (var i in data) {
if (data[i].indexOf(':') == -1 || !data[i])
continue;
try {
data[i] = data[i].replace(/:/, "\":\"");
var json = "{\"" + data[i] + "\"}";
var jsonObject = JSON.parse(json);
for (var key in jsonObject) {
finalData[key] = jsonObject[key];
}
} catch (err) {
winston.warn('can\'t parse redis status variable, ignoring', i, data[i], err);
}
}
callback(null, finalData);
});
}
// key // key
module.exists = function(key, callback) { module.exists = function(key, callback) {
@ -80,6 +115,14 @@
redisClient.del(key, callback); redisClient.del(key, callback);
} }
module.get = function(key, callback) {
redisClient.get(key, callback);
}
module.set = function(key, callback) {
redisClient.get(key, callback);
}
//hashes //hashes
module.setObject = function(key, data, callback) { module.setObject = function(key, data, callback) {
@ -91,7 +134,7 @@
} }
module.getObject = function(key, callback) { module.getObject = function(key, callback) {
redisClient.hgetall(key, callback) redisClient.hgetall(key, callback);
} }
module.getObjectField = function(key, field, callback) { module.getObjectField = function(key, field, callback) {
@ -138,6 +181,10 @@
redisClient.hincrby(key, field, 1, callback); redisClient.hincrby(key, field, 1, callback);
} }
module.decrObjectField = function(key, field, callback) {
redisClient.hincrby(key, field, -1, callback);
}
module.incrObjectFieldBy = function(key, field, value, callback) { module.incrObjectFieldBy = function(key, field, value, callback) {
redisClient.hincrby(key, field, value, callback); redisClient.hincrby(key, field, value, callback);
} }
@ -157,6 +204,16 @@
redisClient.sismember(key, value, callback); redisClient.sismember(key, value, callback);
} }
module.isMemberOfSets = function(sets, value, callback) {
var batch = redisClient.multi();
for (var i = 0, ii = sets.length; i < ii; i++) {
batch.sismember(sets[i], value);
}
batch.exec(callback);
}
module.getSetMembers = function(key, callback) { module.getSetMembers = function(key, callback) {
redisClient.smembers(key, callback); redisClient.smembers(key, callback);
} }
@ -165,6 +222,10 @@
redisClient.scard(key, callback); redisClient.scard(key, callback);
} }
module.setRemoveRandom = function(key, callback) {
redisClient.spop(key, callback);
}
// sorted sets // sorted sets
module.sortedSetAdd = function(key, score, value, callback) { module.sortedSetAdd = function(key, score, value, callback) {
@ -176,11 +237,15 @@
} }
module.getSortedSetRange = function(key, start, stop, callback) { module.getSortedSetRange = function(key, start, stop, callback) {
redisClient.zrange(set, start, stop, callback); redisClient.zrange(key, start, stop, callback);
} }
module.getSortedSetRevRange = function(key, start, stop, callback) { module.getSortedSetRevRange = function(key, start, stop, callback) {
redisClient.zrevrange(set, start, stop, callback); redisClient.zrevrange(key, start, stop, callback);
}
module.getSortedSetRevRangeByScore = function(args, callback) {
redisClient.zrevrangebyscore(args, callback);
} }
module.sortedSetCount = function(key, min, max, callback) { module.sortedSetCount = function(key, min, max, callback) {
@ -200,5 +265,7 @@
redisClient.lrange(key, start, stop, callback); redisClient.lrange(key, start, stop, callback);
} }
}(exports)); }(exports));

@ -1,4 +1,4 @@
var RDB = require('./redis'), var db = require('./database'),
posts = require('./posts'), posts = require('./posts'),
user = require('./user'), user = require('./user'),
websockets = require('./websockets') websockets = require('./websockets')
@ -26,14 +26,14 @@ var RDB = require('./redis'),
Favourites.hasFavourited(pid, uid, function (hasFavourited) { Favourites.hasFavourited(pid, uid, function (hasFavourited) {
if (hasFavourited === 0) { if (hasFavourited === 0) {
RDB.sadd('pid:' + pid + ':users_favourited', uid); db.setAdd('pid:' + pid + ':users_favourited', uid);
RDB.zadd('uid:' + uid + ':favourites', postData.timestamp, pid); db.sortedSetAdd('uid:' + uid + ':favourites', postData.timestamp, pid);
RDB.hincrby('post:' + pid, 'reputation', 1); db.incrObjectFieldBy('post:' + pid, 'reputation', 1);
if (uid !== postData.uid) { if (uid !== postData.uid) {
user.incrementUserFieldBy(postData.uid, 'reputation', 1, function (err, newreputation) { user.incrementUserFieldBy(postData.uid, 'reputation', 1, function (err, newreputation) {
RDB.zadd('users:reputation', newreputation, postData.uid); db.sortedSetAdd('users:reputation', newreputation, postData.uid);
}); });
} }
@ -61,14 +61,14 @@ var RDB = require('./redis'),
posts.getPostField(pid, 'uid', function (err, uid_of_poster) { posts.getPostField(pid, 'uid', function (err, uid_of_poster) {
Favourites.hasFavourited(pid, uid, function (hasFavourited) { Favourites.hasFavourited(pid, uid, function (hasFavourited) {
if (hasFavourited === 1) { if (hasFavourited === 1) {
RDB.srem('pid:' + pid + ':users_favourited', uid); db.setRemove('pid:' + pid + ':users_favourited', uid);
RDB.zrem('uid:' + uid + ':favourites', pid); db.sortedSetRemove('uid:' + uid + ':favourites', pid);
RDB.hincrby('post:' + pid, 'reputation', -1); db.incrObjectFieldBy('post:' + pid, 'reputation', -1);
if (uid !== uid_of_poster) { if (uid !== uid_of_poster) {
user.incrementUserFieldBy(uid_of_poster, 'reputation', -1, function (err, newreputation) { user.incrementUserFieldBy(uid_of_poster, 'reputation', -1, function (err, newreputation) {
RDB.zadd('users:reputation', newreputation, uid_of_poster); db.sortedSetAdd('users:reputation', newreputation, uid_of_poster);
}); });
} }
@ -89,8 +89,7 @@ var RDB = require('./redis'),
}; };
Favourites.hasFavourited = function (pid, uid, callback) { Favourites.hasFavourited = function (pid, uid, callback) {
RDB.sismember('pid:' + pid + ':users_favourited', uid, function (err, hasFavourited) { db.isSetMember('pid:' + pid + ':users_favourited', uid, function (err, hasFavourited) {
RDB.handle(err);
callback(hasFavourited); callback(hasFavourited);
}); });

@ -1,7 +1,7 @@
(function (Feed) { (function (Feed) {
var RDB = require('./redis.js'), var db = require('./database'),
posts = require('./posts.js'), posts = require('./posts'),
topics = require('./topics.js'), topics = require('./topics'),
categories = require('./categories'), categories = require('./categories'),
fs = require('fs'), fs = require('fs'),

@ -1,9 +1,9 @@
var user = require('./user.js'), var user = require('./user'),
bcrypt = require('bcrypt'), bcrypt = require('bcrypt'),
RDB = require('./redis.js'), db = require('./database'),
path = require('path'), path = require('path'),
winston = require('winston'), winston = require('winston'),
utils = require('./../public/src/utils.js'); utils = require('./../public/src/utils');
(function(Login) { (function(Login) {
@ -77,7 +77,7 @@ var user = require('./user.js'),
// Save twitter-specific information to the user // Save twitter-specific information to the user
user.setUserField(uid, 'twid', twid); user.setUserField(uid, 'twid', twid);
RDB.hset('twid:uid', twid, uid); db.setObjectField('twid:uid', twid, uid);
// Save their photo, if present // Save their photo, if present
if (photos && photos.length > 0) { if (photos && photos.length > 0) {
@ -111,7 +111,7 @@ var user = require('./user.js'),
var success = function(uid) { var success = function(uid) {
// Save google-specific information to the user // Save google-specific information to the user
user.setUserField(uid, 'gplusid', gplusid); user.setUserField(uid, 'gplusid', gplusid);
RDB.hset('gplusid:uid', gplusid, uid); db.setObjectField('gplusid:uid', gplusid, uid);
callback(null, { callback(null, {
uid: uid uid: uid
}); });
@ -154,7 +154,7 @@ var user = require('./user.js'),
var success = function(uid) { var success = function(uid) {
// Save facebook-specific information to the user // Save facebook-specific information to the user
user.setUserField(uid, 'fbid', fbid); user.setUserField(uid, 'fbid', fbid);
RDB.hset('fbid:uid', fbid, uid); db.setObjectField('fbid:uid', fbid, uid);
callback(null, { callback(null, {
uid: uid uid: uid
}); });

@ -1,4 +1,4 @@
var RDB = require('./redis'), var db = require('./database'),
async = require('async'), async = require('async'),
user = require('./user'); user = require('./user');
@ -14,9 +14,10 @@ var RDB = require('./redis'),
Messaging.addMessage = function(fromuid, touid, content, callback) { Messaging.addMessage = function(fromuid, touid, content, callback) {
var uids = sortUids(fromuid, touid); var uids = sortUids(fromuid, touid);
RDB.incr('global:next_message_id', function(err, mid) { db.incrObjectField('global', 'nextMid', function(err, mid) {
if (err) if (err) {
return callback(err, null); return callback(err, null);
}
var message = { var message = {
content: content, content: content,
@ -25,8 +26,8 @@ var RDB = require('./redis'),
touid: touid touid: touid
}; };
RDB.hmset('message:' + mid, message); db.setObject('message:' + mid, message);
RDB.rpush('messages:' + uids[0] + ':' + uids[1], mid); db.listAppend('messages:' + uids[0] + ':' + uids[1], mid);
callback(null, message); callback(null, message);
}); });
@ -35,9 +36,10 @@ var RDB = require('./redis'),
Messaging.getMessages = function(fromuid, touid, callback) { Messaging.getMessages = function(fromuid, touid, callback) {
var uids = sortUids(fromuid, touid); var uids = sortUids(fromuid, touid);
RDB.lrange('messages:' + uids[0] + ':' + uids[1], 0, -1, function(err, mids) { db.getListRange('messages:' + uids[0] + ':' + uids[1], 0, -1, function(err, mids) {
if (err) if (err) {
return callback(err, null); return callback(err, null);
}
if (!mids || !mids.length) { if (!mids || !mids.length) {
return callback(null, []); return callback(null, []);
@ -49,14 +51,16 @@ var RDB = require('./redis'),
var messages = []; var messages = [];
function getMessage(mid, next) { function getMessage(mid, next) {
RDB.hgetall('message:' + mid, function(err, message) { db.getObject('message:' + mid, function(err, message) {
if (err) if (err) {
return next(err); return next(err);
}
if (message.fromuid === fromuid) if (message.fromuid === fromuid) {
message.content = 'You : ' + message.content; message.content = 'You : ' + message.content;
else } else {
message.content = tousername + ' : ' + message.content; message.content = tousername + ' : ' + message.content;
}
messages.push(message); messages.push(message);
next(null); next(null);
@ -64,8 +68,9 @@ var RDB = require('./redis'),
} }
async.eachSeries(mids, getMessage, function(err) { async.eachSeries(mids, getMessage, function(err) {
if (err) if (err) {
return callback(err, null); return callback(err, null);
}
callback(null, messages); callback(null, messages);
}); });

@ -1,9 +1,9 @@
var RDB = require('./redis'), var async = require('async'),
async = require('async'),
utils = require('../public/src/utils'),
winston = require('winston'), winston = require('winston'),
cron = require('cron').CronJob, cron = require('cron').CronJob,
db = require('./database'),
utils = require('../public/src/utils'),
websockets = require('./websockets'); websockets = require('./websockets');
@ -49,9 +49,9 @@ var RDB = require('./redis'),
* (un)read list contains the same uniqueId, it will be removed, and * (un)read list contains the same uniqueId, it will be removed, and
* the new one put in its place. * the new one put in its place.
*/ */
RDB.incr('notifications:next_nid', function(err, nid) { db.incrObjectField('global', 'nextNid', function(err, nid) {
RDB.sadd('notifications', nid); db.setAdd('notifications', nid);
RDB.hmset('notifications:' + nid, { db.setObject('notifications:' + nid, {
text: text || '', text: text || '',
path: path || null, path: path || null,
datetime: Date.now(), datetime: Date.now(),
@ -91,7 +91,7 @@ var RDB = require('./redis'),
if (parseInt(uids[x], 10) > 0) { if (parseInt(uids[x], 10) > 0) {
(function(uid) { (function(uid) {
remove_by_uniqueId(notif_data.uniqueId, uid, function() { remove_by_uniqueId(notif_data.uniqueId, uid, function() {
RDB.zadd('uid:' + uid + ':notifications:unread', notif_data.datetime, nid); db.sortedSetAdd('uid:' + uid + ':notifications:unread', notif_data.datetime, nid);
websockets.in('uid_' + uid).emit('event:new_notification'); websockets.in('uid_' + uid).emit('event:new_notification');
@ -108,12 +108,12 @@ var RDB = require('./redis'),
function remove_by_uniqueId(uniqueId, uid, callback) { function remove_by_uniqueId(uniqueId, uid, callback) {
async.parallel([ async.parallel([
function(next) { function(next) {
RDB.zrange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) { db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) {
if (nids && nids.length > 0) { if (nids && nids.length > 0) {
async.each(nids, function(nid, next) { async.each(nids, function(nid, next) {
Notifications.get(nid, uid, function(nid_info) { Notifications.get(nid, uid, function(nid_info) {
if (nid_info.uniqueId === uniqueId) { if (nid_info.uniqueId === uniqueId) {
RDB.zrem('uid:' + uid + ':notifications:unread', nid); db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid);
} }
next(); next();
@ -127,12 +127,12 @@ var RDB = require('./redis'),
}); });
}, },
function(next) { function(next) {
RDB.zrange('uid:' + uid + ':notifications:read', 0, -1, function(err, nids) { db.getSortedSetRange('uid:' + uid + ':notifications:read', 0, -1, function(err, nids) {
if (nids && nids.length > 0) { if (nids && nids.length > 0) {
async.each(nids, function(nid, next) { async.each(nids, function(nid, next) {
Notifications.get(nid, uid, function(nid_info) { Notifications.get(nid, uid, function(nid_info) {
if (nid_info && nid_info.uniqueId === uniqueId) { if (nid_info && nid_info.uniqueId === uniqueId) {
RDB.zrem('uid:' + uid + ':notifications:read', nid); db.sortedSetRemove('uid:' + uid + ':notifications:read', nid);
} }
next(); next();
@ -155,8 +155,8 @@ var RDB = require('./redis'),
Notifications.mark_read = function(nid, uid, callback) { Notifications.mark_read = function(nid, uid, callback) {
if (parseInt(uid) > 0) { if (parseInt(uid) > 0) {
Notifications.get(nid, uid, function(notif_data) { Notifications.get(nid, uid, function(notif_data) {
RDB.zrem('uid:' + uid + ':notifications:unread', nid); db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid);
RDB.zadd('uid:' + uid + ':notifications:read', notif_data.datetime, nid); db.sortedSetAdd('uid:' + uid + ':notifications:read', notif_data.datetime, nid);
if (callback) { if (callback) {
callback(); callback();
} }
@ -183,7 +183,7 @@ var RDB = require('./redis'),
}; };
Notifications.mark_all_read = function(uid, callback) { Notifications.mark_all_read = function(uid, callback) {
RDB.zrange('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) { db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
@ -217,9 +217,9 @@ var RDB = require('./redis'),
RDB.keys('uid:*:notifications:unread', next); RDB.keys('uid:*:notifications:unread', next);
}, },
"nids": function(next) { "nids": function(next) {
RDB.smembers('notifications', function(err, nids) { db.getSetMembers('notifications', function(err, nids) {
async.filter(nids, function(nid, next) { async.filter(nids, function(nid, next) {
RDB.hget('notifications:' + nid, 'datetime', function(err, datetime) { db.getObjectField('notifications:' + nid, 'datetime', function(err, datetime) {
if (parseInt(datetime, 10) < cutoffTime) { if (parseInt(datetime, 10) < cutoffTime) {
next(true); next(true);
} else { } else {

@ -1,4 +1,4 @@
var RDB = require('./redis'), var db = require('./database'),
posts = require('./posts'), posts = require('./posts'),
topics = require('./topics'), topics = require('./topics'),
threadTools = require('./threadTools'), threadTools = require('./threadTools'),
@ -19,7 +19,7 @@ var RDB = require('./redis'),
(function(PostTools) { (function(PostTools) {
PostTools.isMain = function(pid, tid, callback) { PostTools.isMain = function(pid, tid, callback) {
RDB.lrange('tid:' + tid + ':posts', 0, 0, function(err, pids) { db.getListRange('tid:' + tid + ':posts', 0, 0, function(err, pids) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
@ -131,14 +131,14 @@ var RDB = require('./redis'),
PostTools.delete = function(uid, pid, callback) { PostTools.delete = function(uid, pid, callback) {
var success = function() { var success = function() {
posts.setPostField(pid, 'deleted', 1); posts.setPostField(pid, 'deleted', 1);
RDB.decr('totalpostcount'); db.decrObjectField('global', 'postCount');
postSearch.remove(pid); postSearch.remove(pid);
posts.getPostFields(pid, ['tid', 'uid'], function(err, postData) { posts.getPostFields(pid, ['tid', 'uid'], function(err, postData) {
RDB.hincrby('topic:' + postData.tid, 'postcount', -1); db.incrObjectFieldBy('topic:' + postData.tid, 'postcount', -1);
user.decrementUserFieldBy(postData.uid, 'postcount', 1, function(err, postcount) { user.decrementUserFieldBy(postData.uid, 'postcount', 1, function(err, postcount) {
RDB.zadd('users:postcount', postcount, postData.uid); db.sortedSetAdd('users:postcount', postcount, postData.uid);
}); });
// Delete the thread if it is the last undeleted post // Delete the thread if it is the last undeleted post
@ -180,10 +180,10 @@ var RDB = require('./redis'),
PostTools.restore = function(uid, pid, callback) { PostTools.restore = function(uid, pid, callback) {
var success = function() { var success = function() {
posts.setPostField(pid, 'deleted', 0); posts.setPostField(pid, 'deleted', 0);
RDB.incr('totalpostcount'); db.incrObjectField('global', 'postCount');
posts.getPostFields(pid, ['tid', 'uid', 'content'], function(err, postData) { posts.getPostFields(pid, ['tid', 'uid', 'content'], function(err, postData) {
RDB.hincrby('topic:' + postData.tid, 'postcount', 1); db.incrObjectFieldBy('topic:' + postData.tid, 'postcount', 1);
user.incrementUserFieldBy(postData.uid, 'postcount', 1); user.incrementUserFieldBy(postData.uid, 'postcount', 1);

@ -1,4 +1,4 @@
var RDB = require('./redis'), var db = require('./database'),
utils = require('./../public/src/utils'), utils = require('./../public/src/utils'),
user = require('./user'), user = require('./user'),
topics = require('./topics'), topics = require('./topics'),
@ -34,7 +34,7 @@ var RDB = require('./redis'),
callback(new Error('topic-locked'), null); callback(new Error('topic-locked'), null);
} }
RDB.incr('global:next_post_id', function(err, pid) { db.incrObjectField('global', 'nextPid', function(err, pid) {
if(err) { if(err) {
return callback(err, null); return callback(err, null);
} }
@ -57,7 +57,7 @@ var RDB = require('./redis'),
'deleted': 0 'deleted': 0
}; };
RDB.hmset('post:' + pid, postData); db.setObject('post:' + pid, postData);
postData.favourited = false; postData.favourited = false;
postData.display_moderator_tools = true; postData.display_moderator_tools = true;
@ -67,26 +67,24 @@ var RDB = require('./redis'),
topics.increasePostCount(tid); topics.increasePostCount(tid);
topics.updateTimestamp(tid, timestamp); topics.updateTimestamp(tid, timestamp);
RDB.incr('totalpostcount'); db.incrObjectField('global', 'postCount');
topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) { topics.getTopicFields(tid, ['cid', 'pinned'], function(err, topicData) {
RDB.handle(err);
var cid = topicData.cid; var cid = topicData.cid;
feed.updateTopic(tid); feed.updateTopic(tid);
feed.updateRecent(); feed.updateRecent();
RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid); db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
if(topicData.pinned === '0') { if(topicData.pinned === '0') {
RDB.zadd('categories:' + cid + ':tid', timestamp, tid); db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid);
} }
RDB.scard('cid:' + cid + ':active_users', function(err, amount) { db.setCount('cid:' + cid + ':active_users', function(err, amount) {
if (amount > 15) { if (amount > 15) {
RDB.spop('cid:' + cid + ':active_users'); db.setRemoveRandom('cid:' + cid + ':active_users');
} }
categories.addActiveUser(cid, uid); categories.addActiveUser(cid, uid);
@ -155,7 +153,7 @@ var RDB = require('./redis'),
return next(err); return next(err);
} }
RDB.del('cid:' + cid + ':read_by_uid'); db.delete('cid:' + cid + ':read_by_uid');
next(); next();
}); });
}, },
@ -182,8 +180,10 @@ var RDB = require('./redis'),
} }
Posts.getPostsByTid = function(tid, start, end, callback) { Posts.getPostsByTid = function(tid, start, end, callback) {
RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) { db.getListRange('tid:' + tid + ':posts', start, end, function(err, pids) {
RDB.handle(err); if(err) {
return callback(err);
}
if (pids.length) { if (pids.length) {
plugins.fireHook('filter:post.getTopic', pids, function(err, posts) { plugins.fireHook('filter:post.getTopic', pids, function(err, posts) {
@ -310,7 +310,7 @@ var RDB = require('./redis'),
}; };
Posts.getPostData = function(pid, callback) { Posts.getPostData = function(pid, callback) {
RDB.hgetall('post:' + pid, function(err, data) { db.getObject('post:' + pid, function(err, data) {
if(err) { if(err) {
return callback(err, null); return callback(err, null);
} }
@ -325,7 +325,7 @@ var RDB = require('./redis'),
} }
Posts.getPostFields = function(pid, fields, callback) { Posts.getPostFields = function(pid, fields, callback) {
RDB.hmgetObject('post:' + pid, fields, function(err, data) { db.getObjectFields('post:' + pid, fields, function(err, data) {
if(err) { if(err) {
return callback(err, null); return callback(err, null);
} }
@ -355,7 +355,7 @@ var RDB = require('./redis'),
} }
Posts.setPostField = function(pid, field, value, callback) { Posts.setPostField = function(pid, field, value, callback) {
RDB.hset('post:' + pid, field, value, callback); db.setObjectField('post:' + pid, field, value, callback);
plugins.fireHook('action:post.setField', { plugins.fireHook('action:post.setField', {
'pid': pid, 'pid': pid,
'field': field, 'field': field,
@ -499,7 +499,7 @@ var RDB = require('./redis'),
} }
Posts.getFavourites = function(uid, callback) { Posts.getFavourites = function(uid, callback) {
RDB.zrevrange('uid:' + uid + ':favourites', 0, -1, function(err, pids) { db.getSortedSetRevRange('uid:' + uid + ':favourites', 0, -1, function(err, pids) {
if (err) if (err)
return callback(err, null); return callback(err, null);

@ -3,7 +3,7 @@ var nconf = require('nconf'),
path = require('path'), path = require('path'),
winston = require('winston'), winston = require('winston'),
RDB = require('./../redis'), db = require('./../database'),
user = require('./../user'), user = require('./../user'),
groups = require('../groups'), groups = require('../groups'),
topics = require('./../topics'), topics = require('./../topics'),
@ -267,29 +267,8 @@ var nconf = require('nconf'),
app.namespace('/redis', function () { app.namespace('/redis', function () {
app.get('/', function (req, res) { app.get('/', function (req, res) {
RDB.info(function (err, data) { db.info(function (err, data) {
data = data.split("\r\n"); res.json(data);
var finalData = {};
for (var i in data) {
if (data[i].indexOf(':') == -1 || !data[i])
continue;
try {
data[i] = data[i].replace(/:/, "\":\"");
var json = "{\"" + data[i] + "\"}";
var jsonObject = JSON.parse(json);
for (var key in jsonObject) {
finalData[key] = jsonObject[key];
}
} catch (err) {
winston.warn('can\'t parse redis status variable, ignoring', i, data[i], err);
}
}
res.json(finalData);
}); });
}); });

@ -9,7 +9,7 @@ var fs = require('fs'),
postTools = require('../postTools'), postTools = require('../postTools'),
utils = require('./../../public/src/utils'), utils = require('./../../public/src/utils'),
meta = require('./../meta'), meta = require('./../meta'),
RDB = require('./../redis'), db = require('./../database'),
websockets = require('./../websockets'); websockets = require('./../websockets');
(function (User) { (function (User) {
@ -454,7 +454,7 @@ var fs = require('fs'),
if(websockets.isUserOnline(user.uid)) { if(websockets.isUserOnline(user.uid)) {
onlineUsers.push(user); onlineUsers.push(user);
} else { } else {
RDB.zrem('users:online', user.uid); db.sortedSetRemove('users:online', user.uid);
} }
callback(null); callback(null);
} }

@ -1,4 +1,4 @@
var RDB = require('./redis'), var db = require('./database'),
topics = require('./topics'), topics = require('./topics'),
categories = require('./categories'), categories = require('./categories'),
CategoryTools = require('./categoryTools'), CategoryTools = require('./categoryTools'),
@ -17,8 +17,11 @@ var RDB = require('./redis'),
(function(ThreadTools) { (function(ThreadTools) {
ThreadTools.exists = function(tid, callback) { ThreadTools.exists = function(tid, callback) {
RDB.sismember('topics:tid', tid, function(err, ismember) { db.isSetMember('topics:tid', tid, function(err, ismember) {
if (err) RDB.handle(err); if (err) {
callback(false);
}
callback( !! ismember || false); callback( !! ismember || false);
}); });
} }
@ -85,7 +88,7 @@ var RDB = require('./redis'),
ThreadTools.delete = function(tid, callback) { ThreadTools.delete = function(tid, callback) {
topics.delete(tid); topics.delete(tid);
RDB.decr('totaltopiccount'); db.decrObjectField('global', 'topicCount');
ThreadTools.lock(tid); ThreadTools.lock(tid);
@ -103,7 +106,7 @@ var RDB = require('./redis'),
ThreadTools.restore = function(tid, socket, callback) { ThreadTools.restore = function(tid, socket, callback) {
topics.restore(tid); topics.restore(tid);
RDB.incr('totaltopiccount'); db.incrObjectField('global', 'topicCount');
ThreadTools.unlock(tid); ThreadTools.unlock(tid);
websockets.in('topic_' + tid).emit('event:topic_restored', { websockets.in('topic_' + tid).emit('event:topic_restored', {
@ -123,7 +126,7 @@ var RDB = require('./redis'),
ThreadTools.pin = function(tid, socket) { ThreadTools.pin = function(tid, socket) {
topics.setTopicField(tid, 'pinned', 1); topics.setTopicField(tid, 'pinned', 1);
topics.getTopicField(tid, 'cid', function(err, cid) { topics.getTopicField(tid, 'cid', function(err, cid) {
RDB.zadd('categories:' + cid + ':tid', Math.pow(2, 53), tid); db.sortedSetAdd('categories:' + cid + ':tid', Math.pow(2, 53), tid);
}); });
if (socket) { if (socket) {
@ -144,7 +147,7 @@ var RDB = require('./redis'),
ThreadTools.unpin = function(tid, socket) { ThreadTools.unpin = function(tid, socket) {
topics.setTopicField(tid, 'pinned', 0); topics.setTopicField(tid, 'pinned', 0);
topics.getTopicFields(tid, ['cid', 'lastposttime'], function(err, topicData) { topics.getTopicFields(tid, ['cid', 'lastposttime'], function(err, topicData) {
RDB.zadd('categories:' + topicData.cid + ':tid', topicData.lastposttime, tid); db.sortedSetAdd('categories:' + topicData.cid + ':tid', topicData.lastposttime, tid);
}); });
if (socket) { if (socket) {
websockets.in('topic_' + tid).emit('event:topic_unpinned', { websockets.in('topic_' + tid).emit('event:topic_unpinned', {
@ -208,7 +211,7 @@ var RDB = require('./redis'),
} }
ThreadTools.isFollowing = function(tid, current_user, callback) { ThreadTools.isFollowing = function(tid, current_user, callback) {
RDB.sismember('tid:' + tid + ':followers', current_user, function(err, following) { db.isSetMember('tid:' + tid + ':followers', current_user, function(err, following) {
callback(following); callback(following);
}); });
} }
@ -216,7 +219,7 @@ var RDB = require('./redis'),
ThreadTools.toggleFollow = function(tid, current_user, callback) { ThreadTools.toggleFollow = function(tid, current_user, callback) {
ThreadTools.isFollowing(tid, current_user, function(following) { ThreadTools.isFollowing(tid, current_user, function(following) {
if (!following) { if (!following) {
RDB.sadd('tid:' + tid + ':followers', current_user, function(err, success) { db.setAdd('tid:' + tid + ':followers', current_user, function(err, success) {
if (callback) { if (callback) {
if (!err) { if (!err) {
callback({ callback({
@ -229,7 +232,7 @@ var RDB = require('./redis'),
} }
}); });
} else { } else {
RDB.srem('tid:' + tid + ':followers', current_user, function(err, success) { db.setRemove('tid:' + tid + ':followers', current_user, function(err, success) {
if (callback) { if (callback) {
if (!err) { if (!err) {
callback({ callback({
@ -246,7 +249,7 @@ var RDB = require('./redis'),
} }
ThreadTools.getFollowers = function(tid, callback) { ThreadTools.getFollowers = function(tid, callback) {
RDB.smembers('tid:' + tid + ':followers', function(err, followers) { db.getSetMembers('tid:' + tid + ':followers', function(err, followers) {
callback(err, followers.map(function(follower) { callback(err, followers.map(function(follower) {
return parseInt(follower, 10); return parseInt(follower, 10);
})); }));
@ -274,24 +277,33 @@ var RDB = require('./redis'),
}); });
} }
], function(err, results) { ], function(err, results) {
if (!err) notifications.push(results[0], results[1]); if (!err) {
// Otherwise, do nothing notifications.push(results[0], results[1]);
}
}); });
} }
ThreadTools.getLatestUndeletedPid = function(tid, callback) { ThreadTools.getLatestUndeletedPid = function(tid, callback) {
RDB.lrange('tid:' + tid + ':posts', 0, -1, function(err, pids) { db.getListRange('tid:' + tid + ':posts', 0, -1, function(err, pids) {
if (pids.length === 0) return callback(new Error('no-undeleted-pids-found')); if (pids.length === 0) {
return callback(new Error('no-undeleted-pids-found'));
}
pids.reverse(); pids.reverse();
async.detectSeries(pids, function(pid, next) { async.detectSeries(pids, function(pid, next) {
posts.getPostField(pid, 'deleted', function(err, deleted) { posts.getPostField(pid, 'deleted', function(err, deleted) {
if (deleted === '0') next(true); if (deleted === '0') {
else next(false); next(true);
} else {
next(false);
}
}); });
}, function(pid) { }, function(pid) {
if (pid) callback(null, pid); if (pid) {
else callback(new Error('no-undeleted-pids-found')); callback(null, pid);
} else {
callback(new Error('no-undeleted-pids-found'));
}
}); });
}); });
} }

@ -5,7 +5,7 @@ var async = require('async'),
reds = require('reds'), reds = require('reds'),
topicSearch = reds.createSearch('nodebbtopicsearch'), topicSearch = reds.createSearch('nodebbtopicsearch'),
RDB = require('./redis'), db = require('./database'),
posts = require('./posts'), posts = require('./posts'),
utils = require('./../public/src/utils'), utils = require('./../public/src/utils'),
user = require('./user'), user = require('./user'),
@ -58,16 +58,16 @@ var async = require('async'),
return callback(new Error('too-many-posts'), null); return callback(new Error('too-many-posts'), null);
} }
RDB.incr('next_topic_id', function(err, tid) { db.incrObjectField('global', 'nextTid', function(err, tid) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
RDB.sadd('topics:tid', tid); db.setAdd('topics:tid', tid);
var slug = tid + '/' + utils.slugify(title); var slug = tid + '/' + utils.slugify(title);
var timestamp = Date.now(); var timestamp = Date.now();
RDB.hmset('topic:' + tid, { db.setObject('topic:' + tid, {
'tid': tid, 'tid': tid,
'uid': uid, 'uid': uid,
'cid': cid, 'cid': cid,
@ -87,15 +87,15 @@ var async = require('async'),
user.addTopicIdToUser(uid, tid); user.addTopicIdToUser(uid, tid);
// let everyone know that there is an unread topic in this category // let everyone know that there is an unread topic in this category
RDB.del('cid:' + cid + ':read_by_uid', function(err, data) { db.delete('cid:' + cid + ':read_by_uid', function(err, data) {
Topics.markAsRead(tid, uid); Topics.markAsRead(tid, uid);
}); });
// in future it may be possible to add topics to several categories, so leaving the door open here. // in future it may be possible to add topics to several categories, so leaving the door open here.
RDB.zadd('categories:' + cid + ':tid', timestamp, tid); db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid);
RDB.hincrby('category:' + cid, 'topic_count', 1); db.incrObjectField('category:' + cid, 'topic_count', 1);
RDB.incr('totaltopiccount'); db.incrObjectField('global', 'topicCount');
feed.updateCategory(cid); feed.updateCategory(cid);
@ -124,7 +124,7 @@ var async = require('async'),
}; };
Topics.getTopicData = function(tid, callback) { Topics.getTopicData = function(tid, callback) {
RDB.hgetall('topic:' + tid, function(err, data) { db.getObject('topic:' + tid, function(err, data) {
if(err) { if(err) {
return callback(err, null); return callback(err, null);
} }
@ -236,8 +236,7 @@ var async = require('async'),
since = terms[term]; since = terms[term];
var args = ['topics:recent', '+inf', timestamp - since, 'LIMIT', start, end - start + 1]; var args = ['topics:recent', '+inf', timestamp - since, 'LIMIT', start, end - start + 1];
db.getSortedSetRevRangeByScore(args, function(err, tids) {
RDB.zrevrangebyscore(args, function(err, tids) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
@ -272,7 +271,7 @@ var async = require('async'),
return unreadTids.length < 21 && !done; return unreadTids.length < 21 && !done;
}, },
function(callback) { function(callback) {
RDB.zrevrange('topics:recent', start, stop, function(err, tids) { db.getSortedSetRevRange('topics:recent', start, stop, function(err, tids) {
if (err) if (err)
return callback(err); return callback(err);
@ -344,7 +343,7 @@ var async = require('async'),
async.whilst( async.whilst(
continueCondition, continueCondition,
function(callback) { function(callback) {
RDB.zrevrange('topics:recent', start, stop, function(err, tids) { db.getSortedSetRevRange('topics:recent', start, stop, function(err, tids) {
if (err) if (err)
return callback(err); return callback(err);
@ -597,7 +596,7 @@ var async = require('async'),
} }
Topics.getAllTopics = function(limit, after, callback) { Topics.getAllTopics = function(limit, after, callback) {
RDB.smembers('topics:tid', function(err, tids) { db.getSetMembers('topics:tid', function(err, tids) {
if(err) { if(err) {
return callback(err, null); return callback(err, null);
} }
@ -643,7 +642,7 @@ var async = require('async'),
} }
Topics.markAllRead = function(uid, callback) { Topics.markAllRead = function(uid, callback) {
RDB.smembers('topics:tid', function(err, tids) { db.getSetMembers('topics:tid', function(err, tids) {
if (err) { if (err) {
return callback(err, null); return callback(err, null);
} }
@ -667,12 +666,12 @@ var async = require('async'),
} }
Topics.markUnRead = function(tid, callback) { Topics.markUnRead = function(tid, callback) {
RDB.del('tid:' + tid + ':read_by_uid', callback); db.delete('tid:' + tid + ':read_by_uid', callback);
} }
Topics.markAsRead = function(tid, uid) { Topics.markAsRead = function(tid, uid) {
RDB.sadd('tid:' + tid + ':read_by_uid', uid); db.setAdd('tid:' + tid + ':read_by_uid', uid);
Topics.getTopicField(tid, 'cid', function(err, cid) { Topics.getTopicField(tid, 'cid', function(err, cid) {
@ -691,19 +690,19 @@ var async = require('async'),
} }
Topics.hasReadTopics = function(tids, uid, callback) { Topics.hasReadTopics = function(tids, uid, callback) {
var batch = RDB.multi(); var sets = [];
for (var i = 0, ii = tids.length; i < ii; i++) { for (var i = 0, ii = tids.length; i < ii; i++) {
batch.sismember('tid:' + tids[i] + ':read_by_uid', uid); sets.push('tid:' + tids[i] + ':read_by_uid');
} }
batch.exec(function(err, hasRead) { db.isMemberOfSets(sets, uid, function(err, hasRead) {
callback(hasRead); callback(hasRead);
}); });
} }
Topics.hasReadTopic = function(tid, uid, callback) { Topics.hasReadTopic = function(tid, uid, callback) {
RDB.sismember('tid:' + tid + ':read_by_uid', uid, function(err, hasRead) { db.isSetMember('tid:' + tid + ':read_by_uid', uid, function(err, hasRead) {
if (err === null) { if (err === null) {
callback(hasRead); callback(hasRead);
@ -719,7 +718,9 @@ var async = require('async'),
if (Array.isArray(tids)) { if (Array.isArray(tids)) {
async.eachSeries(tids, function(tid, next) { async.eachSeries(tids, function(tid, next) {
Topics.getTeaser(tid, function(err, teaser_info) { Topics.getTeaser(tid, function(err, teaser_info) {
if (err) teaser_info = {}; if (err) {
teaser_info = {};
}
teasers.push(teaser_info); teasers.push(teaser_info);
next(); next();
}); });
@ -783,23 +784,23 @@ var async = require('async'),
} }
Topics.getTopicField = function(tid, field, callback) { Topics.getTopicField = function(tid, field, callback) {
RDB.hget('topic:' + tid, field, callback); db.getObjectField('topic:' + tid, field, callback);
} }
Topics.getTopicFields = function(tid, fields, callback) { Topics.getTopicFields = function(tid, fields, callback) {
RDB.hmgetObject('topic:' + tid, fields, callback); db.getObjectFields('topic:' + tid, fields, callback);
} }
Topics.setTopicField = function(tid, field, value, callback) { Topics.setTopicField = function(tid, field, value, callback) {
RDB.hset('topic:' + tid, field, value, callback); db.setObjectField('topic:' + tid, field, value, callback);
} }
Topics.increasePostCount = function(tid, callback) { Topics.increasePostCount = function(tid, callback) {
RDB.hincrby('topic:' + tid, 'postcount', 1, callback); db.incrObjectField('topic:' + tid, 'postcount', callback);
} }
Topics.increaseViewCount = function(tid, callback) { Topics.increaseViewCount = function(tid, callback) {
RDB.hincrby('topic:' + tid, 'viewcount', 1, callback); db.incrObjectField('topic:' + tid, 'viewcount', callback);
} }
Topics.isLocked = function(tid, callback) { Topics.isLocked = function(tid, callback) {
@ -812,16 +813,16 @@ var async = require('async'),
} }
Topics.updateTimestamp = function(tid, timestamp) { Topics.updateTimestamp = function(tid, timestamp) {
RDB.zadd('topics:recent', timestamp, tid); db.sortedSetAdd('topics:recent', timestamp, tid);
Topics.setTopicField(tid, 'lastposttime', timestamp); Topics.setTopicField(tid, 'lastposttime', timestamp);
} }
Topics.addPostToTopic = function(tid, pid) { Topics.addPostToTopic = function(tid, pid) {
RDB.rpush('tid:' + tid + ':posts', pid); db.listAppend('tid:' + tid + ':posts', pid);
} }
Topics.getPids = function(tid, callback) { Topics.getPids = function(tid, callback) {
RDB.lrange('tid:' + tid + ':posts', 0, -1, callback); db.getListRange('tid:' + tid + ':posts', 0, -1, callback);
} }
Topics.getUids = function(tid, callback) { Topics.getUids = function(tid, callback) {
@ -848,23 +849,23 @@ var async = require('async'),
Topics.delete = function(tid) { Topics.delete = function(tid) {
Topics.setTopicField(tid, 'deleted', 1); Topics.setTopicField(tid, 'deleted', 1);
RDB.zrem('topics:recent', tid); db.sortedSetRemove('topics:recent', tid);
Topics.getTopicField(tid, 'cid', function(err, cid) { Topics.getTopicField(tid, 'cid', function(err, cid) {
feed.updateCategory(cid); feed.updateCategory(cid);
RDB.hincrby('category:' + cid, 'topic_count', -1); db.incrObjectFieldBy('category:' + cid, 'topic_count', -1);
}); });
} }
Topics.restore = function(tid) { Topics.restore = function(tid) {
Topics.setTopicField(tid, 'deleted', 0); Topics.setTopicField(tid, 'deleted', 0);
Topics.getTopicField(tid, 'lastposttime', function(err, lastposttime) { Topics.getTopicField(tid, 'lastposttime', function(err, lastposttime) {
RDB.zadd('topics:recent', lastposttime, tid); db.sortedSetAdd('topics:recent', lastposttime, tid);
}); });
Topics.getTopicField(tid, 'cid', function(err, cid) { Topics.getTopicField(tid, 'cid', function(err, cid) {
feed.updateCategory(cid); feed.updateCategory(cid);
RDB.hincrby('category:' + cid, 'topic_count', 1); db.incrObjectFieldBy('category:' + cid, 'topic_count', 1);
}); });
} }
@ -885,7 +886,7 @@ var async = require('async'),
} }
Topics.reIndexAll = function(callback) { Topics.reIndexAll = function(callback) {
RDB.smembers('topics:tid', function(err, tids) { db.getSetMembers('topics:tid', function(err, tids) {
if (err) { if (err) {
callback(err, null); callback(err, null);
} else { } else {

@ -1,6 +1,11 @@
"use strict"; "use strict";
var RDB = require('./redis.js'), var db = require('./database'),
// TODO: temp until upgrade is figured out with dbal,
// db.client is redisClient for now
RDB = db.client,
async = require('async'), async = require('async'),
winston = require('winston'), winston = require('winston'),
notifications = require('./notifications'), notifications = require('./notifications'),

@ -486,7 +486,7 @@ var bcrypt = require('bcrypt'),
User.unfollow = function(uid, unfollowid, callback) { User.unfollow = function(uid, unfollowid, callback) {
db.setRemove('following:' + uid, unfollowid, function(err, data) { db.setRemove('following:' + uid, unfollowid, function(err, data) {
if (!err) { if (!err) {
RDB.srem('followers:' + unfollowid, uid, function(err, data) { db.setRemove('followers:' + unfollowid, uid, function(err, data) {
callback(data); callback(data);
}); });
} else { } else {

@ -10,9 +10,13 @@ var cookie = require('cookie'),
winston = require('winston'), winston = require('winston'),
RedisStoreLib = require('connect-redis')(express), RedisStoreLib = require('connect-redis')(express),
RDB = require('./redis'), db = require('./database'),
redis = require('redis'),
redisClient = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host')),
RedisStore = new RedisStoreLib({ RedisStore = new RedisStoreLib({
client: RDB, client: redisClient,
ttl: 60 * 60 * 24 * 14 ttl: 60 * 60 * 24 * 14
}), }),
@ -71,8 +75,11 @@ websockets.init = function(io) {
socketCookieParser(hs, {}, function(err) { socketCookieParser(hs, {}, function(err) {
sessionID = socket.handshake.signedCookies["express.sid"]; sessionID = socket.handshake.signedCookies["express.sid"];
RedisStore.get(sessionID, function(err, sessionData) { RedisStore.get(sessionID, function(err, sessionData) {
if (!err && sessionData && sessionData.passport && sessionData.passport.user) uid = users[sessionID] = sessionData.passport.user; if (!err && sessionData && sessionData.passport && sessionData.passport.user) {
else uid = users[sessionID] = 0; uid = users[sessionID] = sessionData.passport.user;
} else {
uid = users[sessionID] = 0;
}
userSockets[uid] = userSockets[uid] || []; userSockets[uid] = userSockets[uid] || [];
userSockets[uid].push(socket); userSockets[uid].push(socket);
@ -89,7 +96,7 @@ websockets.init = function(io) {
if (uid) { if (uid) {
RDB.zadd('users:online', Date.now(), uid, function(err, data) { db.sortedSetAdd('users:online', Date.now(), uid, function(err, data) {
socket.join('uid_' + uid); socket.join('uid_' + uid);
user.getUserField(uid, 'username', function(err, username) { user.getUserField(uid, 'username', function(err, username) {
@ -119,7 +126,7 @@ websockets.init = function(io) {
delete users[sessionID]; delete users[sessionID];
delete userSockets[uid]; delete userSockets[uid];
if (uid) { if (uid) {
RDB.zrem('users:online', uid, function(err, data) { db.sortedSetRemove('users:online', uid, function(err, data) {
}); });
} }
} }
@ -1108,14 +1115,14 @@ websockets.init = function(io) {
function emitTopicPostStats() { function emitTopicPostStats() {
RDB.mget(['totaltopiccount', 'totalpostcount'], function(err, data) { db.getObjectFields('global', ['topicCount', 'postCount'], function(err, data) {
if (err) { if (err) {
return winston.err(err); return winston.err(err);
} }
var stats = { var stats = {
topics: data[0] ? data[0] : 0, topics: data.topicCount ? data.topicCount : 0,
posts: data[1] ? data[1] : 0 posts: data.postCount ? data.postCount : 0
}; };
io.sockets.emit('post.stats', stats); io.sockets.emit('post.stats', stats);
@ -1123,7 +1130,7 @@ websockets.init = function(io) {
} }
websockets.emitUserCount = function() { websockets.emitUserCount = function() {
RDB.get('usercount', function(err, count) { db.getObjectField('global', 'userCount', function(err, count) {
io.sockets.emit('user.count', { io.sockets.emit('user.count', {
count: count count: count
}); });

Loading…
Cancel
Save