Permalink
Browse files

Replaced node-orm2 with jugglingdb due to build issues and broken-nes…

…s of code. NOTE: You may need to delete your existing subway.db for this to work!
  • Loading branch information...
1 parent 08d9a8b commit 0cd784b5244b8f327b7d6c354d010077a031a0eb @ericbarch ericbarch committed Jul 30, 2013
Showing with 129 additions and 120 deletions.
  1. +18 −16 lib/irclink.js
  2. +46 −44 lib/models.js
  3. +2 −2 lib/restore.js
  4. +31 −22 lib/socket.js
  5. +27 −31 lib/subway.js
  6. +5 −5 package.json
View
@@ -8,21 +8,21 @@ module.exports = function IRCLink(connID, app) {
this.created = new Date();
// establish db models
- var User = app.db.models.user;
- var Connection = app.db.models.connection;
- var Message = app.db.models.message;
- var PrivateMessage = app.db.models.pm;
- var Channel = app.db.models.channel;
+ var User = app.db.models.User;
+ var Connection = app.db.models.Connection;
+ var Message = app.db.models.Message;
+ var PrivateMessage = app.db.models.PM;
+ var Channel = app.db.models.Channel;
var instance = this;
// find the connection ID in the db and wake it up
- Connection.get(connID, function (err, userConn) {
+ Connection.find(connID, function (err, userConn) {
// that's not good, give up
if (err || userConn === null)
return;
- Channel.find({conn_id: connID}, function (err, userChans) {
+ Channel.all({conn_id: connID}, function (err, userChans) {
var channels = [];
for (var i=0; i<userChans.length; i++) {
channels.push(userChans[i].name);
@@ -81,24 +81,24 @@ module.exports = function IRCLink(connID, app) {
instance.logMessage = function(from, to, msg) {
if (!userConn.temporary) {
- Message.create([{
+ Message.create({
conn_id: connID
- , from: from
+ , by: from
, chan: to
, at: Date.now()
, msg: msg
- }], function (err, items) {});
+ });
}
};
instance.logPM = function(from, msg) {
if (!userConn.temporary) {
- PrivateMessage.create([{
+ PrivateMessage.create({
conn_id: connID
- , from: from
+ , by: from
, at: Date.now()
, msg: msg
- }], function (err, items) {});
+ });
}
};
@@ -169,17 +169,19 @@ module.exports = function IRCLink(connID, app) {
// add to db for connection
Channel.count({conn_id: connID, name: args.channel.toLowerCase()}, function (err, count) {
if (count === 0) {
- Channel.create([{
+ Channel.create({
conn_id: connID
, name: args.channel.toLowerCase()
- }], function (err, items) {});
+ });
}
});
break;
case 'part':
// remove from db for connection
- Channel.find({conn_id: connID, name: args.channel.toLowerCase()}).remove(function (err) {
+ Channel.findOne({conn_id: connID, name: args.channel.toLowerCase()}, function (err, chan) {
// boom
+ if (!err && chan)
+ chan.destroy();
});
break;
case 'message#':
View
@@ -1,55 +1,57 @@
-module.exports = function (db, cb) {
- db.define('user', {
- user_id: String
- , username: String
- , password: String
- , joined: Date
+module.exports = function (schema) {
+ schema.define('User', {
+ user_id: { type: String },
+ username: { type: String },
+ password: { type: String },
+ joined: { type: Date, default: function () { return new Date; } }
});
- db.define('connection', {
- user_id: Number
- , label: String // how the user has labeled this connection
- , hostname: String // host of the server
- , port: Number // port of the server
- , nick: String // nick to use on the server
- , away: String // away message on the server
- , ssl: Boolean // connect using ssl?
- , selfSigned: Boolean // allow self signed ssl certs
- , encoding: String
- , server_password: String // password for logging into server
- , nickserv_password: String // nickserv password for logging into
- , nickserv_enabled: Boolean // auth with nickserv?
- , sasl_enabled: Boolean
- , real_name: String // real name to set for connection
- , creation: Date // when this connection was created
- , disabled: Boolean // is this connection prevented from starting?
- , disabled_timeout: Date // do not allow the user to re-enable this connection until this date
- , disabled_reason: String // why is this connection disabled? (could be user-initiated)
- , keep_alive: Boolean // keep this connection active even if the user is disconnected
- , temporary: Boolean // this was created for a guest (no account)
+ schema.define('Connection', {
+ user_id: { type: String }
+ , label: { type: String } // how the user has labeled this connection
+ , hostname: { type: String } // host of the server
+ , port: { type: Number } // port of the server
+ , nick: { type: String } // nick to use on the server
+ , away: { type: String } // away message on the server
+ , ssl: { type: Boolean } // connect using ssl?
+ , selfSigned: { type: Boolean } // allow self signed ssl certs
+ , encoding: { type: String }
+ , server_password: { type: String } // password for logging into server
+ , nickserv_password: { type: String } // nickserv password for logging into
+ , nickserv_enabled: { type: Boolean } // auth with nickserv?
+ , sasl_enabled: { type: Boolean }
+ , real_name: { type: String } // real name to set for connection
+ , creation: { type: Date, default: function () { return new Date; } } // when this connection was created
+ , disabled: { type: Boolean } // is this connection prevented from starting?
+ , disabled_timeout: { type: Date } // do not allow the user to re-enable this connection until this date
+ , disabled_reason: { type: String } // why is this connection disabled? (could be user-initiated)
+ , keep_alive: { type: Boolean } // keep this connection active even if the user is disconnected
+ , temporary: { type: Boolean } // this was created for a guest (no account)
});
- db.define('message', {
- conn_id: Number
- , from: String
- , chan: String
- , at: Date
- , msg: String
+ schema.define('Message', {
+ conn_id: { type: Number },
+ by: { type: String },
+ chan: { type: String },
+ at: { type: Date, default: function () { return new Date; } },
+ msg: { type: String }
});
- db.define('pm', {
- conn_id: Number
- , from: String
- , at: Date
- , msg: String
+ schema.define('PM', {
+ conn_id: { type: Number },
+ by: { type: String },
+ at: { type: Date, default: function () { return new Date; } },
+ msg: { type: String }
});
- db.define('channel', {
- conn_id: Number
- , name: String
- , chanserv_enabled: Boolean
- , chanserv_password: String
+ schema.define('Channel', {
+ conn_id: { type: Number },
+ name: { type: String },
+ chanserv_enabled: { type: Boolean },
+ chanserv_password: { type: String }
});
- return cb();
+ schema.autoupdate();
+
+ return schema;
};
View
@@ -1,11 +1,11 @@
module.exports = function(app) {
// pull in the connection db model
- var Connection = app.db.models.connection;
+ var Connection = app.db.models.Connection;
// is restoration enabled?
if (app.config.restore_connections) {
// restore connections
- Connection.find({keep_alive: 1}, function (err, conns) {
+ Connection.all({where: { keep_alive: true }}, function(err, conns) {
if (!err) {
for (var i=0; i<conns.length; i++) {
app.ircbridge.emit('restore_irclink', conns[i].id);
View
@@ -14,11 +14,11 @@ module.exports = function(socket, app) {
socket.socketIORoom = null;
// establish db models
- var User = app.db.models.user;
- var Connection = app.db.models.connection;
- var Message = app.db.models.message;
- var PM = app.db.models.pm;
- var Channel = app.db.models.channel;
+ var User = app.db.models.User;
+ var Connection = app.db.models.Connection;
+ var Message = app.db.models.Message;
+ var PM = app.db.models.PM;
+ var Channel = app.db.models.Channel;
// signal an IRC connection belonging to the user
socket.signalIRC = function(connection, event, dict) {
@@ -33,27 +33,27 @@ module.exports = function(socket, app) {
// user registration
socket.on('register', function(data) {
// make sure user doesn't already exist
- User.count({ username: data.username }, function (err, count) {
- if (count === 0) {
+ User.findOne({where: { username: data.username }}, function (err, fUser) {
+ if (!err && !fUser) {
// hash the password
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(data.password, salt, null, function(err, hash) {
// create the new user
- User.create([{
+ User.create({
user_id: uuid.v1()
, username: data.username
, password: hash
, joined: Date.now()
- }], function (err, users) {
+ }, function (err, user) {
socket.emit('register_success', {username: data.username});
- socket.userID = users[0].user_id;
+ socket.userID = user.user_id;
// subscribe to all online IRC connections using socket.io room
socket.join(socket.userID);
socket.socketIORoom = socket.userID;
});
});
});
- } else {
+ } else {
socket.emit('register_error', {message: 'User exists.'});
}
});
@@ -62,20 +62,20 @@ module.exports = function(socket, app) {
// user login
socket.on('login', function(data){
// find the user
- User.find({username: data.username}, function(err, users) {
+ User.findOne({where: {username: data.username}}, function(err, user) {
// does the user exist?
- if(users[0]){
+ if(user){
// check password
- bcrypt.compare(data.password, users[0].password, function(err, res) {
+ bcrypt.compare(data.password, user.password, function(err, res) {
// if the password matched...
if(res === true){
- socket.userID = users[0].user_id;
+ socket.userID = user.user_id;
// subscribe to all online IRC connections using socket.io room
socket.join(socket.userID);
socket.socketIORoom = socket.userID;
// find all connections belonging to the user
- Connection.find({ user_id: socket.userID }, function (err, connections) {
+ Connection.all({where: { user_id: socket.userID } }, function (err, connections) {
var exists = false;
if (connections.length > 0) {
exists = true;
@@ -121,7 +121,7 @@ module.exports = function(socket, app) {
socket.socketIORoom = user_id;
}
- Connection.create([{
+ Connection.create({
user_id: user_id
, label: data.server
, hostname: data.server
@@ -142,11 +142,11 @@ module.exports = function(socket, app) {
, disabled_reason: ''
, keep_alive: keepAlive
, temporary: temp
- }], function (err, items) {
+ }, function (err, item) {
if (!err) {
// wake up the new connection
- socket.connID = items[0].id;
- socket.conn = items[0];
+ socket.connID = item.id;
+ socket.conn = item;
app.ircbridge.emit('restore_irclink', socket.connID);
socket.signalIRC(socket.connID, 'restore');
}
@@ -240,11 +240,20 @@ module.exports = function(socket, app) {
// make sure we're actually connected
if (socket.connID !== -1 && socket.userID !== null) {
// find requested messages
- Message.find({conn_id: socket.connID, chan:data.channelName.toLowerCase()}, { offset: data.skip }, data.amount, [ "at", "Z" ], function (err, messages) {
+ Message.all({where: {conn_id: socket.connID, chan:data.channelName.toLowerCase()}, order: 'at DESC', limit: data.amount, skip: data.skip}, function (err, messages) {
+ var parsedMessages = [];
+
if (!err) {
+ // frontend expects 'by' field as 'from'
+ for (var i=0; i<messages.length; i++) {
+ var msg = messages[i].toObject();
+ msg['from'] = msg['by'];
+ parsedMessages.push(msg);
+ }
+
var returnData = {};
returnData.name = data.channelName.toLowerCase();
- returnData.messages = messages;
+ returnData.messages = parsedMessages;
socket.emit('oldMessages', returnData);
}
});
View
@@ -4,10 +4,10 @@
var config = require('../config')
, events = require("events")
+ , models = require("./models")
, app = require('./webserver').app
, server = require('./webserver').server
, io = require('socket.io')
- , orm = require("orm")
, path = require("path");
@@ -27,37 +27,33 @@ var Subway = function() {
Subway.prototype.start = function () {
var instance = this;
- // get the db rolling
- orm.connect({ protocol: "sqlite", pathname: config.sqlite_path }, function (err, db) {
- // load in models
- db.load(path.join(__dirname, "/models"), function (err) {
- // create tables if needed
- db.sync(function (err) {
- // map ORM to app object
- instance.app.db = db;
-
- // link up socket.io with our express app
- instance.app.io = io.listen(server);
-
- // setup the IRC manager
- require('./ircmanager')(app);
-
- // setup the socket API
- var socketHandler = require('./socket');
- instance.app.io.sockets.on('connection', function(socket) {
- socketHandler(socket, app);
- });
-
- // restore connections
- require('./restore')(app);
-
- // debug
- if (server.address()) {
- console.log('Subway started on port %s', server.address().port);
- }
- });
- });
+ var Schema = require('jugglingdb').Schema;
+ var schema = new Schema('sqlite3', {
+ database: 'subway.db'
});
+
+ // load in models, map ORM to app object
+ instance.app.db = models(schema);
+
+ // link up socket.io with our express app
+ instance.app.io = io.listen(server);
+
+ // setup the IRC manager
+ require('./ircmanager')(app);
+
+ // setup the socket API
+ var socketHandler = require('./socket');
+ instance.app.io.sockets.on('connection', function(socket) {
+ socketHandler(socket, app);
+ });
+
+ // restore connections
+ require('./restore')(app);
+
+ // debug
+ if (server.address()) {
+ console.log('Subway started on port %s', server.address().port);
+ }
}
module.exports = Subway;
Oops, something went wrong.

0 comments on commit 0cd784b

Please sign in to comment.