tons more changes

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

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

@ -1,4 +1,4 @@
var RDB = require('./redis.js'),
var db = require('./database.js'),
posts = require('./posts.js'),
utils = require('./../public/src/utils.js'),
user = require('./user.js'),
@ -12,13 +12,13 @@ var RDB = require('./redis.js'),
"use strict";
Categories.create = function(data, callback) {
RDB.incr('global:next_category_id', function(err, cid) {
db.incrObjectField('global', 'nextCid', function(err, cid) {
if (err) {
return callback(err, null);
}
var slug = cid + '/' + utils.slugify(data.name);
RDB.rpush('categories:cid', cid);
db.listAppend('categories:cid', cid);
var category = {
cid: cid,
@ -33,7 +33,7 @@ var RDB = require('./redis.js'),
order: data.order
};
RDB.hmset('category:' + cid, category, function(err, data) {
db.setObject('category:' + cid, category, function(err, data) {
callback(err, category);
});
});
@ -134,15 +134,15 @@ var RDB = require('./redis.js'),
};
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) {
RDB.smembers('cid:' + cid + ':active_users', callback);
db.getSetMembers('cid:' + cid + ':active_users', 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) {
return callback(err);
}
@ -155,7 +155,7 @@ var RDB = require('./redis.js'),
};
Categories.getModerators = function(cid, callback) {
RDB.smembers('cid:' + cid + ':moderators', function(err, mods) {
db.getSetMembers('cid:' + cid + ':moderators', function(err, mods) {
if (!err) {
if (mods && mods.length) {
user.getMultipleUserFields(mods, ['username'], function(err, moderators) {
@ -172,7 +172,7 @@ var RDB = require('./redis.js'),
};
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) {
@ -189,7 +189,7 @@ var RDB = require('./redis.js'),
};
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) {
@ -205,15 +205,17 @@ var RDB = require('./redis.js'),
};
Categories.hasReadCategory = function(cid, uid, callback) {
RDB.sismember('cid:' + cid + ':read_by_uid', uid, function(err, hasRead) {
RDB.handle(err);
db.isSetMember('cid:' + cid + ':read_by_uid', uid, function(err, hasRead) {
if(err) {
return callback(false);
}
callback(hasRead);
});
};
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) {
winston.err(err);
@ -242,8 +244,8 @@ var RDB = require('./redis.js'),
return callback(err);
}
RDB.zrem('categories:recent_posts:cid:' + oldCid, pid);
RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid);
db.sortedSetRemove('categories:recent_posts:cid:' + oldCid, pid);
db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid);
callback(null);
});
}
@ -283,9 +285,9 @@ var RDB = require('./redis.js'),
};
Categories.getCategoryData = function(cid, callback) {
RDB.exists('category:' + cid, function(err, exists) {
db.exists('category:' + cid, function(err, exists) {
if (exists) {
RDB.hgetall('category:' + cid, callback);
db.getObject('category:' + cid, callback);
} else {
callback(new Error('No category found!'));
}
@ -293,19 +295,19 @@ var RDB = require('./redis.js'),
};
Categories.getCategoryField = function(cid, field, callback) {
RDB.hget('category:' + cid, field, callback);
db.getObjectField('category:' + cid, field, 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) {
RDB.hset('category:' + cid, field, value, callback);
db.setObjectField('category:' + 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) {
@ -349,7 +351,7 @@ var RDB = require('./redis.js'),
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) {
return callback(err, null);
}
@ -387,12 +389,13 @@ var RDB = require('./redis.js'),
};
Categories.addActiveUser = function(cid, uid) {
if(parseInt(uid, 10))
RDB.sadd('cid:' + cid + ':active_users', uid);
if(parseInt(uid, 10)) {
db.setAdd('cid:' + cid + ':active_users', uid);
}
};
Categories.removeActiveUser = function(cid, uid) {
RDB.srem('cid:' + cid + ':active_users', uid);
db.setRemove('cid:' + cid + ':active_users', uid);
};
}(exports));

@ -17,6 +17,9 @@
redisClient = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
}
module.client = redisClient;
module.type = 'redis';
if (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
module.exists = function(key, callback) {
@ -80,6 +115,14 @@
redisClient.del(key, callback);
}
module.get = function(key, callback) {
redisClient.get(key, callback);
}
module.set = function(key, callback) {
redisClient.get(key, callback);
}
//hashes
module.setObject = function(key, data, callback) {
@ -91,7 +134,7 @@
}
module.getObject = function(key, callback) {
redisClient.hgetall(key, callback)
redisClient.hgetall(key, callback);
}
module.getObjectField = function(key, field, callback) {
@ -138,6 +181,10 @@
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) {
redisClient.hincrby(key, field, value, callback);
}
@ -157,6 +204,16 @@
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) {
redisClient.smembers(key, callback);
}
@ -165,6 +222,10 @@
redisClient.scard(key, callback);
}
module.setRemoveRandom = function(key, callback) {
redisClient.spop(key, callback);
}
// sorted sets
module.sortedSetAdd = function(key, score, value, callback) {
@ -176,11 +237,15 @@
}
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) {
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) {
@ -200,5 +265,7 @@
redisClient.lrange(key, start, stop, callback);
}
}(exports));

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

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

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

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

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

@ -1,4 +1,4 @@
var RDB = require('./redis'),
var db = require('./database'),
posts = require('./posts'),
topics = require('./topics'),
threadTools = require('./threadTools'),
@ -19,7 +19,7 @@ var RDB = require('./redis'),
(function(PostTools) {
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) {
return callback(err);
}
@ -131,14 +131,14 @@ var RDB = require('./redis'),
PostTools.delete = function(uid, pid, callback) {
var success = function() {
posts.setPostField(pid, 'deleted', 1);
RDB.decr('totalpostcount');
db.decrObjectField('global', 'postCount');
postSearch.remove(pid);
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) {
RDB.zadd('users:postcount', postcount, postData.uid);
db.sortedSetAdd('users:postcount', postcount, postData.uid);
});
// Delete the thread if it is the last undeleted post
@ -180,10 +180,10 @@ var RDB = require('./redis'),
PostTools.restore = function(uid, pid, callback) {
var success = function() {
posts.setPostField(pid, 'deleted', 0);
RDB.incr('totalpostcount');
db.incrObjectField('global', 'postCount');
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);

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

@ -3,7 +3,7 @@ var nconf = require('nconf'),
path = require('path'),
winston = require('winston'),
RDB = require('./../redis'),
db = require('./../database'),
user = require('./../user'),
groups = require('../groups'),
topics = require('./../topics'),
@ -267,29 +267,8 @@ var nconf = require('nconf'),
app.namespace('/redis', function () {
app.get('/', function (req, res) {
RDB.info(function (err, data) {
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);
}
}
res.json(finalData);
db.info(function (err, data) {
res.json(data);
});
});

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

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

@ -5,7 +5,7 @@ var async = require('async'),
reds = require('reds'),
topicSearch = reds.createSearch('nodebbtopicsearch'),
RDB = require('./redis'),
db = require('./database'),
posts = require('./posts'),
utils = require('./../public/src/utils'),
user = require('./user'),
@ -58,16 +58,16 @@ var async = require('async'),
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) {
return callback(err);
}
RDB.sadd('topics:tid', tid);
db.setAdd('topics:tid', tid);
var slug = tid + '/' + utils.slugify(title);
var timestamp = Date.now();
RDB.hmset('topic:' + tid, {
db.setObject('topic:' + tid, {
'tid': tid,
'uid': uid,
'cid': cid,
@ -87,15 +87,15 @@ var async = require('async'),
user.addTopicIdToUser(uid, tid);
// 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);
});
// 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);
RDB.hincrby('category:' + cid, 'topic_count', 1);
RDB.incr('totaltopiccount');
db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid);
db.incrObjectField('category:' + cid, 'topic_count', 1);
db.incrObjectField('global', 'topicCount');
feed.updateCategory(cid);
@ -124,7 +124,7 @@ var async = require('async'),
};
Topics.getTopicData = function(tid, callback) {
RDB.hgetall('topic:' + tid, function(err, data) {
db.getObject('topic:' + tid, function(err, data) {
if(err) {
return callback(err, null);
}
@ -236,8 +236,7 @@ var async = require('async'),
since = terms[term];
var args = ['topics:recent', '+inf', timestamp - since, 'LIMIT', start, end - start + 1];
RDB.zrevrangebyscore(args, function(err, tids) {
db.getSortedSetRevRangeByScore(args, function(err, tids) {
if (err) {
return callback(err);
}
@ -272,7 +271,7 @@ var async = require('async'),
return unreadTids.length < 21 && !done;
},
function(callback) {
RDB.zrevrange('topics:recent', start, stop, function(err, tids) {
db.getSortedSetRevRange('topics:recent', start, stop, function(err, tids) {
if (err)
return callback(err);
@ -344,7 +343,7 @@ var async = require('async'),
async.whilst(
continueCondition,
function(callback) {
RDB.zrevrange('topics:recent', start, stop, function(err, tids) {
db.getSortedSetRevRange('topics:recent', start, stop, function(err, tids) {
if (err)
return callback(err);
@ -597,7 +596,7 @@ var async = require('async'),
}
Topics.getAllTopics = function(limit, after, callback) {
RDB.smembers('topics:tid', function(err, tids) {
db.getSetMembers('topics:tid', function(err, tids) {
if(err) {
return callback(err, null);
}
@ -643,7 +642,7 @@ var async = require('async'),
}
Topics.markAllRead = function(uid, callback) {
RDB.smembers('topics:tid', function(err, tids) {
db.getSetMembers('topics:tid', function(err, tids) {
if (err) {
return callback(err, null);
}
@ -667,12 +666,12 @@ var async = require('async'),
}
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) {
RDB.sadd('tid:' + tid + ':read_by_uid', uid);
db.setAdd('tid:' + tid + ':read_by_uid', uid);
Topics.getTopicField(tid, 'cid', function(err, cid) {
@ -691,19 +690,19 @@ var async = require('async'),
}
Topics.hasReadTopics = function(tids, uid, callback) {
var batch = RDB.multi();
var sets = [];
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);
});
}
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) {
callback(hasRead);
@ -719,7 +718,9 @@ var async = require('async'),
if (Array.isArray(tids)) {
async.eachSeries(tids, function(tid, next) {
Topics.getTeaser(tid, function(err, teaser_info) {
if (err) teaser_info = {};
if (err) {
teaser_info = {};
}
teasers.push(teaser_info);
next();
});
@ -783,23 +784,23 @@ var async = require('async'),
}
Topics.getTopicField = function(tid, field, callback) {
RDB.hget('topic:' + tid, field, callback);
db.getObjectField('topic:' + tid, field, 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) {
RDB.hset('topic:' + tid, field, value, callback);
db.setObjectField('topic:' + tid, field, value, callback);
}
Topics.increasePostCount = function(tid, callback) {
RDB.hincrby('topic:' + tid, 'postcount', 1, callback);
db.incrObjectField('topic:' + tid, 'postcount', callback);
}
Topics.increaseViewCount = function(tid, callback) {
RDB.hincrby('topic:' + tid, 'viewcount', 1, callback);
db.incrObjectField('topic:' + tid, 'viewcount', callback);
}
Topics.isLocked = function(tid, callback) {
@ -812,16 +813,16 @@ var async = require('async'),
}
Topics.updateTimestamp = function(tid, timestamp) {
RDB.zadd('topics:recent', timestamp, tid);
db.sortedSetAdd('topics:recent', timestamp, tid);
Topics.setTopicField(tid, 'lastposttime', timestamp);
}
Topics.addPostToTopic = function(tid, pid) {
RDB.rpush('tid:' + tid + ':posts', pid);
db.listAppend('tid:' + tid + ':posts', pid);
}
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) {
@ -848,23 +849,23 @@ var async = require('async'),
Topics.delete = function(tid) {
Topics.setTopicField(tid, 'deleted', 1);
RDB.zrem('topics:recent', tid);
db.sortedSetRemove('topics:recent', tid);
Topics.getTopicField(tid, 'cid', function(err, cid) {
feed.updateCategory(cid);
RDB.hincrby('category:' + cid, 'topic_count', -1);
db.incrObjectFieldBy('category:' + cid, 'topic_count', -1);
});
}
Topics.restore = function(tid) {
Topics.setTopicField(tid, 'deleted', 0);
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) {
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) {
RDB.smembers('topics:tid', function(err, tids) {
db.getSetMembers('topics:tid', function(err, tids) {
if (err) {
callback(err, null);
} else {

@ -1,6 +1,11 @@
"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'),
winston = require('winston'),
notifications = require('./notifications'),

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

@ -10,9 +10,13 @@ var cookie = require('cookie'),
winston = require('winston'),
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({
client: RDB,
client: redisClient,
ttl: 60 * 60 * 24 * 14
}),
@ -71,8 +75,11 @@ websockets.init = function(io) {
socketCookieParser(hs, {}, function(err) {
sessionID = socket.handshake.signedCookies["express.sid"];
RedisStore.get(sessionID, function(err, sessionData) {
if (!err && sessionData && sessionData.passport && sessionData.passport.user) uid = users[sessionID] = sessionData.passport.user;
else uid = users[sessionID] = 0;
if (!err && sessionData && sessionData.passport && sessionData.passport.user) {
uid = users[sessionID] = sessionData.passport.user;
} else {
uid = users[sessionID] = 0;
}
userSockets[uid] = userSockets[uid] || [];
userSockets[uid].push(socket);
@ -89,7 +96,7 @@ websockets.init = function(io) {
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);
user.getUserField(uid, 'username', function(err, username) {
@ -119,7 +126,7 @@ websockets.init = function(io) {
delete users[sessionID];
delete userSockets[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() {
RDB.mget(['totaltopiccount', 'totalpostcount'], function(err, data) {
db.getObjectFields('global', ['topicCount', 'postCount'], function(err, data) {
if (err) {
return winston.err(err);
}
var stats = {
topics: data[0] ? data[0] : 0,
posts: data[1] ? data[1] : 0
topics: data.topicCount ? data.topicCount : 0,
posts: data.postCount ? data.postCount : 0
};
io.sockets.emit('post.stats', stats);
@ -1123,7 +1130,7 @@ websockets.init = function(io) {
}
websockets.emitUserCount = function() {
RDB.get('usercount', function(err, count) {
db.getObjectField('global', 'userCount', function(err, count) {
io.sockets.emit('user.count', {
count: count
});

Loading…
Cancel
Save