more mongo work

v1.18.x
Baris Usakli 11 years ago
parent 1f52717f1e
commit 91d6f83de4

@ -89,14 +89,15 @@
webserver = require('./src/webserver'),
SocketIO = require('socket.io').listen(global.server, { log: false, transports: ['websocket', 'xhr-polling', 'jsonp-polling', 'flashsocket'], 'browser client minification': true}),
websockets = require('./src/websockets'),
plugins = require('./src/plugins'), // Don't remove this - plugins initializes itself
plugins = require('./src/plugins'),
notifications = require('./src/notifications'),
upgrade = require('./src/upgrade');
upgrade.check(function(schema_ok) {
if (schema_ok || nconf.get('check-schema') === false) {
websockets.init(SocketIO);
console.log('calling plugins init');
plugins.init();
global.templates = {};
global.translator = translator;

@ -15,11 +15,13 @@
"dependencies": {
"socket.io": "~0.9.16",
"redis": "0.8.3",
"mongodb": "1.3.20",
"express": "3.2.0",
"express-namespace": "~0.1.1",
"emailjs": "0.3.4",
"cookie": "0.0.6",
"connect-redis": "1.4.5",
"connect-mongo": "0.4.0",
"passport": "0.1.17",
"passport-local": "0.1.6",
"passport-twitter": "0.1.5",

@ -2,74 +2,212 @@
(function(module) {
'use strict';
var mongodb = require('mongodb')
mongoClient = mongodb.MongoClient,
var Db = require('mongodb').Db,
mongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
winston = require('winston'),
nconf = require('nconf'),
express = require('express'),
mongoStore = require('connect-mongo')(express);
mongoHost = nconf.get('mongo:host');
mongoStore = require('connect-mongo')(express),
mongoHost = nconf.get('mongo:host'),
db;
// mongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
mongoClient.connect('mongodb://' + mongoHost + ':' + nconf.get('mongo:port') + '/' + nconf.get('mongo:database'), function(err, db) {
var db = new Db(nconf.get('mongo:database'), new Server(mongoHost, nconf.get('mongo:port')), {w:1});
//console.log(db.collection);
db.open(function(err, _db) {
//mongoClient.connect('mongodb://'+ mongoHost + ':' + nconf.get('mongo:port') + '/' + nconf.get('mongo:database'), function(err, _db) {
console.log('WE ARE CONNECTED');
if(err) {
winston.error("NodeBB could not connect to your Mongo database. Mongo returned the following error: " + error.message);
winston.error("NodeBB could not connect to your Mongo database. Mongo returned the following error: " + err.message);
process.exit();
}
// TODO: fill out settings.db
module.sessionStore = new mongoStore({
db: db
});
db.collection('objects').findOne({_key:'config'}, {timeout:true}, function(err, item) {
console.log('fail');
console.log(item);
callback(err, item);
});
});
db.createCollection('objects', function(err, collection) {
console.log('collection created', err, collection);
});
db.createCollection('sets', function(err, collection) {
});
// look up how its done in mongo
/*if (nconf.get('mongo:password')) {
redisClient.auth(nconf.get('mongo:password'));
}
*/
// TODO: fill out settings.db
module.sessionStore = new mongoStore({
db: settings.db
});
//
// Exported functions
//
module.getFileName = function(callback) {
// TODO : get mongodb filename
throw new Error('not-implemented');
}
module.info = function(callback) {
throw new Error('not-implemented');
}
// key
module.exists = function(key, callback) {
throw new Error('not-implemented');
}
module.delete = function(key, callback) {
throw new Error('not-implemented');
}
module.get = function(key, callback) {
throw new Error('not-implemented');
}
module.set = function(key, callback) {
throw new Error('not-implemented');
}
//hashes
module.setObject = function(key, data, callback) {
// TODO : implement in mongo
data['_key'] = key;
db.collection('objects').insert(data, {w:1}, function(err, result) {
callback(err, result);
});
}
module.setObjectField = function(key, field, callback) {
// TODO : implement in mongo
module.setObjectField = function(key, field, value, callback) {
db.collection('objects').update();
}
module.getObject = function(key, callback) {
// TODO : implement in mongo
console.log('calling findOne');
db.collection('objects').findOne({_key:key}, {timeout:true},function(err, item) {
console.log(item);
callback(err, item);
});
}
module.getObjectField = function(key, field, callback) {
// TODO : implement in mongo
throw new Error('not-implemented');
}
module.getObjectFields = function(key, fields, callback) {
// TODO : implement in mongo
throw new Error('not-implemented');
}
module.getObjectValues = function(key, callback) {
throw new Error('not-implemented');
}
module.isObjectField = function(key, field, callback) {
throw new Error('not-implemented');
}
module.deleteObjectField = function(key, field, callback) {
// TODO : implement in mongo
throw new Error('not-implemented');
}
module.incrObjectField = function(key, field, value, callback) {
// TODO : implement in mongo
module.incrObjectField = function(key, field, callback) {
throw new Error('not-implemented');
}
module.decrObjectField = function(key, field, callback) {
throw new Error('not-implemented');
}
module.incrObjectFieldBy = function(key, field, value, callback) {
throw new Error('not-implemented');
}
// sets
module.setAdd = function(key, value, callback) {
throw new Error('not-implemented');
}
module.setRemove = function(key, value, callback) {
throw new Error('not-implemented');
}
module.isSetMember = function(key, value, callback) {
throw new Error('not-implemented');
}
module.isMemberOfSets = function(sets, value, callback) {
throw new Error('not-implemented');
}
module.getSetMembers = function(key, callback) {
console.log('getting set members');
db.collection('sets').findOne({_key:key}, function(err, data) {
console.log('derp', err, data);
callback(err, data);
});
}
module.setCount = function(key, callback) {
throw new Error('not-implemented');
}
module.setRemoveRandom = function(key, callback) {
throw new Error('not-implemented');
}
// sorted sets
module.sortedSetAdd = function(key, score, value, callback) {
throw new Error('not-implemented');
}
module.sortedSetRemove = function(key, value, callback) {
throw new Error('not-implemented');
}
module.getSortedSetRange = function(key, start, stop, callback) {
throw new Error('not-implemented');
}
module.getSortedSetRevRange = function(key, start, stop, callback) {
throw new Error('not-implemented');
}
module.getSortedSetRevRangeByScore = function(args, callback) {
throw new Error('not-implemented');
}
module.sortedSetCount = function(key, min, max, callback) {
throw new Error('not-implemented');
}
// lists
module.listPrepend = function(key, value, callback) {
throw new Error('not-implemented');
}
module.listAppend = function(key, value, callback) {
throw new Error('not-implemented');
}
module.getListRange = function(key, start, stop, callback) {
throw new Error('not-implemented');
}
}(exports));

@ -3,45 +3,59 @@ var fs = require('fs'),
async = require('async'),
winston = require('winston'),
eventEmitter = require('events').EventEmitter,
db = require('./database'),
db = require('./database');
plugins = {
libraries: {},
loadedHooks: {},
staticDirs: {},
cssFiles: [],
(function(Plugins) {
Plugins.libraries = {};
Plugins.loadedHooks = {};
Plugins.staticDirs = {};
Plugins.cssFiles = [];
Plugins.initialized = false;
// Events
readyEvent: new eventEmitter,
Plugins.readyEvent = new eventEmitter;
Plugins.init = function() {
console.log('plugins init called');
if (Plugins.initialized) {
return;
}
init: function() {
if (this.initialized) return;
if (global.env === 'development') winston.info('[plugins] Initializing plugins system');
if (global.env === 'development') {
winston.info('[plugins] Initializing plugins system');
}
this.reload(function(err) {
Plugins.reload(function(err) {
if (err) {
if (global.env === 'development') winston.info('[plugins] NodeBB encountered a problem while loading plugins', err.message);
if (global.env === 'development') {
winston.info('[plugins] NodeBB encountered a problem while loading plugins', err.message);
}
return;
}
if (global.env === 'development') winston.info('[plugins] Plugins OK');
plugins.initialized = true;
if (global.env === 'development') {
winston.info('[plugins] Plugins OK');
}
Plugins.initialized = true;
plugins.readyEvent.emit('ready');
});
},
ready: function(callback) {
if (!this.initialized) this.readyEvent.once('ready', callback);
else callback();
},
initialized: false,
reload: function(callback) {
var _self = this;
};
Plugins.ready = function(callback) {
if (!Plugins.initialized) {
Plugins.readyEvent.once('ready', callback);
} else {
callback();
}
};
Plugins.reload = function(callback) {
// Resetting all local plugin data
this.loadedHooks = {};
this.staticDirs = {};
this.cssFiles.length = 0;
Plugins.loadedHooks = {};
Plugins.staticDirs = {};
Plugins.cssFiles.length = 0;
// Read the list of activated plugins and require their libraries
async.waterfall([
@ -52,9 +66,12 @@ var fs = require('fs'),
if (plugins && Array.isArray(plugins) && plugins.length > 0) {
async.each(plugins, function(plugin, next) {
var modulePath = path.join(__dirname, '../node_modules/', plugin);
if (fs.existsSync(modulePath)) _self.loadPlugin(modulePath, next);
else {
if (global.env === 'development') winston.warn('[plugins] Plugin \'' + plugin + '\' not found');
if (fs.existsSync(modulePath)) {
Plugins.loadPlugin(modulePath, next);
} else {
if (global.env === 'development') {
winston.warn('[plugins] Plugin \'' + plugin + '\' not found');
}
next(); // Ignore this plugin silently
}
}, next);
@ -62,8 +79,8 @@ var fs = require('fs'),
},
function(next) {
if (global.env === 'development') winston.info('[plugins] Sorting hooks to fire in priority sequence');
Object.keys(_self.loadedHooks).forEach(function(hook) {
var hooks = _self.loadedHooks[hook];
Object.keys(Plugins.loadedHooks).forEach(function(hook) {
var hooks = Plugins.loadedHooks[hook];
hooks = hooks.sort(function(a, b) {
return a.priority - b.priority;
});
@ -72,12 +89,13 @@ var fs = require('fs'),
next();
}
], callback);
},
loadPlugin: function(pluginPath, callback) {
var _self = this;
};
Plugins.loadPlugin = function(pluginPath, callback) {
fs.readFile(path.join(pluginPath, 'plugin.json'), function(err, data) {
if (err) return callback(err);
if (err) {
return callback(err);
}
var pluginData = JSON.parse(data),
libraryPath, staticDir;
@ -89,14 +107,14 @@ var fs = require('fs'),
fs.exists(libraryPath, function(exists) {
if (exists) {
if (!_self.libraries[pluginData.id]) {
_self.libraries[pluginData.id] = require(libraryPath);
if (!Plugins.libraries[pluginData.id]) {
Plugins.libraries[pluginData.id] = require(libraryPath);
}
// Register hooks for this plugin
if (pluginData.hooks && Array.isArray(pluginData.hooks) && pluginData.hooks.length > 0) {
async.each(pluginData.hooks, function(hook, next) {
_self.registerHook(pluginData.id, hook, next);
Plugins.registerHook(pluginData.id, hook, next);
}, next);
} else {
next(null);
@ -118,7 +136,7 @@ var fs = require('fs'),
fs.exists(staticDir, function(exists) {
if (exists) {
_self.staticDirs[pluginData.id] = staticDir;
Plugins.staticDirs[pluginData.id] = staticDir;
next();
} else next();
});
@ -131,7 +149,7 @@ var fs = require('fs'),
winston.info('[plugins] Found ' + pluginData.css.length + ' CSS file(s) for plugin ' + pluginData.id);
}
_self.cssFiles = _self.cssFiles.concat(pluginData.css.map(function(file) {
Plugins.cssFiles = Plugins.cssFiles.concat(pluginData.css.map(function(file) {
return path.join('/plugins', pluginData.id, file);
}));
@ -145,8 +163,9 @@ var fs = require('fs'),
} else callback(new Error('Could not load plugin system'))
});
});
},
registerHook: function(id, data, callback) {
};
Plugins.registerHook = function(id, data, callback) {
/*
`data` is an object consisting of (* is required):
`data.hook`*, the name of the NodeBB hook
@ -154,7 +173,6 @@ var fs = require('fs'),
`data.callbacked`, whether or not the hook expects a callback (true), or a return (false). Only used for filters. (Default: false)
`data.priority`, the relative priority of the method when it is eventually called (default: 10)
*/
var _self = this;
if (data.hook && data.method) {
data.id = id;
@ -164,20 +182,22 @@ var fs = require('fs'),
return memo[prop];
} else {
// Couldn't find method by path, assuming property with periods in it (evil!)
_self.libraries[data.id][data.method];
Plugins.libraries[data.id][data.method];
}
}, _self.libraries[data.id]);
}, Plugins.libraries[data.id]);
_self.loadedHooks[data.hook] = _self.loadedHooks[data.hook] || [];
_self.loadedHooks[data.hook].push(data);
Plugins.loadedHooks[data.hook] = Plugins.loadedHooks[data.hook] || [];
Plugins.loadedHooks[data.hook].push(data);
if (global.env === 'development') winston.info('[plugins] Hook registered: ' + data.hook + ' will call ' + id);
if (global.env === 'development') {
winston.info('[plugins] Hook registered: ' + data.hook + ' will call ' + id);
}
callback();
} else return;
},
fireHook: function(hook, args, callback) {
var _self = this
hookList = this.loadedHooks[hook];
};
Plugins.fireHook = function(hook, args, callback) {
hookList = Plugins.loadedHooks[hook];
if (hookList && Array.isArray(hookList)) {
//if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\'');
@ -187,9 +207,9 @@ var fs = require('fs'),
async.reduce(hookList, args, function(value, hookObj, next) {
if (hookObj.method) {
if (hookObj.callbacked) { // If a callback is present (asynchronous method)
hookObj.method.call(_self.libraries[hookObj.id], value, next);
hookObj.method.call(Plugins.libraries[hookObj.id], value, next);
} else { // Synchronous method
value = hookObj.method.call(_self.libraries[hookObj.id], value);
value = hookObj.method.call(Plugins.libraries[hookObj.id], value);
next(null, value);
}
} else {
@ -209,7 +229,7 @@ var fs = require('fs'),
case 'action':
async.each(hookList, function(hookObj) {
if (hookObj.method) {
hookObj.method.call(_self.libraries[hookObj.id], args);
hookObj.method.call(Plugins.libraries[hookObj.id], args);
} else {
if (global.env === 'development') {
winston.info('[plugins] Expected method \'' + hookObj.method + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
@ -228,12 +248,14 @@ var fs = require('fs'),
callback(null, returnVal);
}
}
},
isActive: function(id, callback) {
};
Plugins.isActive = function(id, callback) {
db.isSetMember('plugins:active', id, callback);
},
toggleActive: function(id, callback) {
this.isActive(id, function(err, active) {
};
Plugins.toggleActive = function(id, callback) {
Plugins.isActive(id, function(err, active) {
if (err) {
if (global.env === 'development') winston.info('[plugins] Could not toggle active state on plugin \'' + id + '\'');
return;
@ -259,9 +281,9 @@ var fs = require('fs'),
});
});
});
},
showInstalled: function(callback) {
var _self = this;
}
Plugins.showInstalled = function(callback) {
npmPluginPath = path.join(__dirname, '../node_modules');
async.waterfall([
@ -296,7 +318,7 @@ var fs = require('fs'),
return next(err, null);
}
_self.isActive(config.id, function(err, active) {
Plugins.isActive(config.id, function(err, active) {
if (err) next(new Error('no-active-state'));
delete config.library;
@ -320,8 +342,4 @@ var fs = require('fs'),
callback(err, plugins);
});
}
}
plugins.init();
module.exports = plugins;
}(exports));

@ -103,7 +103,7 @@ var bcrypt = require('bcrypt'),
if (email !== undefined) {
db.setObjectField('email:uid', email, uid);
//User.sendConfirmationEmail(email);
User.sendConfirmationEmail(email);
}
plugins.fireHook('action:user.create', {uid: uid, username: username, email: email, picture: gravatar, timestamp: timestamp});

Loading…
Cancel
Save