3 // This file consists of the main webserver for FinalsClub.org
4 // and is split between a standard CRUD style webserver and
5 // a websocket based realtime webserver.
7 // A note on house keeping: Anything with XXX is marked
8 // as such because it should be looked at and possibly
9 // revamped or removed depending on circumstances.
12 var sys = require( 'sys' );
13 var os = require( 'os' );
14 var url = require( 'url' );
15 var express = require( 'express' );
16 var mongoStore = require( 'connect-mongo' );
17 var async = require( 'async' );
18 var db = require( './db.js' );
19 var mongoose = require( './models.js' ).mongoose;
20 var Mailer = require( './mailer.js' );
21 var hat = require('hat');
22 var connect = require( 'connect' );
23 var Session = connect.middleware.session.Session;
24 var parseCookie = connect.utils.parseCookie;
25 var Backchannel = require('./bc/backchannel');
29 // Used for initial testing
30 var log3 = function() {}
33 var app = module.exports = express.createServer();
35 // Load Mongoose Schemas
36 // The actual schemas are located in models.j
37 var User = mongoose.model( 'User' );
38 var School = mongoose.model( 'School' );
39 var Course = mongoose.model( 'Course' );
40 var Lecture = mongoose.model( 'Lecture' );
41 var Note = mongoose.model( 'Note' );
43 // More schemas used for legacy data
44 var ArchivedCourse = mongoose.model( 'ArchivedCourse' );
45 var ArchivedNote = mongoose.model( 'ArchivedNote' );
46 var ArchivedSubject = mongoose.model( 'ArchivedSubject' );
48 // XXX Not sure if necessary
49 var ObjectId = mongoose.SchemaTypes.ObjectId;
52 // Use the environment variable DEV_EMAIL for testing
53 var ADMIN_EMAIL = process.env.DEV_EMAIL || 'info@finalsclub.org';
55 // Set server hostname and port from environment variables,
57 // XXX Can be cleaned up
58 var serverHost = process.env.SERVER_HOST;
59 var serverPort = process.env.SERVER_PORT;
62 console.log( 'Using server hostname defined in environment: %s', serverHost );
64 serverHost = os.hostname();
65 console.log( 'No hostname defined, defaulting to os.hostname(): %s', serverHost );
68 // Express configuration depending on environment
69 // development is intended for developing locally or
70 // when not in production, otherwise production is used
71 // when the site will be run live for regular usage.
72 app.configure( 'development', function() {
73 // In development mode, all errors and stack traces will be
74 // dumped to the console and on page for easier troubleshooting
76 app.set( 'errorHandler', express.errorHandler( { dumpExceptions: true, showStack: true } ) );
78 // Set database connection information from environment
79 // variables otherwise use localhost.
80 app.set( 'dbHost', process.env.MONGO_HOST || 'localhost' );
81 app.set( 'dbUri', 'mongodb://' + app.set( 'dbHost' ) + '/fc' );
83 // Set Amazon access and secret keys from environment
84 // variables. These keys are intended to be secret, so
85 // are not included in the source code, but set on the server
87 app.set( 'awsAccessKey', process.env.AWS_ACCESS_KEY_ID );
88 app.set( 'awsSecretKey', process.env.AWS_SECRET_ACCESS_KEY );
90 // If a port wasn't set earlier, set to 3000
96 // Production configuration settings
97 app.configure( 'production', function() {
98 // At the moment we have errors outputting everything
99 // so if there are any issues it is easier to track down.
100 // Once the site is more stable it will be prudent to
101 // use less error tracing.
102 app.set( 'errorHandler', express.errorHandler( { dumpExceptions: true, showStack: true } ) );
104 // Disable view cache due to stale views.
105 // XXX Disable view caching temp
106 app.disable( 'view cache' )
108 // Against setting the database connection information
109 // XXX Can be cleaned up or combined
110 app.set( 'dbHost', process.env.MONGO_HOST || 'localhost' );
111 app.set( 'dbUri', 'mongodb://' + app.set( 'dbHost' ) + '/fc' );
113 // XXX Can be cleaned up or combined
114 app.set( 'awsAccessKey', process.env.AWS_ACCESS_KEY_ID );
115 app.set( 'awsSecretKey', process.env.AWS_SECRET_ACCESS_KEY );
117 // Set to port 80 if not set through environment variables
123 // General Express configuration settings
124 app.configure(function(){
125 // Views are rendered from public/index.html and main.js except for the pad that surrounds EPL and BC
126 // FIXME: make all views exist inside of public/index.html
127 // Views are housed in the views folder
128 app.set( 'views', __dirname + '/views' );
129 // All templates use jade for rendering
130 app.set( 'view engine', 'jade' );
132 // Bodyparser is required to handle form submissions
133 // without manually parsing them.
134 app.use( express.bodyParser() );
136 app.use( express.cookieParser() );
138 // Sessions are stored in mongodb which allows them
139 // to be persisted even between server restarts.
140 app.set( 'sessionStore', new mongoStore( {
142 'url' : app.set( 'dbUri' )
145 // This is where the actual Express session handler
146 // is defined, with a mongoStore being set as the
147 // session storage versus in memory storage that is
149 app.use( express.session( {
150 // A secret 'password' for encrypting and decrypting
152 // XXX Should be handled differently
153 'secret' : 'finalsclub',
154 // The max age of the cookies that is allowed
155 // 60 (seconds) * 60 (minutes) * 24 (hours) * 30 (days) * 1000 (milliseconds)
156 'maxAge' : new Date(Date.now() + (60 * 60 * 24 * 30 * 1000)),
157 'store' : app.set( 'sessionStore' )
160 // methodOverride is used to handle PUT and DELETE HTTP
161 // requests that otherwise aren't handled by default.
162 app.use( express.methodOverride() );
163 // Static files are loaded when no dynamic views match.
164 app.use( express.static( __dirname + '/public' ) );
166 // Sets the routers middleware to load after everything set
167 // before it, but before static files.
168 app.use( app.router );
170 app.use(express.logger({ format: ':method :url' }));
171 // This is the command to use the default express error logger/handler
172 app.use(express.errorHandler({ dumpExceptions: true }));
176 // Mailer functions and helpers
177 // These are helper functions that make for cleaner code.
179 // sendUserActivation is for when a user registers and
180 // first needs to activate their account to use it.
181 function sendUserActivation( user ) {
185 'subject' : 'Activate your FinalsClub.org Account',
187 // Templates are in the email folder and use ejs
188 'template' : 'userActivation',
189 // Locals are used inside ejs so dynamic information
190 // can be rendered properly.
193 'serverHost' : serverHost
197 // Email is sent here
198 mailer.send( message, function( err, result ) {
200 // XXX: Add route to resend this email
201 console.log( 'Error sending user activation email\nError Message: '+err.Message );
203 console.log( 'Successfully sent user activation email.' );
208 // sendUserWelcome is for when a user registers and
209 // a welcome email is sent.
210 function sendUserWelcome( user, school ) {
211 // If a user is not apart of a supported school, they are
212 // sent a different template than if they are apart of a
214 var template = school ? 'userWelcome' : 'userWelcomeNoSchool';
218 'subject' : 'Welcome to FinalsClub',
220 'template' : template,
223 'serverHost' : serverHost
227 mailer.send( message, function( err, result ) {
229 // XXX: Add route to resend this email
230 console.log( 'Error sending user welcome email\nError Message: '+err.Message );
232 console.log( 'Successfully sent user welcome email.' );
238 // These functions are used later in the routes to help
239 // load information and variables, as well as handle
240 // various instances like checking if a user is logged in
242 function loggedIn( req, res, next ) {
243 // If req.user is set, then pass on to the next function
244 // or else alert the user with an error message.
248 req.flash( 'error', 'You must be logged in to access that feature!' );
253 // This loads the user if logged in
254 function loadUser( req, res, next ) {
255 var sid = req.sessionID;
257 console.log( 'got request from session ID: %s', sid );
259 // Find a user based on their stored session id
260 User.findOne( { session : sid }, function( err, user ) {
265 // If a user is found then set req.user the contents of user
266 // and make sure req.user.loggedIn is true.
270 req.user.loggedIn = true;
272 log3( 'authenticated user: '+req.user._id+' / '+req.user.email+'');
274 // Check if a user is activated. If not, then redirec
275 // to the homepage and tell them to check their email
276 // for the activation email.
277 if( req.user.activated ) {
278 // Is the user's profile complete? If not, redirect to their profile
279 if( ! req.user.isComplete ) {
280 if( url.parse( req.url ).pathname != '/profile' ) {
281 req.flash( 'info', 'Your profile is incomplete. Please complete your profile to fully activate your account.' );
283 res.redirect( '/profile' );
291 req.flash( 'info', 'This account has not been activated. Check your email for the activation URL.' );
295 } else if('a'==='b'){
296 console.log('never. in. behrlin.');
298 // If no user record was found, then we store the requested
299 // path they intended to view and redirect them after they
300 // login if it is requred.
301 var path = url.parse( req.url ).pathname;
302 req.session.redirect = path;
304 // Set req.user to an empty object so it doesn't throw errors
305 // later on that it isn't defined.
315 // loadSchool is used to load a school by it's id
316 function loadSchool( req, res, next ) {
318 var schoolName = req.params.name;
319 console.log( 'loading a school by id' );
321 School.findOne({'name': schoolName}).run( function( err, school ) {
326 // If a school is found, the user is checked to see if they are
327 // authorized to see or interact with anything related to that
329 school.authorize( user, function( authorized ){
330 req.school.authorized = authorized;
334 // If no school is found, display an appropriate error.
335 sendJson(res, {status: 'not_found', message: 'Invalid school specified!'} );
340 // loadSchool is used to load a course by it's id
341 function loadCourse( req, res, next ) {
343 var courseId = req.params.id;
345 Course.findById( courseId, function( err, course ) {
346 if( course && !course.deleted ) {
349 // If a course is found, the user is checked to see if they are
350 // authorized to see or interact with anything related to that
352 course.authorize( user, function( authorized ) {
353 req.course.authorized = authorized;
358 // If no course is found, display an appropriate error.
359 sendJson(res, {status: 'not_found', message: 'Invalid course specified!'} );
364 // loadLecture is used to load a lecture by it's id
365 function loadLecture( req, res, next ) {
367 var lectureId = req.params.id;
369 Lecture.findById( lectureId, function( err, lecture ) {
370 if( lecture && !lecture.deleted ) {
371 req.lecture = lecture;
373 // If a lecture is found, the user is checked to see if they are
374 // authorized to see or interact with anything related to that
376 lecture.authorize( user, function( authorized ) {
377 req.lecture.authorized = authorized;
382 // If no lecture is found, display an appropriate error.
383 sendJson(res, {status: 'not_found', message: 'Invalid lecture specified!'} );
388 // loadNote is used to load a note by it's id
389 // This is a lot more complicated than the above
390 // due to public/private handling of notes.
391 function loadNote( req, res, next ) {
392 var user = req.user ? req.user : false;
393 var noteId = req.params.id;
395 Note.findById( noteId, function( err, note ) {
396 // If a note is found, and user is set, check if
397 // user is authorized to interact with that note.
398 if( note && user && !note.deleted ) {
399 note.authorize( user, function( auth ) {
401 // If authorzied, then set req.note to be used later
405 } else if ( note.public ) {
406 // If not authorized, but the note is public, then
407 // designate the note read only (RO) and store req.note
413 // If the user is not authorized and the note is private
414 // then display and error.
415 sendJson(res, {status: 'error', message: 'You do not have permission to access that note.'} );
418 } else if ( note && note.public && !note.deleted ) {
419 // If note is found, but user is not set because they are not
420 // logged in, and the note is public, set the note to read only
421 // and store the note for later.
426 } else if ( note && !note.public && !note.deleted ) {
427 // If the note is found, but user is not logged in and the note is
428 // not public, then ask them to login to view the note. Once logged
429 // in they will be redirected to the note, at which time authorization
430 // handling will be put in effect above.
431 //req.session.redirect = '/note/' + note._id;
432 sendJson(res, {status: 'error', message: 'You must be logged in to view that note.'} );
435 sendJson(res, {status: 'error', message: 'Invalid note specified!'} );
440 function checkAjax( req, res, next ) {
444 res.sendfile( 'public/index.html' );
448 // Dynamic Helpers are loaded automatically into views
449 app.dynamicHelpers( {
450 // express-messages is for flash messages for easy
451 // errors and information display
452 'messages' : require( 'express-messages' ),
454 // By default the req object isn't sen't to views
455 // during rendering, this allows you to use the
456 // user object if available in views.
457 'user' : function( req, res ) {
458 console.log("When the fuck is a dynamic helper used?");
462 // Same, this allows session to be available in views.
463 'session' : function( req, res ) {
468 function sendJson( res, obj ) {
469 res.header('Cache-Control', 'no-cache, no-store');
475 // Used to display all available schools
476 // Public with some private information
477 app.get( '/schools', checkAjax, loadUser, function( req, res ) {
481 // Find all schools and sort by name
482 // XXX mongoose's documentation on sort is extremely poor, tread carefully
483 School.find( {} ).sort( 'name', '1' ).run( function( err, schools ) {
485 // If schools are found, loop through them gathering any courses that are
486 // associated with them and then render the page with that information.
487 var schools_todo = schools.length;
488 schools.map(function (school) {
489 Course.find( { 'school': school.id } ).run(function (err, courses) {
490 school.courses_length = courses.length
492 if (schools_todo <= 0) {
493 sendJson(res, { 'user': user.sanitized, 'schools': schools.map( function(s) {
497 description: s.description,
500 courses: s.courses_length,
501 courseNum: s.courseNum
510 // If no schools have been found, display none
511 //res.render( 'schools', { 'schools' : [] } );
512 sendJson(res, { 'schools' : [] , 'user': user.sanitized });
517 app.get( '/school/:name', checkAjax, loadUser, loadSchool, function( req, res ) {
518 var school = req.school;
521 console.log("Loading a school");
522 school.authorize( user, function( authorized ) {
523 // This is used to display interface elements for those users
524 // that are are allowed to see th)m, for instance a 'New Course' button.
525 var sanitizedSchool = school.sanitized;
526 //var sanitizedSchool = {
528 // name: school.name,
529 // description: school.description,
532 sanitizedSchool.authorized = authorized;
533 // Find all courses for school by it's id and sort by name
534 Course.find( { 'school' : school._id } ).sort( 'name', '1' ).run( function( err, courses ) {
535 // If any courses are found, set them to the appropriate school, otherwise
538 if( courses.length > 0 ) {
539 sanitizedSchool.courses = courses.filter(function(course) {
540 if (!course.deleted) return course;
541 }).map(function(course) {
542 return course.sanitized;
548 // This tells async (the module) that each iteration of forEach is
549 // done and will continue to call the rest until they have all been
550 // completed, at which time the last function below will be called.
551 sendJson(res, { 'school': sanitizedSchool, 'user': user.sanitized })
557 // Recieves new course form
558 app.post( '/school/:id', checkAjax, loadUser, loadSchool, function( req, res ) {
559 var school = req.school;
560 // Creates new course from Course Schema
561 var course = new Course;
562 // Gathers instructor information from form
563 var instructorEmail = req.body.email.toLowerCase();
564 var instructorName = req.body.instructorName;
566 if( ( ! school ) || ( ! school.authorized ) ) {
567 return sendJson(res, {status: 'error', message: 'There was a problem trying to create a course'})
570 if ( !instructorName ) {
571 return sendJson(res, {status: 'error', message: 'Invalid parameters!'} )
574 if ( ( instructorEmail === '' ) || ( !isValidEmail( instructorEmail ) ) ) {
575 return sendJson(res, {status: 'error', message:'Please enter a valid email'} );
578 // Fill out the course with information from the form
579 course.number = req.body.number;
580 course.name = req.body.name;
581 course.description = req.body.description;
582 course.school = school._id;
583 course.creator = req.user._id;
584 course.subject = req.body.subject;
585 course.department = req.body.department;
587 // Check if a user exists with the instructorEmail, if not then create
588 // a new user and send them an instructor welcome email.
589 User.findOne( { 'email' : instructorEmail }, function( err, user ) {
593 user.name = instructorName
594 user.email = instructorEmail;
595 user.affil = 'Instructor';
596 user.school = school.name;
598 user.activated = false;
600 // Once the new user information has been completed, save the user
601 // to the database then email them the instructor welcome email.
602 user.save(function( err ) {
603 // If there was an error saving the instructor, prompt the user to fill out
604 // the information again.
606 return sendJson(res, {status: 'error', message: 'Invalid parameters!'} )
611 'subject' : 'A non-profit open education initiative',
613 'template' : 'instructorInvite',
618 'serverHost' : serverHost
622 mailer.send( message, function( err, result ) {
624 console.log( 'Error inviting instructor to course!' );
626 console.log( 'Successfully invited instructor to course.' );
630 // After emails are sent, set the courses instructor to the
631 // new users id and then save the course to the database.
632 course.instructor = user._id;
633 course.save( function( err ) {
635 return sendJson(res, {status: 'error', message: 'Invalid parameters!'} )
637 // Once the course has been completed email the admin with information
638 // on the course and new instructor
642 'subject' : school.name+' has a new course: '+course.name,
644 'template' : 'newCourse',
649 'serverHost' : serverHost
653 mailer.send( message, function( err, result ) {
655 console.log( 'Error sending new course email to info@finalsclub.org' )
657 console.log( 'Successfully invited instructor to course')
660 // Redirect the user to the schools page where they can see
662 // XXX Redirect to the new course instead
663 return sendJson(res, {status: 'ok', message: 'Course created'} )
669 // If the user exists, then check if they are already and instructor
670 if (user.affil === 'Instructor') {
671 // If they are an instructor, then save the course with the appropriate
672 // information and email the admin.
673 course.instructor = user._id;
674 course.save( function( err ) {
676 // XXX better validation
677 return sendJson(res, {status: 'error', message: 'Invalid parameters!'} )
682 'subject' : school.name+' has a new course: '+course.name,
684 'template' : 'newCourse',
689 'serverHost' : serverHost
693 mailer.send( message, function( err, result ) {
695 console.log( 'Error sending new course email to info@finalsclub.org' )
697 console.log( 'Successfully invited instructor to course')
700 // XXX Redirect to the new course instead
701 return sendJson(res, {status: 'ok', message: 'Course created'} )
705 // The existing user isn't an instructor, so the user is notified of the error
706 // and the course isn't created.
707 sendJson(res, {status: 'error', message: 'The existing user\'s email you entered is not an instructor'} )
713 // Individual Course Listing
714 // Public with private information
715 app.get( '/course/:id', checkAjax, loadUser, loadCourse, function( req, res ) {
716 var userId = req.user._id;
717 var course = req.course;
719 // Check if the user is subscribed to the course
720 // XXX Not currently used for anything
721 //var subscribed = course.subscribed( userId );
723 // Find lectures associated with this course and sort by name
724 Lecture.find( { 'course' : course._id } ).sort( 'name', '1' ).run( function( err, lectures ) {
725 // Get course instructor information using their id
726 User.findById( course.instructor, function( err, instructor ) {
727 // Render course and lectures
728 var sanitizedInstructor = instructor.sanitized;
729 var sanitizedCourse = course.sanitized;
730 if (!course.authorized) {
731 delete sanitizedInstructor.email;
733 sanitizedCourse.authorized = course.authorized;
735 sendJson(res, { 'course' : sanitizedCourse, 'instructor': sanitizedInstructor, 'lectures' : lectures.map(function(lecture) { return lecture.sanitized })} );
740 // Recieve New Lecture Form
741 app.post( '/course/:id', checkAjax, loadUser, loadCourse, function( req, res ) {
742 var course = req.course;
743 // Create new lecture from Lecture schema
744 var lecture = new Lecture;
746 if( ( ! course ) || ( ! course.authorized ) ) {
747 return sendJson(res, {status: 'error', message: 'There was a problem trying to create a lecture'})
750 // Populate lecture with form data
751 lecture.name = req.body.name;
752 lecture.date = req.body.date;
753 lecture.course = course._id;
754 lecture.creator = req.user._id;
756 // Save lecture to database
757 lecture.save( function( err ) {
759 // XXX better validation
760 sendJson(res, {status: 'error', message: 'Invalid parameters!'} );
762 sendJson(res, {status: 'ok', message: 'Created new lecture'} );
768 app.get( '/course/:id/edit', loadUser, loadCourse, function( req, res) {
769 var course = req.course;
773 res.render( 'course/new', {course: course} )
775 req.flash( 'error', 'You don\'t have permission to do that' )
776 res.redirect( '/schools' );
780 // Recieve Course Edit Form
781 app.post( '/course/:id/edit', loadUser, loadCourse, function( req, res ) {
782 var course = req.course;
786 var courseChanges = req.body;
787 course.number = courseChanges.number;
788 course.name = courseChanges.name;
789 course.description = courseChanges.description;
790 course.department = courseChanges.department;
792 course.save(function(err) {
794 req.flash( 'error', 'There was an error saving the course' );
796 res.redirect( '/course/'+ course._id.toString());
799 req.flash( 'error', 'You don\'t have permission to do that' )
800 res.redirect( '/schools' );
805 app.get( '/course/:id/delete', loadUser, loadCourse, function( req, res) {
806 var course = req.course;
810 course.delete(function( err ) {
811 if ( err ) req.flash( 'info', 'There was a problem removing course: ' + err )
812 else req.flash( 'info', 'Successfully removed course' )
813 res.redirect( '/schools' );
816 req.flash( 'error', 'You don\'t have permission to do that' )
817 res.redirect( '/schools' );
821 // Subscribe to course
822 // XXX Not currently used for anything
823 app.get( '/course/:id/subscribe', loadUser, loadCourse, function( req, res ) {
824 var course = req.course;
825 var userId = req.user._id;
827 course.subscribe( userId, function( err ) {
829 req.flash( 'error', 'Error subscribing to course!' );
832 res.redirect( '/course/' + course._id );
836 // Unsubscribe from course
837 // XXX Not currently used for anything
838 app.get( '/course/:id/unsubscribe', loadUser, loadCourse, function( req, res ) {
839 var course = req.course;
840 var userId = req.user._id;
842 course.unsubscribe( userId, function( err ) {
844 req.flash( 'error', 'Error unsubscribing from course!' );
847 res.redirect( '/course/' + course._id );
854 // Display individual lecture and related notes
855 app.get( '/lecture/:id', checkAjax, loadUser, loadLecture, function( req, res ) {
856 var lecture = req.lecture;
858 // Grab the associated course
859 // XXX this should be done with DBRefs eventually
860 Course.findById( lecture.course, function( err, course ) {
862 // If course is found, find instructor information to be displayed on page
863 User.findById( course.instructor, function( err, instructor ) {
864 // Pull out our notes
865 Note.find( { 'lecture' : lecture._id } ).sort( 'name', '1' ).run( function( err, notes ) {
866 if ( !req.user.loggedIn || !req.lecture.authorized ) {
867 // Loop through notes and only return those that are public if the
868 // user is not logged in or not authorized for that lecture
869 notes = notes.filter(function( note ) {
870 if ( note.public ) return note;
873 var sanitizedInstructor = instructor.sanitized;
874 var sanitizedLecture = lecture.sanitized;
875 if (!lecture.authorized) {
876 delete sanitizedInstructor.email;
878 sanitizedLecture.authorized = lecture.authorized;
881 'lecture' : sanitizedLecture,
882 'course' : course.sanitized,
883 'instructor' : sanitizedInstructor,
884 'notes' : notes.map(function(note) {
885 return note.sanitized;
891 sendJson(res, { status: 'not_found', msg: 'This course is orphaned' })
897 // Recieve new note form
898 app.post( '/lecture/:id', checkAjax, loadUser, loadLecture, function( req, res ) {
899 var lecture = req.lecture;
901 if( ( ! lecture ) || ( ! lecture.authorized ) ) {
902 return sendJson(res, {status: 'error', message: 'There was a problem trying to create a note pad'})
905 // Create note from Note schema
908 // Populate note from form data
909 note.name = req.body.name;
910 note.date = req.body.date;
911 note.lecture = lecture._id;
912 note.public = req.body.private ? false : true;
913 note.creator = req.user._id;
915 // Save note to database
916 note.save( function( err ) {
918 // XXX better validation
919 sendJson(res, {status: 'error', message: 'There was a problem trying to create a note pad'})
921 sendJson(res, {status: 'ok', message: 'Successfully created a new note pad'})
927 // Display individual note page
928 app.get( '/note/:id', /*checkAjax,*/ loadUser, loadNote, function( req, res ) {
930 // Set read only id for etherpad-lite or false for later check
931 var roID = note.roID || false;
933 var lectureId = note.lecture;
935 // Count the amount of visits, but only once per session
936 if ( req.session.visited ) {
937 if ( req.session.visited.indexOf( note._id.toString() ) == -1 ) {
938 req.session.visited.push( note._id );
942 req.session.visited = [];
943 req.session.visited.push( note._id );
947 // If a read only id exists process note
951 // If read only id doesn't, then fetch the read only id from the database and then
953 // XXX Soon to be depracated due to a new API in etherpad that makes for a
954 // much cleaner solution.
955 db.open('mongodb://' + app.set( 'dbHost' ) + '/etherpad/etherpad', function( err, epl ) {
956 epl.findOne( { key: 'pad2readonly:' + note._id }, function(err, record) {
958 roID = record.value.replace(/"/g, '');
967 function processReq() {
969 Lecture.findById( lectureId, function( err, lecture ) {
971 req.flash( 'error', 'That notes page is orphaned!' );
975 // Find notes based on lecture id, which will be displayed in a dropdown
977 Note.find( { 'lecture' : lecture._id }, function( err, otherNotes ) {
981 'note' : note.sanitized,
982 'lecture' : lecture.sanitized,
983 'otherNotes' : otherNotes.map(function(note) {
984 return note.sanitized;
991 // User is logged in and sees full notepad
993 res.render( 'notes/index', {
994 'layout' : 'noteLayout',
998 'otherNotes' : otherNotes,
1001 'stylesheets' : [ 'dropdown.css', 'fc2.css' ],
1002 'javascripts' : [ 'dropdown.js', 'counts.js', 'backchannel.js', 'jquery.tmpl.min.js' ]
1005 // User is not logged in and sees notepad that is public
1006 res.render( 'notes/public', {
1007 'layout' : 'noteLayout',
1008 'host' : serverHost,
1010 'otherNotes' : otherNotes,
1012 'lecture' : lecture,
1013 'stylesheets' : [ 'dropdown.css', 'fc2.css' ],
1014 'javascripts' : [ 'dropdown.js', 'counts.js', 'backchannel.js', 'jquery.tmpl.min.js' ]
1022 // Static pages and redirects
1024 app.get( '/about', loadUser, function( req, res ) {
1025 res.redirect( 'http://blog.finalsclub.org/about.html' );
1028 app.get( '/press', loadUser, function( req, res ) {
1029 res.render( 'static/press' );
1032 app.get( '/conduct', loadUser, function( req, res ) {
1033 res.render( 'static/conduct' );
1036 app.get( '/legal', loadUser, function( req, res ) {
1037 res.redirect( 'http://blog.finalsclub.org/legal.html' );
1040 app.get( '/contact', loadUser, function( req, res ) {
1041 res.redirect( 'http://blog.finalsclub.org/contact.html' );
1044 app.get( '/privacy', loadUser, function( req, res ) {
1045 res.render( 'static/privacy' );
1050 // Authentication routes
1051 // These are used for logging in, logging out, registering
1052 // and other user authentication purposes
1054 // Render login page
1056 app.get( '/login', function( req, res ) {
1057 log3("get login page")
1059 res.render( 'login' );
1063 app.get( '/checkuser', checkAjax, loadUser, function( req, res ) {
1064 sendJson(res, {user: req.user.sanitized});
1067 // Recieve login form
1068 app.post( '/login', checkAjax, function( req, res ) {
1069 var email = req.body.email;
1070 var password = req.body.password;
1071 log3("post login ...")
1073 // Find user from email
1074 User.findOne( { 'email' : email.toLowerCase() }, function( err, user ) {
1078 // If user exists, check if activated, if not notify them and send them to
1081 if( ! user.activated ) {
1082 // (undocumented) markdown-esque link functionality in req.flash
1083 req.session.activateCode = user._id;
1084 sendJson(res, {status: 'error', message: 'This account isn\'t activated.'} );
1087 // If user is activated, check if their password is correct
1088 if( user.authenticate( password ) ) {
1091 var sid = req.sessionID;
1095 // Set the session then save the user to the database
1096 user.save( function() {
1097 var redirect = req.session.redirect;
1099 // login complete, remember the user's email for next time
1100 req.session.email = email;
1102 // alert the successful login
1103 sendJson(res, {status: 'ok', message:'Successfully logged in!'} );
1105 // redirect to profile if we don't have a stashed request
1106 //res.redirect( redirect || '/profile' );
1109 // Notify user of bad login
1110 sendJson(res, {status: 'error', message: 'Invalid login!'} );
1112 //res.render( 'login' );
1116 // Notify user of bad login
1118 sendJson(res, {status: 'error', message: 'Invalid login!'} );
1120 //res.render( 'login' );
1125 // Recieve reset password request form
1126 app.post( '/resetpass', checkAjax, function( req, res ) {
1127 log3("post resetpw");
1128 var email = req.body.email
1132 User.findOne( { 'email' : email.toLowerCase() }, function( err, user ) {
1135 // If user exists, create reset code
1136 var resetPassCode = hat(64);
1137 user.setResetPassCode(resetPassCode);
1139 // Construct url that the user can then click to reset password
1140 var resetPassUrl = 'http://' + serverHost + ((app.address().port != 80)? ':'+app.address().port: '') + '/resetpw/' + resetPassCode;
1142 // Save user to database
1143 user.save( function( err ) {
1144 log3('save '+user.email);
1146 // Construct email and send it to the user
1150 'subject' : 'Your FinalsClub.org Password has been Reset!',
1152 'template' : 'userPasswordReset',
1154 'resetPassCode' : resetPassCode,
1155 'resetPassUrl' : resetPassUrl
1159 mailer.send( message, function( err, result ) {
1161 // XXX: Add route to resend this email
1163 console.log( 'Error sending user password reset email!' );
1165 console.log( 'Successfully sent user password reset email.' );
1170 // Render request success page
1171 sendJson(res, {status: 'ok', message: 'Your password has been reset. An email has been sent to ' + email })
1175 sendJson(res, {status: 'error', message: 'We were unable to reset the password using that email address. Please try again.' })
1180 // Recieve reset password form
1181 app.post( '/resetpw/:id', checkAjax, function( req, res ) {
1182 log3("post resetpw.code");
1183 var resetPassCode = req.params.id
1184 var email = req.body.email
1185 var pass1 = req.body.pass1
1186 var pass2 = req.body.pass2
1188 // Find user by email
1189 User.findOne( { 'email' : email.toLowerCase() }, function( err, user ) {
1191 // If user exists, and the resetPassCode is valid, pass1 and pass2 match, then
1192 // save user with new password and display success message.
1194 var valid = user.resetPassword(resetPassCode, pass1, pass2);
1196 user.save( function( err ) {
1197 sendJson(res, {status: 'ok', message: 'Your password has been reset. You can now login with your the new password you just created.'})
1201 // If there was a problem, notify user
1203 sendJson(res, {status: 'error', message: 'We were unable to reset the password. Please try again.' })
1208 // Display registration page
1210 app.get( '/register', function( req, res ) {
1211 log3("get reg page");
1213 // Populate school dropdown list
1214 School.find( {} ).sort( 'name', '1' ).run( function( err, schools ) {
1215 res.render( 'register', { 'schools' : schools } );
1220 // Recieve registration form
1221 app.post( '/register', checkAjax, function( req, res ) {
1222 var sid = req.sessionId;
1224 // Create new user from User schema
1225 var user = new User;
1227 // Populate user from form
1228 user.email = req.body.email.toLowerCase();
1229 user.password = req.body.password;
1231 // If school is set to other, then fill in school as what the user entered
1232 user.school = req.body.school === 'Other' ? req.body.otherSchool : req.body.school;
1233 user.name = req.body.name;
1234 user.affil = req.body.affil;
1235 user.activated = false;
1238 if ( ( user.email === '' ) || ( !isValidEmail( user.email ) ) ) {
1239 return sendJson(res, {status: 'error', message: 'Please enter a valid email'} );
1242 // Check if password is greater than 6 characters, otherwise notify user
1243 if ( req.body.password.length < 6 ) {
1244 return sendJson(res, {status: 'error', message: 'Please enter a password longer than eight characters'} );
1247 // Pull out hostname from email
1248 var hostname = user.email.split( '@' ).pop();
1250 // Check if email is from one of the special domains
1251 if( /^(finalsclub.org)$/.test( hostname ) ) {
1255 // Save user to database
1256 user.save( function( err ) {
1257 // If error, check if it is because the user already exists, if so
1258 // get the user information and let them know
1260 if( /dup key/.test( err.message ) ) {
1261 // attempting to register an existing address
1262 User.findOne({ 'email' : user.email }, function(err, result ) {
1263 if (result.activated) {
1264 // If activated, make sure they know how to contact the admin
1265 return sendJson(res, {status: 'error', message: 'There is already someone registered with this email, if this is in error contact info@finalsclub.org for help'} );
1267 // If not activated, direct them to the resendActivation page
1268 return sendJson(res, {status: 'error', message: 'There is already someone registered with this email, if this is you, please check your email for the activation code'} );
1272 // If any other type of error, prompt them to enter the registration again
1273 return sendJson(res, {status: 'error', message: 'An error occurred during registration.'} );
1276 // send user activation email
1277 sendUserActivation( user );
1279 // Check if the hostname matches any in the approved schools
1280 School.findOne( { 'hostnames' : hostname }, function( err, school ) {
1282 // If there is a match, send associated welcome message
1283 sendUserWelcome( user, true );
1284 log3('school recognized '+school.name);
1285 // If no users exist for the school, create empty array
1286 if (!school.users) school.users = [];
1287 // Add user to the school
1288 school.users.push( user._id );
1290 // Save school to the database
1291 school.save( function( err ) {
1292 log3('school.save() done');
1293 // Notify user that they have been added to the school
1294 sendJson(res, {status: 'ok', message: 'You have automatically been added to the ' + school.name + ' network. Please check your email for the activation link'} );
1296 // Construct admin email about user registration
1300 'subject' : 'FC User Registration : User added to ' + school.name,
1302 'template' : 'userSchool',
1308 // If there isn't a match, send associated welcome message
1309 sendUserWelcome( user, false );
1310 // Tell user to check for activation link
1311 sendJson(res, {status: 'ok', message: 'Your account has been created, please check your email for the activation link'} );
1312 // Construct admin email about user registration
1316 'subject' : 'FC User Registration : Email did not match any schools',
1318 'template' : 'userNoSchool',
1324 // Send email to admin
1325 mailer.send( message, function( err, result ) {
1328 console.log( 'Error sending user has no school email to admin\nError Message: '+err.Message );
1330 console.log( 'Successfully sent user has no school email to admin.' );
1340 // Display resendActivation request page
1341 app.get( '/resendActivation', function( req, res ) {
1342 var activateCode = req.session.activateCode;
1344 // Check if user exists by activateCode set in their session
1345 User.findById( activateCode, function( err, user ) {
1346 if( ( ! user ) || ( user.activated ) ) {
1347 res.redirect( '/' );
1349 // Send activation and redirect to login
1350 sendUserActivation( user );
1352 req.flash( 'info', 'Your activation code has been resent.' );
1354 res.redirect( '/login' );
1359 // Display activation page
1360 app.get( '/activate/:code', checkAjax, function( req, res ) {
1361 var code = req.params.code;
1363 // XXX could break this out into a middleware
1365 return sendJson(res, {status:'error', message: 'Invalid activation code!'} );
1368 // Find user by activation code
1369 User.findById( code, function( err, user ) {
1370 if( err || ! user ) {
1371 // If not found, notify user of invalid code
1372 sendJson(res, {status:'error', message:'Invalid activation code!'} );
1374 // If valid, then activate user
1375 user.activated = true;
1377 // Regenerate our session and log in as the new user
1378 req.session.regenerate( function() {
1379 user.session = req.sessionID;
1381 // Save user to database
1382 user.save( function( err ) {
1384 sendJson(res, {status: 'error', message: 'Unable to activate account.'} );
1386 sendJson(res, {status: 'info', message: 'Account successfully activated. Please complete your profile.'} );
1395 app.get( '/logout', checkAjax, function( req, res ) {
1396 sys.puts("logging out");
1397 var sid = req.sessionID;
1399 // Find user by session id
1400 User.findOne( { 'session' : sid }, function( err, user ) {
1402 // Empty out session id
1405 // Save user to database
1406 user.save( function( err ) {
1407 sendJson(res, {status: 'ok', message: 'Successfully logged out'});
1410 sendJson(res, {status: 'ok', message: ''});
1415 // Recieve profile edit page form
1416 app.post( '/profile', checkAjax, loadUser, loggedIn, function( req, res ) {
1417 var user = req.user;
1418 var fields = req.body;
1421 var wasComplete = user.isComplete;
1423 if( ! fields.name ) {
1424 return sendJson(res, {status: 'error', message: 'Please enter a valid name!'} );
1426 user.name = fields.name;
1429 if( [ 'Student', 'Teachers Assistant' ].indexOf( fields.affiliation ) == -1 ) {
1430 return sendJson(res, {status: 'error', message: 'Please select a valid affiliation!'} );
1432 user.affil = fields.affiliation;
1435 if( fields.existingPassword || fields.newPassword || fields.newPasswordConfirm ) {
1436 // changing password
1437 if( ( ! user.hashed ) || user.authenticate( fields.existingPassword ) ) {
1438 if( fields.newPassword === fields.newPasswordConfirm ) {
1439 // test password strength?
1441 user.password = fields.newPassword;
1443 return sendJson(res, {status: 'error', message: 'Mismatch in new password!'} );
1446 return sendJson(res, {status: 'error', message: 'Please supply your existing password.'} );
1450 user.major = fields.major;
1451 user.bio = fields.bio;
1453 user.showName = ( fields.showName ? true : false );
1455 user.save( function( err ) {
1457 sendJson(res, {status: 'error', message: 'Unable to save user profile!'} );
1459 if( ( user.isComplete ) && ( ! wasComplete ) ) {
1460 sendJson(res, {status: 'ok', message: 'Your account is now fully activated. Thank you for joining FinalsClub!'} );
1462 sendJson(res, {status:'ok', message:'Your profile was successfully updated!'} );
1471 function loadSubject( req, res, next ) {
1472 if( url.parse( req.url ).pathname.match(/subject/) ) {
1473 ArchivedSubject.findOne({id: req.params.id }, function(err, subject) {
1474 if ( err || !subject) {
1475 sendJson(res, {status: 'not_found', message: 'Subject with this ID does not exist'} )
1477 req.subject = subject;
1486 function loadOldCourse( req, res, next ) {
1487 if( url.parse( req.url ).pathname.match(/course/) ) {
1488 ArchivedCourse.findOne({id: req.params.id }, function(err, course) {
1489 if ( err || !course ) {
1490 sendJson(res, {status: 'not_found', message: 'Course with this ID does not exist'} )
1492 req.course = course;
1501 var featuredCourses = [
1502 {name: 'The Human Mind', 'id': 1563},
1503 {name: 'Justice', 'id': 797},
1504 {name: 'Protest Literature', 'id': 1681},
1505 {name: 'Animal Cognition', 'id': 681},
1506 {name: 'Positive Psychology', 'id': 1793},
1507 {name: 'Social Psychology', 'id': 660},
1508 {name: 'The Book from Gutenberg to the Internet', 'id': 1439},
1509 {name: 'Cyberspace in Court', 'id': 1446},
1510 {name: 'Nazi Cinema', 'id': 2586},
1511 {name: 'Media and the American Mind', 'id': 2583},
1512 {name: 'Social Thought in Modern America', 'id': 2585},
1513 {name: 'Major British Writers II', 'id': 869},
1514 {name: 'Civil Procedure', 'id': 2589},
1515 {name: 'Evidence', 'id': 2590},
1516 {name: 'Management of Industrial and Nonprofit Organizations', 'id': 2591},
1519 app.get( '/learn', loadUser, function( req, res ) {
1520 res.render( 'archive/learn', { 'courses' : featuredCourses } );
1523 app.get( '/learn/random', checkAjax, function( req, res ) {
1524 sendJson(res, {status: 'ok', data: '/archive/course/'+ featuredCourses[Math.floor(Math.random()*featuredCourses.length)].id });
1527 app.get( '/archive', checkAjax, loadUser, function( req, res ) {
1528 ArchivedSubject.find({}).sort( 'name', '1' ).run( function( err, subjects ) {
1529 if ( err || subjects.length === 0) {
1530 sendJson(res, {status: 'error', message: 'There was a problem gathering the archived courses, please try again later.'} );
1532 sendJson(res, { 'subjects' : subjects, 'user': req.user.sanitized } );
1537 app.get( '/archive/subject/:id', checkAjax, loadUser, loadSubject, function( req, res ) {
1538 ArchivedCourse.find({subject_id: req.params.id}).sort('name', '1').run(function(err, courses) {
1539 if ( err || courses.length === 0 ) {
1540 sendJson(res, {status: 'not_found', message: 'There are no archived courses'} );
1542 sendJson(res, { 'courses' : courses, 'subject': req.subject, 'user': req.user.sanitized } );
1547 app.get( '/archive/course/:id', checkAjax, loadUser, loadOldCourse, function( req, res ) {
1548 ArchivedNote.find({course_id: req.params.id}).sort('name', '1').run(function(err, notes) {
1549 if ( err || notes.length === 0) {
1550 sendJson(res, {status: 'not_found', message: 'There are no notes in this course'} );
1552 notes = notes.map(function(note) { return note.sanitized });
1553 sendJson(res, { 'notes': notes, 'course' : req.course, 'user': req.user.sanitized } );
1558 app.get( '/archive/note/:id', checkAjax, loadUser, function( req, res ) {
1559 console.log( "id="+req.params.id)
1560 ArchivedNote.findById(req.params.id, function(err, note) {
1561 if ( err || !note ) {
1562 sendJson(res, {status: 'not_found', message: 'This is not a valid id for a note'} );
1564 ArchivedCourse.findOne({id: note.course_id}, function(err, course) {
1565 if ( err || !course ) {
1566 sendJson(res, {status: 'not_found', message: 'There is no course for this note'} )
1568 sendJson(res, { 'layout' : 'notesLayout', 'note' : note, 'course': course, 'user': req.user.sanitized } );
1576 app.get( '*', function(req, res) {
1577 res.sendfile('public/index.html');
1582 // The finalsclub backchannel server uses socket.io to handle communication between the server and
1583 // the browser which facilitates near realtime interaction. This allows the user to post questions
1584 // and comments and other users to get those almost immediately after they are posted, without
1585 // reloading the page or pressing a button to refresh.
1587 // The server code itself is fairly simple, mainly taking incomming messages from client browsers,
1588 // saving the data to the database, and then sending it out to everyone else connected.
1591 // Posts - Posts are the main items in backchannel, useful for questions or discussion points
1592 // [[ example object needed with explanation E.G:
1594 Post: { postID: '999-1',
1596 userName: 'Bob Jones',
1597 userAffil: 'Instructor',
1598 body: 'This is the text content of the post.',
1599 comments: { {<commentObj>, <commentObj>, ...},
1601 votes: [ <userID>, <userID>, ...],
1602 reports: [ <userID>, <userID>, ...]
1604 Comment: { body: 'foo bar', userName: 'Bob Jones', userAffil: 'Instructor' }
1606 if anonymous: userName => 'Anonymous', userAffil => 'N/A'
1611 // Comments - Comments are replies to posts, for clarification or answering questions
1612 // [[ example object needed]]
1613 // Votes - Votes signifyg a users approval of a post
1614 // [[ example object needed]]
1615 // Flags - Flagging a post signifies that it is against the rules, 2 flags moves it to the bottomw
1616 // [[ example object needed]]
1620 // body - Main content of the post
1621 // userId - Not currently used, but would contain the users id that made the post
1622 // userName - Users name that made post
1623 // userAffil - Users affiliation to their school
1624 // public - Boolean which denotes if the post is public to everyone, or private to school users only
1625 // date - Date post was made, updates when any comments are made for the post
1626 // comments - An array of comments which contain a body, userName, and userAffil
1627 // votes - An array of user ids which are the users that voted
1628 // [[ example needed ]]
1629 // reports - An array of user ids which are the users that reported the post
1630 // [[ reports would be "this post is flagged as inappropriate"? ]]
1631 // [[ bruml: consistent terminology needed ]]
1633 // Posts and comments can be made anonymously. When a post is anonymous, the users info is stripped
1634 // from the post and the userName is set to Anonymous and the userAffil to N/A. This is to allow
1635 // users the ability to make posts or comments that they might not otherwise due to not wanting
1636 // the content of the post/comment to be attributed to them.
1638 // Each time a user connects to the server, it passes through authorization which checks for a cookie
1639 // that is set by Express. If a session exists and it is for a valid logged in user, then handshake.user
1640 // is set to the users data, otherwise it is set to false. handshake.user is used later on to check if a
1641 // user is logged in, and if so display information that otherwise might not be visible to them if they
1642 // aren't apart of a particular school.
1644 // After the authorization step, the client browser sends the lecture id which is rendered into the html
1645 // page on page load from Express. This is then used to assign a 'room' for the user which is grouped
1646 // by lecture. All posts are grouped by lecture, and only exist for that lecture. After the user is
1647 // grouped into a 'room', they are sent a payload of all existing posts for that lecture, which are then
1648 // rendered in the browser.
1650 // Everything else from this point on is handled in an event form and requires a user initiating it. The
1651 // events are as follows.
1654 // A user makes a new post. A payload of data containing the post and lecture id is sent to the server.
1655 // The server recieves the data, assembles a new post object for the database and then fills it with
1656 // the appropriate data. If a user selected for the post to be anonymous, the userName and userAffil are
1657 // replaced. If the user chose for the post to be private, then public will be set to false and it
1658 // will be filtered from being sent to users not logged into and not having access to the school. Once
1659 // the post has been created and saved into the database, it is sent to all connected users to that
1660 // particular lecture, unless it is private, than only logged in users will get it.
1663 // A user votes for a post. A payload of data containing the post id and lecture id are sent along with
1664 // the user id. A new vote is created by first fetching the parent post, then adding the user id to the
1665 // votes array, and then the post is subsequently saved back to the database and sent to all connected
1666 // users unless the post is private, which then it will be only sent to logged in users.
1669 // Similar to the vote event, reports are sent as a payload of a post id, lecture id, and user id, which
1670 // are then used to fetch the parent post, add the user id to the reports array, and then saved to the db.
1671 // Then the report is sent out to all connected users unless it is a private post, which will be only sent
1672 // to logged in users. On the client, once a post has more two (2) or more reports, it will be moved to the
1673 // bottom of the interface.
1676 // A user posts a comment to a post. A payload of data containing the post id, lecture id, comment body,
1677 // user name, and user affiliation are sent to the server, which are then used to find the parent post
1678 // and then a new comment object is assembled. When new comments are made, it updates the posts date
1679 // which allows the post to be sorted by date and the posts with the freshest comments would be pushed
1680 // to the top of the interface. The comment can be anonymous, which then will have the user
1681 // name and affiliation stripped before saving to the database. The comment then will be sent out to all
1682 // connected users unless the post is private, then only logged in users will recieve the comment.
1684 var io = require( 'socket.io' ).listen( app );
1686 var Post = mongoose.model( 'Post' );
1688 io.set('authorization', function ( handshake, next ) {
1689 var rawCookie = handshake.headers.cookie;
1691 handshake.cookie = parseCookie(rawCookie);
1692 handshake.sid = handshake.cookie['connect.sid'];
1694 if ( handshake.sid ) {
1695 app.set( 'sessionStore' ).get( handshake.sid, function( err, session ) {
1697 handshake.user = false;
1698 return next(null, true);
1700 // bake a new session object for full r/w
1701 handshake.session = new Session( handshake, session );
1703 User.findOne( { session : handshake.sid }, function( err, user ) {
1705 handshake.user = user;
1706 return next(null, true);
1708 handshake.user = false;
1709 return next(null, true);
1717 return next(null, true);
1721 var backchannel = new Backchannel(app, io.of('/backchannel'), {
1722 // TODO: if lecture belongs to course (find pinker's courseId) pass a 'no-answers' true/false
1723 subscribe: function(lecture, send) {
1724 Post.find({'lecture': lecture}, function(err, posts) {
1728 post: function(fillPost) {
1729 var post = new Post;
1730 fillPost(post, function(send) {
1731 post.save(function(err) {
1736 items: function(postId, addItem) {
1737 Post.findById(postId, function( err, post ) {
1738 addItem(post, function(send) {
1739 post.save(function(err) {
1754 .on( 'connection', function( socket ) {
1755 // pull out user/session information etc.
1756 var handshake = socket.handshake;
1757 var userID = handshake.user._id;
1764 socket.on( 'join', function( note ) {
1765 if (handshake.user === false) {
1767 // XXX: replace by addToSet (once it's implemented in mongoose)
1768 Note.findById( noteID, function( err, note ) {
1770 if( note.collaborators.indexOf( userID ) == -1 ) {
1771 note.collaborators.push( userID );
1779 socket.on( 'watch', function( l ) {
1780 var sendCounts = function() {
1783 Note.find( { '_id' : { '$in' : watched } }, function( err, notes ) {
1786 function( note, callback ) {
1788 var count = note.collaborators.length;
1794 socket.emit( 'counts', send );
1796 timer = setTimeout( sendCounts, 5000 );
1802 Note.find( { 'lecture' : l }, [ '_id' ], function( err, notes ) {
1803 notes.forEach( function( note ) {
1804 watched.push( note._id );
1811 socket.on( 'disconnect', function() {
1812 clearTimeout( timer );
1814 if (handshake.user === false) {
1815 // XXX: replace with $pull once it's available
1817 Note.findById( noteID, function( err, note ) {
1819 var index = note.collaborators.indexOf( userID );
1822 note.collaborators.splice( index, 1 );
1833 // Exception Catch-All
1835 process.on('uncaughtException', function (e) {
1836 console.log("!!!!!! UNCAUGHT EXCEPTION\n" + e.stack);
1842 // mongoose now exepects a mongo url
1843 mongoose.connect( 'mongodb://localhost/fc' ); // FIXME: make relative to hostname
1845 var mailer = new Mailer( app.set('awsAccessKey'), app.set('awsSecretKey') );
1847 app.listen( serverPort, function() {
1848 console.log( "Express server listening on port %d in %s mode", app.address().port, app.settings.env );
1850 // if run as root, downgrade to the owner of this file
1851 if (process.getuid() === 0) {
1852 require('fs').stat(__filename, function(err, stats) {
1853 if (err) { return console.log(err); }
1854 process.setuid(stats.uid);
1859 function isValidEmail(email) {
1860 var re = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
1861 return email.match(re);