// FinalsClub Server
-//
+//
// This file consists of the main webserver for FinalsClub.org
// and is split between a standard CRUD style webserver and
// a websocket based realtime webserver.
var parseCookie = connect.utils.parseCookie;
var Backchannel = require('./bc/backchannel');
+
// Depracated
// Used for initial testing
var log3 = function() {}
// Load Mongoose Schemas
// The actual schemas are located in models.j
-var User = mongoose.model( 'User' );
-var School = mongoose.model( 'School' );
-var Course = mongoose.model( 'Course' );
-var Lecture = mongoose.model( 'Lecture' );
-var Note = mongoose.model( 'Note' );
+var User = mongoose.model( 'User' );
+var School = mongoose.model( 'School' );
+var Course = mongoose.model( 'Course' );
+var Lecture = mongoose.model( 'Lecture' );
+var Note = mongoose.model( 'Note' );
// More schemas used for legacy data
var ArchivedCourse = mongoose.model( 'ArchivedCourse' );
var ArchivedSubject = mongoose.model( 'ArchivedSubject' );
// XXX Not sure if necessary
-var ObjectId = mongoose.SchemaTypes.ObjectId;
+var ObjectId = mongoose.SchemaTypes.ObjectId;
// Configuration
// Use the environment variable DEV_EMAIL for testing
// If a port wasn't set earlier, set to 3000
if ( !serverPort ) {
serverPort = 3000;
- }
+ }
});
// Production configuration settings
// Set to port 80 if not set through environment variables
if ( !serverPort ) {
serverPort = 80;
- }
+ }
});
// General Express configuration settings
app.configure(function(){
+ // Views are rendered from public/index.html and main.js except for the pad that surrounds EPL and BC
+ // FIXME: make all views exist inside of public/index.html
// Views are housed in the views folder
app.set( 'views', __dirname + '/views' );
// All templates use jade for rendering
app.set( 'view engine', 'jade' );
+
// Bodyparser is required to handle form submissions
// without manually parsing them.
app.use( express.bodyParser() );
// requests that otherwise aren't handled by default.
app.use( express.methodOverride() );
// Static files are loaded when no dynamic views match.
- app.use( express.static( __dirname + '/public' ) );
+ app.use( express.static( __dirname + '/public', {maxAge: 900000} ) );
+
// Sets the routers middleware to load after everything set
// before it, but before static files.
app.use( app.router );
res.redirect( '/' );
}
+ } else if('a'==='b'){
+ console.log('never. in. behrlin.');
} else {
// If no user record was found, then we store the requested
// path they intended to view and redirect them after they
// loadSchool is used to load a school by it's id
function loadSchool( req, res, next ) {
var user = req.user;
- var schoolId = req.params.id;
+ var schoolName = req.params.name;
+ console.log( 'loading a school by id' );
- School.findById( schoolId, function( err, school ) {
+ School.findOne({'name': schoolName}).run( function( err, school ) {
+ //sys.puts(school);
if( school ) {
req.school = school;
// authorized to see or interact with anything related to that
// school.
school.authorize( user, function( authorized ){
- req.school.authorized = authorized;
- next();
+ req.school.authorized = authorized;
});
+ next();
} else {
// If no school is found, display an appropriate error.
sendJson(res, {status: 'not_found', message: 'Invalid school specified!'} );
if ( req.xhr ) {
next();
} else {
- res.sendfile( 'public/index.html' );
+ res.sendfile( 'public/index.html', function(err){
+ if(err){
+ console.log(err);
+ }
+ });
}
}
res.json(obj);
}
-// Routes
-// The following are the main CRUD routes that are used
-// to make up this web app.
-
-// Homepage
-// Public
-/*
-app.get( '/', loadUser, function( req, res ) {
- log3("get / page");
-
- res.render( 'index' );
-});
-*/
// Schools list
-// Used to display all available schools and any courses
-// in those schools.
+// Used to display all available schools
// Public with some private information
app.get( '/schools', checkAjax, loadUser, function( req, res ) {
var user = req.user;
if( schools ) {
// If schools are found, loop through them gathering any courses that are
// associated with them and then render the page with that information.
- sendJson(res, { 'user': user.sanitized, 'schools' : schools.map(function(school) {
- return school.sanitized;
- })})
+ var schools_todo = schools.length;
+ schools.map(function (school) {
+ Course.find( { 'school': school.id } ).run(function (err, courses) {
+ school.courses_length = courses.length
+ schools_todo -= 1;
+ if (schools_todo <= 0) {
+ sendJson(res, { 'user': user.sanitized, 'schools': schools.map( function(s) {
+ var school = {
+ _id: s._id,
+ name: s.name,
+ description: s.description,
+ url: s.url,
+ slug: s.slug,
+ courses: s.courses_length,
+ courseNum: s.courseNum
+ };
+ return school;
+ })
+ });
+ }
+ });
+ });
} else {
// If no schools have been found, display none
//res.render( 'schools', { 'schools' : [] } );
});
});
-app.get( '/school/:id', checkAjax, loadUser, loadSchool, function( req, res ) {
+app.get( '/school/:name', checkAjax, loadUser, loadSchool, function( req, res ) {
var school = req.school;
var user = req.user;
+ console.log("Loading a school");
school.authorize( user, function( authorized ) {
// This is used to display interface elements for those users
// that are are allowed to see th)m, for instance a 'New Course' button.
var sanitizedSchool = school.sanitized;
+ //var sanitizedSchool = {
+ // _id: school.id,
+ // name: school.name,
+ // description: school.description,
+ // url: school.url
+ //};
sanitizedSchool.authorized = authorized;
// Find all courses for school by it's id and sort by name
Course.find( { 'school' : school._id } ).sort( 'name', '1' ).run( function( err, courses ) {
// If any courses are found, set them to the appropriate school, otherwise
// leave empty.
+
if( courses.length > 0 ) {
sanitizedSchool.courses = courses.filter(function(course) {
if (!course.deleted) return course;
return course.sanitized;
});
} else {
- sanitizedSchool.courses = [];
+ school.courses = [];
}
+
// This tells async (the module) that each iteration of forEach is
// done and will continue to call the rest until they have all been
// completed, at which time the last function below will be called.
// Find user from email
User.findOne( { 'email' : email.toLowerCase() }, function( err, user ) {
- log3(err)
- log3(user)
+ log3(err)
+ log3(user)
// If user exists, check if activated, if not notify them and send them to
// the login form
if (valid) {
user.save( function( err ) {
sendJson(res, {status: 'ok', message: 'Your password has been reset. You can now login with your the new password you just created.'})
- });
+ });
}
}
// If there was a problem, notify user
user.email = req.body.email.toLowerCase();
user.password = req.body.password;
user.session = sid;
- // If school is set to other, then fill in school as what the
- // user entered
- user.school = req.body.school === 'Other' ? req.body.otherSchool : req.body.school;
+ // If school is set to other, then fill in school as what the user entered
+ user.school = req.body.school === 'Other' ? req.body.otherSchool : req.body.school;
user.name = req.body.name;
user.affil = req.body.affil;
user.activated = false;
var hostname = user.email.split( '@' ).pop();
// Check if email is from one of the special domains
- if( /^(finalsclub.org|sleepless.com)$/.test( hostname ) ) {
+ if( /^(finalsclub.org)$/.test( hostname ) ) {
user.admin = true;
}
// Logut user
app.get( '/logout', checkAjax, function( req, res ) {
+ sys.puts("logging out");
var sid = req.sessionID;
// Find user by session id
}
}
- user.major = fields.major;
- user.bio = fields.bio;
+ user.major = fields.major;
+ user.bio = fields.bio;
user.showName = ( fields.showName ? true : false );
});
var backchannel = new Backchannel(app, io.of('/backchannel'), {
+ // TODO: if lecture belongs to course (find pinker's courseId) pass a 'no-answers' true/false
subscribe: function(lecture, send) {
Post.find({'lecture': lecture}, function(err, posts) {
send(posts);
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])?/;
return email.match(re);
}
+
+// Facebook connect