Permalink
Browse files

code cleanup

  • Loading branch information...
1 parent ad39472 commit ec1b8b4d4af6a78e23e720bb7c9e9be74846528e Jen Fong-Adwent committed Jul 3, 2012
Showing with 230 additions and 220 deletions.
  1. +1 −1 .gitignore
  2. +9 −7 app.js
  3. +8 −7 lib/authenticate.js
  4. +59 −53 lib/message-maker.js
  5. +96 −95 lib/noodle-redis.js
  6. +18 −11 lib/web-remix.js
  7. +9 −9 package.json
  8. +1 −1 public/javascripts/main.js
  9. +14 −0 public/noodletalk.webapp
  10. +3 −3 routes/auth.js
  11. +9 −30 routes/index.js
  12. +3 −3 routes/message.js
View
@@ -1,4 +1,4 @@
.DS_Store
node_modules
-settings.js
+local.json
npm-debug.log
View
16 app.js
@@ -3,12 +3,14 @@ var noodle = require('./package');
var express = require('express');
var configurations = module.exports;
var app = express.createServer();
-var settings = require('./settings')(app, configurations, express);
var redis = require("redis");
var client = redis.createClient();
-var mime = require('mime');
+var nconf = require('nconf');
+var settings = require('./settings')(app, configurations, express);
+
+nconf.argv().env().file({ file: 'local.json' });
-client.select(settings.app.set('redisnoodle'), function(errDb, res) {
+client.select(app.set('redisnoodle'), function(errDb, res) {
console.log('PROD/DEV database connection status: ', res);
});
@@ -33,8 +35,8 @@ io.sockets.on('connection', function (socket) {
});
// routes
-require("./routes")(client, noodle, app, io);
-require("./routes/message")(client, settings, app, io);
-require("./routes/auth")(client, settings, app, io);
+require("./routes")(client, noodle, nconf, app, io);
+require("./routes/message")(client, nconf, app, io);
+require("./routes/auth")(client, nconf, app, io);
-app.listen(settings.options.port);
+app.listen(process.env.PORT || nconf.get('port'));
View
@@ -6,11 +6,11 @@ var request = require('request');
* Requires: web request, settings configuration
* Returns: a user's browser id email if they logged in successfully.
*/
-exports.verify = function(req, settings, callback) {
+exports.verify = function(req, nconf, callback) {
var authUrl = settings.options.authUrl + '/verify';
var siteUrl = settings.options.domain + ':' + settings.options.authPort;
- if(!req.body.bid_assertion) {
+ if (!req.body.bid_assertion) {
return false;
}
@@ -27,24 +27,25 @@ exports.verify = function(req, settings, callback) {
request.post(params, function(error, resp, body) {
var email = false;
- if(error) {
+ if (error) {
return callback(error);
}
try {
var jsonResp = JSON.parse(body);
- if(!error && jsonResp.status === 'okay') {
+ if (!error && jsonResp.status === 'okay') {
email = jsonResp.email;
} else {
return callback(error);
}
- } catch(error) {
+ } catch (error) {
return callback(error);
}
return callback(null, email);
});
+
return true;
};
@@ -53,9 +54,9 @@ exports.getUserHash = function(req, nickname, channel, callback) {
var user = {
emailHash: crypto.createHash('md5').update(req.session.email).digest("hex"),
avatar: gravatar.url(req.session.email, {}, true),
- nickname: nickname,
- pubKey: ''
+ nickname: nickname
};
+
return callback(null, user);
};
View
@@ -43,6 +43,7 @@ exports.getMessage = function(client, channel, req, io, actionType, callback) {
is_action: isAction,
action_type: actionType
};
+
return message;
};
@@ -54,63 +55,68 @@ exports.getMessage = function(client, channel, req, io, actionType, callback) {
};
content.generate(messageContent.substring(0, 399), function(errCnt, message) {
- try {
- var newNickname = content.getNickName(req.body.message);
+ if (errCnt) {
+ return callback(errCnt);
+ }
+
+ var newNickname = content.getNickName(req.body.message);
+
+ var oldNickname = self.nicknameIsEmpty(req.session.nickname[channel]);
+ var nickname = oldNickname;
- var oldNickname = self.nicknameIsEmpty(req.session.nickname[channel]);
- var nickname = oldNickname;
+ if (newNickname) {
+ nickname = newNickname;
+ }
+
+ req.session.nickname[channel] = nickname;
- if (newNickname) {
- nickname = newNickname;
+ auth.getUserHash(req, nickname, channel, function(errHash, userHash) {
+ if (errHash) {
+ return callback(errHash);
}
- req.session.nickname[channel] = nickname;
-
- auth.getUserHash(req, nickname, channel, function(errHash, userHash) {
- noodleRedis.setChannelUser(client, channel, userHash, oldNickname, newNickname, function(err, usernameUsed) {
- try {
- // if this is a /nick change, check to see if we can set it
- if (actionType === "nick") {
- if (!usernameUsed) {
- message = '<em>' + oldNickname + ' has changed to ' + newNickname + '</em>';
- req.session.nickname[channel] = newNickname;
- req.session.updated = new Date();
-
- noodleRedis.getUserlist(client, channel, function(err, userList) {
- io.sockets.in(channel).emit('userlist', userList);
- io.sockets.in(channel).emit('userHash', userHash);
- });
-
- } else {
- message = '';
- }
- isAction = true;
-
- // if this is a /me prepend with the nick
- } else if (actionType === "activity") {
- var meMatch = /^(\s\/me\s?)/i;
- message = '<em>' + req.session.nickname[channel] + ' ' + message.replace(meMatch, '') + '</em>';
- isAction = true;
-
- // user joining new channel
- } else if (actionType === "joined") {
- message = self.generateWelcomeMessage();
- isAction = true;
-
- // clear invalid commands
- } else if (actionType === "dummy") {
- message = '';
- }
- } catch(err) {
- return callback(err);
- }
-
- message = self.generateMessage(message);
- return callback(null, message);
- });
+ noodleRedis.setChannelUser(client, channel, userHash, oldNickname, newNickname, function(err, usernameUsed) {
+ if (err) {
+ return callback(err);
+ }
+
+ // if this is a /nick change, check to see if we can set it
+ if (actionType === "nick") {
+ if (!usernameUsed) {
+ message = '<em>' + oldNickname + ' has changed to ' + newNickname + '</em>';
+ req.session.nickname[channel] = newNickname;
+ req.session.updated = new Date();
+
+ noodleRedis.getUserlist(client, channel, function(err, userList) {
+ io.sockets.in(channel).emit('userlist', userList);
+ io.sockets.in(channel).emit('userHash', userHash);
+ });
+
+ } else {
+ message = '';
+ }
+ isAction = true;
+
+ // if this is a /me prepend with the nick
+ } else if (actionType === "activity") {
+ var meMatch = /^(\s\/me\s?)/i;
+ message = '<em>' + req.session.nickname[channel] + ' ' + message.replace(meMatch, '') + '</em>';
+ isAction = true;
+
+ // user joining new channel
+ } else if (actionType === "joined") {
+ message = self.generateWelcomeMessage();
+ isAction = true;
+
+ // clear invalid commands
+ } else if (actionType === "dummy") {
+ message = '';
+ }
+
+ message = self.generateMessage(message);
+
+ return callback(null, message);
});
- } catch(errCnt) {
- return callback(errCnt);
- }
+ });
});
};
Oops, something went wrong.

0 comments on commit ec1b8b4

Please sign in to comment.