Advertisement
Guest User

Untitled

a guest
Jan 8th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.get('/auths', ensureAuthenticated, ensureCompleteProfile, function(req, res){
  2.         withRoom(+req.param('id'), req.user.id, function(err, room) {
  3.             if (!room) return res.render('error.jade', { error: "No room" });
  4.             room.path = roomPath(room)
  5.             mdb.con(function(err, con){
  6.                 if (err) return res.render('error.jade', { error: "No connection" });
  7.                 con.listRoomAuths(room.id, function(err, auths){
  8.                     if (err) return res.render('error.jade', { error: err.toString() });
  9.                     con.listOpenAccessRequests(room.id, function(err, requests){
  10.                         if (err) return res.render('error.jade', { error: err.toString() });
  11.                         con.listRecentUsers(room.id, 50, function(err, recentUsers){
  12.                             if (err) return res.render('error.jade', { error: err.toString() });
  13.                             var authorizedUsers = {}, unauthorizedUsers = [];
  14.                             auths.forEach(function(a){
  15.                                 authorizedUsers[a.player] = true;
  16.                                 });
  17.                             recentUsers.forEach(function(u){
  18.                                 if (!authorizedUsers[u.id]) unauthorizedUsers.push(u);
  19.                                 });
  20.                             con.ok();
  21.                             res.render('auths.jade', { room:room, auths:auths, requests:requests, unauthorizedUsers:unauthorizedUsers });
  22.                         });
  23.                     });
  24.                 });
  25.             });
  26.         });
  27. });
  28.  
  29. // with promises
  30. var Promise = require('bluebird');
  31.  
  32. var mdb = Promise.promisifyAll(require('./pgdb.js'));
  33. var withRoomAsync = Promise.promisify(withRoom);
  34.  
  35. app.get('/auths', ensureAuthenticated, ensureCompleteProfile, function(req, res) {
  36.     var con, room;
  37.     withRoomAsync(+req.param('id'), req.user.id).then(function(roomArg) {
  38.         room = roomArg;
  39.         if (!room) throw NoRoomError();
  40.  
  41.         room.path = roomPath(room);
  42.         return mdb.conAsync();
  43.     }).then(function(conArg) {
  44.         con = conArg;
  45.         if (!con) throw NoConnectError();
  46.         return con.listRoomAuthsAsync(room.id);
  47.     }).then(function(auths) {
  48.         return con.listOpenAccessRequestsAsync(room.id);
  49.     }).then(function(requests) {
  50.         return con.listRecentUsersAsync(room.id, 50);
  51.     }).then(function(recentUsers) {
  52.         var authorizedUsers = {};
  53.         var unauthorizedUsers = [];
  54.  
  55.         auths.forEach(function(a) {
  56.             authorizedUsers[a.player] = true;
  57.         });
  58.         recentUsers.forEach(function(u) {
  59.             if (!authorizedUsers[u.id]) unauthorizedUsers.push(u);
  60.         });
  61.         con.ok();
  62.         return res.render('auths.jade', {
  63.             room: room,
  64.             auths: auths,
  65.             requests: requests,
  66.             unauthorizedUsers: unauthorizedUsers
  67.         });
  68.     }).catch(NoRoomError, function(err) {
  69.         // If you want to handle this error specifically
  70.         // In this case it's not really needed because Error does the same,
  71.         // but it's to give you an idea.
  72.         return renderError(res, err.message);
  73.     }).catch(Error, function(err) {
  74.         // NoConnectError is caught here too
  75.         return renderError(res, err.message);
  76.     });
  77. });
  78.  
  79. function renderError(res, message) {
  80.     return res.render('error.jade', { error: message });
  81. }
  82.  
  83. // Error subclasses
  84. function NoRoomError() {
  85.     this.name = 'NoRoomError';
  86.     this.message = 'No room';
  87.     this.stack = (new Error()).stack;
  88. }
  89. NoRoomError.prototype = new Error();
  90.  
  91. function NoConnectError() {
  92.     this.name = 'NoConnectError';
  93.     this.message = 'No connect';
  94.     this.stack = (new Error()).stack;
  95. }
  96. NoConnectError.prototype = new Error();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement