Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.get('/auths', ensureAuthenticated, ensureCompleteProfile, function(req, res){
- withRoom(+req.param('id'), req.user.id, function(err, room) {
- if (!room) return res.render('error.jade', { error: "No room" });
- room.path = roomPath(room)
- mdb.con(function(err, con){
- if (err) return res.render('error.jade', { error: "No connection" });
- con.listRoomAuths(room.id, function(err, auths){
- if (err) return res.render('error.jade', { error: err.toString() });
- con.listOpenAccessRequests(room.id, function(err, requests){
- if (err) return res.render('error.jade', { error: err.toString() });
- con.listRecentUsers(room.id, 50, function(err, recentUsers){
- if (err) return res.render('error.jade', { error: err.toString() });
- var authorizedUsers = {}, unauthorizedUsers = [];
- auths.forEach(function(a){
- authorizedUsers[a.player] = true;
- });
- recentUsers.forEach(function(u){
- if (!authorizedUsers[u.id]) unauthorizedUsers.push(u);
- });
- con.ok();
- res.render('auths.jade', { room:room, auths:auths, requests:requests, unauthorizedUsers:unauthorizedUsers });
- });
- });
- });
- });
- });
- });
- // with promises
- var Promise = require('bluebird');
- var mdb = Promise.promisifyAll(require('./pgdb.js'));
- var withRoomAsync = Promise.promisify(withRoom);
- app.get('/auths', ensureAuthenticated, ensureCompleteProfile, function(req, res) {
- var con, room;
- withRoomAsync(+req.param('id'), req.user.id).then(function(roomArg) {
- room = roomArg;
- if (!room) throw NoRoomError();
- room.path = roomPath(room);
- return mdb.conAsync();
- }).then(function(conArg) {
- con = conArg;
- if (!con) throw NoConnectError();
- return con.listRoomAuthsAsync(room.id);
- }).then(function(auths) {
- return con.listOpenAccessRequestsAsync(room.id);
- }).then(function(requests) {
- return con.listRecentUsersAsync(room.id, 50);
- }).then(function(recentUsers) {
- var authorizedUsers = {};
- var unauthorizedUsers = [];
- auths.forEach(function(a) {
- authorizedUsers[a.player] = true;
- });
- recentUsers.forEach(function(u) {
- if (!authorizedUsers[u.id]) unauthorizedUsers.push(u);
- });
- con.ok();
- return res.render('auths.jade', {
- room: room,
- auths: auths,
- requests: requests,
- unauthorizedUsers: unauthorizedUsers
- });
- }).catch(NoRoomError, function(err) {
- // If you want to handle this error specifically
- // In this case it's not really needed because Error does the same,
- // but it's to give you an idea.
- return renderError(res, err.message);
- }).catch(Error, function(err) {
- // NoConnectError is caught here too
- return renderError(res, err.message);
- });
- });
- function renderError(res, message) {
- return res.render('error.jade', { error: message });
- }
- // Error subclasses
- function NoRoomError() {
- this.name = 'NoRoomError';
- this.message = 'No room';
- this.stack = (new Error()).stack;
- }
- NoRoomError.prototype = new Error();
- function NoConnectError() {
- this.name = 'NoConnectError';
- this.message = 'No connect';
- this.stack = (new Error()).stack;
- }
- NoConnectError.prototype = new Error();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement