diff options
52 files changed, 234 insertions, 266 deletions
diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..6c46b78 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +media +migrootions diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..85c4a70 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,21 @@ +{ + "env": { + "node": true + }, + + "rules": { + "no-underscore-dangle": 0, + "global-strict": [2, "always"], + "quotes": [2, "single"], + "strict": [2, "global"], + + "consistent-return": 0, + + // Disable certain checks until we have time to fix these issues + "no-shadow": 0, + "camelcase": 0, + "no-path-concat": 0, + "no-process-exit": 0, + "new-cap": 0 + } +} diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 7f64af7..0000000 --- a/.jshintrc +++ /dev/null @@ -1,66 +0,0 @@ -{ - // Settings - "passfail" : false, // Stop on first error. - "maxerr" : 20, // Maximum error before stopping. - - - // Predefined globals whom JSHint will ignore. - "browser" : true, // Standard browser globals e.g. `window`, `document`. - - "node" : true, - "rhino" : false, - "couch" : false, - "wsh" : false, // Windows Scripting Host. - - "jquery" : true, - "prototypejs" : false, - "mootools" : false, - "dojo" : false, - - // Development. - "debug" : false, // Allow debugger statements e.g. browser breakpoints. - "devel" : true, // Allow developments statements e.g. `console.log();`. - - - // ECMAScript 5. - "es5" : true, // Allow ECMAScript 5 syntax. - "strict" : false, // Require `use strict` pragma in every file. - "globalstrict" : false, // Allow global "use strict" (also enables 'strict'). - - - // The Good Parts. - "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons). - "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons. - "bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.). - "boss" : false, // Tolerate assignments inside if, for & while. - "curly" : true, // Require {} for every new block or scope. - "eqeqeq" : true, // Require triple equals i.e. `===`. - "eqnull" : false, // Tolerate use of `== null`. - "evil" : false, // Tolerate use of `eval`. - "expr" : false, // Tolerate `ExpressionStatement` as Programs. - "forin" : false, // Tolerate `for in` loops without `hasOwnPrototype`. - "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` - "latedef" : true, // Prohibit variable use before definition. - "loopfunc" : false, // Allow functions to be defined within loops. - "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`. - "regexp" : true, // Prohibit unsafe `.` and `[^...]` in regular expressions. - "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`. - "scripturl" : false, // Tolerate script-targeted URLs. - "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`. - "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`. - "undef" : true, // Require all non-global variables be declared before they are used. - - - // Personal styling preferences. - "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`. - "noempty" : true, // Prohibit use of empty blocks. - "nonew" : false, // Prohibit use of constructors for side-effects. - "nomen" : false, // Prohibit use of initial or trailing underbars in names. - "onevar" : false, // Allow only one `var` statement per function. - "plusplus" : false, // Prohibit use of `++` & `--`. - "sub" : false, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`. - "trailing" : true, // Prohibit trailing whitespaces. - "white" : true, // Check against strict whitespace and indentation rules. - "indent" : 4 // Specify indentation spacing -} - diff --git a/Gruntfile.js b/Gruntfile.js index 082ccff..272eccf 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -2,6 +2,8 @@ // Gruntfile // +'use strict'; + module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), @@ -12,7 +14,7 @@ module.exports = function(grunt) { cleanTargetDir: true, verbose: true, targetDir: 'media/js/vendor', - layout: 'byComponent', + layout: 'byComponent' } } } @@ -4,11 +4,12 @@ 'use strict'; -process.title = "letschat"; +process.title = 'letschat'; + +require('colors'); var _ = require('lodash'), fs = require('fs'), - colors = require('colors'), express = require('express.oi'), bodyParser = require('body-parser'), cookieParser = require('cookie-parser'), @@ -18,19 +19,19 @@ var _ = require('lodash'), nunjucks = require('nunjucks'), mongoose = require('mongoose'), migroose = require('./migroose'), - MongoStore = require('connect-mongo')(express.session), - all = require('require-tree'); - -var psjon = require('./package.json'), + connectMongo = require('connect-mongo'), + all = require('require-tree'), + psjon = require('./package.json'), settings = require('./app/config'), - httpEnabled = settings.http && settings.http.enable, - httpsEnabled = settings.https && settings.https.enable; + auth = require('./app/auth/index'), + core = require('./app/core/index'); -var auth = require('./app/auth/index'), +var MongoStore = connectMongo(express.session), + httpEnabled = settings.http && settings.http.enable, + httpsEnabled = settings.https && settings.https.enable, models = all('./app/models'), middlewares = all('./app/middlewares'), controllers = all('./app/controllers'), - core = require('./app/core/index'), app; // @@ -103,9 +104,7 @@ var bundles = {}; app.use(require('connect-assets')({ paths: [ 'media/js', - 'media/less', - // 'media/img', - // 'media/font', + 'media/less' ], helperContext: bundles, build: settings.env === 'production', @@ -115,7 +114,7 @@ app.use(require('connect-assets')({ // Public app.use('/media', express.static(__dirname + '/media', { - maxAge: '364d', + maxAge: '364d' })); // Templates @@ -253,8 +252,12 @@ mongoose.connect(settings.database.uri, function(err) { checkForMongoTextSearch(); - migroose.needsMigration(function(migrationRequired) { - if (migrationRequired) { + migroose.needsMigration(function(err, migrationRequired) { + if (err) { + console.error(err); + } + + else if (migrationRequired) { console.log('Database migration required'.red); console.log('Ensure you backup your database first.'); console.log(''); diff --git a/app/auth/index.js b/app/auth/index.js index 6f95e46..8ffcfd3 100644 --- a/app/auth/index.js +++ b/app/auth/index.js @@ -1,6 +1,7 @@ +'use strict'; + var _ = require('lodash'), async = require('async'), - express = require('express.oi'), cookieParser = require('cookie-parser'), mongoose = require('mongoose'), passport = require('passport'), @@ -8,8 +9,9 @@ var _ = require('lodash'), BearerStrategy = require('passport-http-bearer'), BasicStrategy = require('passport-http').BasicStrategy, settings = require('./../config'), - plugins = require('./../plugins'), - providerSettings = {}, + plugins = require('./../plugins'); + +var providerSettings = {}, MAX_AUTH_DELAY_TIME = 24 * 60 * 60 * 1000, loginAttempts = {}, enabledProviders = []; @@ -86,8 +88,8 @@ function setup(app, session, core) { } socket.request.user = user; - socket.request.user.logged_in = true; - socket.request.user.using_token = true; + socket.request.user.loggedIn = true; + socket.request.user.usingToken = true; next(); }); } else { @@ -122,7 +124,7 @@ function wrapAuthCallback(username, cb) { attempt.attempts++; if (attempt.attempts >= settings.auth.throttling.threshold) { - var lock = Math.min(5000 * Math.pow(2,(attempt.attempts - settings.auth.throttling.threshold), MAX_AUTH_DELAY_TIME)); + var lock = Math.min(5000 * Math.pow(2, (attempt.attempts - settings.auth.throttling.threshold), MAX_AUTH_DELAY_TIME)); attempt.lockedUntil = Date.now() + lock; return cb(err, user, { locked: true, @@ -204,7 +206,7 @@ function authenticate() { return callback(null, args[0]); } - provider.authenticate(req, function(err, user, info) { + provider.authenticate(req, function(err, user) { if (err) { return callback(err); } diff --git a/app/auth/local.js b/app/auth/local.js index 029173f..15854cf 100644 --- a/app/auth/local.js +++ b/app/auth/local.js @@ -1,3 +1,5 @@ +'use strict'; + var mongoose = require('mongoose'), passport = require('passport'), LocalStrategy = require('passport-local').Strategy; @@ -17,7 +19,7 @@ Local.prototype.setup = function() { var User = mongoose.model('User'); User.authenticate(identifier, password, function(err, user) { if (err) { - return done(null, false, { + return done(null, false, { message: 'Some fields did not validate.' }); } diff --git a/app/config.js b/app/config.js index dc34c8f..9842316 100644 --- a/app/config.js +++ b/app/config.js @@ -1,3 +1,5 @@ +'use strict'; + var _ = require('lodash'), fs = require('fs'), yaml = require('js-yaml'), @@ -28,13 +30,13 @@ var pipeline = [ function getDefaultSettings(context) { var file = fs.readFileSync('defaults.yml', 'utf8'); - context.defaults = yaml.safeLoad(file); + context.defaults = yaml.safeLoad(file); }, function getFileSettings(context) { if (fs.existsSync('settings.yml')) { var file = fs.readFileSync('settings.yml', 'utf8'); - context.file = yaml.safeLoad(file) || {}; + context.file = yaml.safeLoad(file) || {}; } else { context.file = {}; } diff --git a/app/controllers/account.js b/app/controllers/account.js index 63ba26d..59819fd 100644 --- a/app/controllers/account.js +++ b/app/controllers/account.js @@ -6,7 +6,6 @@ var _ = require('lodash'), fs = require('fs'), - passport = require('passport'), auth = require('./../auth/index'), path = require('path'), settings = require('./../config'); @@ -15,9 +14,7 @@ module.exports = function() { var app = this.app, core = this.core, - middlewares = this.middlewares, - models = this.models, - User = models.user; + middlewares = this.middlewares; core.on('account:update', function(data) { app.io.emit('users:update', data.user); @@ -50,31 +47,31 @@ module.exports = function() { res.redirect('/login'); }); - app.post('/account/login', function(req, res) { + app.post('/account/login', function(req) { req.io.route('account:login'); }); - app.post('/account/register', function(req, res) { + app.post('/account/register', function(req) { req.io.route('account:register'); }); - app.get('/account', middlewares.requireLogin, function(req, res) { + app.get('/account', middlewares.requireLogin, function(req) { req.io.route('account:whoami'); }); - app.post('/account/profile', middlewares.requireLogin, function(req, res) { + app.post('/account/profile', middlewares.requireLogin, function(req) { req.io.route('account:profile'); }); - app.post('/account/settings', middlewares.requireLogin, function(req, res) { + app.post('/account/settings', middlewares.requireLogin, function(req) { req.io.route('account:settings'); }); - app.post('/account/token/generate', middlewares.requireLogin, function(req, res) { + app.post('/account/token/generate', middlewares.requireLogin, function(req) { req.io.route('account:generate_token'); }); - app.post('/account/token/revoke', middlewares.requireLogin, function(req, res) { + app.post('/account/token/revoke', middlewares.requireLogin, function(req) { req.io.route('account:revoke_token'); }); @@ -110,7 +107,7 @@ module.exports = function() { }); }, settings: function(req, res) { - if (req.user.using_token) { + if (req.user.usingToken) { return res.status(403).json({ status: 'error', message: 'Cannot change account settings ' + @@ -160,7 +157,7 @@ module.exports = function() { }); }, generate_token: function(req, res) { - if (req.user.using_token) { + if (req.user.usingToken) { return res.status(403).json({ status: 'error', message: 'Cannot generate a new token ' + @@ -185,7 +182,7 @@ module.exports = function() { }); }, revoke_token: function(req, res) { - if (req.user.using_token) { + if (req.user.usingToken) { return res.status(403).json({ status: 'error', message: 'Cannot revoke token ' + @@ -242,7 +239,7 @@ module.exports = function() { displayName: fields.displayName || fields.displayname || fields['display-name'] }; - core.account.create('local', data, function(err, user) { + core.account.create('local', data, function(err) { if (err) { var message = 'Sorry, we could not process your request'; // User already exists diff --git a/app/controllers/connections.js b/app/controllers/connections.js index 1fcdb1d..810c677 100644 --- a/app/controllers/connections.js +++ b/app/controllers/connections.js @@ -6,26 +6,22 @@ module.exports = function() {
- var _ = require('lodash');
-
var app = this.app,
core = this.core,
- middlewares = this.middlewares,
- models = this.models,
- User = models.user;
+ middlewares = this.middlewares;
//
// Routes
//
- app.get('/connections', middlewares.requireLogin, function(req, res) {
+ app.get('/connections', middlewares.requireLogin, function(req) {
req.io.route('connections:list');
});
- app.get('/connections/type/:type', middlewares.requireLogin, function(req, res) {
+ app.get('/connections/type/:type', middlewares.requireLogin, function(req) {
req.io.route('connections:list');
});
- app.get('/connections/user/:user', middlewares.requireLogin, function(req, res) {
+ app.get('/connections/user/:user', middlewares.requireLogin, function(req) {
req.io.route('connections:list');
});
diff --git a/app/controllers/extras.js b/app/controllers/extras.js index 5ead7a9..228b0f3 100644 --- a/app/controllers/extras.js +++ b/app/controllers/extras.js @@ -13,23 +13,22 @@ module.exports = function() { express = require('express.oi'); var app = this.app, - core = this.core, middlewares = this.middlewares; // // Routes // - app.get('/extras/emotes', middlewares.requireLogin, function(req, res) { + app.get('/extras/emotes', middlewares.requireLogin, function(req) { req.io.route('extras:emotes:list'); }); app.use('/extras/emotes/', - express.static(path.join(process.cwd(), 'extras/emotes/public'), { - maxage: '364d' - }) + express.static(path.join(process.cwd(), 'extras/emotes/public'), { + maxage: '364d' + }) ); - app.get('/extras/replacements', middlewares.requireLogin, function(req, res) { + app.get('/extras/replacements', middlewares.requireLogin, function(req) { req.io.route('extras:replacements:list'); }); @@ -42,6 +41,10 @@ module.exports = function() { var dir = path.join(process.cwd(), 'extras/emotes'); fs.readdir(dir, function(err, files) { + if (err) { + return res.json(emotes); + } + var regex = new RegExp('\\.yml$'); files = files.filter(function(fileName) { diff --git a/app/controllers/files.js b/app/controllers/files.js index d87ce6e..008ac05 100644 --- a/app/controllers/files.js +++ b/app/controllers/files.js @@ -4,10 +4,7 @@ 'use strict';
-var fs = require('fs'),
- _ = require('lodash'),
- async = require('async'),
- multer = require('multer'),
+var multer = require('multer'),
settings = require('./../config').files;
module.exports = function() {
@@ -19,8 +16,7 @@ module.exports = function() { var app = this.app,
core = this.core,
middlewares = this.middlewares,
- models = this.models,
- Room = models.room;
+ models = this.models;
core.on('files:new', function(file, room, user) {
var fil = file.toJSON();
@@ -44,19 +40,19 @@ module.exports = function() { //
app.route('/files')
.all(middlewares.requireLogin)
- .get(function(req, res) {
+ .get(function(req) {
req.io.route('files:list');
})
- .post(fileUpload, middlewares.cleanupFiles, function(req, res) {
+ .post(fileUpload, middlewares.cleanupFiles, function(req) {
req.io.route('files:create');
});
app.route('/rooms/:room/files')
.all(middlewares.requireLogin, middlewares.roomRoute)
- .get(function(req, res) {
+ .get(function(req) {
req.io.route('files:list');
})
- .post(fileUpload, middlewares.cleanupFiles, function(req, res) {
+ .post(fileUpload, middlewares.cleanupFiles, function(req) {
req.io.route('files:create');
});
diff --git a/app/controllers/messages.js b/app/controllers/messages.js index 9f0b182..fa1f306 100644 --- a/app/controllers/messages.js +++ b/app/controllers/messages.js @@ -8,9 +8,7 @@ module.exports = function() { var app = this.app,
core = this.core,
- middlewares = this.middlewares,
- models = this.models,
- Room = models.room;
+ middlewares = this.middlewares;
core.on('messages:new', function(message, room, user) {
var msg = message.toJSON();
@@ -26,19 +24,19 @@ module.exports = function() { //
app.route('/messages')
.all(middlewares.requireLogin)
- .get(function(req, res) {
+ .get(function(req) {
req.io.route('messages:list');
})
- .post(function(req, res) {
+ .post(function(req) {
req.io.route('messages:create');
});
app.route('/rooms/:room/messages')
.all(middlewares.requireLogin, middlewares.roomRoute)
- .get(function(req, res) {
+ .get(function(req) {
req.io.route('messages:list');
})
- .post(function(req, res) {
+ .post(function(req) {
req.io.route('messages:create');
});
@@ -66,7 +64,7 @@ module.exports = function() { since_id: req.param('since_id'),
from: req.param('from'),
to: req.param('to'),
- query: req.param("query"),
+ query: req.param('query'),
reverse: req.param('reverse'),
skip: req.param('skip'),
take: req.param('take'),
diff --git a/app/controllers/misc.js b/app/controllers/misc.js index 8e81298..e2c1a29 100644 --- a/app/controllers/misc.js +++ b/app/controllers/misc.js @@ -18,7 +18,7 @@ module.exports = function() { if (!settings.noRobots) { return res.sendStatus(404); } - + res.sendFile(path.resolve(__dirname, '../misc/robots.txt')); }); diff --git a/app/controllers/rooms.js b/app/controllers/rooms.js index 749d4bb..ac72255 100644 --- a/app/controllers/rooms.js +++ b/app/controllers/rooms.js @@ -11,22 +11,25 @@ module.exports = function() { core = this.core,
middlewares = this.middlewares,
models = this.models,
- Room = models.room,
User = models.user;
core.on('presence:user_join', function(data) {
User.findById(data.userId, function (err, user) {
- user = user.toJSON();
- user.room = data.roomId;
- app.io.emit('users:join', user);
+ if (!err && user) {
+ user = user.toJSON();
+ user.room = data.roomId;
+ app.io.emit('users:join', user);
+ }
});
});
core.on('presence:user_leave', function(data) {
User.findById(data.userId, function (err, user) {
- user = user.toJSON();
- user.room = data.roomId;
- app.io.emit('users:leave', user);
+ if (!err && user) {
+ user = user.toJSON();
+ user.room = data.roomId;
+ app.io.emit('users:leave', user);
+ }
});
});
@@ -48,28 +51,28 @@ module.exports = function() { //
app.route('/rooms')
.all(middlewares.requireLogin)
- .get(function(req, res) {
+ .get(function(req) {
req.io.route('rooms:list');
})
- .post(function(req, res) {
+ .post(function(req) {
req.io.route('rooms:create');
});
app.route('/rooms/:room')
.all(middlewares.requireLogin, middlewares.roomRoute)
- .get(function(req, res) {
+ .get(function(req) {
req.io.route('rooms:get');
})
- .put(function(req, res) {
+ .put(function(req) {
req.io.route('rooms:update');
})
- .delete(function(req, res) {
+ .delete(function(req) {
req.io.route('rooms:archive');
});
app.route('/rooms/:room/users')
.all(middlewares.requireLogin, middlewares.roomRoute)
- .get(function(req, res) {
+ .get(function(req) {
req.io.route('rooms:users');
});
diff --git a/app/controllers/transcript.js b/app/controllers/transcript.js index fb5de38..44d0155 100644 --- a/app/controllers/transcript.js +++ b/app/controllers/transcript.js @@ -4,15 +4,10 @@ 'use strict';
-var _ = require('lodash');
-
module.exports = function() {
var app = this.app,
core = this.core,
- middlewares = this.middlewares,
- models = this.models,
- Room = models.room,
- User = models.user;
+ middlewares = this.middlewares;
//
// Routes
@@ -20,10 +15,15 @@ module.exports = function() { app.get('/transcript', middlewares.requireLogin, function(req, res) {
var roomId = req.param('room');
core.rooms.get(roomId, function(err, room) {
- if (err || !room) {
- err && console.error(err)
+ if (err) {
+ console.error(err);
return res.sendStatus(404);
}
+
+ if (!room) {
+ return res.sendStatus(404);
+ }
+
res.render('transcript.html', {
room: {
id: roomId,
@@ -32,4 +32,4 @@ module.exports = function() { });
});
});
-}
+};
diff --git a/app/controllers/users.js b/app/controllers/users.js index 903e3d6..e44c050 100644 --- a/app/controllers/users.js +++ b/app/controllers/users.js @@ -6,11 +6,9 @@ module.exports = function() {
- var _ = require('lodash'),
- helpers = require('./../core/helpers');
+ var helpers = require('./../core/helpers');
var app = this.app,
- core = this.core,
middlewares = this.middlewares,
models = this.models,
User = models.user;
@@ -18,11 +16,11 @@ module.exports = function() { //
// Routes
//
- app.get('/users', middlewares.requireLogin, function(req, res) {
+ app.get('/users', middlewares.requireLogin, function(req) {
req.io.route('users:list');
});
- app.get('/users/:id', middlewares.requireLogin, function(req, res) {
+ app.get('/users/:id', middlewares.requireLogin, function(req) {
req.io.route('users:get');
});
diff --git a/app/core/files.js b/app/core/files.js index f9ef61d..a51d859 100644 --- a/app/core/files.js +++ b/app/core/files.js @@ -1,12 +1,12 @@ 'use strict'; -var fs = require('fs'), - _ = require('lodash'), +var _ = require('lodash'), mongoose = require('mongoose'), helpers = require('./helpers'), plugins = require('./../plugins'), - settings = require('./../config').files, - enabled = settings.enable; + settings = require('./../config').files; + +var enabled = settings.enable; function FileManager(options) { this.core = options.core; @@ -32,8 +32,8 @@ FileManager.prototype.create = function(options, cb) { } var File = mongoose.model('File'), - Room = mongoose.model('Room'), - User = mongoose.model('User'); + Room = mongoose.model('Room'), + User = mongoose.model('User'); if (settings.restrictTypes && settings.allowedTypes && @@ -63,11 +63,16 @@ FileManager.prototype.create = function(options, cb) { size: options.file.size, room: options.room }).save(function(err, savedFile) { + if (err) { + return cb(err); + } + this.provider.save({file: options.file, doc: savedFile}, function(err) { if (err) { savedFile.remove(); return cb(err); } + // Temporary workaround for _id until populate can do aliasing User.findOne(options.owner, function(err, user) { if (err) { @@ -111,8 +116,7 @@ FileManager.prototype.list = function(options, cb) { maxTake: 5000 }); - var File = mongoose.model('File'), - User = mongoose.model('User'); + var File = mongoose.model('File'); var find = File.find({ room: options.room diff --git a/app/core/files/local.js b/app/core/files/local.js index 9cc494d..bedc530 100644 --- a/app/core/files/local.js +++ b/app/core/files/local.js @@ -1,3 +1,5 @@ +'use strict'; + var fs = require('fs'), path = require('path'); diff --git a/app/core/helpers.js b/app/core/helpers.js index 94fb85a..18a29e5 100644 --- a/app/core/helpers.js +++ b/app/core/helpers.js @@ -1,3 +1,5 @@ +'use strict'; + module.exports = { sanitizeQuery: function(query, options) { if (options.defaults.take && !query.take) { diff --git a/app/core/messages.js b/app/core/messages.js index 8c2d893..7165161 100644 --- a/app/core/messages.js +++ b/app/core/messages.js @@ -38,7 +38,11 @@ MessageManager.prototype.create = function(options, cb) { console.error(err); return cb(err); } - typeof cb === 'function' && cb(null, message, room, user); + + if (typeof cb === 'function') { + cb(null, message, room, user); + } + this.core.emit('messages:new', message, room, user); }.bind(this)); }.bind(this)); @@ -60,8 +64,7 @@ MessageManager.prototype.list = function(options, cb) { maxTake: 5000 }); - var Message = mongoose.model('Message'), - User = mongoose.model('User'); + var Message = mongoose.model('Message'); var find = Message.find({ room: options.room diff --git a/app/core/presence.js b/app/core/presence.js index 08711e5..17b3533 100644 --- a/app/core/presence.js +++ b/app/core/presence.js @@ -1,7 +1,6 @@ 'use strict'; -var _ = require('lodash'), - Connection = require('./presence/connection'), +var Connection = require('./presence/connection'), ConnectionCollection = require('./presence/connection-collection'), RoomCollection = require('./presence/room-collection'), UserCollection = require('./presence/user-collection'); diff --git a/app/core/presence/connection-collection.js b/app/core/presence/connection-collection.js index f4a3e41..3adb2c4 100644 --- a/app/core/presence/connection-collection.js +++ b/app/core/presence/connection-collection.js @@ -1,8 +1,6 @@ 'use strict'; -var EventEmitter = require('events').EventEmitter, - util = require('util'), - _ = require('lodash'); +var _ = require('lodash'); function ConnectionCollection() { this.connections = {}; @@ -58,7 +56,7 @@ ConnectionCollection.prototype.query = function(options) { options.userId = options.userId.toString(); } - return _.map(this.connections, function(value, key) { + return _.map(this.connections, function(value) { return value; }).filter(function(conn) { var result = true; diff --git a/app/core/presence/room.js b/app/core/presence/room.js index 07f1cee..985f78e 100644 --- a/app/core/presence/room.js +++ b/app/core/presence/room.js @@ -2,7 +2,6 @@ var EventEmitter = require('events').EventEmitter, util = require('util'), - _ = require('lodash'), ConnectionCollection = require('./connection-collection'); function Room(roomId, roomSlug) { diff --git a/app/core/presence/user-collection.js b/app/core/presence/user-collection.js index e5187ce..0ecbf8d 100644 --- a/app/core/presence/user-collection.js +++ b/app/core/presence/user-collection.js @@ -1,8 +1,6 @@ 'use strict'; -var EventEmitter = require('events').EventEmitter, - util = require('util'), - _ = require('lodash'); +var _ = require('lodash'); function UserCollection() { this.users = {}; diff --git a/app/middlewares/cleanupFiles.js b/app/middlewares/cleanupFiles.js index cc99740..437b8df 100644 --- a/app/middlewares/cleanupFiles.js +++ b/app/middlewares/cleanupFiles.js @@ -1,3 +1,5 @@ +'use strict'; + var fs = require('fs'), _ = require('lodash'), async = require('async'), @@ -9,7 +11,7 @@ function cleanupReqFiles(req, cb) { } var files = _.chain(req.files) - .map(function(x) { return x;}) + .map(function(x) { return x; }) .flatten() .value(); @@ -29,11 +31,11 @@ function cleanupReqFiles(req, cb) { } module.exports = function(req, res, next) { - res.on('error', function(err) { + res.on('error', function() { cleanupReqFiles(req); }); - onFinished(res, function (err, res) { + onFinished(res, function () { cleanupReqFiles(req); }); diff --git a/app/middlewares/requireLogin.js b/app/middlewares/requireLogin.js index 8040434..5d7785e 100644 --- a/app/middlewares/requireLogin.js +++ b/app/middlewares/requireLogin.js @@ -17,7 +17,6 @@ function getMiddleware(fail) { var parts = req.headers.authorization.split(' '); if (parts.length === 2) { var scheme = parts[0], - credentials = parts[1], auth; if (/^Bearer$/i.test(scheme)) { diff --git a/app/models/file.js b/app/models/file.js index c9155e8..f04e01d 100644 --- a/app/models/file.js +++ b/app/models/file.js @@ -1,7 +1,7 @@ 'use strict'; -var mongoose = require('mongoose'), - Schema = mongoose.Schema, +var mongoose = require('mongoose'); +var Schema = mongoose.Schema, ObjectId = Schema.ObjectId; var FileSchema = new Schema({ @@ -34,7 +34,7 @@ var FileSchema = new Schema({ } }); -FileSchema.virtual('url').get(function(file) { +FileSchema.virtual('url').get(function() { return 'files/' + this._id + '/' + encodeURIComponent(this.name); }); diff --git a/app/models/message.js b/app/models/message.js index 5ab18fb..80bea08 100644 --- a/app/models/message.js +++ b/app/models/message.js @@ -4,8 +4,8 @@ 'use strict'; -var mongoose = require('mongoose'), - ObjectId = mongoose.Schema.Types.ObjectId; +var mongoose = require('mongoose'); +var ObjectId = mongoose.Schema.Types.ObjectId; var MessageSchema = new mongoose.Schema({ room: { diff --git a/app/models/room.js b/app/models/room.js index 86ad322..d36157f 100644 --- a/app/models/room.js +++ b/app/models/room.js @@ -5,9 +5,9 @@ 'use strict'; var mongoose = require('mongoose'), - ObjectId = mongoose.Schema.Types.ObjectId, - uniqueValidator = require('mongoose-unique-validator'), - settings = require('./../config'); + uniqueValidator = require('mongoose-unique-validator'); + +var ObjectId = mongoose.Schema.Types.ObjectId; var RoomSchema = new mongoose.Schema({ slug: { diff --git a/app/models/user.js b/app/models/user.js index 5190d1e..d065ab0 100644 --- a/app/models/user.js +++ b/app/models/user.js @@ -8,14 +8,13 @@ var bcrypt = require('bcryptjs'), crypto = require('crypto'), md5 = require('MD5'), hash = require('node_hash'), - settings = require('./../config'); - -var mongoose = require('mongoose'), - ObjectId = mongoose.Schema.Types.ObjectId, + mongoose = require('mongoose'), uniqueValidator = require('mongoose-unique-validator'), validate = require('mongoose-validate'), settings = require('./../config'); +var ObjectId = mongoose.Schema.Types.ObjectId; + var UserSchema = new mongoose.Schema({ provider: { type: String, @@ -190,6 +189,10 @@ UserSchema.statics.findByToken = function(token, cb) { } bcrypt.compare(hash, user.token, function(err, isMatch) { + if (err) { + return cb(err); + } + if (isMatch) { return cb(null, user); } @@ -201,6 +204,10 @@ UserSchema.statics.findByToken = function(token, cb) { UserSchema.methods.comparePassword = function(password, cb) { bcrypt.compare(password, this.password, function(err, isMatch) { + if (err) { + return cb(err); + } + if (isMatch) { return cb(null, true); } diff --git a/app/plugins.js b/app/plugins.js index 9520a47..b567c0b 100644 --- a/app/plugins.js +++ b/app/plugins.js @@ -1,3 +1,5 @@ +'use strict'; + function PluginManager() { this.types = [ 'auth', @@ -6,8 +8,8 @@ function PluginManager() { } PluginManager.prototype.getPlugin = function(key, type) { - var name = 'lets-chat-' + key, - plugin = require(name); + var name = 'lets-chat-' + key; + var plugin = require(name); if (!type) { return plugin; diff --git a/app/xmpp/event-listener.js b/app/xmpp/event-listener.js index c869700..a886853 100644 --- a/app/xmpp/event-listener.js +++ b/app/xmpp/event-listener.js @@ -1,9 +1,6 @@ 'use strict'; -var xmpp = require('node-xmpp-server'), - Stanza = require('node-xmpp-core').Stanza, - mongoose = require('mongoose'), - settings = require('./../config'), +var settings = require('./../config'), _ = require('lodash'), util = require('util'); diff --git a/app/xmpp/events/room-archived.js b/app/xmpp/events/room-archived.js index c3532ea..1806062 100644 --- a/app/xmpp/events/room-archived.js +++ b/app/xmpp/events/room-archived.js @@ -21,7 +21,7 @@ module.exports = EventListener.extend({ var x = presence .c('x', { - xmlns:'http://jabber.org/protocol/muc#user' + xmlns: 'http://jabber.org/protocol/muc#user' }); x.c('item', { diff --git a/app/xmpp/events/user-join.js b/app/xmpp/events/user-join.js index 58f6612..ed4d12a 100644 --- a/app/xmpp/events/user-join.js +++ b/app/xmpp/events/user-join.js @@ -18,7 +18,7 @@ module.exports = EventListener.extend({ presence .c('x', { - xmlns:'http://jabber.org/protocol/muc#user' + xmlns: 'http://jabber.org/protocol/muc#user' }) .c('item', { jid: connection.getUserJid(data.username), diff --git a/app/xmpp/index.js b/app/xmpp/index.js index 85837e1..66f43e4 100644 --- a/app/xmpp/index.js +++ b/app/xmpp/index.js @@ -1,19 +1,17 @@ 'use strict'; var xmpp = require('node-xmpp-server'), - Stanza = require('node-xmpp-core').Stanza, - mongoose = require('mongoose'), settings = require('./../config'), auth = require('./../auth/index'), all = require('require-tree'), - allArray = function(path) { + XmppConnection = require('./xmpp-connection'); + +var allArray = function(path) { var modules = all(path); return Object.keys(modules).map(function(key) { return modules[key]; }); - }; - -var XmppConnection = require('./xmpp-connection'), + }, msgProcessors = allArray('./msg-processors'), eventListeners = allArray('./events'); diff --git a/app/xmpp/msg-processor.js b/app/xmpp/msg-processor.js index b9ec0e8..3d6ab58 100644 --- a/app/xmpp/msg-processor.js +++ b/app/xmpp/msg-processor.js @@ -1,8 +1,6 @@ 'use strict'; -var xmpp = require('node-xmpp-server'), - Stanza = require('node-xmpp-core').Stanza, - mongoose = require('mongoose'), +var Stanza = require('node-xmpp-core').Stanza, settings = require('./../config'), _ = require('lodash'), util = require('util'); diff --git a/app/xmpp/msg-processors/conf-info.js b/app/xmpp/msg-processors/conf-info.js index e623e26..7c5402d 100644 --- a/app/xmpp/msg-processors/conf-info.js +++ b/app/xmpp/msg-processors/conf-info.js @@ -1,7 +1,6 @@ 'use strict'; -var MessageProcessor = require('./../msg-processor'), - settings = require('./../../config'); +var MessageProcessor = require('./../msg-processor'); module.exports = MessageProcessor.extend({ @@ -14,7 +13,7 @@ module.exports = MessageProcessor.extend({ var stanza = this.Iq(); var query = stanza.c('query', { - xmlns:'http://jabber.org/protocol/disco#info' + xmlns: 'http://jabber.org/protocol/disco#info' }); query.c('identity', { diff --git a/app/xmpp/msg-processors/conf-items.js b/app/xmpp/msg-processors/conf-items.js index fc55e37..5b536ac 100644 --- a/app/xmpp/msg-processors/conf-items.js +++ b/app/xmpp/msg-processors/conf-items.js @@ -1,7 +1,6 @@ 'use strict'; -var MessageProcessor = require('./../msg-processor'), - settings = require('./../../config'); +var MessageProcessor = require('./../msg-processor'); module.exports = MessageProcessor.extend({ @@ -19,7 +18,7 @@ module.exports = MessageProcessor.extend({ var stanza = this.Iq(); var query = stanza.c('query', { - xmlns:'http://jabber.org/protocol/disco#items' + xmlns: 'http://jabber.org/protocol/disco#items' }); rooms.forEach(function(room) { diff --git a/app/xmpp/msg-processors/last-activity.js b/app/xmpp/msg-processors/last-activity.js index 85daf71..0cc58fb 100644 --- a/app/xmpp/msg-processors/last-activity.js +++ b/app/xmpp/msg-processors/last-activity.js @@ -1,8 +1,6 @@ 'use strict'; -var mongoose = require('mongoose'), - MessageProcessor = require('./../msg-processor'), - settings = require('./../../config'); +var MessageProcessor = require('./../msg-processor'); module.exports = MessageProcessor.extend({ diff --git a/app/xmpp/msg-processors/room-info.js b/app/xmpp/msg-processors/room-info.js index 3884ff0..ebfef80 100644 --- a/app/xmpp/msg-processors/room-info.js +++ b/app/xmpp/msg-processors/room-info.js @@ -1,7 +1,6 @@ 'use strict'; -var MessageProcessor = require('./../msg-processor'), - settings = require('./../../config'); +var MessageProcessor = require('./../msg-processor'); module.exports = MessageProcessor.extend({ @@ -31,7 +30,7 @@ module.exports = MessageProcessor.extend({ var stanza = this.Iq(); var query = stanza.c('query', { - xmlns:'http://jabber.org/protocol/disco#info' + xmlns: 'http://jabber.org/protocol/disco#info' }); query.c('identity', { @@ -71,7 +70,7 @@ module.exports = MessageProcessor.extend({ var stanza = this.Iq(); var query = stanza.c('query', { - xmlns:'http://jabber.org/protocol/disco#info' + xmlns: 'http://jabber.org/protocol/disco#info' }); query.c('error', { diff --git a/app/xmpp/msg-processors/room-items.js b/app/xmpp/msg-processors/room-items.js index d95042d..c9a52b2 100644 --- a/app/xmpp/msg-processors/room-items.js +++ b/app/xmpp/msg-processors/room-items.js @@ -1,7 +1,6 @@ 'use strict'; -var MessageProcessor = require('./../msg-processor'), - settings = require('./../../config'); +var MessageProcessor = require('./../msg-processor'); module.exports = MessageProcessor.extend({ @@ -13,8 +12,8 @@ module.exports = MessageProcessor.extend({ then: function(cb) { var stanza = this.Iq(); - var query = stanza.c('query', { - xmlns:'http://jabber.org/protocol/disco#items' + stanza.c('query', { + xmlns: 'http://jabber.org/protocol/disco#items' }); cb(null, stanza); diff --git a/app/xmpp/msg-processors/room-join.js b/app/xmpp/msg-processors/room-join.js index d8f8a79..541175c 100644 --- a/app/xmpp/msg-processors/room-join.js +++ b/app/xmpp/msg-processors/room-join.js @@ -36,8 +36,7 @@ module.exports = MessageProcessor.extend({ var toParts = this.request.attrs.to.split('/'), roomUrl = toParts[0], nickname = toParts[1], - roomSlug = roomUrl.split('@')[0], - connection = this.client.conn; + roomSlug = roomUrl.split('@')[0]; this.connection.nickname(roomSlug, nickname); @@ -80,7 +79,7 @@ module.exports = MessageProcessor.extend({ }); presence.c('x', { - xmlns:'http://jabber.org/protocol/muc' + xmlns: 'http://jabber.org/protocol/muc' }); presence.c('error', { @@ -114,7 +113,7 @@ module.exports = MessageProcessor.extend({ presence .c('x', { - xmlns:'http://jabber.org/protocol/muc#user' + xmlns: 'http://jabber.org/protocol/muc#user' }) .c('item', { jid: this.connection.getUserJid(username), @@ -172,6 +171,9 @@ module.exports = MessageProcessor.extend({ } this.core.messages.list(query, function(err, messages) { + if (err) { + return cb(err); + } messages.reverse(); diff --git a/app/xmpp/msg-processors/room-leave.js b/app/xmpp/msg-processors/room-leave.js index 2b2f6d6..14c4b64 100644 --- a/app/xmpp/msg-processors/room-leave.js +++ b/app/xmpp/msg-processors/room-leave.js @@ -1,7 +1,6 @@ 'use strict'; -var MessageProcessor = require('./../msg-processor'), - settings = require('./../../config'); +var MessageProcessor = require('./../msg-processor'); module.exports = MessageProcessor.extend({ diff --git a/app/xmpp/msg-processors/room-message.js b/app/xmpp/msg-processors/room-message.js index 7d30bdb..f398f1d 100644 --- a/app/xmpp/msg-processors/room-message.js +++ b/app/xmpp/msg-processors/room-message.js @@ -1,8 +1,7 @@ 'use strict'; var _ = require('lodash'), - MessageProcessor = require('./../msg-processor'), - settings = require('./../../config'); + MessageProcessor = require('./../msg-processor'); var mentionPattern = /^([a-z0-9_]+\:)\B/; @@ -26,6 +25,10 @@ module.exports = MessageProcessor.extend({ } this.core.rooms.slug(roomSlug, function(err, room) { + if (err) { + return cb(err); + } + if (!room) { return cb(); } @@ -52,10 +55,10 @@ module.exports = MessageProcessor.extend({ text: text }; - this.core.messages.create(options, function(err, message) { + this.core.messages.create(options, function(err) { // Message will be sent by listener cb(err); - }.bind(this)); + }); }.bind(this)); } diff --git a/app/xmpp/msg-processors/root-info.js b/app/xmpp/msg-processors/root-info.js index 94ddeac..554ba3d 100644 --- a/app/xmpp/msg-processors/root-info.js +++ b/app/xmpp/msg-processors/root-info.js @@ -16,7 +16,7 @@ module.exports = MessageProcessor.extend({ var stanza = this.Iq(); var query = stanza.c('query', { - xmlns:'http://jabber.org/protocol/disco#info' + xmlns: 'http://jabber.org/protocol/disco#info' }); query.c('identity', { diff --git a/app/xmpp/msg-processors/root-items.js b/app/xmpp/msg-processors/root-items.js index 7569f5d..2aed38b 100644 --- a/app/xmpp/msg-processors/root-items.js +++ b/app/xmpp/msg-processors/root-items.js @@ -16,7 +16,7 @@ module.exports = MessageProcessor.extend({ var stanza = this.Iq(); var query = stanza.c('query', { - xmlns:'http://jabber.org/protocol/disco#items' + xmlns: 'http://jabber.org/protocol/disco#items' }); query.c('item', { diff --git a/app/xmpp/msg-processors/roster-get.js b/app/xmpp/msg-processors/roster-get.js index ac5e8b5..03b393f 100644 --- a/app/xmpp/msg-processors/roster-get.js +++ b/app/xmpp/msg-processors/roster-get.js @@ -1,7 +1,6 @@ 'use strict'; -var MessageProcessor = require('./../msg-processor'), - settings = require('./../../config'); +var MessageProcessor = require('./../msg-processor'); module.exports = MessageProcessor.extend({ @@ -14,7 +13,7 @@ module.exports = MessageProcessor.extend({ then: function(cb) { var stanza = this.Iq(); - var v = stanza.c('query', { + stanza.c('query', { xmlns: 'jabber:iq:roster' }); diff --git a/app/xmpp/msg-processors/vcard-get.js b/app/xmpp/msg-processors/vcard-get.js index 32c75a4..b0990fd 100644 --- a/app/xmpp/msg-processors/vcard-get.js +++ b/app/xmpp/msg-processors/vcard-get.js @@ -1,8 +1,7 @@ 'use strict'; var mongoose = require('mongoose'), - MessageProcessor = require('./../msg-processor'), - settings = require('./../../config'); + MessageProcessor = require('./../msg-processor'); module.exports = MessageProcessor.extend({ @@ -40,7 +39,7 @@ module.exports = MessageProcessor.extend({ var User = mongoose.model('User'); var username = this.to.split('@')[0]; User.findByIdentifier(username, function(err, user) { - if (user) { + if (!err && user) { sendVcard(user); } }); diff --git a/app/xmpp/msg-processors/vcard-set.js b/app/xmpp/msg-processors/vcard-set.js index 98bf2fa..de864c3 100644 --- a/app/xmpp/msg-processors/vcard-set.js +++ b/app/xmpp/msg-processors/vcard-set.js @@ -1,7 +1,6 @@ 'use strict'; -var MessageProcessor = require('./../msg-processor'), - settings = require('./../../config'); +var MessageProcessor = require('./../msg-processor'); module.exports = MessageProcessor.extend({ diff --git a/migroose.js b/migroose.js index e56021f..e27f9ba 100644 --- a/migroose.js +++ b/migroose.js @@ -1,9 +1,12 @@ -var mongoose = require("mongoose"), +'use strict'; + +var mongoose = require('mongoose'), settings = require('./app/config'), migroose = require('migroose'), - MigrationModel = migroose.MigrationModel, Runner = require('migroose-cli/cli/runner/index'); +var MigrationModel = migroose.MigrationModel; + module.exports = { connect: function(cb){ mongoose.connect(settings.database.uri, function(err){ @@ -13,15 +16,18 @@ module.exports = { }, needsMigration: function(cb) { - var cwd = process.cwd(); var runner = new Runner(process.cwd(), 'migrootions'); var migrations = runner.getMigrations(); var lastMigration = migrations.slice(migrations.length - 1)[0]; - MigrationModel.findOne({migrationId: lastMigration.migrationId}, function(err, model){ + MigrationModel.findOne({migrationId: lastMigration.migrationId}, function(err, model) { + if (err) { + return cb(err); + } + if (model) { - cb(false); + cb(null, false); } else { - cb(true); + cb(null, true); } }); } diff --git a/package.json b/package.json index 9f56503..7c73ae8 100644 --- a/package.json +++ b/package.json @@ -95,6 +95,6 @@ "grunt": "~0.4.5", "grunt-bower": "~0.16.0", "grunt-bower-task": "~0.4.0", - "socket.io-client": "~1.3.2" + "eslint": "^0.21.2" } } |