node.js/app.js
author carine
Mon, 01 Dec 2014 15:15:25 +0100
changeset 743 0e58fabc0175
parent 722 3578e7e14654
permissions -rw-r--r--
test
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
     1
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
     2
/**
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
     3
 * Module dependencies.
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
     4
 */
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
     5
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
     6
var express = require('express');
273
368433dd9ba5 namespacing
Dominique Hazael-Massieux <dom@w3.org>
parents: 272
diff changeset
     7
require("express-namespace");
32
e7585ff50173 more progress toward taxi pool management
Dominique Hazaël-Massieux <dom@w3.org>
parents: 27
diff changeset
     8
var everyauth = require('everyauth'),
124
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
     9
    EventEmitter = require('events').EventEmitter;
188
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
    10
var imports = require("./imports.js"),
245
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    11
    twitter = require("./twitter.js");
675
Dominique Hazaël-Massieux <dom@w3.org>
parents: 674
diff changeset
    12
var sanitizer = require('sanitizer');
124
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
    13
32
e7585ff50173 more progress toward taxi pool management
Dominique Hazaël-Massieux <dom@w3.org>
parents: 27
diff changeset
    14
form = require("express-form"),
e7585ff50173 more progress toward taxi pool management
Dominique Hazaël-Massieux <dom@w3.org>
parents: 27
diff changeset
    15
filter = form.filter,
e7585ff50173 more progress toward taxi pool management
Dominique Hazaël-Massieux <dom@w3.org>
parents: 27
diff changeset
    16
validate = form.validate;
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
    17
177
f4a39c62913f using https
Dominqique Hazael-Massieux <dom@w3.org>
parents: 176
diff changeset
    18
var fs = require('fs');
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
    19
245
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    20
// Reading command line options
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    21
var argv = require("optimist")
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    22
  .options('c', {
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    23
	   alias: 'config',
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    24
	   default:'config.ini'}).argv;
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    25
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    26
// Reading configuration file
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    27
var config = require('iniparser').parseSync(argv.c);
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    28
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    29
var app;
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    30
if (config.https.key_file && config.https.certificate_file) {
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    31
    app = express.createServer({key: fs.readFileSync(config.https.key_file), cert: fs.readFileSync(config.https.certificate_file)});
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    32
} else {
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    33
    app = express.createServer();
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    34
}
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    35
module.exports = app;
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    36
124
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
    37
var emitter = new EventEmitter();
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
    38
245
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    39
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
    40
var mongoose = require('mongoose'),
245
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    41
db = mongoose.connect('mongodb://' + config.mongo.host + '/' + config.mongo.dbname);
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
    42
var People = require('./model.js').People(db);
39
54d9001dfd80 using populate feature from mongoose 2
Dominique Hazaël-Massieux <dom@w3.org>
parents: 32
diff changeset
    43
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
    44
var Organization = require('./model.js').Organization(db);
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
    45
var Place = require('./model.js').Place(db);
234
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
    46
var Event = require('./model.js').Event(db);
545
fe8f4b755fd9 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 544
diff changeset
    47
var Status = require('./model.js').StatusUpdate(db);
21
34143a5da640 preliminary support for taxi pooling
Dominqique Hazael-Massieux <dom@w3.org>
parents: 15
diff changeset
    48
var TaxiFromAirport = require('./model.js').TaxiFromAirport(db);
34143a5da640 preliminary support for taxi pooling
Dominqique Hazael-Massieux <dom@w3.org>
parents: 15
diff changeset
    49
var TaxiToAirport = require('./model.js').TaxiToAirport(db);
188
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
    50
var TwitterSettings = require('./model.js').TwitterSettings(db);
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
    51
721
3108187a71db *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 720
diff changeset
    52
var places = {};
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
    53
22
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
    54
// Authentication 
27
fe65f730d451 using mongoose for session store
Dominique Hazaël-Massieux <dom@w3.org>
parents: 24
diff changeset
    55
// Session Store
fe65f730d451 using mongoose for session store
Dominique Hazaël-Massieux <dom@w3.org>
parents: 24
diff changeset
    56
var SessionMongoose = require("session-mongoose");
fe65f730d451 using mongoose for session store
Dominique Hazaël-Massieux <dom@w3.org>
parents: 24
diff changeset
    57
var mongooseSessionStore = new SessionMongoose({
245
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
    58
    url: "mongodb://" + config.mongo.host + '/'  + config.authentication.session_db,
27
fe65f730d451 using mongoose for session store
Dominique Hazaël-Massieux <dom@w3.org>
parents: 24
diff changeset
    59
    interval: 120000 // expiration check worker run interval in millisec (default: 60000)
fe65f730d451 using mongoose for session store
Dominique Hazaël-Massieux <dom@w3.org>
parents: 24
diff changeset
    60
});
fe65f730d451 using mongoose for session store
Dominique Hazaël-Massieux <dom@w3.org>
parents: 24
diff changeset
    61
324
a37bc34b2051 base path for static content too
Dominique Hazaël-Massieux <dom@w3.org>
parents: 310
diff changeset
    62
everyauth.everymodule.logoutPath(config.hosting.basepath + '/logout');
297
ae5d6145417c fixing logout path
Dominique Hazaël-Massieux <dom@w3.org>
parents: 294
diff changeset
    63
294
576329f37a93 longer timeout for auth; import url by default in views
Dominique Hazael-Massieux <dom@w3.org>
parents: 275
diff changeset
    64
everyauth.everymodule.moduleTimeout(40000);
576329f37a93 longer timeout for auth; import url by default in views
Dominique Hazael-Massieux <dom@w3.org>
parents: 275
diff changeset
    65
22
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
    66
everyauth.everymodule.findUserById( function (userId, callback) {
166
1474507936e8 fix login for first import
Dominqique Hazael-Massieux <dom@w3.org>
parents: 165
diff changeset
    67
  // is there anyone in the db yet? 
1474507936e8 fix login for first import
Dominqique Hazael-Massieux <dom@w3.org>
parents: 165
diff changeset
    68
    People.count({}, function(err, count) {
1474507936e8 fix login for first import
Dominqique Hazael-Massieux <dom@w3.org>
parents: 165
diff changeset
    69
	if (!count) {
1474507936e8 fix login for first import
Dominqique Hazael-Massieux <dom@w3.org>
parents: 165
diff changeset
    70
	    // No one in the db, we create a mock user to allow for import
260
71286d68e90e replaced w3cId with slug
Dominqique Hazael-Massieux <dom@w3.org>
parents: 253
diff changeset
    71
	    callback(err, {'login': userId, 'given': 'Admin', 'family': 'Istrator', 'slug': 'admin'});
166
1474507936e8 fix login for first import
Dominqique Hazael-Massieux <dom@w3.org>
parents: 165
diff changeset
    72
	} else {
1474507936e8 fix login for first import
Dominqique Hazael-Massieux <dom@w3.org>
parents: 165
diff changeset
    73
	    People.findOne({login: userId}, callback);
1474507936e8 fix login for first import
Dominqique Hazael-Massieux <dom@w3.org>
parents: 165
diff changeset
    74
	}
1474507936e8 fix login for first import
Dominqique Hazael-Massieux <dom@w3.org>
parents: 165
diff changeset
    75
    });
22
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
    76
});
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
    77
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
    78
// Adapted from everyauth ldap module
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
    79
everyauth.password
478
6f4ad881b088 go back to login page after logout
Dominique Hazael-Massieux <dom@w3.org>
parents: 464
diff changeset
    80
   .logoutRedirectPath(config.hosting.basepath + '/login')
324
a37bc34b2051 base path for static content too
Dominique Hazaël-Massieux <dom@w3.org>
parents: 310
diff changeset
    81
  .getLoginPath(config.hosting.basepath + '/login')
a37bc34b2051 base path for static content too
Dominique Hazaël-Massieux <dom@w3.org>
parents: 310
diff changeset
    82
  .postLoginPath(config.hosting.basepath + '/login') // Uri path that your login form POSTs to
22
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
    83
  .loginView('login.ejs')
561
Dominique Hazael-Massieux <dom@w3.org>
parents: 560
diff changeset
    84
  .registerView('index.ejs') // @@@ need fixing
562
5320f00e655b *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 561
diff changeset
    85
    .registerLocals(function (req, res, done) {
5320f00e655b *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 561
diff changeset
    86
	Status.find({})
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
    87
	    .sort('time', -1)
562
5320f00e655b *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 561
diff changeset
    88
	    .limit(20)
568
95fa9ab59923 limit data retrieved on status updates
Dominique Hazael-Massieux <dom@w3.org>
parents: 566
diff changeset
    89
	    .populate('author', ['given', 'family', 'slug', 'picture_thumb'])
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
    90
	    .exec(function(err, statusupdates) { 
562
5320f00e655b *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 561
diff changeset
    91
		if (err) return done(err);
5320f00e655b *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 561
diff changeset
    92
		done(null, {statusupdates: statusupdates});
5320f00e655b *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 561
diff changeset
    93
	    });
5320f00e655b *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 561
diff changeset
    94
    })
484
0aaec398d254 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 483
diff changeset
    95
  .loginSuccessRedirect(config.hosting.basepath + '/')
0aaec398d254 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 483
diff changeset
    96
/*  .respondToLoginSucceed( function (res, user, data) {
479
b21f187bb0a1 trying to improve redirect after login/logout
Dominique Hazael-Massieux <dom@w3.org>
parents: 478
diff changeset
    97
    var redirectTo = config.hosting.basepath + "/";
b21f187bb0a1 trying to improve redirect after login/logout
Dominique Hazael-Massieux <dom@w3.org>
parents: 478
diff changeset
    98
      if (data.params.redirectTo) {
b21f187bb0a1 trying to improve redirect after login/logout
Dominique Hazael-Massieux <dom@w3.org>
parents: 478
diff changeset
    99
	  redirectTo = data.params.redirectTo;
b21f187bb0a1 trying to improve redirect after login/logout
Dominique Hazael-Massieux <dom@w3.org>
parents: 478
diff changeset
   100
      }
b21f187bb0a1 trying to improve redirect after login/logout
Dominique Hazael-Massieux <dom@w3.org>
parents: 478
diff changeset
   101
      if (user) {
b21f187bb0a1 trying to improve redirect after login/logout
Dominique Hazael-Massieux <dom@w3.org>
parents: 478
diff changeset
   102
	  res.writeHead(303, {'Location': redirectTo});
b21f187bb0a1 trying to improve redirect after login/logout
Dominique Hazael-Massieux <dom@w3.org>
parents: 478
diff changeset
   103
	  res.end();
b21f187bb0a1 trying to improve redirect after login/logout
Dominique Hazael-Massieux <dom@w3.org>
parents: 478
diff changeset
   104
      }   
484
0aaec398d254 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 483
diff changeset
   105
  })*/
22
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   106
  .authenticate( function (login, password) {
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   107
    var promise = this.Promise();  
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   108
    var errors = [];
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   109
      if (!login) errors.push('Missing login');
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   110
      if (!password) errors.push('Missing password');
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   111
      if (errors.length) return errors;
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   112
      var ldap = require('./ldapauth');
32
e7585ff50173 more progress toward taxi pool management
Dominique Hazaël-Massieux <dom@w3.org>
parents: 27
diff changeset
   113
      // modified version of ldapauth that takes an additional scheme parameter available from https://github.com/dontcallmedom/node-ldapauth
248
2655e32fd32e conversion
Dominqique Hazael-Massieux <dom@w3.org>
parents: 247
diff changeset
   114
      console.log(config.ldap);
2655e32fd32e conversion
Dominqique Hazael-Massieux <dom@w3.org>
parents: 247
diff changeset
   115
      ldap.authenticate(config.ldap.scheme, config.ldap.host, parseInt(config.ldap.port),'uid=' + login + ',' + config.ldap.dn_realm , password, function(err, result) {
22
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   116
        if (err) {
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   117
          return promise.fail(err);
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   118
        }
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   119
	if (result === false) {
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   120
          errors = ['Login failed.'];
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   121
          return promise.fulfill(errors);
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   122
	} else {
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   123
          var user = {id: login};
159
a1509663b873 remove need for pre-auth in db config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 141
diff changeset
   124
	  // We'll use this to get data from WBS when importing registrants list
250
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   125
	  // @@@ move to import.js? use cleaner abstraction
159
a1509663b873 remove need for pre-auth in db config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 141
diff changeset
   126
	  app.set('w3c_auth', new Buffer(login + ':' + password).toString('base64')); 	  
22
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   127
          return promise.fulfill(user);	    
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   128
	}
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   129
      });
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   130
      return promise;
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   131
  })
324
a37bc34b2051 base path for static content too
Dominique Hazaël-Massieux <dom@w3.org>
parents: 310
diff changeset
   132
  .getRegisterPath(config.hosting.basepath + '/')
a37bc34b2051 base path for static content too
Dominique Hazaël-Massieux <dom@w3.org>
parents: 310
diff changeset
   133
  .postRegisterPath(config.hosting.basepath + '/')
22
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   134
  .registerUser(function() {
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   135
      return null;
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   136
   });
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   137
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   138
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   139
// Configuration
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   140
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   141
app.configure(function(){
159
a1509663b873 remove need for pre-auth in db config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 141
diff changeset
   142
  emitter.setMaxListeners(0);
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   143
  app.use(express.logger());
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   144
  app.set('views', __dirname + '/views');
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   145
  app.set('view engine', 'ejs');
23
b8cf14b7f7bd dev vs prod
Dominique Hazaël-Massieux <dom@w3.org>
parents: 22
diff changeset
   146
  app.set('port', 3000);
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   147
  app.use(express.bodyParser());
342
3d79784e723d *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 340
diff changeset
   148
  app.use(config.hosting.basepath, express.static(__dirname + '/public', { maxAge: 86400000}));
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   149
  app.use(express.methodOverride());
22
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   150
 app.use(express.cookieParser()); 
245
df74fb863540 starting to move some w3c-specific settings to configuration file
Dominique Hazaël-Massieux <dom@w3.org>
parents: 239
diff changeset
   151
  app.use(express.session({store: mongooseSessionStore, secret:config.authentication.session_secret}));
22
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   152
  app.use(everyauth.middleware());
720
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   153
  // Loading up list of places
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   154
  Place.find({}).sort('name', 1).exec( function(err, rooms) {
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   155
      if (err) {
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   156
	  console.log("No room known in the system");		     
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   157
      } else {
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   158
	for (i in rooms) {
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   159
	    places[rooms[i].shortname] = rooms[i];
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   160
	}	  
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   161
      }
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   162
  });
188
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   163
  // Loading up list of twitter users
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   164
  TwitterSettings.findOne(
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   165
      {}, 
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   166
      function(err, settings) {
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   167
	  // No twitter settings
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   168
	  if (err || !settings) {
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   169
	      console.log("No twitter settings found, won't get updates from Twitter");
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   170
	  } else {
212
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   171
	      if (!(settings.username && settings.password)) {
188
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   172
		  console.log("Incomplete twitter settings found, won't get updates from Twitter: " + JSON.stringify(settings));
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   173
	      } else {
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   174
		  app.set('twitter_auth', new Buffer(settings.username + ':' + settings.password).toString('base64')); 	  
582
87b4dbc27f83 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 578
diff changeset
   175
		  if (true) {
213
5dd580906c8c bug fix
Dominqique Hazael-Massieux <dom@w3.org>
parents: 212
diff changeset
   176
		      People.find({}, ['twitterAccount'], function(err, people) {
212
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   177
			  for (p in people) {
213
5dd580906c8c bug fix
Dominqique Hazael-Massieux <dom@w3.org>
parents: 212
diff changeset
   178
			      if (people[p].twitterAccount && people[p].twitterAccount.id) {
212
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   179
				  settings.ids.push(people[p].twitterAccount.id);
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   180
			      }
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   181
			  }
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   182
			  // defaulting to registered list of users
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   183
			  if (!settings.ids.length) {
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   184
			      if (settings.list && settings.list.owner && settings.list.slug) {
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   185
				  // load a list of users from Twitter
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   186
				  twitter.listTwitterIdsFromTwitterList(
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   187
				      settings.list.owner,
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   188
				      settings.list.slug,
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   189
				      function (ids) {
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   190
					  settings.ids = ids;
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   191
					  settings.save();
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   192
					  twitter.listenToTweets(emitter, settings.ids, app.set('twitter_auth'));
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   193
				      });
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   194
			      }
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   195
			  } else {
188
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   196
			      settings.save();
212
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   197
			      twitter.listenToTweets(emitter, settings.ids, app.set('twitter_auth'));
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   198
			  }
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   199
		      });
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   200
		  } else {
edf5d4158735 renamed listTwitterIds to listTwitterIdsFromTwitterlist
Dominqique Hazael-Massieux <dom@w3.org>
parents: 211
diff changeset
   201
		      twitter.listenToTweets(emitter, settings.ids, app.set('twitter_auth'));
188
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   202
		  }
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   203
	      }
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   204
	  }
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   205
      });
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   206
});
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   207
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   208
app.configure('test', function(){
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   209
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   210
  //db = mongoose.connect('mongodb://localhost/tpac-test');
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   211
});
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   212
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   213
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   214
app.configure('development', function(){
23
b8cf14b7f7bd dev vs prod
Dominique Hazaël-Massieux <dom@w3.org>
parents: 22
diff changeset
   215
  everyauth.debug = true;
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   216
  app.use(express.logger());
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   217
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   218
});
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   219
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   220
app.configure('production', function(){
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   221
  app.use(express.logger());
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   222
  app.use(express.errorHandler()); 
272
6e96c34bb433 adapting to new deployment set up
Dominique Hazael-Massieux <dom@w3.org>
parents: 264
diff changeset
   223
  app.set('port', 80);
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   224
});
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   225
22
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   226
338
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   227
// update twitter search on registering new twitter id
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   228
emitter.on("twitterListChange", function (id) {
485
885ff23e3ec9 trying to fix twitter insert
Dominique Hazael-Massieux <dom@w3.org>
parents: 484
diff changeset
   229
    if (id) {
885ff23e3ec9 trying to fix twitter insert
Dominique Hazael-Massieux <dom@w3.org>
parents: 484
diff changeset
   230
	TwitterSettings.findOne(
885ff23e3ec9 trying to fix twitter insert
Dominique Hazael-Massieux <dom@w3.org>
parents: 484
diff changeset
   231
	    {}, 
885ff23e3ec9 trying to fix twitter insert
Dominique Hazael-Massieux <dom@w3.org>
parents: 484
diff changeset
   232
	    function(err, settings) {
885ff23e3ec9 trying to fix twitter insert
Dominique Hazael-Massieux <dom@w3.org>
parents: 484
diff changeset
   233
		var ids = settings.ids;
885ff23e3ec9 trying to fix twitter insert
Dominique Hazael-Massieux <dom@w3.org>
parents: 484
diff changeset
   234
		ids.push(id);
885ff23e3ec9 trying to fix twitter insert
Dominique Hazael-Massieux <dom@w3.org>
parents: 484
diff changeset
   235
		settings.ids = ids;
885ff23e3ec9 trying to fix twitter insert
Dominique Hazael-Massieux <dom@w3.org>
parents: 484
diff changeset
   236
		twitter.listenToTweets(emitter, ids, app.set('twitter_auth'));
885ff23e3ec9 trying to fix twitter insert
Dominique Hazael-Massieux <dom@w3.org>
parents: 484
diff changeset
   237
		settings.save();
885ff23e3ec9 trying to fix twitter insert
Dominique Hazael-Massieux <dom@w3.org>
parents: 484
diff changeset
   238
	    }
885ff23e3ec9 trying to fix twitter insert
Dominique Hazael-Massieux <dom@w3.org>
parents: 484
diff changeset
   239
	);
885ff23e3ec9 trying to fix twitter insert
Dominique Hazael-Massieux <dom@w3.org>
parents: 484
diff changeset
   240
    }
338
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   241
});
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   242
544
5e6022043b9c save status updates
Dominique Hazael-Massieux <dom@w3.org>
parents: 542
diff changeset
   243
// Record events as statusupdates
5e6022043b9c save status updates
Dominique Hazael-Massieux <dom@w3.org>
parents: 542
diff changeset
   244
emitter.on("checkin", function(user, left, entered) {
674
462bc1f15045 sanitize contentHTML
Dominique Hazaël-Massieux <dom@w3.org>
parents: 669
diff changeset
   245
    var status  = new Status({author: user, time: Date.now(), statusType: "checkin", contentHTML: (left && left.name ? " left <a href='" + sanitizer.escape(config.hosting.basepath) + "/locations/" + sanitizer.escape(left.shortname) + "'>" + sanitizer.escape(left.name) + "</a>" + (entered && entered.name ? " and " : "") : "") + (entered && entered.name ? " entered <a href='" + sanitizer.escape(config.hosting.basepath) + "/locations/" + sanitizer.escape(entered.shortname) + "'>" + sanitizer.escape(entered.name) + "</a>" : "")});
669
ad8f7b3ed445 allow to have html content in trusted/sanitized status updates
Dominique Hazaël-Massieux <dom@w3.org>
parents: 667
diff changeset
   246
    
547
9e5ffedf9998 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 546
diff changeset
   247
    status.save();
544
5e6022043b9c save status updates
Dominique Hazael-Massieux <dom@w3.org>
parents: 542
diff changeset
   248
});
5e6022043b9c save status updates
Dominique Hazael-Massieux <dom@w3.org>
parents: 542
diff changeset
   249
574
e867b1e765d9 status updates for new ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 572
diff changeset
   250
emitter.on("newevent", function(event) {
e867b1e765d9 status updates for new ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 572
diff changeset
   251
    var time = event.time;
576
4c54e509aa9d *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 574
diff changeset
   252
    if (time) {
4c54e509aa9d *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 574
diff changeset
   253
	time.setUTCHours(time.getUTCHours() + parseInt(config.schedule.timezone_offset, 10));
674
462bc1f15045 sanitize contentHTML
Dominique Hazaël-Massieux <dom@w3.org>
parents: 669
diff changeset
   254
        var status  = new Status({author: event.proposedBy, time: Date.now(), statusType: "event", contentHTML: " scheduled a new ad-hoc event: " + sanitizer.escape(event.name) + " at " + sanitizer.escape(time) + " in <a href='" + sanitizer.escape(config.hosting.basepath + "/locations/" + event.room.shortname) + "'>" + sanitizer.escape(event.room.name) + "</a>"});
576
4c54e509aa9d *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 574
diff changeset
   255
	status.save();
4c54e509aa9d *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 574
diff changeset
   256
    }
574
e867b1e765d9 status updates for new ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 572
diff changeset
   257
});
e867b1e765d9 status updates for new ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 572
diff changeset
   258
e867b1e765d9 status updates for new ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 572
diff changeset
   259
544
5e6022043b9c save status updates
Dominique Hazael-Massieux <dom@w3.org>
parents: 542
diff changeset
   260
emitter.on("tweet", function(tweet) {
5e6022043b9c save status updates
Dominique Hazael-Massieux <dom@w3.org>
parents: 542
diff changeset
   261
    People.findOne(
5e6022043b9c save status updates
Dominique Hazael-Massieux <dom@w3.org>
parents: 542
diff changeset
   262
	{"twitterAccount.id":tweet.user.id},
5e6022043b9c save status updates
Dominique Hazael-Massieux <dom@w3.org>
parents: 542
diff changeset
   263
	function(err, indiv) {
546
6cbc1e8512a8 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 545
diff changeset
   264
	    if (indiv) {
638
356e693e4509 trying to simplify recent activity strings by avoiding the repetition of the user name (to be reviewed)
tgambet
parents: 592
diff changeset
   265
		var status = new Status({author: indiv, time: Date.now(), statusType: "tweet", content: /*indiv.given + " " + indiv.family + */" tweeted: “" + tweet.text+ "”"});
547
9e5ffedf9998 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 546
diff changeset
   266
		status.save();
546
6cbc1e8512a8 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 545
diff changeset
   267
	    }
544
5e6022043b9c save status updates
Dominique Hazael-Massieux <dom@w3.org>
parents: 542
diff changeset
   268
	});
5e6022043b9c save status updates
Dominique Hazael-Massieux <dom@w3.org>
parents: 542
diff changeset
   269
});
5e6022043b9c save status updates
Dominique Hazael-Massieux <dom@w3.org>
parents: 542
diff changeset
   270
574
e867b1e765d9 status updates for new ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 572
diff changeset
   271
e867b1e765d9 status updates for new ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 572
diff changeset
   272
338
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   273
// Utility function
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   274
// Parses YYYYMMDDTHHmm into a Date object
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   275
function parseDate(datestring) {
708
Dominique Hazaël-Massieux <dom@w3.org>
parents: 707
diff changeset
   276
    console.log("parsing date " + datestring);
338
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   277
    var ret = new Date();
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   278
    ret.setUTCFullYear(datestring.substr(0,4));
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   279
    ret.setUTCMonth(datestring.substr(4,2) - 1);
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   280
    ret.setUTCDate( datestring.substr(6,2));
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   281
    ret.setUTCHours(parseInt(datestring.substr(9,2), 10));
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   282
    ret.setUTCMinutes(datestring.substr(11,2));
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   283
    ret.setUTCSeconds(0);
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   284
    ret.setUTCMilliseconds(0);
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   285
    return ret;
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   286
};
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   287
681
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   288
function elapsedTime(time) {
685
8666474107a2 factor 1000 in time calculation
Dominique Hazaël-Massieux <dom@w3.org>
parents: 684
diff changeset
   289
    var elapsedTime = (Date.now() - time) / 1000 ;
681
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   290
    var weekDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; 
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   291
    var months = ["January", "February", "March", "April", "May",   
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   292
		  "June", "July", "August", "September", "October", "November", "December"];
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   293
    var days = Math.floor(elapsedTime / 86400);
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   294
    var hours = Math.floor((elapsedTime - (days*86400)) / 3600);
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   295
    var minutes = Math.floor((elapsedTime - (days * 86400) - (hours * 3600)) / 60);
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   296
    if (days > 7) {
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   297
	return time.getDate() + ' ' + months[time.getMonth()] + ' ' + time.getFullYear();
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   298
    } else if (days > 1) {
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   299
	return weekDays[time.getDay()] + ' (' + days.toString() + ' days ago)';
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   300
    } else if (days == 2) {
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   301
	return '2 days ago';
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   302
    } else if (days == 1) {
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   303
	return 'yesterday';
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   304
    } else if (hours > 1) {
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   305
	return hours.toString() + ' hours ago';
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   306
    } else if (hours == 1) {
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   307
	return '1 hour ago';
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   308
    } else {
684
10e3380c50f3 missing space
Dominique Hazaël-Massieux <dom@w3.org>
parents: 683
diff changeset
   309
	return minutes.toString() + ' minute' + (minutes > 1 ? 's' : '') + ' ago';
681
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   310
    }
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   311
}
9f0f6a2e4894 added helper function for views: elapsedTime
Dominique Hazaël-Massieux <dom@w3.org>
parents: 675
diff changeset
   312
487
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   313
function prepareEventsList(events) {
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   314
    var days = [];
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   315
    var timeslots = [];
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   316
    var schedule = {};
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   317
    for (var i in events) {
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   318
	events[i].timeStart.setUTCHours(events[i].timeStart.getUTCHours() + parseInt(config.schedule.timezone_offset, 10));
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   319
	events[i].timeEnd.setUTCHours(events[i].timeEnd.getUTCHours() +  parseInt(config.schedule.timezone_offset, 10));
542
9dbb9ab7b437 bug fix in listing events per day local time
Dominique Hazael-Massieux <dom@w3.org>
parents: 541
diff changeset
   320
	var day = events[i].timeStart.toDateString();
487
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   321
	var timeslot = {timeStart: events[i].timeStart , timeEnd: events[i].timeEnd}; 
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   322
	if (!schedule[day]) {
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   323
	    days.push(day);
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   324
	    schedule[day] = {};
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   325
	    timeslots[day] = [];
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   326
	}
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   327
	if (!schedule[day][JSON.stringify(timeslot)]) {
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   328
	    schedule[day][JSON.stringify(timeslot)] = [];
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   329
	    timeslots[day].push(timeslot);
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   330
	}
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   331
	schedule[day][JSON.stringify(timeslot)].push(events[i]);
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   332
    }
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   333
    return [days, timeslots, schedule];
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   334
}
22
5a1d3900dbaa support for LDAP authentication based on everyauth
Dominique Hazaël-Massieux <dom@w3.org>
parents: 21
diff changeset
   335
523
0c90bddcafc6 record who's proposing an adhoc event
Dominique Hazael-Massieux <dom@w3.org>
parents: 521
diff changeset
   336
function addEvent(req, res, next, eventType, proposedBy) {
521
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   337
    if (!req.body.name){
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   338
	req.flash("error", "Missing event name");
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   339
	next();
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   340
    } else if (!req.body.day) {
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   341
	req.flash("error", "Missing event day");
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   342
	next();
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   343
    } else if (!req.body.start) {
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   344
	req.flash("error", "Missing event start time");
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   345
	next();
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   346
    } else if (!req.body.end) {
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   347
	req.flash("error", "Missing event end time");
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   348
	next();
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   349
    } 
720
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   350
    // uuid generation from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   351
    var slug = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);});
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   352
    var event = new Event(
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   353
	{timeStart: parseDate(req.body.day + 'T' + String('0000' + (parseInt(req.body.start.replace(":",""), 10) - 100* parseInt(config.schedule.timezone_offset, 10))).slice(-4) + '00'),
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   354
	 timeEnd: parseDate(req.body.day + 'T' + String('0000' + (parseInt(req.body.end.replace(":",""), 10) - 100 * parseInt(config.schedule.timezone_offset, 10))).slice(-4) + '00'),
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   355
	 name: req.body.name,
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   356
	 presenters: req.body.presenters,
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   357
	 slug: slug,
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   358
	 confidentiality: req.body.confidentiality,
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   359
	 observers: req.body.observers,
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   360
	 eventType: eventType
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   361
	});
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   362
    if (proposedBy) {
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   363
	event.proposedBy = proposedBy._id;
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   364
    }
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   365
    if (places[req.body.room]) {
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   366
	event.room = places[req.body.room]._id;
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   367
    } else {
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   368
	req.flash('error', 'Failed to locate event “' + event.name + '” as it is set for a room with unknown shortname ' + req.body.room);			
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   369
    }
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   370
    event.save(function (err) {
521
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   371
	    if (err) {
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   372
		req.flash('error',err);
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   373
	    } else {
574
e867b1e765d9 status updates for new ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 572
diff changeset
   374
		if (event.proposedBy) {
e867b1e765d9 status updates for new ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 572
diff changeset
   375
		    emitter.emit("newevent", event);
e867b1e765d9 status updates for new ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 572
diff changeset
   376
		}
521
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   377
		req.flash('info', req.body.name + ' successfully added to schedule')	 ;
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   378
		     }
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   379
	    next();
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   380
    });
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   381
}
d9d199191c80 organizing code for allowing to add ad-hoc events
Dominique Hazael-Massieux <dom@w3.org>
parents: 518
diff changeset
   382
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   383
// Routes
380
6aa4eba4d1d5 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 373
diff changeset
   384
6aa4eba4d1d5 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 373
diff changeset
   385
app.error(function(err, req, res, next){
6aa4eba4d1d5 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 373
diff changeset
   386
  res.send(err.message, 500);
6aa4eba4d1d5 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 373
diff changeset
   387
});
6aa4eba4d1d5 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 373
diff changeset
   388
382
5c3c0fadffc6 working around express-namespace bug https://github.com/visionmedia/express-namespace/issues/5
Dominique Hazaël-Massieux <dom@w3.org>
parents: 380
diff changeset
   389
// Would be better in a post(/.*/) route but that breaks with express-namespace
5c3c0fadffc6 working around express-namespace bug https://github.com/visionmedia/express-namespace/issues/5
Dominique Hazaël-Massieux <dom@w3.org>
parents: 380
diff changeset
   390
// cf https://github.com/visionmedia/express-namespace/issues/5
5c3c0fadffc6 working around express-namespace bug https://github.com/visionmedia/express-namespace/issues/5
Dominique Hazaël-Massieux <dom@w3.org>
parents: 380
diff changeset
   391
function setFormatOutput(req) {
5c3c0fadffc6 working around express-namespace bug https://github.com/visionmedia/express-namespace/issues/5
Dominique Hazaël-Massieux <dom@w3.org>
parents: 380
diff changeset
   392
   // if the _format parameter is set, we set req.outputFormat
5c3c0fadffc6 working around express-namespace bug https://github.com/visionmedia/express-namespace/issues/5
Dominique Hazaël-Massieux <dom@w3.org>
parents: 380
diff changeset
   393
   if (req.body && req.body._format) {
5c3c0fadffc6 working around express-namespace bug https://github.com/visionmedia/express-namespace/issues/5
Dominique Hazaël-Massieux <dom@w3.org>
parents: 380
diff changeset
   394
	req.outputFormat = req.body._format;
5c3c0fadffc6 working around express-namespace bug https://github.com/visionmedia/express-namespace/issues/5
Dominique Hazaël-Massieux <dom@w3.org>
parents: 380
diff changeset
   395
   }    
5c3c0fadffc6 working around express-namespace bug https://github.com/visionmedia/express-namespace/issues/5
Dominique Hazaël-Massieux <dom@w3.org>
parents: 380
diff changeset
   396
}
5c3c0fadffc6 working around express-namespace bug https://github.com/visionmedia/express-namespace/issues/5
Dominique Hazaël-Massieux <dom@w3.org>
parents: 380
diff changeset
   397
380
6aa4eba4d1d5 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 373
diff changeset
   398
app.namespace(config.hosting.basepath, function(){
141
3bbf0988fb67 defined override output format
Dominqique Hazael-Massieux <dom@w3.org>
parents: 136
diff changeset
   399
273
368433dd9ba5 namespacing
Dominique Hazael-Massieux <dom@w3.org>
parents: 272
diff changeset
   400
app.get('/', function(req, res){
174
Dominqique Hazael-Massieux <dom@w3.org>
parents: 173
diff changeset
   401
  // skipped by middleware at this point, need fixing @@@
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   402
  People.count({}, function(err, count) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   403
      if (!count) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   404
	  // no user, need to import data
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   405
	  if (! req.loggedIn) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   406
	      return res.redirect(everyauth.password.getLoginPath());
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   407
	  } else {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   408
	      // Import basic data: people and rooms
173
227bc0f96d2f force import of data when empty db
Dominqique Hazael-Massieux <dom@w3.org>
parents: 172
diff changeset
   409
	      imports.importUserList(app.set("w3c_auth"), function(success, info, errors) {
227bc0f96d2f force import of data when empty db
Dominqique Hazael-Massieux <dom@w3.org>
parents: 172
diff changeset
   410
		  req.flash("info", "First run, importing registrants list");
227bc0f96d2f force import of data when empty db
Dominqique Hazael-Massieux <dom@w3.org>
parents: 172
diff changeset
   411
		  if (success) success.forEach(function(i) { req.flash('success',i);});
227bc0f96d2f force import of data when empty db
Dominqique Hazael-Massieux <dom@w3.org>
parents: 172
diff changeset
   412
		  if (info) info.forEach(function(i) { req.flash('info',i);});
227bc0f96d2f force import of data when empty db
Dominqique Hazael-Massieux <dom@w3.org>
parents: 172
diff changeset
   413
		  if (errors) errors.forEach(function(i) { req.flash('error',i);});
227bc0f96d2f force import of data when empty db
Dominqique Hazael-Massieux <dom@w3.org>
parents: 172
diff changeset
   414
		  res.render('index');
227bc0f96d2f force import of data when empty db
Dominqique Hazael-Massieux <dom@w3.org>
parents: 172
diff changeset
   415
	      });
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   416
	  }
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   417
      } else {
562
5320f00e655b *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 561
diff changeset
   418
	  res.render('index');
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   419
      }
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   420
  });
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   421
});
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   422
347
1a72e2c17eb7 about apge
Dominique Hazaël-Massieux <dom@w3.org>
parents: 343
diff changeset
   423
app.get('/about', function(req, res){
1a72e2c17eb7 about apge
Dominique Hazaël-Massieux <dom@w3.org>
parents: 343
diff changeset
   424
	    res.render('about', {locals: {title: 'About W3Connect'}});
1a72e2c17eb7 about apge
Dominique Hazaël-Massieux <dom@w3.org>
parents: 343
diff changeset
   425
});
1a72e2c17eb7 about apge
Dominique Hazaël-Massieux <dom@w3.org>
parents: 343
diff changeset
   426
236
07f5a4a05666 factored out post rendering
Dominqique Hazael-Massieux <dom@w3.org>
parents: 235
diff changeset
   427
app.post('/admin/', function(req, res, next){
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   428
  if (req.body.peopleUpdate !== undefined) {
159
a1509663b873 remove need for pre-auth in db config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 141
diff changeset
   429
    if (! req.loggedIn) {
a1509663b873 remove need for pre-auth in db config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 141
diff changeset
   430
      return res.redirect(everyauth.password.getLoginPath());
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   431
    } else if (!app.set("w3c_auth")) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   432
	// in case user logged in a previous session
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   433
	// should find how to logout?
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   434
	return res.redirect(everyauth.password.getLoginPath());
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   435
    }
338
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   436
    var isAdmin = new RegExp("^" + config.admin.login.replace(",","|") + "$");
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   437
    if (!isAdmin.test(req.user.login)) {
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   438
	return res.render("403");
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   439
    }    
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   440
      imports.importUserList(app.set("w3c_auth"), function(success, info, errors) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   441
	  if (success) success.forEach(function(i) { req.flash('success',i);});
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   442
	  if (info) info.forEach(function(i) { req.flash('info',i);});
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   443
	  if (errors) errors.forEach(function(i) { req.flash('error',i);});
234
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   444
	  next();
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents: 166
diff changeset
   445
      });
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   446
  } else if (req.body.registrationUpdate !== undefined) {
452
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 451
diff changeset
   447
      imports.importRegistrationData(app.set("w3c_auth"), function(success, info, errors) {
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 451
diff changeset
   448
	  if (success) success.forEach(function(i) { req.flash('success',i);});
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 451
diff changeset
   449
	  if (info) info.forEach(function(i) { req.flash('info',i);});
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 451
diff changeset
   450
	  if (errors) errors.forEach(function(i) { req.flash('error',i);});
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 451
diff changeset
   451
	  next();
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 451
diff changeset
   452
      });    
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   453
  } else if (req.body.clearInterested !== undefined) {
464
b71c877d2655 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 463
diff changeset
   454
      var counter = 0;
461
11ac0d829eb0 kill switch for bogus data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 452
diff changeset
   455
      Event.find(
11ac0d829eb0 kill switch for bogus data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 452
diff changeset
   456
	  {}, 
462
355332143a46 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 461
diff changeset
   457
	  function(err, events) {
355332143a46 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 461
diff changeset
   458
	      for (e in events) {
355332143a46 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 461
diff changeset
   459
		  var event = events[e];
355332143a46 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 461
diff changeset
   460
		  event.interested = [];
464
b71c877d2655 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 463
diff changeset
   461
		  event.save(
b71c877d2655 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 463
diff changeset
   462
		      function(err) {
b71c877d2655 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 463
diff changeset
   463
			  counter++;
b71c877d2655 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 463
diff changeset
   464
			  if (counter == events.length) {
b71c877d2655 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 463
diff changeset
   465
			      req.flash("info", "Cleared");
b71c877d2655 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 463
diff changeset
   466
			      next();			      
b71c877d2655 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 463
diff changeset
   467
			  }
b71c877d2655 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 463
diff changeset
   468
		      });
462
355332143a46 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 461
diff changeset
   469
	      }
461
11ac0d829eb0 kill switch for bogus data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 452
diff changeset
   470
	  });
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   471
  } else  if (req.body.placeAdd !== undefined) {
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   472
      var place = new Place();
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   473
      place.shortname = req.body.shortname;
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   474
      place.name = req.body.name;
234
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   475
      place.save(
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   476
	  function(err) {
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   477
	      if (err) {
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   478
		  req.flash("error", "Error while adding new place: " + err);
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   479
	      } else {
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   480
		  req.flash("success", "New place added: " + p.name);
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   481
	      }
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   482
	      next();
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   483
	  }
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   484
      );
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   485
  } else if (req.body.placesUpdate !== undefined) {
251
Dominqique Hazael-Massieux <dom@w3.org>
parents: 250
diff changeset
   486
      var url = require("url").parse(config.map.rooms_json);
250
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   487
      var http;
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   488
      if (url.protocol == "http:") {
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   489
	  http = require('http');
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   490
      } else if (url.protocol == "https:") {
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   491
	  http = require('https');
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   492
      } else {
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   493
	  req.flash("error", "Unrecognized protocol for room descriptions: " + config.map.rooms_json);
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   494
	  next();
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   495
      }
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   496
      if (http) {
338
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   497
	  var request = http.get({host: url.hostname, port: url.port , path: url.pathname + (url.search ? url.search : '') + (url.hash ? url.hash : '')}, function (response) {
250
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   498
	      response.setEncoding('utf8');
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   499
	      var placesJSON = "", placesData;
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   500
	      response.on('data', function (chunk) {
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   501
		  placesJSON = placesJSON + chunk;
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   502
	      });
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   503
	      response.on('end', function () {
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   504
		  Place.find({}).remove();
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   505
		  placesData = JSON.parse(placesJSON);
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   506
		  for (i in placesData) {
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   507
		      var p = new Place(placesData[i]);
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   508
		      var counter = 0;
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   509
		      var addCounter = 0;
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   510
		      p.save(function (err) {
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   511
			  counter++;
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   512
			  if (err) {
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   513
			      req.flash('error',err);
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   514
			  } else {
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   515
			      addCounter++;
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   516
			  }
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   517
			  if (counter == placesData.length) {
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   518
			      next();
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   519
			  }
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   520
		      });
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   521
		  }
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   522
	      });
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   523
	  });
fddcfcdaa704 moved url of json rooms file to config
Dominqique Hazael-Massieux <dom@w3.org>
parents: 248
diff changeset
   524
      }
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   525
  } else if (req.body.twitterSetting !== undefined) {
338
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   526
      if (!req.body.username) {
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   527
	  req.flash("error", "Missing Twitter username");
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   528
	  next();
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   529
      } else if (!req.body.password) {
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   530
	  req.flash("error", "Missing Twitter password");
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   531
	  next();
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   532
      }
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   533
      TwitterSettings.findOne(
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   534
      {}, 
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   535
      function(err, settings) {
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   536
	  if (!settings) {
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   537
	      settings = new TwitterSettings();
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   538
	  }
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   539
	  settings.username = req.body.username;
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   540
	  settings.password = req.body.password;
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   541
	  settings.save(function(err) {
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   542
	      if (err)  { 
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   543
		  req.flash("error", "Saving Twitter settings failed with error " + err);
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   544
	      } else {
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   545
		  req.flash("success", "Successfully saved Twitter settings");
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   546
	      }
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   547
	      next();
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   548
	  });
b08bcf2e5554 importing code from w3connect@OTA
Dominique Hazaël-Massieux <dom@w3.org>
parents: 324
diff changeset
   549
      });
416
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   550
   } else {
234
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   551
      next();
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   552
  }
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   553
});
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   554
234
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   555
app.all('/admin/', function(req, res){
416
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   556
    res.render('admin/index', {locals: { title: 'Administration'}});
234
8b177b95f0c7 avoid duplication of render for admin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 231
diff changeset
   557
});
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   558
369
92ea985ea3f3 changed url for user profiles
Dominique Hazaël-Massieux <dom@w3.org>
parents: 367
diff changeset
   559
app.post('/people/profile/:id.:format?', function(req, res, next){
382
5c3c0fadffc6 working around express-namespace bug https://github.com/visionmedia/express-namespace/issues/5
Dominique Hazaël-Massieux <dom@w3.org>
parents: 380
diff changeset
   560
    setFormatOutput(req);
210
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   561
    if (! req.loggedIn) {
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   562
      return res.redirect(everyauth.password.getLoginPath());
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   563
    } else {
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   564
	if (req.body.updateProfile !== undefined && req.body.twitter && req.user.slug == req.params.id) {
541
0f3d9194548c *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 540
diff changeset
   565
	    twitterAccount = req.body.twitter.replace(/^@/, '');
540
66035fb204cb remove leading @ on twitter accounts
Dominique Hazael-Massieux <dom@w3.org>
parents: 538
diff changeset
   566
	    twitter.getTwitterId(twitterAccount, function(err, id) {
210
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   567
		if (err) {
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   568
		    req.flash("error", err);
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   569
		    next();
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   570
		} else {
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   571
		    People.findOne({slug: req.params.id}).exec( function(err, indiv) {
210
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   572
			if (err) {
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   573
			    next();
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   574
			} else {
540
66035fb204cb remove leading @ on twitter accounts
Dominique Hazael-Massieux <dom@w3.org>
parents: 538
diff changeset
   575
			    indiv.twitterAccount = {"name": twitterAccount, id: id};
210
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   576
			    indiv.save(function(err) {
223
a813b90b2412 update list of twitter ids on update of twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 217
diff changeset
   577
				// re-start twitter listener
485
885ff23e3ec9 trying to fix twitter insert
Dominique Hazael-Massieux <dom@w3.org>
parents: 484
diff changeset
   578
				emitter.emit("twitterListChange", id);
210
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   579
				if (!err) {
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   580
				    req.flash("success", "Successfully added your twitter account");
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   581
				} else {
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   582
				    req.flash("error", "Failed to add your twitter account: " + err);
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   583
				}
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   584
				next();
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   585
			    });
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   586
			}
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   587
		    });
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   588
		}
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   589
	    });
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   590
	} else {
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   591
	    next();
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   592
	}
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   593
    }
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   594
});
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   595
dc44f5d58e90 add support for adding twitter account
Dominqique Hazael-Massieux <dom@w3.org>
parents: 197
diff changeset
   596
369
92ea985ea3f3 changed url for user profiles
Dominique Hazaël-Massieux <dom@w3.org>
parents: 367
diff changeset
   597
app.all('/people/profile/:id.:format?', function(req, res, next){
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   598
    People.findOne({slug: req.params.id}).populate('affiliation', ['slug', 'name']).exec( function(err, indiv) {
164
64315ae73440 404 on unknown people id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 163
diff changeset
   599
	if (indiv) {
487
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   600
	    Event.find({})
536
6f57df84df8b populate proposer for events
Dominique Hazael-Massieux <dom@w3.org>
parents: 532
diff changeset
   601
	    	.populate('proposedBy')
489
55fa086eb210 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 487
diff changeset
   602
		//.$where('RegExp("^" + this.interested.join("|") + "$").test(' + indiv._id + ')')
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   603
	        .sort('timeStart', 1)
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   604
		.exec(function(err, events) {
487
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   605
		    var days, timeslots, schedule;
489
55fa086eb210 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 487
diff changeset
   606
		    var userEvents = [];
487
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   607
		    if (events) {
489
55fa086eb210 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 487
diff changeset
   608
			for (var i = 0; i < events.length ; i++) {
55fa086eb210 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 487
diff changeset
   609
			    var isInterested = new RegExp("^"  + events[i].interested.join("|") + "$");
55fa086eb210 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 487
diff changeset
   610
			    if (isInterested.test(indiv._id)) {
55fa086eb210 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 487
diff changeset
   611
				userEvents.push(events[i]);
55fa086eb210 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 487
diff changeset
   612
			    }
55fa086eb210 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 487
diff changeset
   613
			}
55fa086eb210 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 487
diff changeset
   614
			var data = prepareEventsList(userEvents);
487
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   615
			days = data[0];
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   616
			timeslots = data[1];
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   617
			schedule = data[2];
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   618
		    }
571
9d24eecadaf1 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 569
diff changeset
   619
		    Status.find({"author": indiv})
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   620
			.sort("time", -1)
566
53a52387f9f2 adding recent activity to user profiles
Dominique Hazael-Massieux <dom@w3.org>
parents: 564
diff changeset
   621
			.limit(20)
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   622
			.exec(function(err, statusupdates) {
566
53a52387f9f2 adding recent activity to user profiles
Dominique Hazael-Massieux <dom@w3.org>
parents: 564
diff changeset
   623
			    switch (req.params.format) {
53a52387f9f2 adding recent activity to user profiles
Dominique Hazael-Massieux <dom@w3.org>
parents: 564
diff changeset
   624
				// When json, generate suitable data
53a52387f9f2 adding recent activity to user profiles
Dominique Hazael-Massieux <dom@w3.org>
parents: 564
diff changeset
   625
			    case 'json':
53a52387f9f2 adding recent activity to user profiles
Dominique Hazael-Massieux <dom@w3.org>
parents: 564
diff changeset
   626
				res.send(indiv);
53a52387f9f2 adding recent activity to user profiles
Dominique Hazael-Massieux <dom@w3.org>
parents: 564
diff changeset
   627
				break;
53a52387f9f2 adding recent activity to user profiles
Dominique Hazael-Massieux <dom@w3.org>
parents: 564
diff changeset
   628
			    default:
53a52387f9f2 adding recent activity to user profiles
Dominique Hazael-Massieux <dom@w3.org>
parents: 564
diff changeset
   629
				res.render('people/indiv.ejs', { locals: { indiv: indiv, title: indiv.given + ' ' + indiv.family, days: days, timeslots: timeslots, schedule:schedule, statusupdates: statusupdates }});
53a52387f9f2 adding recent activity to user profiles
Dominique Hazael-Massieux <dom@w3.org>
parents: 564
diff changeset
   630
			    }
53a52387f9f2 adding recent activity to user profiles
Dominique Hazael-Massieux <dom@w3.org>
parents: 564
diff changeset
   631
			});
487
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   632
		});
164
64315ae73440 404 on unknown people id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 163
diff changeset
   633
	} else {
64315ae73440 404 on unknown people id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 163
diff changeset
   634
	    next();
64315ae73440 404 on unknown people id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 163
diff changeset
   635
	}
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   636
  });  
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   637
});
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   638
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   639
app.get('/locations.:format?', function(req, res) {
120
505e250e5674 sending checkedin with places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 118
diff changeset
   640
    var counter=0;
722
3578e7e14654 bug fix in change of structure for places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 721
diff changeset
   641
    for (var p in places) {
253
79e27cd98c39 replacing w3cId by slug
Dominqique Hazael-Massieux <dom@w3.org>
parents: 251
diff changeset
   642
      People.find({"lastKnownPosition.shortname": places[p].shortname}, ['slug', 'given', 'family', 'picture_thumb'],  (function(place) { return function(err, people) {
120
505e250e5674 sending checkedin with places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 118
diff changeset
   643
         counter++;
505e250e5674 sending checkedin with places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 118
diff changeset
   644
         place.checkedin = people;
722
3578e7e14654 bug fix in change of structure for places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 721
diff changeset
   645
         if (counter==Object.keys(places).length) {	     
120
505e250e5674 sending checkedin with places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 118
diff changeset
   646
          switch (req.params.format) {
505e250e5674 sending checkedin with places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 118
diff changeset
   647
          // When json, generate suitable data
505e250e5674 sending checkedin with places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 118
diff changeset
   648
           case 'json':
505e250e5674 sending checkedin with places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 118
diff changeset
   649
	     res.send(places);
505e250e5674 sending checkedin with places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 118
diff changeset
   650
	     break;
505e250e5674 sending checkedin with places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 118
diff changeset
   651
           default:
339
09b79816daee setting page titles
Dominique Hazaël-Massieux <dom@w3.org>
parents: 338
diff changeset
   652
             res.render('locations/index.ejs', { locals: { places: places, title: 'Rooms'}});
120
505e250e5674 sending checkedin with places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 118
diff changeset
   653
           }
505e250e5674 sending checkedin with places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 118
diff changeset
   654
	 }
505e250e5674 sending checkedin with places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 118
diff changeset
   655
      };})(places[p]));
505e250e5674 sending checkedin with places
Dominique Hazaël-Massieux <dom@w3.org>
parents: 118
diff changeset
   656
    }		    
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   657
});
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   658
124
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
   659
app.get('/locations/stream', function(req, res) {
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
   660
    res.setHeader("Content-Type", 'text/event-stream');
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
   661
    res.setHeader("Cache-Control", "no-cache");
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
   662
    res.setHeader("Connection", "keep-alive");
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
   663
    res.writeHead(200);
343
1ffd3660207c miliseconds vs seconds
Dominique Hazaël-Massieux <dom@w3.org>
parents: 342
diff changeset
   664
    setInterval(function() { res.write(":\n"); }, 30000);
124
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
   665
    emitter.on("checkin", function(user, left, entered) {
134
3ab2d5725dc6 in checkin event, inform if this is an event of the logged in user
Dominqique Hazael-Massieux <dom@w3.org>
parents: 130
diff changeset
   666
	res.write("data: " + JSON.stringify({"user": user, "left": left, "entered": entered, "you": (req.user && JSON.stringify(user._id) == JSON.stringify(req.user._id))}) + "\n\n");
124
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
   667
    });
188
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   668
    emitter.on("tweet", function(tweet) {
214
c02d78f59e45 send data about location of tweeting user (if known)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 213
diff changeset
   669
	People.findOne(
217
91a48321122a bug fix
Dominqique Hazael-Massieux <dom@w3.org>
parents: 214
diff changeset
   670
	    {"twitterAccount.id":tweet.user.id},
91a48321122a bug fix
Dominqique Hazael-Massieux <dom@w3.org>
parents: 214
diff changeset
   671
	    ["lastKnownPosition"],
214
c02d78f59e45 send data about location of tweeting user (if known)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 213
diff changeset
   672
	    function(err, indiv) {
c02d78f59e45 send data about location of tweeting user (if known)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 213
diff changeset
   673
		res.write("event: tweet\n");
c02d78f59e45 send data about location of tweeting user (if known)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 213
diff changeset
   674
		tweet.position = "";
c02d78f59e45 send data about location of tweeting user (if known)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 213
diff changeset
   675
		if (indiv && indiv.lastKnownPosition && indiv.lastKnownPosition.shortname) {
c02d78f59e45 send data about location of tweeting user (if known)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 213
diff changeset
   676
		    tweet.position = indiv.lastKnownPosition.shortname;
c02d78f59e45 send data about location of tweeting user (if known)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 213
diff changeset
   677
		}
c02d78f59e45 send data about location of tweeting user (if known)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 213
diff changeset
   678
		res.write("data: " + JSON.stringify(tweet) + "\n\n");		
c02d78f59e45 send data about location of tweeting user (if known)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 213
diff changeset
   679
	    });
188
bc0c9a5c85f5 now loading twitter ids at start up
Dominique Hazaël-Massieux <dom@w3.org>
parents: 181
diff changeset
   680
    });
124
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
   681
});
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
   682
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   683
236
07f5a4a05666 factored out post rendering
Dominqique Hazael-Massieux <dom@w3.org>
parents: 235
diff changeset
   684
app.post('/locations/:id.:format?', function(req, res, next) {
382
5c3c0fadffc6 working around express-namespace bug https://github.com/visionmedia/express-namespace/issues/5
Dominique Hazaël-Massieux <dom@w3.org>
parents: 380
diff changeset
   685
  setFormatOutput(req);
32
e7585ff50173 more progress toward taxi pool management
Dominique Hazaël-Massieux <dom@w3.org>
parents: 27
diff changeset
   686
  if (! req.loggedIn) {
e7585ff50173 more progress toward taxi pool management
Dominique Hazaël-Massieux <dom@w3.org>
parents: 27
diff changeset
   687
    req.session.redirectTo = '/locations/' + req.params.id;
e7585ff50173 more progress toward taxi pool management
Dominique Hazaël-Massieux <dom@w3.org>
parents: 27
diff changeset
   688
    return res.redirect(everyauth.password.getLoginPath());
e7585ff50173 more progress toward taxi pool management
Dominique Hazaël-Massieux <dom@w3.org>
parents: 27
diff changeset
   689
  }
720
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   690
    var place = places[req.params.id];
4
dd0a9d7e557b prototype of checkin
Dominique Hazaël-Massieux <dom@w3.org>
parents: 2
diff changeset
   691
    if (place) {
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   692
	if ((req.body.checkin !== undefined || req.body.checkout !== undefined) && req.user) {
176
26883d8ed1f1 don't record check in when already at location
Dominqique Hazael-Massieux <dom@w3.org>
parents: 174
diff changeset
   693
	    // the user is already checked in in that place
538
26e7cacae8f9 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 537
diff changeset
   694
	    var actionNeeded = true;
176
26883d8ed1f1 don't record check in when already at location
Dominqique Hazael-Massieux <dom@w3.org>
parents: 174
diff changeset
   695
	    if (req.user.lastKnownPosition.shortname == place.shortname) {
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   696
		if (req.body.checkin !== undefined) {
538
26e7cacae8f9 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 537
diff changeset
   697
		    actionNeeded = false;
513
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   698
		    switch (req.outputFormat) {
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   699
		    case "json":
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   700
			res.send(JSON.stringify({success: "Already checked in at " + place.name}));
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   701
		    
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   702
			break;
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   703
		    default:
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   704
			req.flash('info', "You’re already checked in at " + place.name);
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   705
			next();
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   706
		    }
176
26883d8ed1f1 don't record check in when already at location
Dominqique Hazael-Massieux <dom@w3.org>
parents: 174
diff changeset
   707
		}
26883d8ed1f1 don't record check in when already at location
Dominqique Hazael-Massieux <dom@w3.org>
parents: 174
diff changeset
   708
	    } else {
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   709
		if (req.body.checkout !== undefined) {
538
26e7cacae8f9 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 537
diff changeset
   710
		    actionNeeded = false;
513
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   711
		    switch (req.outputFormat) {
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   712
		    case "json":
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   713
			res.send(JSON.stringify({error: "You’re not checked in at " + place.name + ", so you can’t checkout from it"}));
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   714
		    
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   715
			break;
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   716
		    default:
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   717
			req.flash('info', "You’re not checked in at " + place.name + ", so you can’t checkout from it");
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   718
			next();
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   719
		    }
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   720
		}
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   721
	    }
538
26e7cacae8f9 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 537
diff changeset
   722
	    if (actionNeeded) {
513
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   723
	    var indiv = req.user ;
130
e0a634edfd36 bug fix in reporting previous location
Dominqique Hazael-Massieux <dom@w3.org>
parents: 124
diff changeset
   724
	    var prevPosition = {shortname: indiv.lastKnownPosition.shortname,
e0a634edfd36 bug fix in reporting previous location
Dominqique Hazael-Massieux <dom@w3.org>
parents: 124
diff changeset
   725
				name: indiv.lastKnownPosition.name,
e0a634edfd36 bug fix in reporting previous location
Dominqique Hazael-Massieux <dom@w3.org>
parents: 124
diff changeset
   726
				time: indiv.lastKnownPosition.time};
517
6ae34bcd087c *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 514
diff changeset
   727
	    var newPosition = {shortname: null, name: null, time: Date.now()};
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   728
	    if (req.body.checkin !== undefined) {
513
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   729
		newPosition.shortname = place.shortname;
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   730
		newPosition.name = place.name;
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   731
	    }
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   732
	    indiv.lastKnownPosition = newPosition; 
24
f0b89064c8ac check in now use authentication data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 23
diff changeset
   733
	   indiv.save(function(err) {
40
cd5e5f86c292 using flash info and error
Dominique Hazaël-Massieux <dom@w3.org>
parents: 39
diff changeset
   734
             req.flash('error',err);
124
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
   735
	       if (!err) {
513
7a6c0c6f9f27 support for checkout
Dominique Hazael-Massieux <dom@w3.org>
parents: 509
diff changeset
   736
		   emitter.emit("checkin", req.user, prevPosition, newPosition);
124
92ab27df6a3d server sent events for checkins :)
Dominqique Hazael-Massieux <dom@w3.org>
parents: 120
diff changeset
   737
	       }
141
3bbf0988fb67 defined override output format
Dominqique Hazael-Massieux <dom@w3.org>
parents: 136
diff changeset
   738
               switch (req.outputFormat) {
136
822ac572bbed build JSON response to checkin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 134
diff changeset
   739
                 case 'json':
822ac572bbed build JSON response to checkin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 134
diff changeset
   740
		   if (!err) { 
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   741
                       res.send(JSON.stringify({success: (req.body.checkin !== undefined ? 'Checked in at ' + newPosition.name : "Checked out")}));
136
822ac572bbed build JSON response to checkin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 134
diff changeset
   742
		   } else {
537
b9b1e07b18c4 bug fix
Dominique Hazael-Massieux <dom@w3.org>
parents: 536
diff changeset
   743
		       res.send(JSON.stringify({error: err}));
136
822ac572bbed build JSON response to checkin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 134
diff changeset
   744
		   }
822ac572bbed build JSON response to checkin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 134
diff changeset
   745
    	           break;
822ac572bbed build JSON response to checkin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 134
diff changeset
   746
                 default:
822ac572bbed build JSON response to checkin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 134
diff changeset
   747
		   if (!err) {
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   748
		       req.flash('info', (req.body.checkin !== undefined ? 'Checked in at '  + newPosition.name : "Checked out"));
136
822ac572bbed build JSON response to checkin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 134
diff changeset
   749
		   } else {
822ac572bbed build JSON response to checkin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 134
diff changeset
   750
		       req.flash('error', err);
822ac572bbed build JSON response to checkin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 134
diff changeset
   751
		   }
235
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   752
		   next();
136
822ac572bbed build JSON response to checkin
Dominqique Hazael-Massieux <dom@w3.org>
parents: 134
diff changeset
   753
	       }
4
dd0a9d7e557b prototype of checkin
Dominique Hazaël-Massieux <dom@w3.org>
parents: 2
diff changeset
   754
	   });
538
26e7cacae8f9 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 537
diff changeset
   755
	    }
4
dd0a9d7e557b prototype of checkin
Dominique Hazaël-Massieux <dom@w3.org>
parents: 2
diff changeset
   756
	} else {
235
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   757
	    next();
4
dd0a9d7e557b prototype of checkin
Dominique Hazaël-Massieux <dom@w3.org>
parents: 2
diff changeset
   758
	}
dd0a9d7e557b prototype of checkin
Dominique Hazaël-Massieux <dom@w3.org>
parents: 2
diff changeset
   759
   } else {
235
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   760
       next();
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   761
   }
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   762
});
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   763
508
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   764
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   765
235
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   766
app.all('/locations/:id.:format?', function(req, res) {
720
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   767
    var place = places[req.params.id];
235
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   768
    if (place) {
591
affd07cc6d02 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 584
diff changeset
   769
	People.find({"lastKnownPosition.shortname": place.shortname}, ['slug', 'given', 'family', 'picture_thumb', 'lastKnownPosition'])
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   770
	    .exec(function(err, people) {
592
33b795eb1560 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 591
diff changeset
   771
	people.sort(function (a,b) { return (a.lastKnownPosition.time > b.lastKnownPosition.time ? -1 : (b.lastKnownPosition.time  > a.lastKnownPosition.time ? 1 : 0));});
359
3189580e456d added support for current event in a given room
Dominique Hazaël-Massieux <dom@w3.org>
parents: 347
diff changeset
   772
	  var current = new Date();
3189580e456d added support for current event in a given room
Dominique Hazaël-Massieux <dom@w3.org>
parents: 347
diff changeset
   773
	  if (req.query.datetime) {
3189580e456d added support for current event in a given room
Dominique Hazaël-Massieux <dom@w3.org>
parents: 347
diff changeset
   774
	      current = parseDate(req.query.datetime);
3189580e456d added support for current event in a given room
Dominique Hazaël-Massieux <dom@w3.org>
parents: 347
diff changeset
   775
	      if (isNaN(current.getTime())) {
3189580e456d added support for current event in a given room
Dominique Hazaël-Massieux <dom@w3.org>
parents: 347
diff changeset
   776
		  current = new Date();
3189580e456d added support for current event in a given room
Dominique Hazaël-Massieux <dom@w3.org>
parents: 347
diff changeset
   777
	      }
3189580e456d added support for current event in a given room
Dominique Hazaël-Massieux <dom@w3.org>
parents: 347
diff changeset
   778
	  }
3189580e456d added support for current event in a given room
Dominique Hazaël-Massieux <dom@w3.org>
parents: 347
diff changeset
   779
	  Event.findOne({"room": place._id})
3189580e456d added support for current event in a given room
Dominique Hazaël-Massieux <dom@w3.org>
parents: 347
diff changeset
   780
	      .where('timeStart').lte(current)
3189580e456d added support for current event in a given room
Dominique Hazaël-Massieux <dom@w3.org>
parents: 347
diff changeset
   781
	      .where('timeEnd').gte(current)
536
6f57df84df8b populate proposer for events
Dominique Hazael-Massieux <dom@w3.org>
parents: 532
diff changeset
   782
	      .populate('proposedBy')
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   783
		.exec( 
359
3189580e456d added support for current event in a given room
Dominique Hazaël-Massieux <dom@w3.org>
parents: 347
diff changeset
   784
		    function(err, event) {	  
3189580e456d added support for current event in a given room
Dominique Hazaël-Massieux <dom@w3.org>
parents: 347
diff changeset
   785
235
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   786
        switch (req.params.format) {
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   787
	    // When json, generate suitable data
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   788
	case 'json':
239
af6d258e9d8b send actual info about checkedin users
Dominique Hazaël-Massieux <dom@w3.org>
parents: 236
diff changeset
   789
	    var placeData = place.toObject();
af6d258e9d8b send actual info about checkedin users
Dominique Hazaël-Massieux <dom@w3.org>
parents: 236
diff changeset
   790
	    placeData.checkedin = people.map(function(x) { return x.toObject();});
af6d258e9d8b send actual info about checkedin users
Dominique Hazaël-Massieux <dom@w3.org>
parents: 236
diff changeset
   791
            res.send(placeData);
235
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   792
	    break;
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   793
	default:
367
7cf1fa09f984 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 366
diff changeset
   794
	    res.render('locations/place.ejs', { locals: { place: place, people: people, title: place.name, places:places, event: event}});
235
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   795
	}
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   796
      });
366
623c4b7a7639 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 365
diff changeset
   797
    });
365
96dddcd80fff *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 364
diff changeset
   798
235
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   799
    } else {
339
09b79816daee setting page titles
Dominique Hazaël-Massieux <dom@w3.org>
parents: 338
diff changeset
   800
       res.render('locations/unknown.ejs', {locals: { shortname: req.params.id, title: 'Unknown location'}});
4
dd0a9d7e557b prototype of checkin
Dominique Hazaël-Massieux <dom@w3.org>
parents: 2
diff changeset
   801
   }
dd0a9d7e557b prototype of checkin
Dominique Hazaël-Massieux <dom@w3.org>
parents: 2
diff changeset
   802
});
dd0a9d7e557b prototype of checkin
Dominique Hazaël-Massieux <dom@w3.org>
parents: 2
diff changeset
   803
508
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   804
    app.post("/locations/:id/admin", function(req,res, next) {
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   805
	if (req.body.placeUpdate !== undefined) {
508
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   806
	    if (!req.body.name) {
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   807
		req.flash("error", "Missing name of room to update");
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   808
		next();
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   809
	    }
720
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   810
	    var place = places[req.params.id];
508
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   811
		    if (!place) {
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   812
			req.flash("error", "No room found with shortname " + req.params.id);
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   813
			next();
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   814
		    }
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   815
		    place.name = req.body.name;
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   816
		    place.save(
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   817
			function(err) {
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   818
			    if (err) {
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   819
				req.flash("error", err);
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   820
			    } else {
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   821
				req.flash("success", "Room successfully updated");
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   822
			    }
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   823
			    next();
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   824
			});
509
489516c346aa *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 508
diff changeset
   825
	} else {
489516c346aa *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 508
diff changeset
   826
	    next();
508
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   827
	}
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   828
    });
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   829
	    
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   830
    app.all("/locations/:id/admin", function(req,res, next) {
720
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   831
	var place = places[req.params.id];
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   832
	if (place) {
508
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   833
		    res.render("locations/admin.ejs", {locals: {place: place}});
720
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   834
	} else {
508
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   835
		    next();
720
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
   836
	}
508
f1d504a4e490 support for updating room names
Dominique Hazael-Massieux <dom@w3.org>
parents: 489
diff changeset
   837
    });
235
d80e98a065b0 avoid duplication of render for locations/:id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 234
diff changeset
   838
382
5c3c0fadffc6 working around express-namespace bug https://github.com/visionmedia/express-namespace/issues/5
Dominique Hazaël-Massieux <dom@w3.org>
parents: 380
diff changeset
   839
app.get('/people/:letter?.:format?', function (req, res, next){
371
17bcc8e845c5 paging list of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 369
diff changeset
   840
  var letter = req.params.letter;
17bcc8e845c5 paging list of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 369
diff changeset
   841
  if (!letter) {
17bcc8e845c5 paging list of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 369
diff changeset
   842
      letter = "a";
17bcc8e845c5 paging list of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 369
diff changeset
   843
  } 
17bcc8e845c5 paging list of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 369
diff changeset
   844
  if  (!(letter=='all' || letter.match(/^[a-z]$/))) {
17bcc8e845c5 paging list of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 369
diff changeset
   845
      next();
17bcc8e845c5 paging list of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 369
diff changeset
   846
  }
17bcc8e845c5 paging list of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 369
diff changeset
   847
  var people = People.find({})
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   848
		.exec(function (err, people) {
686
d622a5df9a5c computer number of people per letter
Dominique Hazaël-Massieux <dom@w3.org>
parents: 685
diff changeset
   849
    var activeLetters = {};
688
bda0984629b3 fix filtering of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 686
diff changeset
   850
    var letterPeople = [];
bda0984629b3 fix filtering of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 686
diff changeset
   851
    people.sort(function (a,b) { return (a.family > b.family ? 1 : (b.family > a.family ? -1 : 0));});	
686
d622a5df9a5c computer number of people per letter
Dominique Hazaël-Massieux <dom@w3.org>
parents: 685
diff changeset
   852
    for (var p in people) {
d622a5df9a5c computer number of people per letter
Dominique Hazaël-Massieux <dom@w3.org>
parents: 685
diff changeset
   853
	activeLetters[people[p].family[0]]++;
688
bda0984629b3 fix filtering of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 686
diff changeset
   854
	if (letter != 'all' && people[p].family[0].toLowerCase() == letter.toLowerCase()) {
bda0984629b3 fix filtering of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 686
diff changeset
   855
	    letterPeople.push(people[p]);
bda0984629b3 fix filtering of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 686
diff changeset
   856
	}
bda0984629b3 fix filtering of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 686
diff changeset
   857
    }
bda0984629b3 fix filtering of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 686
diff changeset
   858
    if (letter == 'all') {
bda0984629b3 fix filtering of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 686
diff changeset
   859
	letterPeople = people;
686
d622a5df9a5c computer number of people per letter
Dominique Hazaël-Massieux <dom@w3.org>
parents: 685
diff changeset
   860
    }
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   861
    switch (req.params.format) {
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   862
      // When json, generate suitable data
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   863
      case 'json':
688
bda0984629b3 fix filtering of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 686
diff changeset
   864
        res.send(letterPeople);
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   865
	break;
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   866
      default:
688
bda0984629b3 fix filtering of people
Dominique Hazaël-Massieux <dom@w3.org>
parents: 686
diff changeset
   867
        res.render('people/index.ejs', { locals: { activeLetters: activeLetters, letter: letter, people: letterPeople, title: 'People — ' + letter}});
0
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   868
    }
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   869
  });
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   870
  
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   871
});
e7f093ebd304 import of initial code for prototype of node.js based server for tpac web app
Dominique Hazaël-Massieux <dom@w3.org>
parents:
diff changeset
   872
58
794728d54fc9 show list of orgs
Dominqique Hazael-Massieux <dom@w3.org>
parents: 56
diff changeset
   873
app.get('/orgs.:format?', function (req, res){
304
64620be50c1f sort employees
Dominique Hazaël-Massieux <dom@w3.org>
parents: 297
diff changeset
   874
  var orgs = Organization.find({})
64620be50c1f sort employees
Dominique Hazaël-Massieux <dom@w3.org>
parents: 297
diff changeset
   875
		.populate('employees', ['login'])
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   876
		.exec( function (err, orgs) {
58
794728d54fc9 show list of orgs
Dominqique Hazael-Massieux <dom@w3.org>
parents: 56
diff changeset
   877
    orgs.sort(function (a,b) { return (a.name > b.name ? 1 : (b.name > a.name ? -1 : 0));});
794728d54fc9 show list of orgs
Dominqique Hazael-Massieux <dom@w3.org>
parents: 56
diff changeset
   878
    switch (req.params.format) {
794728d54fc9 show list of orgs
Dominqique Hazael-Massieux <dom@w3.org>
parents: 56
diff changeset
   879
      // When json, generate suitable data
794728d54fc9 show list of orgs
Dominqique Hazael-Massieux <dom@w3.org>
parents: 56
diff changeset
   880
      case 'json':
305
Dominique Hazaël-Massieux <dom@w3.org>
parents: 304
diff changeset
   881
        res.send(orgs);
58
794728d54fc9 show list of orgs
Dominqique Hazael-Massieux <dom@w3.org>
parents: 56
diff changeset
   882
	break;
794728d54fc9 show list of orgs
Dominqique Hazael-Massieux <dom@w3.org>
parents: 56
diff changeset
   883
      default:
339
09b79816daee setting page titles
Dominique Hazaël-Massieux <dom@w3.org>
parents: 338
diff changeset
   884
        res.render('orgs/index.ejs', { locals: { orgs: orgs, title: 'Organizations' }});
58
794728d54fc9 show list of orgs
Dominqique Hazael-Massieux <dom@w3.org>
parents: 56
diff changeset
   885
    }
794728d54fc9 show list of orgs
Dominqique Hazael-Massieux <dom@w3.org>
parents: 56
diff changeset
   886
  });  
794728d54fc9 show list of orgs
Dominqique Hazael-Massieux <dom@w3.org>
parents: 56
diff changeset
   887
});
794728d54fc9 show list of orgs
Dominqique Hazael-Massieux <dom@w3.org>
parents: 56
diff changeset
   888
165
f90ad174185e *** empty log message ***
Dominqique Hazael-Massieux <dom@w3.org>
parents: 164
diff changeset
   889
app.get('/orgs/:id.:format?', function(req, res, next){
253
79e27cd98c39 replacing w3cId by slug
Dominqique Hazael-Massieux <dom@w3.org>
parents: 251
diff changeset
   890
    Organization.findOne({slug: req.params.id})
79e27cd98c39 replacing w3cId by slug
Dominqique Hazael-Massieux <dom@w3.org>
parents: 251
diff changeset
   891
        .populate('employees', ['login', 'slug', 'given', 'family', 'picture_thumb'])
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   892
	.exec( function(err, org) {
163
4f3a55755dd2 404 on unknown org id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 162
diff changeset
   893
	    if (org) {
4f3a55755dd2 404 on unknown org id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 162
diff changeset
   894
		var employees = org.employees.slice(0); // slice(0) to work around bug in populating arrays
304
64620be50c1f sort employees
Dominique Hazaël-Massieux <dom@w3.org>
parents: 297
diff changeset
   895
		employees.sort(function (a,b) { return (a.family > b.family ? 1 : (b.family > a.family ? -1 : 0));});
163
4f3a55755dd2 404 on unknown org id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 162
diff changeset
   896
		switch (req.params.format) {
4f3a55755dd2 404 on unknown org id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 162
diff changeset
   897
		case 'json':
4f3a55755dd2 404 on unknown org id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 162
diff changeset
   898
		    res.send(org);
4f3a55755dd2 404 on unknown org id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 162
diff changeset
   899
		    break;
4f3a55755dd2 404 on unknown org id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 162
diff changeset
   900
		default:
339
09b79816daee setting page titles
Dominique Hazaël-Massieux <dom@w3.org>
parents: 338
diff changeset
   901
		    res.render('orgs/org.ejs', { locals: { org: org, people:employees, title: org.name}}); 
163
4f3a55755dd2 404 on unknown org id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 162
diff changeset
   902
		}
4f3a55755dd2 404 on unknown org id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 162
diff changeset
   903
	    } else {
4f3a55755dd2 404 on unknown org id
Dominqique Hazael-Massieux <dom@w3.org>
parents: 162
diff changeset
   904
		next();
162
d9f473ea6ad4 only add people to employees list if they're not already in the system
Dominqique Hazael-Massieux <dom@w3.org>
parents: 159
diff changeset
   905
	    }
d9f473ea6ad4 only add people to employees list if they're not already in the system
Dominqique Hazael-Massieux <dom@w3.org>
parents: 159
diff changeset
   906
	});  
61
d8dffbdf570f added support for page per org
Dominqique Hazael-Massieux <dom@w3.org>
parents: 58
diff changeset
   907
});
d8dffbdf570f added support for page per org
Dominqique Hazael-Massieux <dom@w3.org>
parents: 58
diff changeset
   908
416
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   909
app.post('/schedule/admin', function(req,res, next) {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   910
    if (! req.loggedIn) {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   911
      return res.redirect(everyauth.password.getLoginPath());
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   912
    }
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   913
    var isAdmin = new RegExp("^" + config.admin.login.replace(",","|") + "$");
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   914
    if (!isAdmin.test(req.user.login)) {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   915
	return res.render("403");
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   916
    }    
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   917
707
afb2b2c59474 another button fix
Dominique Hazaël-Massieux <dom@w3.org>
parents: 705
diff changeset
   918
  if (req.body.addEvent !== undefined) { 
523
0c90bddcafc6 record who's proposing an adhoc event
Dominique Hazael-Massieux <dom@w3.org>
parents: 521
diff changeset
   919
      addEvent(req, res, next, 'meeting', null);
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
   920
  } else if (req.body.updateSchedule !== undefined) {
416
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   921
      if (!req.body.schedule) {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   922
	  req.flash("error", "Missing URL of schedule");
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   923
	  next();
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   924
      }
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   925
      var http;
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   926
      var url = require("url").parse(req.body.schedule);
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   927
      if (url.protocol == "http:") {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   928
	  http = require('http');
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   929
      } else if (url.protocol == "https:") {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   930
	  http = require('https');
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   931
      } else {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   932
	  req.flash("error", "Unrecognized protocol for room descriptions: " + config.map.rooms_json);
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   933
	  next();
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   934
      }
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   935
      if (http) {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   936
	  var request = http.get({host: url.hostname, port: url.port , path: url.pathname + (url.search ? url.search : '') + (url.hash ? url.hash : '')}, function (response) {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   937
	      response.setEncoding('utf8');
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   938
	      var scheduleJSON = "", events;
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   939
	      response.on('data', function (chunk) {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   940
		  scheduleJSON = scheduleJSON + chunk;
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   941
	      });
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   942
	      response.on('end', function () {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   943
		  Event.find({}).remove();
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   944
		  try {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   945
		      events = JSON.parse(scheduleJSON);
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   946
		  } catch (err) {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   947
		      req.flash("error", "Couldn't parse schedule as JSON: " + err);
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   948
		      console.log(scheduleJSON);
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   949
		      next();
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   950
		  }
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   951
		  for (i in events) {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   952
		      var e = events[i];
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   953
		      var counter = 0;
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   954
		      var addCounter = 0;
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   955
		      var event = new Event(
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   956
			  {timeStart: parseDate(e.timeStart),
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   957
			   timeEnd: parseDate(e.timeEnd),
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   958
			   name: e.name,
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   959
			   presenters: e.presenters,
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   960
			   slug: e.slug,
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   961
			   confidentiality: e.confidentiality,
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   962
			   observers: e.observers
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   963
			  });
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   964
		      if (places[e.room]) {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   965
			  event.room = places[e.room]._id;
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   966
		      } else {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   967
			  req.flash('error', 'Failed to locate event “' + e.name + '” as it is set for a room with unknown shortname ' + e.room);			
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   968
		      }
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   969
		      event.save(function (err) {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   970
			  counter++;
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   971
			  if (err) {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   972
			      req.flash('error',err);
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   973
			  } else {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   974
			      addCounter++;
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   975
			  }
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   976
			  if (counter == events.length) {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   977
			      req.flash("success", "Schedule successfully loaded");
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   978
			      next();
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   979
			  }
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   980
		      });
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   981
		  }
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   982
	      });
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   983
	  });
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   984
      }
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   985
 } else {
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   986
     next();
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   987
 }
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   988
});
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   989
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
   990
app.all('/schedule/admin', function(req,res) {
420
eda8ded90aa7 loading event list for admin updates
Dominique Hazaël-Massieux <dom@w3.org>
parents: 416
diff changeset
   991
    Event.find({})
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   992
	        .sort('timeStart', 1, 'name', 1)
420
eda8ded90aa7 loading event list for admin updates
Dominique Hazaël-Massieux <dom@w3.org>
parents: 416
diff changeset
   993
		.populate('room', ['shortname','name'])
536
6f57df84df8b populate proposer for events
Dominique Hazael-Massieux <dom@w3.org>
parents: 532
diff changeset
   994
	  .populate('proposedBy')
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
   995
		.exec( 
420
eda8ded90aa7 loading event list for admin updates
Dominique Hazaël-Massieux <dom@w3.org>
parents: 416
diff changeset
   996
	function(err, events) {
487
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   997
	    var data = prepareEventsList(events);
05f4d6730c19 support for showing per user schedule
Dominique Hazael-Massieux <dom@w3.org>
parents: 485
diff changeset
   998
	    var days = data[0], timeslots = data[1], schedule = data[2];
421
a93973b5d70e making list of events more configurable to allow for admin links
Dominique Hazaël-Massieux <dom@w3.org>
parents: 420
diff changeset
   999
        res.render("schedule/admin", { locals: {title: "Schedule update", places: places, days: days, timeslots: timeslots, schedule:schedule, expanded: true, interestMarker: false, admin: true}});
420
eda8ded90aa7 loading event list for admin updates
Dominique Hazaël-Massieux <dom@w3.org>
parents: 416
diff changeset
  1000
    });
416
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
  1001
});
97ff5573f7d2 moving schedule admin in separate admin page
Dominique Hazaël-Massieux <dom@w3.org>
parents: 413
diff changeset
  1002
423
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1003
app.post("/schedule/events/:slug/admin", function(req, res, next) {
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1004
    if (! req.loggedIn) {
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1005
      return res.redirect(everyauth.password.getLoginPath());
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1006
    }
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1007
    var isAdmin = new RegExp("^" + config.admin.login.replace(",","|") + "$");
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1008
    if (!isAdmin.test(req.user.login)) {
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1009
	return res.render("403");
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1010
    }    
434
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1011
    Event.findOne({slug: req.params.slug}, function(err, event) {
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1012
      if (err) {
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1013
	 next();
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1014
      }
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
  1015
      if (req.body.updateEvent !== undefined){
434
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1016
      if (!req.body.name){
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1017
	  req.flash("error", "Missing event name");
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1018
	  next();
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1019
      } else if (!req.body.day) {
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1020
	  req.flash("error", "Missing event day");
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1021
	  next();
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1022
      } else if (!req.body.start) {
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1023
	  req.flash("error", "Missing event start time");
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1024
	  next();
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1025
      } else if (!req.body.end) {
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1026
	  req.flash("error", "Missing event end time");
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1027
	  next();
720
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
  1028
      }
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
  1029
      var room = places[req.body.room];
b6bd21650f54 making known places a global defined at start time
Dominique Hazaël-Massieux <dom@w3.org>
parents: 719
diff changeset
  1030
	  if (!room) {
440
e8b52a2a8be4 simplify room setting for event update
Dominique Hazaël-Massieux <dom@w3.org>
parents: 439
diff changeset
  1031
	      req.flash("error", "No known room with shortname" + req.body.room);
434
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1032
	  }
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
  1033
	  event.timeStart =  parseDate(req.body.day.replace(/-/g,'') + 'T' + String('0000'  + (parseInt(req.body.start.replace(":",""), 10) - 100* parseInt(config.schedule.timezone_offset, 10))).slice(-4) + '00');
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
  1034
	  event.timeEnd =  parseDate(req.body.day.replace(/-/g,'') + 'T' + String('0000' + (parseInt(req.body.end.replace(":",""), 10) - 100 * parseInt(config.schedule.timezone_offset, 10))).slice(-4) + '00');
434
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1035
	  event.name= req.body.name;
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1036
	  event.presenters= req.body.presenters;
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1037
	  event.confidentiality = req.body.confidentiality;
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1038
	  event.observers = req.body.observers;
440
e8b52a2a8be4 simplify room setting for event update
Dominique Hazaël-Massieux <dom@w3.org>
parents: 439
diff changeset
  1039
	  event.room = room._id;
e8b52a2a8be4 simplify room setting for event update
Dominique Hazaël-Massieux <dom@w3.org>
parents: 439
diff changeset
  1040
          event.save(function (err) {
434
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1041
            if (err) {
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1042
		req.flash('error',err);
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1043
	    } else {
439
4d98a96bc46f *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 435
diff changeset
  1044
		req.flash('success', req.body.name + ' successfully updated')	 ;
434
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1045
	    }
5573043a025b update event on POST
Dominique Hazaël-Massieux <dom@w3.org>
parents: 433
diff changeset
  1046
	    next();
441
Dominique Hazaël-Massieux <dom@w3.org>
parents: 440
diff changeset
  1047
          });
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
  1048
      } else if (req.body.deleteEvent !== undefined) {
446
3dba33226530 add support for deleting an event
Dominique Hazaël-Massieux <dom@w3.org>
parents: 443
diff changeset
  1049
	  if (!req.body.confirm){
3dba33226530 add support for deleting an event
Dominique Hazaël-Massieux <dom@w3.org>
parents: 443
diff changeset
  1050
	      req.flash("error", "If you really want to delete the event, you need to confirm so by checking the checkbox");
3dba33226530 add support for deleting an event
Dominique Hazaël-Massieux <dom@w3.org>
parents: 443
diff changeset
  1051
	      next();
3dba33226530 add support for deleting an event
Dominique Hazaël-Massieux <dom@w3.org>
parents: 443
diff changeset
  1052
	  }
3dba33226530 add support for deleting an event
Dominique Hazaël-Massieux <dom@w3.org>
parents: 443
diff changeset
  1053
	  event.remove(
3dba33226530 add support for deleting an event
Dominique Hazaël-Massieux <dom@w3.org>
parents: 443
diff changeset
  1054
	      function(err) {
3dba33226530 add support for deleting an event
Dominique Hazaël-Massieux <dom@w3.org>
parents: 443
diff changeset
  1055
		  if (err) {
3dba33226530 add support for deleting an event
Dominique Hazaël-Massieux <dom@w3.org>
parents: 443
diff changeset
  1056
		      req.flash("error", err);
448
65b760fec5f8 redirect on delete
Dominique Hazaël-Massieux <dom@w3.org>
parents: 446
diff changeset
  1057
		      next();
446
3dba33226530 add support for deleting an event
Dominique Hazaël-Massieux <dom@w3.org>
parents: 443
diff changeset
  1058
		  } else {
3dba33226530 add support for deleting an event
Dominique Hazaël-Massieux <dom@w3.org>
parents: 443
diff changeset
  1059
		      req.flash("success", "Event successfully deleted");
448
65b760fec5f8 redirect on delete
Dominique Hazaël-Massieux <dom@w3.org>
parents: 446
diff changeset
  1060
		      return res.redirect(config.hosting.basepath + '/schedule/admin');
446
3dba33226530 add support for deleting an event
Dominique Hazaël-Massieux <dom@w3.org>
parents: 443
diff changeset
  1061
		  }
3dba33226530 add support for deleting an event
Dominique Hazaël-Massieux <dom@w3.org>
parents: 443
diff changeset
  1062
	      });
3dba33226530 add support for deleting an event
Dominique Hazaël-Massieux <dom@w3.org>
parents: 443
diff changeset
  1063
      }
442
Dominique Hazaël-Massieux <dom@w3.org>
parents: 441
diff changeset
  1064
   });
423
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1065
});
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1066
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1067
app.all("/schedule/events/:slug/admin", function(req, res, next) {
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
  1068
      Event.findOne({slug: req.params.slug}).populate('room').populate('proposedBy').exec(function(err, event) {
423
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1069
	if (err) {
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1070
	    next();
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1071
	}
429
3a14192b5914 timezone offset as integer
Dominique Hazaël-Massieux <dom@w3.org>
parents: 428
diff changeset
  1072
        res.render("schedule/event-admin", {locals: {title: "Update " + event.name, event: event, places: places, timezone_offset: parseInt(config.schedule.timezone_offset, 10)}});
423
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1073
    });
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1074
});
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1075
310
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1076
app.get('/schedule/stream', function(req, res) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1077
    res.setHeader("Content-Type", 'text/event-stream');
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1078
    res.setHeader("Cache-Control", "no-cache");
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1079
    res.setHeader("Connection", "keep-alive");
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1080
    res.writeHead(200);
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1081
    // Heroku requires activity to avoid request timeout
343
1ffd3660207c miliseconds vs seconds
Dominique Hazaël-Massieux <dom@w3.org>
parents: 342
diff changeset
  1082
    setInterval(function() { res.write(":\n"); }, 30000);
310
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1083
    emitter.on("interest", function(user, event) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1084
	res.write("event: interest\n" + "data:  "+ JSON.stringify({"user": user, "event": event}) + "\n\n");
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1085
    });
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1086
    emitter.on("uninterest", function(user, event) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1087
	res.write("event: uninterest\n" + "data:  "+ JSON.stringify({"user": user, "event": event}) + "\n\n");
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1088
    });
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1089
});
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1090
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1091
423
c6383420d695 building individual event update interface
Dominique Hazaël-Massieux <dom@w3.org>
parents: 421
diff changeset
  1092
app.post("/schedule/events/:slug/", function(req, res, next) {
382
5c3c0fadffc6 working around express-namespace bug https://github.com/visionmedia/express-namespace/issues/5
Dominique Hazaël-Massieux <dom@w3.org>
parents: 380
diff changeset
  1093
  setFormatOutput(req);
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
  1094
    if (req.user && (req.body.interested  !== undefined || req.body.uninterested  !== undefined)) {	
310
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1095
	Event.findOne({slug: req.params.slug}, function(err, event) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1096
	    if (err) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1097
		next();
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1098
	    }
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1099
	    var interestedList = event.interested.slice(0);
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1100
	    var interested = [];
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1101
	    var userFound = false;
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1102
	    var success = false;
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1103
	    for (var i in interestedList) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1104
		if (interestedList[i].toString() == req.user._id.toString()) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1105
		    userFound = true;
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
  1106
		    if (req.body.uninterested == undefined) {
310
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1107
			success = false;
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1108
		    } else {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1109
			emitter.emit("uninterest", req.user, event);
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1110
			success = true;
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1111
		    }
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1112
		} else {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1113
		    interested.push(interestedList[i]);
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1114
		}
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1115
	    }
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
  1116
	    if (!userFound && req.body.interested !== undefined) {
310
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1117
		interested.push(req.user._id);
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1118
		emitter.emit("interest", req.user, event);
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1119
		success = true;
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1120
	    }
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1121
	    if (!success) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1122
		switch (req.outputFormat) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1123
		case 'json':
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1124
		    res.send(JSON.stringify({error: "no change"}));
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1125
		    break;
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1126
		default:
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1127
		    req.flash("error", "no change");
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1128
		    next();
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1129
		}
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1130
	    } else {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1131
		event.interested = interested;
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1132
		event.save(function(err) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1133
		    switch (req.outputFormat) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1134
		    case "json":
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1135
			if (!err) {
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
  1136
			    res.send(JSON.stringify({success: 'Interest ' + (req.body.interested !== undefined ? "recorded for" : "removed from") + event.name}));
310
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1137
			} else { 
537
b9b1e07b18c4 bug fix
Dominique Hazael-Massieux <dom@w3.org>
parents: 536
diff changeset
  1138
			    res.send(JSON.stringify({error: err}));                   
310
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1139
			}
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1140
			break;
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1141
		    default:
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1142
			if (!err) {
705
da78f3e93def more careful verification of submit action
Dominique Hazaël-Massieux <dom@w3.org>
parents: 688
diff changeset
  1143
			    req.flash('success', 'Interest recorded in '  + (req.body.interested !== undefined ? "recorded for " : "removed from ") + event.name);
310
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1144
			} else {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1145
			    req.flash('error', err);
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1146
			}
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1147
			next();
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1148
			break;
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1149
		    }
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1150
		});
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1151
	    }
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1152
	});
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1153
    } else {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1154
	next();
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1155
    }
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1156
});
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1157
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1158
app.all('/schedule/events/:slug.:format?', function(req, res, next) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1159
    Event.findOne({slug: req.params.slug})
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1160
	.populate('room', ['shortname', 'name'])
537
b9b1e07b18c4 bug fix
Dominique Hazael-Massieux <dom@w3.org>
parents: 536
diff changeset
  1161
	    .populate('proposedBy')
712
71eccbad09d3 replace run(), asc() and desc() by their non deprecated equivalents (exec and sort)
Dominique Hazaël-Massieux <dom@w3.org>
parents: 711
diff changeset
  1162
	.exec(
310
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1163
	    function(err, event) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1164
		if (err) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1165
		    console.log("unknown event: " + err);
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1166
		    next();
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1167
		}
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1168
		switch (req.params.format) {
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1169
		case 'json':
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1170
		    res.send(event);
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1171
		    break;
1d47109bb0f0 more schedule stuff
Dominique Hazaël-Massieux <dom@w3.org>
parents: 307
diff changeset
  1172
		default: