Merge branch 'master' of github.com:designcreateplay/NodeBB

v1.18.x
Julian Lam 11 years ago
commit fcb44ae256

@ -1,3 +1,6 @@
'use strict';
var db = require('./database'), var db = require('./database'),
posts = require('./posts'), posts = require('./posts'),
utils = require('./../public/src/utils'), utils = require('./../public/src/utils'),
@ -13,7 +16,6 @@ var db = require('./database'),
nconf = require('nconf'); nconf = require('nconf');
(function(Categories) { (function(Categories) {
"use strict";
Categories.create = function(data, callback) { Categories.create = function(data, callback) {
db.incrObjectField('global', 'nextCid', function(err, cid) { db.incrObjectField('global', 'nextCid', function(err, cid) {
@ -161,11 +163,11 @@ var db = require('./database'),
Categories.getAllCategories = function(uid, callback) { Categories.getAllCategories = function(uid, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
if(err) { if (err) {
return callback(err); return callback(err);
} }
if(cids && cids.length === 0) { if (!cids || (cids && cids.length === 0)) {
return callback(null, {categories : []}); return callback(null, {categories : []});
} }
@ -339,7 +341,6 @@ var db = require('./database'),
'categories': categories 'categories': categories
}); });
}); });
}; };
Categories.isUserActiveIn = function(cid, uid, callback) { Categories.isUserActiveIn = function(cid, uid, callback) {

@ -1,7 +1,8 @@
'use strict';
(function(module) { (function(module) {
'use strict';
var winston = require('winston'), var winston = require('winston'),
async = require('async'), async = require('async'),
nconf = require('nconf'), nconf = require('nconf'),
@ -68,15 +69,15 @@
}); });
if(typeof callback === 'function') { if(typeof callback === 'function') {
callback(null); callback();
} }
} }
}); });
} };
module.close = function() { module.close = function() {
db.close(); db.close();
} };
// //
// helper functions // helper functions
@ -129,7 +130,7 @@
winston.error('Error indexing ' + err.message); winston.error('Error indexing ' + err.message);
} }
}); });
} };
module.search = function(key, term, limit, callback) { module.search = function(key, term, limit, callback) {
db.command({text:"search" , search: term, filter: {key:key}, limit: limit }, function(err, result) { db.command({text:"search" , search: term, filter: {key:key}, limit: limit }, function(err, result) {
@ -150,7 +151,7 @@
callback(null, []); callback(null, []);
} }
}); });
} };
module.searchRemove = function(key, id, callback) { module.searchRemove = function(key, id, callback) {
db.collection('search').remove({id:id, key:key}, function(err, result) { db.collection('search').remove({id:id, key:key}, function(err, result) {
@ -161,27 +162,22 @@
callback(); callback();
} }
}); });
} };
module.flushdb = function(callback) { module.flushdb = function(callback) {
db.dropDatabase(function(err, result) { db.dropDatabase(function(err, result) {
if(err){ if (err) {
winston.error(error); winston.error(err.message);
if(typeof callback === 'function') { if (typeof callback === 'function') {
return callback(err); return callback(err);
} }
} }
if(typeof callback === 'function') { if (typeof callback === 'function') {
callback(null); callback();
} }
}); });
} };
module.getFileName = function(callback) {
throw new Error('not-implemented');
}
module.info = function(callback) { module.info = function(callback) {
db.stats({scale:1024}, function(err, stats) { db.stats({scale:1024}, function(err, stats) {
@ -189,10 +185,6 @@
return callback(err); return callback(err);
} }
// TODO : if this it not deleted the templates break,
// it is a nested object inside stats
delete stats.dataFileVersion;
stats.avgObjSize = (stats.avgObjSize / 1024).toFixed(2); stats.avgObjSize = (stats.avgObjSize / 1024).toFixed(2);
stats.raw = JSON.stringify(stats, null, 4); stats.raw = JSON.stringify(stats, null, 4);
@ -201,7 +193,7 @@
callback(null, stats); callback(null, stats);
}); });
} };
// key // key
@ -209,7 +201,7 @@
db.collection('objects').findOne({_key:key}, function(err, item) { db.collection('objects').findOne({_key:key}, function(err, item) {
callback(err, item !== undefined && item !== null); callback(err, item !== undefined && item !== null);
}); });
} };
module.delete = function(key, callback) { module.delete = function(key, callback) {
db.collection('objects').remove({_key:key}, function(err, result) { db.collection('objects').remove({_key:key}, function(err, result) {
@ -217,22 +209,22 @@
callback(err, result); callback(err, result);
} }
}); });
} };
module.get = function(key, callback) { module.get = function(key, callback) {
module.getObjectField(key, 'value', callback); module.getObjectField(key, 'value', callback);
} };
module.set = function(key, value, callback) { module.set = function(key, value, callback) {
var data = {value:value}; var data = {value:value};
module.setObject(key, data, callback); module.setObject(key, data, callback);
} };
module.keys = function(key, callback) { module.keys = function(key, callback) {
db.collection('objects').find( { _key: { $regex: key /*, $options: 'i'*/ } }, function(err, result) { db.collection('objects').find( { _key: { $regex: key /*, $options: 'i'*/ } }, function(err, result) {
callback(err, result); callback(err, result);
}); });
} };
module.rename = function(oldKey, newKey, callback) { module.rename = function(oldKey, newKey, callback) {
db.collection('objects').update({_key: oldKey}, {$set:{_key: newKey}}, function(err, result) { db.collection('objects').update({_key: oldKey}, {$set:{_key: newKey}}, function(err, result) {
@ -240,25 +232,25 @@
callback(err, result); callback(err, result);
} }
}); });
} };
module.expire = function(key, seconds, callback) { module.expire = function(key, seconds, callback) {
module.expireAt(key, Math.round(Date.now() / 1000) + seconds, callback); module.expireAt(key, Math.round(Date.now() / 1000) + seconds, callback);
} };
module.expireAt = function(key, timestamp, callback) { module.expireAt = function(key, timestamp, callback) {
module.setObjectField(key, 'expireAt', new Date(timestamp * 1000), callback); module.setObjectField(key, 'expireAt', new Date(timestamp * 1000), callback);
} };
//hashes //hashes
module.setObject = function(key, data, callback) { module.setObject = function(key, data, callback) {
data['_key'] = key; data._key = key;
db.collection('objects').update({_key:key}, {$set:data}, {upsert:true, w: 1}, function(err, result) { db.collection('objects').update({_key:key}, {$set:data}, {upsert:true, w: 1}, function(err, result) {
if(typeof callback === 'function') { if(typeof callback === 'function') {
callback(err, result); callback(err, result);
} }
}); });
} };
module.setObjectField = function(key, field, value, callback) { module.setObjectField = function(key, field, value, callback) {
var data = {}; var data = {};
@ -273,7 +265,7 @@
callback(err, result); callback(err, result);
} }
}); });
} };
module.getObject = function(key, callback) { module.getObject = function(key, callback) {
db.collection('objects').findOne({_key:key}, function(err, item) { db.collection('objects').findOne({_key:key}, function(err, item) {
@ -281,7 +273,7 @@
callback(err, item); callback(err, item);
}); });
} };
module.getObjects = function(keys, callback) { module.getObjects = function(keys, callback) {
@ -299,7 +291,7 @@
callback(null, returnData); callback(null, returnData);
}); });
} };
module.getObjectField = function(key, field, callback) { module.getObjectField = function(key, field, callback) {
module.getObjectFields(key, [field], function(err, data) { module.getObjectFields(key, [field], function(err, data) {
@ -309,7 +301,7 @@
callback(null, data[field]); callback(null, data[field]);
}); });
} };
module.getObjectFields = function(key, fields, callback) { module.getObjectFields = function(key, fields, callback) {
@ -342,7 +334,7 @@
callback(null, item); callback(null, item);
}); });
} };
module.getObjectKeys = function(key, callback) { module.getObjectKeys = function(key, callback) {
module.getObject(key, function(err, data) { module.getObject(key, function(err, data) {
@ -356,7 +348,7 @@
callback(null, []); callback(null, []);
} }
}); });
} };
module.getObjectValues = function(key, callback) { module.getObjectValues = function(key, callback) {
module.getObject(key, function(err, data) { module.getObject(key, function(err, data) {
@ -370,7 +362,7 @@
} }
callback(null, values); callback(null, values);
}); });
} };
module.isObjectField = function(key, field, callback) { module.isObjectField = function(key, field, callback) {
var data = {}; var data = {};
@ -385,7 +377,7 @@
} }
callback(err, !!item && item[field] !== undefined && item[field] !== null); callback(err, !!item && item[field] !== undefined && item[field] !== null);
}); });
} };
module.deleteObjectField = function(key, field, callback) { module.deleteObjectField = function(key, field, callback) {
var data = {}; var data = {};
@ -399,15 +391,15 @@
callback(err, result); callback(err, result);
} }
}); });
} };
module.incrObjectField = function(key, field, callback) { module.incrObjectField = function(key, field, callback) {
module.incrObjectFieldBy(key, field, 1, callback); module.incrObjectFieldBy(key, field, 1, callback);
} };
module.decrObjectField = function(key, field, callback) { module.decrObjectField = function(key, field, callback) {
module.incrObjectFieldBy(key, field, -1, callback); module.incrObjectFieldBy(key, field, -1, callback);
} };
module.incrObjectFieldBy = function(key, field, value, callback) { module.incrObjectFieldBy = function(key, field, value, callback) {
var data = {}; var data = {};
@ -422,7 +414,7 @@
callback(err, result ? result[field] : null); callback(err, result ? result[field] : null);
} }
}); });
} };
// sets // sets
@ -437,19 +429,12 @@
}); });
db.collection('objects').update({_key:key}, db.collection('objects').update({_key: key}, { $addToSet: { members: { $each: value } } }, { upsert: true, w: 1 }, function(err, result) {
{
$addToSet: { members: { $each: value } }
},
{
upsert:true, w: 1
}
, function(err, result) {
if(typeof callback === 'function') { if(typeof callback === 'function') {
callback(err, result); callback(err, result);
} }
}); });
} };
module.setRemove = function(key, value, callback) { module.setRemove = function(key, value, callback) {
if(!Array.isArray(value)) { if(!Array.isArray(value)) {
@ -465,7 +450,7 @@
callback(err, result); callback(err, result);
} }
}); });
} };
module.isSetMember = function(key, value, callback) { module.isSetMember = function(key, value, callback) {
if(value !== null && value !== undefined) { if(value !== null && value !== undefined) {
@ -475,7 +460,7 @@
db.collection('objects').findOne({_key:key, members: value}, function(err, item) { db.collection('objects').findOne({_key:key, members: value}, function(err, item) {
callback(err, item !== null && item !== undefined); callback(err, item !== null && item !== undefined);
}); });
} };
module.isMemberOfSets = function(sets, value, callback) { module.isMemberOfSets = function(sets, value, callback) {
@ -498,7 +483,7 @@
callback(err, result); callback(err, result);
}); });
} };
module.getSetMembers = function(key, callback) { module.getSetMembers = function(key, callback) {
db.collection('objects').findOne({_key:key}, {members:1}, function(err, data) { db.collection('objects').findOne({_key:key}, {members:1}, function(err, data) {
@ -512,7 +497,7 @@
callback(null, data.members); callback(null, data.members);
} }
}); });
} };
module.setCount = function(key, callback) { module.setCount = function(key, callback) {
db.collection('objects').findOne({_key:key}, function(err, data) { db.collection('objects').findOne({_key:key}, function(err, data) {
@ -525,7 +510,7 @@
callback(null, data.members.length); callback(null, data.members.length);
}); });
} };
module.setRemoveRandom = function(key, callback) { module.setRemoveRandom = function(key, callback) {
db.collection('objects').findOne({_key:key}, function(err, data) { db.collection('objects').findOne({_key:key}, function(err, data) {
@ -551,7 +536,7 @@
}); });
} }
}); });
} };
// sorted sets // sorted sets
@ -570,7 +555,7 @@
callback(err, result); callback(err, result);
} }
}); });
} };
module.sortedSetRemove = function(key, value, callback) { module.sortedSetRemove = function(key, value, callback) {
if(value !== null && value !== undefined) { if(value !== null && value !== undefined) {
@ -582,7 +567,7 @@
callback(err, result); callback(err, result);
} }
}); });
} };
function getSortedSetRange(key, start, stop, sort, callback) { function getSortedSetRange(key, start, stop, sort, callback) {
db.collection('objects').find({_key:key}, {fields:{value:1}}) db.collection('objects').find({_key:key}, {fields:{value:1}})
@ -590,26 +575,29 @@
.skip(start) .skip(start)
.sort({score: sort}) .sort({score: sort})
.toArray(function(err, data) { .toArray(function(err, data) {
if(err) { if (err) {
return callback(err); return callback(err);
} }
// maybe this can be done with mongo? if (!data) {
return callback(null, null);
}
data = data.map(function(item) { data = data.map(function(item) {
return item.value; return item.value;
}); });
callback(err, data); callback(null, data);
}); });
} }
module.getSortedSetRange = function(key, start, stop, callback) { module.getSortedSetRange = function(key, start, stop, callback) {
getSortedSetRange(key, start, stop, 1, callback); getSortedSetRange(key, start, stop, 1, callback);
} };
module.getSortedSetRevRange = function(key, start, stop, callback) { module.getSortedSetRevRange = function(key, start, stop, callback) {
getSortedSetRange(key, start, stop, -1, callback); getSortedSetRange(key, start, stop, -1, callback);
} };
module.getSortedSetRevRangeByScore = function(args, callback) { module.getSortedSetRevRangeByScore = function(args, callback) {
@ -640,7 +628,7 @@
callback(err, data); callback(err, data);
}); });
} };
module.sortedSetCount = function(key, min, max, callback) { module.sortedSetCount = function(key, min, max, callback) {
db.collection('objects').count({_key:key, score: {$gte:min, $lte:max}}, function(err, count) { db.collection('objects').count({_key:key, score: {$gte:min, $lte:max}}, function(err, count) {
@ -653,7 +641,7 @@
} }
callback(null,count); callback(null,count);
}); });
} };
module.sortedSetCard = function(key, callback) { module.sortedSetCard = function(key, callback) {
db.collection('objects').count({_key:key}, function(err, count) { db.collection('objects').count({_key:key}, function(err, count) {
@ -666,7 +654,7 @@
} }
callback(null, count); callback(null, count);
}); });
} };
module.sortedSetRank = function(key, value, callback) { module.sortedSetRank = function(key, value, callback) {
if(value !== null && value !== undefined) { if(value !== null && value !== undefined) {
@ -683,7 +671,7 @@
callback(null, rank); callback(null, rank);
}); });
} };
module.sortedSetRevRank = function(key, value, callback) { module.sortedSetRevRank = function(key, value, callback) {
if(value !== null && value !== undefined) { if(value !== null && value !== undefined) {
@ -702,7 +690,7 @@
callback(null, rank); callback(null, rank);
}); });
} };
module.sortedSetScore = function(key, value, callback) { module.sortedSetScore = function(key, value, callback) {
if(value !== null && value !== undefined) { if(value !== null && value !== undefined) {
@ -718,13 +706,13 @@
callback(err, null); callback(err, null);
}); });
} };
module.isSortedSetMember = function(key, value, callback) { module.isSortedSetMember = function(key, value, callback) {
module.sortedSetScore(key, value, function(err, score) { module.sortedSetScore(key, value, function(err, score) {
callback(err, !!score); callback(err, !!score);
}); });
} };
module.sortedSetsScore = function(keys, value, callback) { module.sortedSetsScore = function(keys, value, callback) {
if(value !== null && value !== undefined) { if(value !== null && value !== undefined) {
@ -745,13 +733,14 @@
callback(null, returnData); callback(null, returnData);
}); });
} };
// lists // lists
module.listPrepend = function(key, value, callback) { module.listPrepend = function(key, value, callback) {
if(value !== null && value !== undefined) { if(value !== null && value !== undefined) {
value = value.toString(); value = value.toString();
} }
module.isObjectField(key, 'array', function(err, exists) { module.isObjectField(key, 'array', function(err, exists) {
if(err) { if(err) {
if(typeof callback === 'function') { if(typeof callback === 'function') {
@ -762,17 +751,16 @@
} }
if(exists) { if(exists) {
db.collection('objects').update({_key:key}, {'$set': {'array.-1': value}}, {upsert:true, w:1 }, function(err, result) { db.collection('objects').update({_key:key}, {'$set': {'array.-1': value}}, {upsert:true, w:1 }, function(err, result) {
if(typeof callback === 'function') { if(typeof callback === 'function') {
callback(err, result); callback(err, result);
} }
}); });
} else { } else {
module.listAppend(key, value, callback); module.listAppend(key, value, callback);
} }
});
}) };
}
module.listAppend = function(key, value, callback) { module.listAppend = function(key, value, callback) {
if(value !== null && value !== undefined) { if(value !== null && value !== undefined) {
@ -783,7 +771,7 @@
callback(err, result); callback(err, result);
} }
}); });
} };
module.listRemoveLast = function(key, callback) { module.listRemoveLast = function(key, callback) {
module.getListRange(key, -1, 0, function(err, value) { module.getListRange(key, -1, 0, function(err, value) {
@ -808,7 +796,7 @@
} }
}); });
}); });
} };
module.listRemoveAll = function(key, value, callback) { module.listRemoveAll = function(key, value, callback) {
if(value !== null && value !== undefined) { if(value !== null && value !== undefined) {
@ -820,7 +808,7 @@
callback(err, result); callback(err, result);
} }
}); });
} };
module.getListRange = function(key, start, stop, callback) { module.getListRange = function(key, start, stop, callback) {
@ -867,8 +855,7 @@
callback(null, []); callback(null, []);
} }
}); });
} };
}(exports)); }(exports));

@ -11,7 +11,9 @@
redis, redis,
connectRedis, connectRedis,
reds, reds,
redisClient; redisClient,
postSearch,
topicSearch;
try { try {
redis = require('redis'); redis = require('redis');
@ -22,54 +24,52 @@
process.exit(); process.exit();
} }
module.init = function(callback) {
if (redis_socket_or_host && redis_socket_or_host.indexOf('/')>=0) {
/* If redis.host contains a path name character, use the unix dom sock connection. ie, /tmp/redis.sock */
redisClient = redis.createClient(nconf.get('redis:host'));
} else {
/* Else, connect over tcp/ip */
redisClient = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
}
if (redis_socket_or_host && redis_socket_or_host.indexOf('/')>=0) { if (nconf.get('redis:password')) {
/* If redis.host contains a path name character, use the unix dom sock connection. ie, /tmp/redis.sock */ redisClient.auth(nconf.get('redis:password'));
redisClient = redis.createClient(nconf.get('redis:host')); } else {
} else { winston.warn('You have no redis password setup!');
/* Else, connect over tcp/ip */ }
redisClient = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
}
if (nconf.get('redis:password')) {
redisClient.auth(nconf.get('redis:password'));
} else {
winston.warn('You have no redis password setup!');
}
redisClient.on('error', function (err) {
winston.error(err.message);
process.exit();
});
redisClient.on('error', function (err) {
winston.error(err.message);
process.exit();
});
module.client = redisClient; module.client = redisClient;
module.sessionStore = new connectRedis({ module.sessionStore = new connectRedis({
client: redisClient, client: redisClient,
ttl: 60 * 60 * 24 * 14 ttl: 60 * 60 * 24 * 14
}); });
reds.createClient = function () { reds.createClient = function () {
return reds.client || (reds.client = redisClient); return reds.client || (reds.client = redisClient);
}; };
var postSearch = reds.createSearch('nodebbpostsearch'), postSearch = reds.createSearch('nodebbpostsearch'),
topicSearch = reds.createSearch('nodebbtopicsearch'); topicSearch = reds.createSearch('nodebbtopicsearch');
var db = parseInt(nconf.get('redis:database'), 10); var db = parseInt(nconf.get('redis:database'), 10);
if (db) { if (db) {
redisClient.select(db, function(error) { redisClient.select(db, function(error) {
if(error) { if(error) {
winston.error("NodeBB could not connect to your Redis database. Redis returned the following error: " + error.message); winston.error("NodeBB could not connect to your Redis database. Redis returned the following error: " + error.message);
process.exit(); process.exit();
} }
}); });
} }
module.init = function(callback) { callback();
callback(null);
}; };
module.close = function() { module.close = function() {
@ -125,26 +125,6 @@
}); });
}; };
module.getFileName = function(callback) {
var multi = redisClient.multi();
multi.config('get', 'dir');
multi.config('get', 'dbfilename');
multi.exec(function (err, results) {
if (err) {
return callback(err);
}
results = results.reduce(function (memo, config) {
memo[config[0]] = config[1];
return memo;
}, {});
var dbFile = path.join(results.dir, results.dbfilename);
callback(null, dbFile);
});
};
module.info = function(callback) { module.info = function(callback) {
redisClient.info(function (err, data) { redisClient.info(function (err, data) {
if(err) { if(err) {

@ -297,12 +297,6 @@ var fs = require('fs'),
} }
}; };
Meta.db = {
getFile: function (callback) {
db.getFileName(callback);
}
};
Meta.css = { Meta.css = {
cache: undefined cache: undefined
}; };

@ -347,28 +347,6 @@ var nconf = require('nconf'),
res.json(data); res.json(data);
}); });
}); });
// app.get('/export', function (req, res) {
// meta.db.getFile(function (err, dbFile) {
// if (!err) {
// res.download(dbFile, 'redis.rdb', function (err) {
// console.log(err);
// res.send(500);
// if (err) {
// res.send(500);
// switch (err.code) {
// case 'EACCES':
// res.send(500, 'Require permissions from Redis database file: ', dbFile);
// break;
// default:
// res.send(500);
// break;
// }
// }
// });
// } else res.send(500);
// });
// });
}); });
app.get('/events', function(req, res, next) { app.get('/events', function(req, res, next) {

Loading…
Cancel
Save