app.js 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  1. // FinalsClub Server
  2. //
  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.
  6. //
  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.
  10. // Module loading
  11. var sys = require( 'sys' );
  12. var os = require( 'os' );
  13. var url = require( 'url' );
  14. var express = require( 'express' );
  15. var mongoStore = require( 'connect-mongo' );
  16. var async = require( 'async' );
  17. var db = require( './db.js' );
  18. var mongoose = require( './models.js' ).mongoose;
  19. var Mailer = require( './mailer.js' );
  20. var hat = require('hat');
  21. var connect = require( 'connect' );
  22. var Session = connect.middleware.session.Session;
  23. var parseCookie = connect.utils.parseCookie;
  24. var Backchannel = require('../bc/backchannel');
  25. // Depracated
  26. // Used for initial testing
  27. var log3 = function() {}
  28. // Create webserver
  29. var app = module.exports = express.createServer();
  30. // Load Mongoose Schemas
  31. // The actual schemas are located in models.j
  32. var User = mongoose.model( 'User' );
  33. var School = mongoose.model( 'School' );
  34. var Course = mongoose.model( 'Course' );
  35. var Lecture = mongoose.model( 'Lecture' );
  36. var Note = mongoose.model( 'Note' );
  37. // More schemas used for legacy data
  38. var ArchivedCourse = mongoose.model( 'ArchivedCourse' );
  39. var ArchivedNote = mongoose.model( 'ArchivedNote' );
  40. var ArchivedSubject = mongoose.model( 'ArchivedSubject' );
  41. // XXX Not sure if necessary
  42. var ObjectId = mongoose.SchemaTypes.ObjectId;
  43. // Configuration
  44. // Use the environment variable DEV_EMAIL for testing
  45. var ADMIN_EMAIL = process.env.DEV_EMAIL || 'info@finalsclub.org';
  46. // Set server hostname and port from environment variables,
  47. // then check if set.
  48. // XXX Can be cleaned up
  49. var serverHost = process.env.SERVER_HOST;
  50. var serverPort = process.env.SERVER_PORT;
  51. if( serverHost ) {
  52. console.log( 'Using server hostname defined in environment: %s', serverHost );
  53. } else {
  54. serverHost = os.hostname();
  55. console.log( 'No hostname defined, defaulting to os.hostname(): %s', serverHost );
  56. }
  57. // Express configuration depending on environment
  58. // development is intended for developing locally or
  59. // when not in production, otherwise production is used
  60. // when the site will be run live for regular usage.
  61. app.configure( 'development', function() {
  62. // In development mode, all errors and stack traces will be
  63. // dumped to the console and on page for easier troubleshooting
  64. // and debugging.
  65. app.set( 'errorHandler', express.errorHandler( { dumpExceptions: true, showStack: true } ) );
  66. // Set database connection information from environment
  67. // variables otherwise use localhost.
  68. app.set( 'dbHost', process.env.MONGO_HOST || 'localhost' );
  69. app.set( 'dbUri', 'mongodb://' + app.set( 'dbHost' ) + '/fc' );
  70. // Set Amazon access and secret keys from environment
  71. // variables. These keys are intended to be secret, so
  72. // are not included in the source code, but set on the server
  73. // manually.
  74. app.set( 'awsAccessKey', process.env.AWS_ACCESS_KEY_ID );
  75. app.set( 'awsSecretKey', process.env.AWS_SECRET_ACCESS_KEY );
  76. // If a port wasn't set earlier, set to 3000
  77. if ( !serverPort ) {
  78. serverPort = 3000;
  79. }
  80. });
  81. // Production configuration settings
  82. app.configure( 'production', function() {
  83. // At the moment we have errors outputting everything
  84. // so if there are any issues it is easier to track down.
  85. // Once the site is more stable it will be prudent to
  86. // use less error tracing.
  87. app.set( 'errorHandler', express.errorHandler( { dumpExceptions: true, showStack: true } ) );
  88. // Disable view cache due to stale views.
  89. // XXX Disable view caching temp
  90. app.disable( 'view cache' )
  91. // Against setting the database connection information
  92. // XXX Can be cleaned up or combined
  93. app.set( 'dbHost', process.env.MONGO_HOST || 'localhost' );
  94. app.set( 'dbUri', 'mongodb://' + app.set( 'dbHost' ) + '/fc' );
  95. // XXX Can be cleaned up or combined
  96. app.set( 'awsAccessKey', process.env.AWS_ACCESS_KEY_ID );
  97. app.set( 'awsSecretKey', process.env.AWS_SECRET_ACCESS_KEY );
  98. // Set to port 80 if not set through environment variables
  99. if ( !serverPort ) {
  100. serverPort = 80;
  101. }
  102. });
  103. // General Express configuration settings
  104. app.configure(function(){
  105. // Views are housed in the views folder
  106. app.set( 'views', __dirname + '/views' );
  107. // All templates use jade for rendering
  108. app.set( 'view engine', 'jade' );
  109. // Bodyparser is required to handle form submissions
  110. // without manually parsing them.
  111. app.use( express.bodyParser() );
  112. app.use( express.cookieParser() );
  113. // Sessions are stored in mongodb which allows them
  114. // to be persisted even between server restarts.
  115. app.set( 'sessionStore', new mongoStore( {
  116. 'url' : app.set( 'dbUri' )
  117. }));
  118. // This is where the actual Express session handler
  119. // is defined, with a mongoStore being set as the
  120. // session storage versus in memory storage that is
  121. // used by default.
  122. app.use( express.session( {
  123. // A secret 'password' for encrypting and decrypting
  124. // cookies.
  125. // XXX Should be handled differently
  126. 'secret' : 'finalsclub',
  127. // The max age of the cookies that is allowed
  128. // 60 (seconds) * 60 (minutes) * 24 (hours) * 30 (days) * 1000 (milliseconds)
  129. 'maxAge' : new Date(Date.now() + (60 * 60 * 24 * 30 * 1000)),
  130. 'store' : app.set( 'sessionStore' )
  131. }));
  132. // methodOverride is used to handle PUT and DELETE HTTP
  133. // requests that otherwise aren't handled by default.
  134. app.use( express.methodOverride() );
  135. // Sets the routers middleware to load after everything set
  136. // before it, but before static files.
  137. app.use( app.router );
  138. // Static files are loaded when no dynamic views match.
  139. app.use( express.static( __dirname + '/public' ) );
  140. // This is the errorHandler set in configuration earlier
  141. // being set to a variable to be used after all other
  142. // middleware is loaded. Error handling should always
  143. // come last or near the bottom.
  144. var errorHandler = app.set( 'errorHandler' );
  145. app.use( errorHandler );
  146. });
  147. // Mailer functions and helpers
  148. // These are helper functions that make for cleaner code.
  149. // sendUserActivation is for when a user registers and
  150. // first needs to activate their account to use it.
  151. function sendUserActivation( user ) {
  152. var message = {
  153. 'to' : user.email,
  154. 'subject' : 'Activate your FinalsClub.org Account',
  155. // Templates are in the email folder and use ejs
  156. 'template' : 'userActivation',
  157. // Locals are used inside ejs so dynamic information
  158. // can be rendered properly.
  159. 'locals' : {
  160. 'user' : user,
  161. 'serverHost' : serverHost
  162. }
  163. };
  164. // Email is sent here
  165. mailer.send( message, function( err, result ) {
  166. if( err ) {
  167. // XXX: Add route to resend this email
  168. console.log( 'Error sending user activation email\nError Message: '+err.Message );
  169. } else {
  170. console.log( 'Successfully sent user activation email.' );
  171. }
  172. });
  173. }
  174. // sendUserWelcome is for when a user registers and
  175. // a welcome email is sent.
  176. function sendUserWelcome( user, school ) {
  177. // If a user is not apart of a supported school, they are
  178. // sent a different template than if they are apart of a
  179. // supported school.
  180. var template = school ? 'userWelcome' : 'userWelcomeNoSchool';
  181. var message = {
  182. 'to' : user.email,
  183. 'subject' : 'Welcome to FinalsClub',
  184. 'template' : template,
  185. 'locals' : {
  186. 'user' : user,
  187. 'serverHost' : serverHost
  188. }
  189. };
  190. mailer.send( message, function( err, result ) {
  191. if( err ) {
  192. // XXX: Add route to resend this email
  193. console.log( 'Error sending user welcome email\nError Message: '+err.Message );
  194. } else {
  195. console.log( 'Successfully sent user welcome email.' );
  196. }
  197. });
  198. }
  199. // Helper middleware
  200. // These functions are used later in the routes to help
  201. // load information and variables, as well as handle
  202. // various instances like checking if a user is logged in
  203. // or not.
  204. function loggedIn( req, res, next ) {
  205. // If req.user is set, then pass on to the next function
  206. // or else alert the user with an error message.
  207. if( req.user ) {
  208. next();
  209. } else {
  210. req.flash( 'error', 'You must be logged in to access that feature!' );
  211. res.redirect( '/' );
  212. }
  213. }
  214. // This loads the user if logged in
  215. function loadUser( req, res, next ) {
  216. var sid = req.sessionID;
  217. console.log( 'got request from session ID: %s', sid );
  218. // Find a user based on their stored session id
  219. User.findOne( { session : sid }, function( err, user ) {
  220. log3(err);
  221. log3(user);
  222. // If a user is found then set req.user the contents of user
  223. // and make sure req.user.loggedIn is true.
  224. if( user ) {
  225. req.user = user;
  226. req.user.loggedIn = true;
  227. log3( 'authenticated user: '+req.user._id+' / '+req.user.email+'');
  228. // Check if a user is activated. If not, then redirec
  229. // to the homepage and tell them to check their email
  230. // for the activation email.
  231. if( req.user.activated ) {
  232. // Is the user's profile complete? If not, redirect to their profile
  233. if( ! req.user.isComplete ) {
  234. if( url.parse( req.url ).pathname != '/profile' ) {
  235. req.flash( 'info', 'Your profile is incomplete. Please complete your profile to fully activate your account.' );
  236. res.redirect( '/profile' );
  237. } else {
  238. next();
  239. }
  240. } else {
  241. next();
  242. }
  243. } else {
  244. req.flash( 'info', 'This account has not been activated. Check your email for the activation URL.' );
  245. res.redirect( '/' );
  246. }
  247. } else {
  248. // If no user record was found, then we store the requested
  249. // path they intended to view and redirect them after they
  250. // login if it is requred.
  251. var path = url.parse( req.url ).pathname;
  252. req.session.redirect = path;
  253. // Set req.user to an empty object so it doesn't throw errors
  254. // later on that it isn't defined.
  255. req.user = {};
  256. next();
  257. }
  258. });
  259. }
  260. // loadSchool is used to load a school by it's id
  261. function loadSchool( req, res, next ) {
  262. var user = req.user;
  263. var schoolId = req.params.id;
  264. School.findById( schoolId, function( err, school ) {
  265. if( school ) {
  266. req.school = school;
  267. // If a school is found, the user is checked to see if they are
  268. // authorized to see or interact with anything related to that
  269. // school.
  270. school.authorize( user, function( authorized ){
  271. req.school.authorized = authorized;
  272. next();
  273. });
  274. } else {
  275. // If no school is found, display an appropriate error.
  276. req.flash( 'error', 'Invalid school specified!' );
  277. res.redirect( '/' );
  278. }
  279. });
  280. }
  281. // loadSchool is used to load a course by it's id
  282. function loadCourse( req, res, next ) {
  283. var user = req.user;
  284. var courseId = req.params.id;
  285. Course.findById( courseId, function( err, course ) {
  286. if( course && !course.deleted ) {
  287. req.course = course;
  288. // If a course is found, the user is checked to see if they are
  289. // authorized to see or interact with anything related to that
  290. // school.
  291. course.authorize( user, function( authorized ) {
  292. req.course.authorized = authorized;
  293. next();
  294. });
  295. } else {
  296. // If no course is found, display an appropriate error.
  297. req.flash( 'error', 'Invalid course specified!' );
  298. res.redirect( '/' );
  299. }
  300. });
  301. }
  302. // loadLecture is used to load a lecture by it's id
  303. function loadLecture( req, res, next ) {
  304. var user = req.user;
  305. var lectureId = req.params.id;
  306. Lecture.findById( lectureId, function( err, lecture ) {
  307. if( lecture && !lecture.deleted ) {
  308. req.lecture = lecture;
  309. // If a lecture is found, the user is checked to see if they are
  310. // authorized to see or interact with anything related to that
  311. // school.
  312. lecture.authorize( user, function( authorized ) {
  313. req.lecture.authorized = authorized;
  314. next();
  315. });
  316. } else {
  317. // If no lecture is found, display an appropriate error.
  318. req.flash( 'error', 'Invalid lecture specified!' );
  319. res.redirect( '/' );
  320. }
  321. });
  322. }
  323. // loadNote is used to load a note by it's id
  324. // This is a lot more complicated than the above
  325. // due to public/private handling of notes.
  326. function loadNote( req, res, next ) {
  327. var user = req.user ? req.user : false;
  328. var noteId = req.params.id;
  329. Note.findById( noteId, function( err, note ) {
  330. // If a note is found, and user is set, check if
  331. // user is authorized to interact with that note.
  332. if( note && user && !note.deleted ) {
  333. note.authorize( user, function( auth ) {
  334. if( auth ) {
  335. // If authorzied, then set req.note to be used later
  336. req.note = note;
  337. next();
  338. } else if ( note.public ) {
  339. // If not authorized, but the note is public, then
  340. // designate the note read only (RO) and store req.note
  341. req.RO = true;
  342. req.note = note;
  343. next();
  344. } else {
  345. // If the user is not authorized and the note is private
  346. // then display and error.
  347. req.flash( 'error', 'You do not have permission to access that note.' );
  348. res.redirect( '/' );
  349. }
  350. })
  351. } else if ( note && note.public && !note.deleted ) {
  352. // If note is found, but user is not set because they are not
  353. // logged in, and the note is public, set the note to read only
  354. // and store the note for later.
  355. req.note = note;
  356. req.RO = true;
  357. next();
  358. } else if ( note && !note.public && !note.deleted ) {
  359. // If the note is found, but user is not logged in and the note is
  360. // not public, then ask them to login to view the note. Once logged
  361. // in they will be redirected to the note, at which time authorization
  362. // handling will be put in effect above.
  363. req.session.redirect = '/note/' + note._id;
  364. req.flash( 'error', 'You must be logged in to view that note.' );
  365. res.redirect( '/login' );
  366. } else {
  367. // No note was found
  368. req.flash( 'error', 'Invalid note specified!' );
  369. res.redirect( '/schools' );
  370. }
  371. });
  372. }
  373. // Dynamic Helpers are loaded automatically into views
  374. app.dynamicHelpers( {
  375. // express-messages is for flash messages for easy
  376. // errors and information display
  377. 'messages' : require( 'express-messages' ),
  378. // By default the req object isn't sen't to views
  379. // during rendering, this allows you to use the
  380. // user object if available in views.
  381. 'user' : function( req, res ) {
  382. return req.user;
  383. },
  384. // Same, this allows session to be available in views.
  385. 'session' : function( req, res ) {
  386. return req.session;
  387. }
  388. });
  389. // Routes
  390. // The following are the main CRUD routes that are used
  391. // to make up this web app.
  392. // Homepage
  393. // Public
  394. app.get( '/', loadUser, function( req, res ) {
  395. log3("get / page");
  396. res.render( 'index' );
  397. });
  398. // Schools list
  399. // Used to display all available schools and any courses
  400. // in those schools.
  401. // Public with some private information
  402. app.get( '/schools', loadUser, function( req, res ) {
  403. var user = req.user;
  404. // Find all schools and sort by name
  405. // XXX mongoose's documentation on sort is extremely poor, tread carefully
  406. School.find( {} ).sort( 'name', '1' ).run( function( err, schools ) {
  407. if( schools ) {
  408. // If schools are found, loop through them gathering any courses that are
  409. // associated with them and then render the page with that information.
  410. async.forEach(
  411. schools,
  412. function( school, callback ) {
  413. // Check if user is authorized with each school
  414. school.authorize( user, function( authorized ) {
  415. // This is used to display interface elements for those users
  416. // that are are allowed to see them, for instance a 'New Course' button.
  417. school.authorized = authorized;
  418. // Find all courses for school by it's id and sort by name
  419. Course.find( { 'school' : school._id } ).sort( 'name', '1' ).run( function( err, courses ) {
  420. // If any courses are found, set them to the appropriate school, otherwise
  421. // leave empty.
  422. if( courses.length > 0 ) {
  423. school.courses = courses.filter(function(course) {
  424. if (!course.deleted) return course;
  425. });
  426. } else {
  427. school.courses = [];
  428. }
  429. // This tells async (the module) that each iteration of forEach is
  430. // done and will continue to call the rest until they have all been
  431. // completed, at which time the last function below will be called.
  432. callback();
  433. });
  434. });
  435. },
  436. // After all schools and courses have been found, render them
  437. function( err ) {
  438. res.render( 'schools', { 'schools' : schools } );
  439. }
  440. );
  441. } else {
  442. // If no schools have been found, display none
  443. res.render( 'schools', { 'schools' : [] } );
  444. }
  445. });
  446. });
  447. // New course page
  448. // Displays form to create new course
  449. // Private, requires user to be authorized
  450. app.get( '/:id/course/new', loadUser, loadSchool, function( req, res ) {
  451. // Load school from middleware
  452. var school = req.school;
  453. // If school was not loaded for whatever reason, or the user is not authorized
  454. // then redirect to the main schools page.
  455. if( ( ! school ) || ( ! school.authorized ) ) {
  456. return res.redirect( '/schools' );
  457. }
  458. // If they are authorized and the school exists, then render the page
  459. res.render( 'course/new', { 'school': school } );
  460. });
  461. // Recieves new course form
  462. app.post( '/:id/course/new', loadUser, loadSchool, function( req, res ) {
  463. var school = req.school;
  464. // Creates new course from Course Schema
  465. var course = new Course;
  466. // Gathers instructor information from form
  467. var instructorEmail = req.body.email.toLowerCase();
  468. var instructorName = req.body.instructorName;
  469. // If school doesn't exist or user is not authorized redirect to main schools page
  470. if( ( ! school ) || ( ! school.authorized ) ) {
  471. res.redirect( '/schools' );
  472. }
  473. // If instructorEmail isn't set, or name isn't set, display error and re-render the page.
  474. if ( !instructorEmail || !instructorName ) {
  475. req.flash( 'error', 'Invalid parameters!' )
  476. return res.render( 'course/new' );
  477. }
  478. // Fill out the course with information from the form
  479. course.number = req.body.number;
  480. course.name = req.body.name;
  481. course.description = req.body.description;
  482. course.school = school._id;
  483. course.creator = req.user._id;
  484. course.subject = req.body.subject;
  485. course.department = req.body.department;
  486. // Check if a user exists with the instructorEmail, if not then create
  487. // a new user and send them an instructor welcome email.
  488. User.findOne( { 'email' : instructorEmail }, function( err, user ) {
  489. if ( !user ) {
  490. var user = new User;
  491. user.name = instructorName
  492. user.email = instructorEmail;
  493. user.affil = 'Instructor';
  494. user.school = school.name;
  495. user.activated = false;
  496. // Validate instructorEmail
  497. // XXX Probably could be done before checking db
  498. if ( ( user.email === '' ) || ( !isValidEmail( user.email ) ) ) {
  499. req.flash( 'error', 'Please enter a valid email' );
  500. // XXX This needs to be fixed, this is not the proper flow
  501. return res.redirect( '/register' );
  502. }
  503. // Once the new user information has been completed, save the user
  504. // to the database then email them the instructor welcome email.
  505. user.save(function( err ) {
  506. // If there was an error saving the instructor, prompt the user to fill out
  507. // the information again.
  508. if ( err ) {
  509. req.flash( 'error', 'Invalid parameters!' )
  510. return res.render( 'course/new' );
  511. } else {
  512. var message = {
  513. to : user.email,
  514. 'subject' : 'A non-profit open education initiative',
  515. 'template' : 'instructorInvite',
  516. 'locals' : {
  517. 'course' : course,
  518. 'school' : school,
  519. 'user' : user,
  520. 'serverHost' : serverHost
  521. }
  522. };
  523. mailer.send( message, function( err, result ) {
  524. if( err ) {
  525. console.log( 'Error inviting instructor to course!' );
  526. } else {
  527. console.log( 'Successfully invited instructor to course.' );
  528. }
  529. });
  530. // After emails are sent, set the courses instructor to the
  531. // new users id and then save the course to the database.
  532. course.instructor = user._id;
  533. course.save( function( err ) {
  534. if( err ) {
  535. // XXX better validation
  536. req.flash( 'error', 'Invalid parameters!' );
  537. return res.render( 'course/new' );
  538. } else {
  539. // Once the course has been completed email the admin with information
  540. // on the course and new instructor
  541. var message = {
  542. to : ADMIN_EMAIL,
  543. 'subject' : school.name+' has a new course: '+course.name,
  544. 'template' : 'newCourse',
  545. 'locals' : {
  546. 'course' : course,
  547. 'instructor' : user,
  548. 'user' : req.user,
  549. 'serverHost' : serverHost
  550. }
  551. };
  552. mailer.send( message, function( err, result ) {
  553. if ( err ) {
  554. console.log( 'Error sending new course email to info@finalsclub.org' )
  555. } else {
  556. console.log( 'Successfully invited instructor to course')
  557. }
  558. })
  559. // Redirect the user to the schools page where they can see
  560. // their new course.
  561. // XXX Redirect to the new course instead
  562. res.redirect( '/schools' );
  563. }
  564. });
  565. }
  566. })
  567. } else {
  568. // If the user exists, then check if they are already and instructor
  569. if (user.affil === 'Instructor') {
  570. // If they are an instructor, then save the course with the appropriate
  571. // information and email the admin.
  572. course.instructor = user._id;
  573. course.save( function( err ) {
  574. if( err ) {
  575. // XXX better validation
  576. req.flash( 'error', 'Invalid parameters!' );
  577. return res.render( 'course/new' );
  578. } else {
  579. var message = {
  580. to : ADMIN_EMAIL,
  581. 'subject' : school.name+' has a new course: '+course.name,
  582. 'template' : 'newCourse',
  583. 'locals' : {
  584. 'course' : course,
  585. 'instructor' : user,
  586. 'user' : req.user,
  587. 'serverHost' : serverHost
  588. }
  589. };
  590. mailer.send( message, function( err, result ) {
  591. if ( err ) {
  592. console.log( 'Error sending new course email to info@finalsclub.org' )
  593. } else {
  594. console.log( 'Successfully invited instructor to course')
  595. }
  596. })
  597. // XXX Redirect to the new course instead
  598. res.redirect( '/schools' );
  599. }
  600. });
  601. } else {
  602. // The existing user isn't an instructor, so the user is notified of the error
  603. // and the course isn't created.
  604. req.flash( 'error', 'The existing user\'s email you entered is not an instructor' );
  605. res.render( 'course/new' );
  606. }
  607. }
  608. })
  609. });
  610. // Individual Course Listing
  611. // Public with private information
  612. app.get( '/course/:id', loadUser, loadCourse, function( req, res ) {
  613. var userId = req.user._id;
  614. var course = req.course;
  615. // Check if the user is subscribed to the course
  616. // XXX Not currently used for anything
  617. var subscribed = course.subscribed( userId );
  618. // Find lectures associated with this course and sort by name
  619. Lecture.find( { 'course' : course._id } ).sort( 'name', '1' ).run( function( err, lectures ) {
  620. // Get course instructor information using their id
  621. User.findById( course.instructor, function( err, instructor ) {
  622. // Render course and lectures
  623. res.render( 'course/index', { 'course' : course, 'instructor': instructor, 'subscribed' : subscribed, 'lectures' : lectures } );
  624. })
  625. });
  626. });
  627. // Edit Course
  628. app.get( '/course/:id/edit', loadUser, loadCourse, function( req, res) {
  629. var course = req.course;
  630. var user = req.user;
  631. if ( user.admin ) {
  632. res.render( 'course/new', {course: course} )
  633. } else {
  634. req.flash( 'error', 'You don\'t have permission to do that' )
  635. res.redirect( '/schools' );
  636. }
  637. })
  638. // Recieve Course Edit Form
  639. app.post( '/course/:id/edit', loadUser, loadCourse, function( req, res ) {
  640. var course = req.course;
  641. var user = req.user;
  642. if (user.admin) {
  643. var courseChanges = req.body;
  644. course.number = courseChanges.number;
  645. course.name = courseChanges.name;
  646. course.description = courseChanges.description;
  647. course.department = courseChanges.department;
  648. course.save(function(err) {
  649. if (err) {
  650. req.flash( 'error', 'There was an error saving the course' );
  651. }
  652. res.redirect( '/course/'+ course._id.toString());
  653. })
  654. } else {
  655. req.flash( 'error', 'You don\'t have permission to do that' )
  656. res.redirect( '/schools' );
  657. }
  658. });
  659. // Delete Course
  660. app.get( '/course/:id/delete', loadUser, loadCourse, function( req, res) {
  661. var course = req.course;
  662. var user = req.user;
  663. if ( user.admin ) {
  664. course.delete(function( err ) {
  665. if ( err ) req.flash( 'info', 'There was a problem removing course: ' + err )
  666. else req.flash( 'info', 'Successfully removed course' )
  667. res.redirect( '/schools' );
  668. });
  669. } else {
  670. req.flash( 'error', 'You don\'t have permission to do that' )
  671. res.redirect( '/schools' );
  672. }
  673. })
  674. // Subscribe to course
  675. // XXX Not currently used for anything
  676. app.get( '/course/:id/subscribe', loadUser, loadCourse, function( req, res ) {
  677. var course = req.course;
  678. var userId = req.user._id;
  679. course.subscribe( userId, function( err ) {
  680. if( err ) {
  681. req.flash( 'error', 'Error subscribing to course!' );
  682. }
  683. res.redirect( '/course/' + course._id );
  684. });
  685. });
  686. // Unsubscribe from course
  687. // XXX Not currently used for anything
  688. app.get( '/course/:id/unsubscribe', loadUser, loadCourse, function( req, res ) {
  689. var course = req.course;
  690. var userId = req.user._id;
  691. course.unsubscribe( userId, function( err ) {
  692. if( err ) {
  693. req.flash( 'error', 'Error unsubscribing from course!' );
  694. }
  695. res.redirect( '/course/' + course._id );
  696. });
  697. });
  698. // Create new lecture
  699. app.get( '/course/:id/lecture/new', loadUser, loadCourse, function( req, res ) {
  700. var courseId = req.params.id;
  701. var course = req.course;
  702. var lecture = {};
  703. // If course isn't valid or user isn't authorized for course, redirect
  704. if( ( ! course ) || ( ! course.authorized ) ) {
  705. return res.redirect( '/course/' + courseId );
  706. }
  707. // Render new lecture form
  708. res.render( 'lecture/new', { 'lecture' : lecture } );
  709. });
  710. // Recieve New Lecture Form
  711. app.post( '/course/:id/lecture/new', loadUser, loadCourse, function( req, res ) {
  712. var courseId = req.params.id;
  713. var course = req.course;
  714. // Create new lecture from Lecture schema
  715. var lecture = new Lecture;
  716. if( ( ! course ) || ( ! course.authorized ) ) {
  717. res.redirect( '/course/' + courseId );
  718. return;
  719. }
  720. // Populate lecture with form data
  721. lecture.name = req.body.name;
  722. lecture.date = req.body.date;
  723. lecture.course = course._id;
  724. lecture.creator = req.user._id;
  725. // Save lecture to database
  726. lecture.save( function( err ) {
  727. if( err ) {
  728. // XXX better validation
  729. req.flash( 'error', 'Invalid parameters!' );
  730. res.render( 'lecture/new', { 'lecture' : lecture } );
  731. } else {
  732. // XXX Redirect to new lecture instead
  733. res.redirect( '/course/' + course._id );
  734. }
  735. });
  736. });
  737. // Display individual lecture and related notes
  738. app.get( '/lecture/:id', loadUser, loadLecture, function( req, res ) {
  739. var lecture = req.lecture;
  740. // Grab the associated course
  741. // XXX this should be done with DBRefs eventually
  742. Course.findById( lecture.course, function( err, course ) {
  743. if( course ) {
  744. // If course is found, find instructor information to be displayed on page
  745. User.findById( course.instructor, function( err, instructor ) {
  746. // Pull out our notes
  747. Note.find( { 'lecture' : lecture._id } ).sort( 'name', '1' ).run( function( err, notes ) {
  748. if ( !req.user.loggedIn || !req.lecture.authorized ) {
  749. // Loop through notes and only return those that are public if the
  750. // user is not logged in or not authorized for that lecture
  751. notes = notes.filter(function( note ) {
  752. if ( note.public ) return note;
  753. })
  754. }
  755. // Render lecture and notes
  756. res.render( 'lecture/index', {
  757. 'lecture' : lecture,
  758. 'course' : course,
  759. 'instructor' : instructor,
  760. 'notes' : notes,
  761. 'counts' : counts,
  762. 'javascripts' : [ 'counts.js' ]
  763. });
  764. });
  765. })
  766. } else {
  767. // XXX with DBRefs we will be able to reassign orphaned courses/lecture/pads
  768. req.flash( 'error', 'That lecture is orphaned!' );
  769. res.redirect( '/' );
  770. }
  771. });
  772. });
  773. // Display new note form
  774. app.get( '/lecture/:id/notes/new', loadUser, loadLecture, function( req, res ) {
  775. var lectureId = req.params.id;
  776. var lecture = req.lecture;
  777. var note = {};
  778. if( ( ! lecture ) || ( ! lecture.authorized ) ) {
  779. res.redirect( '/lecture/' + lectureId );
  780. return;
  781. }
  782. res.render( 'notes/new', { 'note' : note } );
  783. });
  784. // Recieve new note form
  785. app.post( '/lecture/:id/notes/new', loadUser, loadLecture, function( req, res ) {
  786. var lectureId = req.params.id;
  787. var lecture = req.lecture;
  788. if( ( ! lecture ) || ( ! lecture.authorized ) ) {
  789. res.redirect( '/lecture/' + lectureId );
  790. return;
  791. }
  792. // Create note from Note schema
  793. var note = new Note;
  794. // Populate note from form data
  795. note.name = req.body.name;
  796. note.date = req.body.date;
  797. note.lecture = lecture._id;
  798. note.public = req.body.private ? false : true;
  799. note.creator = req.user._id;
  800. // Save note to database
  801. note.save( function( err ) {
  802. if( err ) {
  803. // XXX better validation
  804. req.flash( 'error', 'Invalid parameters!' );
  805. res.render( 'notes/new', { 'note' : note } );
  806. } else {
  807. // XXX Redirect to new note instead
  808. res.redirect( '/lecture/' + lecture._id );
  809. }
  810. });
  811. });
  812. // Display individual note page
  813. app.get( '/note/:id', loadUser, loadNote, function( req, res ) {
  814. var note = req.note;
  815. // Set read only id for etherpad-lite or false for later check
  816. var roID = note.roID || false;
  817. var lectureId = note.lecture;
  818. // Count the amount of visits, but only once per session
  819. if ( req.session.visited ) {
  820. if ( req.session.visited.indexOf( note._id.toString() ) == -1 ) {
  821. req.session.visited.push( note._id );
  822. note.addVisit();
  823. }
  824. } else {
  825. req.session.visited = [];
  826. req.session.visited.push( note._id );
  827. note.addVisit();
  828. }
  829. // If a read only id exists process note
  830. if (roID) {
  831. processReq();
  832. } else {
  833. // If read only id doesn't, then fetch the read only id from the database and then
  834. // process note.
  835. // XXX Soon to be depracated due to a new API in etherpad that makes for a
  836. // much cleaner solution.
  837. db.open('mongodb://' + app.set( 'dbHost' ) + '/etherpad/etherpad', function( err, epl ) {
  838. epl.findOne( { key: 'pad2readonly:' + note._id }, function(err, record) {
  839. if ( record ) {
  840. roID = record.value.replace(/"/g, '');
  841. } else {
  842. roID = false;
  843. }
  844. processReq();
  845. })
  846. })
  847. }
  848. function processReq() {
  849. // Find lecture
  850. Lecture.findById( lectureId, function( err, lecture ) {
  851. if( ! lecture ) {
  852. req.flash( 'error', 'That notes page is orphaned!' );
  853. res.redirect( '/' );
  854. }
  855. // Find notes based on lecture id, which will be displayed in a dropdown
  856. // on the page
  857. Note.find( { 'lecture' : lecture._id }, function( err, otherNotes ) {
  858. if( !req.RO ) {
  859. // User is logged in and sees full notepad
  860. res.render( 'notes/index', {
  861. 'layout' : 'noteLayout',
  862. 'host' : serverHost,
  863. 'note' : note,
  864. 'lecture' : lecture,
  865. 'otherNotes' : otherNotes,
  866. 'RO' : false,
  867. 'roID' : roID,
  868. 'stylesheets' : [ 'dropdown.css', 'fc2.css' ],
  869. 'javascripts' : [ 'dropdown.js', 'counts.js', 'backchannel.js', 'jquery.tmpl.min.js' ]
  870. });
  871. } else {
  872. // User is not logged in and sees notepad that is public
  873. res.render( 'notes/public', {
  874. 'layout' : 'noteLayout',
  875. 'host' : serverHost,
  876. 'note' : note,
  877. 'otherNotes' : otherNotes,
  878. 'roID' : roID,
  879. 'lecture' : lecture,
  880. 'stylesheets' : [ 'dropdown.css', 'fc2.css' ],
  881. 'javascripts' : [ 'dropdown.js', 'counts.js', 'backchannel.js', 'jquery.tmpl.min.js' ]
  882. });
  883. }
  884. });
  885. });
  886. }
  887. });
  888. // Static pages and redirects
  889. app.get( '/about', loadUser, function( req, res ) {
  890. res.redirect( 'http://blog.finalsclub.org/about.html' );
  891. });
  892. app.get( '/press', loadUser, function( req, res ) {
  893. res.render( 'static/press' );
  894. });
  895. app.get( '/conduct', loadUser, function( req, res ) {
  896. res.render( 'static/conduct' );
  897. });
  898. app.get( '/legal', loadUser, function( req, res ) {
  899. res.redirect( 'http://blog.finalsclub.org/legal.html' );
  900. });
  901. app.get( '/contact', loadUser, function( req, res ) {
  902. res.redirect( 'http://blog.finalsclub.org/contact.html' );
  903. });
  904. app.get( '/privacy', loadUser, function( req, res ) {
  905. res.render( 'static/privacy' );
  906. });
  907. // Authentication routes
  908. // These are used for logging in, logging out, registering
  909. // and other user authentication purposes
  910. // Render login page
  911. app.get( '/login', function( req, res ) {
  912. log3("get login page")
  913. res.render( 'login' );
  914. });
  915. // Recieve login form
  916. app.post( '/login', function( req, res ) {
  917. var email = req.body.email;
  918. var password = req.body.password;
  919. log3("post login ...")
  920. // Find user from email
  921. User.findOne( { 'email' : email.toLowerCase() }, function( err, user ) {
  922. log3(err)
  923. log3(user)
  924. // If user exists, check if activated, if not notify them and send them to
  925. // the login form
  926. if( user ) {
  927. if( ! user.activated ) {
  928. // (undocumented) markdown-esque link functionality in req.flash
  929. req.flash( 'error', 'This account isn\'t activated. Check your inbox or [click here](/resendActivation) to resend the activation email.' );
  930. req.session.activateCode = user._id;
  931. res.render( 'login' );
  932. } else {
  933. // If user is activated, check if their password is correct
  934. if( user.authenticate( password ) ) {
  935. log3("pass ok")
  936. var sid = req.sessionID;
  937. user.session = sid;
  938. // Set the session then save the user to the database
  939. user.save( function() {
  940. var redirect = req.session.redirect;
  941. // login complete, remember the user's email for next time
  942. req.session.email = email;
  943. // alert the successful login
  944. req.flash( 'info', 'Successfully logged in!' );
  945. // redirect to profile if we don't have a stashed request
  946. res.redirect( redirect || '/profile' );
  947. });
  948. } else {
  949. // Notify user of bad login
  950. req.flash( 'error', 'Invalid login!' );
  951. res.render( 'login' );
  952. }
  953. }
  954. } else {
  955. // Notify user of bad login
  956. log3("bad login")
  957. req.flash( 'error', 'Invalid login!' );
  958. res.render( 'login' );
  959. }
  960. });
  961. });
  962. // Request reset password
  963. app.get( '/resetpw', function( req, res ) {
  964. log3("get resetpw page");
  965. res.render( 'resetpw' );
  966. });
  967. // Display reset password from requested email
  968. app.get( '/resetpw/:id', function( req, res ) {
  969. var resetPassCode = req.params.id
  970. res.render( 'resetpw', { 'verify': true, 'resetPassCode' : resetPassCode } );
  971. });
  972. // Recieve reset password request form
  973. app.post( '/resetpw', function( req, res ) {
  974. log3("post resetpw");
  975. var email = req.body.email
  976. // Search for user
  977. User.findOne( { 'email' : email.toLowerCase() }, function( err, user ) {
  978. if( user ) {
  979. // If user exists, create reset code
  980. var resetPassCode = hat(64);
  981. user.setResetPassCode(resetPassCode);
  982. // Construct url that the user can then click to reset password
  983. var resetPassUrl = 'http://' + serverHost + ((app.address().port != 80)? ':'+app.address().port: '') + '/resetpw/' + resetPassCode;
  984. // Save user to database
  985. user.save( function( err ) {
  986. log3('save '+user.email);
  987. // Construct email and send it to the user
  988. var message = {
  989. 'to' : user.email,
  990. 'subject' : 'Your FinalsClub.org Password has been Reset!',
  991. 'template' : 'userPasswordReset',
  992. 'locals' : {
  993. 'resetPassCode' : resetPassCode,
  994. 'resetPassUrl' : resetPassUrl
  995. }
  996. };
  997. mailer.send( message, function( err, result ) {
  998. if( err ) {
  999. // XXX: Add route to resend this email
  1000. console.log( 'Error sending user password reset email!' );
  1001. } else {
  1002. console.log( 'Successfully sent user password reset email.' );
  1003. }
  1004. });
  1005. // Render request success page
  1006. res.render( 'resetpw-success', { 'email' : email } );
  1007. });
  1008. } else {
  1009. // Notify of error
  1010. res.render( 'resetpw-error', { 'email' : email } );
  1011. }
  1012. });
  1013. });
  1014. // Recieve reset password form
  1015. app.post( '/resetpw/:id', function( req, res ) {
  1016. log3("post resetpw.code");
  1017. var resetPassCode = req.params.id
  1018. var email = req.body.email
  1019. var pass1 = req.body.pass1
  1020. var pass2 = req.body.pass2
  1021. // Find user by email
  1022. User.findOne( { 'email' : email.toLowerCase() }, function( err, user ) {
  1023. var valid = false;
  1024. // If user exists, and the resetPassCode is valid, pass1 and pass2 match, then
  1025. // save user with new password and display success message.
  1026. if( user ) {
  1027. var valid = user.resetPassword(resetPassCode, pass1, pass2);
  1028. if (valid) {
  1029. user.save( function( err ) {
  1030. res.render( 'resetpw-success', { 'verify' : true, 'email' : email, 'resetPassCode' : resetPassCode } );
  1031. });
  1032. }
  1033. }
  1034. // If there was a problem, notify user
  1035. if (!valid) {
  1036. res.render( 'resetpw-error', { 'verify' : true, 'email' : email } );
  1037. }
  1038. });
  1039. });
  1040. // Display registration page
  1041. app.get( '/register', function( req, res ) {
  1042. log3("get reg page");
  1043. // Populate school dropdown list
  1044. School.find( {} ).sort( 'name', '1' ).run( function( err, schools ) {
  1045. res.render( 'register', { 'schools' : schools } );
  1046. })
  1047. });
  1048. // Recieve registration form
  1049. app.post( '/register', function( req, res ) {
  1050. var sid = req.sessionId;
  1051. // Create new user from User schema
  1052. var user = new User;
  1053. // Populate user from form
  1054. user.email = req.body.email.toLowerCase();
  1055. user.password = req.body.password;
  1056. user.session = sid;
  1057. // If school is set to other, then fill in school as what the
  1058. // user entered
  1059. user.school = req.body.school === 'Other' ? req.body.otherSchool : req.body.school;
  1060. user.name = req.body.name;
  1061. user.affil = req.body.affil;
  1062. user.activated = false;
  1063. // Validate email
  1064. if ( ( user.email === '' ) || ( !isValidEmail( user.email ) ) ) {
  1065. req.flash( 'error', 'Please enter a valid email' );
  1066. return res.redirect( '/register' );
  1067. }
  1068. // Check if password is greater than 6 characters, otherwise notify user
  1069. if ( req.body.password.length < 6 ) {
  1070. req.flash( 'error', 'Please enter a password longer than eight characters' );
  1071. return res.redirect( '/register' );
  1072. }
  1073. // Pull out hostname from email
  1074. var hostname = user.email.split( '@' ).pop();
  1075. // Check if email is from one of the special domains
  1076. if( /^(finalsclub.org|sleepless.com)$/.test( hostname ) ) {
  1077. user.admin = true;
  1078. }
  1079. // Save user to database
  1080. user.save( function( err ) {
  1081. // If error, check if it is because the user already exists, if so
  1082. // get the user information and let them know
  1083. if ( err ) {
  1084. if( /dup key/.test( err.message ) ) {
  1085. // attempting to register an existing address
  1086. User.findOne({ 'email' : user.email }, function(err, result ) {
  1087. if (result.activated) {
  1088. // If activated, make sure they know how to contact the admin
  1089. req.flash( 'error', 'There is already someone registered with this email, if this is in error contact info@finalsclub.org for help' )
  1090. return res.redirect( '/register' )
  1091. } else {
  1092. // If not activated, direct them to the resendActivation page
  1093. req.flash( 'error', 'There is already someone registered with this email, if this is you, please check your email for the activation code' )
  1094. return res.redirect( '/resendActivation' )
  1095. }
  1096. });
  1097. } else {
  1098. // If any other type of error, prompt them to enter the registration again
  1099. req.flash( 'error', 'An error occurred during registration.' );
  1100. return res.redirect( '/register' );
  1101. }
  1102. } else {
  1103. // send user activation email
  1104. sendUserActivation( user );
  1105. // Check if the hostname matches any in the approved schools
  1106. School.findOne( { 'hostnames' : hostname }, function( err, school ) {
  1107. if( school ) {
  1108. // If there is a match, send associated welcome message
  1109. sendUserWelcome( user, true );
  1110. log3('school recognized '+school.name);
  1111. // If no users exist for the school, create empty array
  1112. if (!school.users) school.users = [];
  1113. // Add user to the school
  1114. school.users.push( user._id );
  1115. // Save school to the database
  1116. school.save( function( err ) {
  1117. log3('school.save() done');
  1118. // Notify user that they have been added to the school
  1119. req.flash( 'info', 'You have automatically been added to the ' + school.name + ' network. Please check your email for the activation link' );
  1120. res.redirect( '/' );
  1121. });
  1122. // Construct admin email about user registration
  1123. var message = {
  1124. 'to' : ADMIN_EMAIL,
  1125. 'subject' : 'FC User Registration : User added to ' + school.name,
  1126. 'template' : 'userSchool',
  1127. 'locals' : {
  1128. 'user' : user
  1129. }
  1130. }
  1131. } else {
  1132. // If there isn't a match, send associated welcome message
  1133. sendUserWelcome( user, false );
  1134. // Tell user to check for activation link
  1135. req.flash( 'info', 'Your account has been created, please check your email for the activation link' )
  1136. res.redirect( '/' );
  1137. // Construct admin email about user registration
  1138. var message = {
  1139. 'to' : ADMIN_EMAIL,
  1140. 'subject' : 'FC User Registration : Email did not match any schools',
  1141. 'template' : 'userNoSchool',
  1142. 'locals' : {
  1143. 'user' : user
  1144. }
  1145. }
  1146. }
  1147. // Send email to admin
  1148. mailer.send( message, function( err, result ) {
  1149. if ( err ) {
  1150. console.log( 'Error sending user has no school email to admin\nError Message: '+err.Message );
  1151. } else {
  1152. console.log( 'Successfully sent user has no school email to admin.' );
  1153. }
  1154. })
  1155. });
  1156. }
  1157. });
  1158. });
  1159. // Display resendActivation request page
  1160. app.get( '/resendActivation', function( req, res ) {
  1161. var activateCode = req.session.activateCode;
  1162. // Check if user exists by activateCode set in their session
  1163. User.findById( activateCode, function( err, user ) {
  1164. if( ( ! user ) || ( user.activated ) ) {
  1165. res.redirect( '/' );
  1166. } else {
  1167. // Send activation and redirect to login
  1168. sendUserActivation( user );
  1169. req.flash( 'info', 'Your activation code has been resent.' );
  1170. res.redirect( '/login' );
  1171. }
  1172. });
  1173. });
  1174. // Display activation page
  1175. app.get( '/activate/:code', function( req, res ) {
  1176. var code = req.params.code;
  1177. // XXX could break this out into a middleware
  1178. if( ! code ) {
  1179. res.redirect( '/' );
  1180. }
  1181. // Find user by activation code
  1182. User.findById( code, function( err, user ) {
  1183. if( err || ! user ) {
  1184. // If not found, notify user of invalid code
  1185. req.flash( 'error', 'Invalid activation code!' );
  1186. res.redirect( '/' );
  1187. } else {
  1188. // If valid, then activate user
  1189. user.activated = true;
  1190. // Regenerate our session and log in as the new user
  1191. req.session.regenerate( function() {
  1192. user.session = req.sessionID;
  1193. // Save user to database
  1194. user.save( function( err ) {
  1195. if( err ) {
  1196. req.flash( 'error', 'Unable to activate account.' );
  1197. res.redirect( '/' );
  1198. } else {
  1199. req.flash( 'info', 'Account successfully activated. Please complete your profile.' );
  1200. res.redirect( '/profile' );
  1201. }
  1202. });
  1203. });
  1204. }
  1205. });
  1206. });
  1207. // Logut user
  1208. app.get( '/logout', function( req, res ) {
  1209. var sid = req.sessionID;
  1210. // Find user by session id
  1211. User.findOne( { 'session' : sid }, function( err, user ) {
  1212. if( user ) {
  1213. // Empty out session id
  1214. user.session = '';
  1215. // Save user to database
  1216. user.save( function( err ) {
  1217. res.redirect( '/' );
  1218. });
  1219. } else {
  1220. res.redirect( '/' );
  1221. }
  1222. });
  1223. });
  1224. // Display users profile page
  1225. app.get( '/profile', loadUser, loggedIn, function( req, res ) {
  1226. var user = req.user;
  1227. res.render( 'profile/index', { 'user' : user } );
  1228. });
  1229. // Recieve profile edit page form
  1230. app.post( '/profile', loadUser, loggedIn, function( req, res ) {
  1231. var user = req.user;
  1232. var fields = req.body;
  1233. var error = false;
  1234. var wasComplete = user.isComplete;
  1235. if( ! fields.name ) {
  1236. req.flash( 'error', 'Please enter a valid name!' );
  1237. error = true;
  1238. } else {
  1239. user.name = fields.name;
  1240. }
  1241. if( [ 'Student', 'Teachers Assistant' ].indexOf( fields.affiliation ) == -1 ) {
  1242. req.flash( 'error', 'Please select a valid affiliation!' );
  1243. error = true;
  1244. } else {
  1245. user.affil = fields.affiliation;
  1246. }
  1247. if( fields.existingPassword || fields.newPassword || fields.newPasswordConfirm ) {
  1248. // changing password
  1249. if( ( ! user.hashed ) || user.authenticate( fields.existingPassword ) ) {
  1250. if( fields.newPassword === fields.newPasswordConfirm ) {
  1251. // test password strength?
  1252. user.password = fields.newPassword;
  1253. } else {
  1254. req.flash( 'error', 'Mismatch in new password!' );
  1255. error = true;
  1256. }
  1257. } else {
  1258. req.flash( 'error', 'Please supply your existing password.' );
  1259. error = true;
  1260. }
  1261. }
  1262. user.major = fields.major;
  1263. user.bio = fields.bio;
  1264. user.showName = ( fields.showName ? true : false );
  1265. if( ! error ) {
  1266. user.save( function( err ) {
  1267. if( err ) {
  1268. req.flash( 'error', 'Unable to save user profile!' );
  1269. } else {
  1270. if( ( user.isComplete ) && ( ! wasComplete ) ) {
  1271. req.flash( 'info', 'Your account is now fully activated. Thank you for joining FinalsClub!' );
  1272. res.redirect( '/' );
  1273. } else {
  1274. res.render( 'info', 'Your profile was successfully updated!' );
  1275. res.render( 'profile/index', { 'user' : user } );
  1276. }
  1277. }
  1278. });
  1279. } else {
  1280. res.render( 'profile/index', { 'user' : user } );
  1281. }
  1282. });
  1283. // Old Notes
  1284. function loadSubject( req, res, next ) {
  1285. if( url.parse( req.url ).pathname.match(/subject/) ) {
  1286. ArchivedSubject.findOne({id: req.params.id }, function(err, subject) {
  1287. if ( err ) {
  1288. req.flash( 'error', 'Subject with this ID does not exist' )
  1289. res.redirect( '/archive' );
  1290. } else {
  1291. req.subject = subject;
  1292. next()
  1293. }
  1294. })
  1295. } else {
  1296. next()
  1297. }
  1298. }
  1299. function loadOldCourse( req, res, next ) {
  1300. if( url.parse( req.url ).pathname.match(/course/) ) {
  1301. ArchivedCourse.findOne({id: req.params.id }, function(err, course) {
  1302. if ( err ) {
  1303. req.flash( 'error', 'Course with this ID does not exist' )
  1304. res.redirect( '/archive' );
  1305. } else {
  1306. req.course = course;
  1307. next()
  1308. }
  1309. })
  1310. } else {
  1311. next()
  1312. }
  1313. }
  1314. var featuredCourses = [
  1315. {name: 'The Human Mind', 'id': 1563},
  1316. {name: 'Justice', 'id': 797},
  1317. {name: 'Protest Literature', 'id': 1681},
  1318. {name: 'Animal Cognition', 'id': 681},
  1319. {name: 'Positive Psychology', 'id': 1793},
  1320. {name: 'Social Psychology', 'id': 660},
  1321. {name: 'The Book from Gutenberg to the Internet', 'id': 1439},
  1322. {name: 'Cyberspace in Court', 'id': 1446},
  1323. {name: 'Nazi Cinema', 'id': 2586},
  1324. {name: 'Media and the American Mind', 'id': 2583},
  1325. {name: 'Social Thought in Modern America', 'id': 2585},
  1326. {name: 'Major British Writers II', 'id': 869},
  1327. {name: 'Civil Procedure', 'id': 2589},
  1328. {name: 'Evidence', 'id': 2590},
  1329. {name: 'Management of Industrial and Nonprofit Organizations', 'id': 2591},
  1330. ];
  1331. app.get( '/learn', loadUser, function( req, res ) {
  1332. res.render( 'archive/learn', { 'courses' : featuredCourses } );
  1333. })
  1334. app.get( '/learn/random', loadUser, function( req, res ) {
  1335. res.redirect( '/archive/course/'+ featuredCourses[Math.floor(Math.random()*featuredCourses.length)].id);
  1336. })
  1337. app.get( '/archive', loadUser, function( req, res ) {
  1338. ArchivedSubject.find({}).sort( 'name', '1' ).run( function( err, subjects ) {
  1339. if ( err ) {
  1340. req.flash( 'error', 'There was a problem gathering the archived courses, please try again later.' );
  1341. res.redirect( '/' );
  1342. } else {
  1343. res.render( 'archive/index', { 'subjects' : subjects } );
  1344. }
  1345. })
  1346. })
  1347. app.get( '/archive/subject/:id', loadUser, loadSubject, function( req, res ) {
  1348. ArchivedCourse.find({subject_id: req.params.id}).sort('name', '1').run(function(err, courses) {
  1349. if ( err ) {
  1350. req.flash( 'error', 'There are no archived courses' );
  1351. res.redirect( '/' );
  1352. } else {
  1353. res.render( 'archive/courses', { 'courses' : courses, 'subject': req.subject } );
  1354. }
  1355. })
  1356. })
  1357. app.get( '/archive/course/:id', loadUser, loadOldCourse, function( req, res ) {
  1358. ArchivedNote.find({course_id: req.params.id}).sort('name', '1').run(function(err, notes) {
  1359. if ( err ) {
  1360. req.flash( 'error', 'There are no notes in this course' );
  1361. res.redirect( '/archive' );
  1362. } else {
  1363. res.render( 'archive/notes', { 'notes' : notes, 'course' : req.course } );
  1364. }
  1365. })
  1366. })
  1367. app.get( '/archive/note/:id', loadUser, function( req, res ) {
  1368. ArchivedNote.findById(req.params.id, function(err, note) {
  1369. if ( err ) {
  1370. req.flash( 'error', 'This is not a valid id for a note' );
  1371. res.redirect( '/archive' );
  1372. } else {
  1373. ArchivedCourse.findOne({id: note.course_id}, function(err, course) {
  1374. if ( err ) {
  1375. req.flash( 'error', 'There is no course for this note' )
  1376. res.redirect( '/archive' )
  1377. } else {
  1378. res.render( 'archive/note', { 'layout' : 'notesLayout', 'note' : note, 'course': course } );
  1379. }
  1380. })
  1381. }
  1382. })
  1383. })
  1384. // socket.io server
  1385. // The finalsclub backchannel server uses socket.io to handle communication between the server and
  1386. // the browser which facilitates near realtime interaction. This allows the user to post questions
  1387. // and comments and other users to get those almost immediately after they are posted, without
  1388. // reloading the page or pressing a button to refresh.
  1389. //
  1390. // The server code itself is fairly simple, mainly taking incomming messages from client browsers,
  1391. // saving the data to the database, and then sending it out to everyone else connected.
  1392. //
  1393. // Data types:
  1394. // Posts - Posts are the main items in backchannel, useful for questions or discussion points
  1395. // [[ example object needed with explanation E.G:
  1396. /*
  1397. Post: { postID: '999-1',
  1398. userID: '1234',
  1399. userName: 'Bob Jones',
  1400. userAffil: 'Instructor',
  1401. body: 'This is the text content of the post.',
  1402. comments: { {<commentObj>, <commentObj>, ...},
  1403. public: true,
  1404. votes: [ <userID>, <userID>, ...],
  1405. reports: [ <userID>, <userID>, ...]
  1406. }
  1407. Comment: { body: 'foo bar', userName: 'Bob Jones', userAffil: 'Instructor' }
  1408. if anonymous: userName => 'Anonymous', userAffil => 'N/A'
  1409. */
  1410. //
  1411. //
  1412. //
  1413. // Comments - Comments are replies to posts, for clarification or answering questions
  1414. // [[ example object needed]]
  1415. // Votes - Votes signifyg a users approval of a post
  1416. // [[ example object needed]]
  1417. // Flags - Flagging a post signifies that it is against the rules, 2 flags moves it to the bottomw
  1418. // [[ example object needed]]
  1419. //
  1420. //
  1421. // Post Schema
  1422. // body - Main content of the post
  1423. // userId - Not currently used, but would contain the users id that made the post
  1424. // userName - Users name that made post
  1425. // userAffil - Users affiliation to their school
  1426. // public - Boolean which denotes if the post is public to everyone, or private to school users only
  1427. // date - Date post was made, updates when any comments are made for the post
  1428. // comments - An array of comments which contain a body, userName, and userAffil
  1429. // votes - An array of user ids which are the users that voted
  1430. // [[ example needed ]]
  1431. // reports - An array of user ids which are the users that reported the post
  1432. // [[ reports would be "this post is flagged as inappropriate"? ]]
  1433. // [[ bruml: consistent terminology needed ]]
  1434. //
  1435. // Posts and comments can be made anonymously. When a post is anonymous, the users info is stripped
  1436. // from the post and the userName is set to Anonymous and the userAffil to N/A. This is to allow
  1437. // users the ability to make posts or comments that they might not otherwise due to not wanting
  1438. // the content of the post/comment to be attributed to them.
  1439. //
  1440. // Each time a user connects to the server, it passes through authorization which checks for a cookie
  1441. // that is set by Express. If a session exists and it is for a valid logged in user, then handshake.user
  1442. // is set to the users data, otherwise it is set to false. handshake.user is used later on to check if a
  1443. // user is logged in, and if so display information that otherwise might not be visible to them if they
  1444. // aren't apart of a particular school.
  1445. //
  1446. // After the authorization step, the client browser sends the lecture id which is rendered into the html
  1447. // page on page load from Express. This is then used to assign a 'room' for the user which is grouped
  1448. // by lecture. All posts are grouped by lecture, and only exist for that lecture. After the user is
  1449. // grouped into a 'room', they are sent a payload of all existing posts for that lecture, which are then
  1450. // rendered in the browser.
  1451. //
  1452. // Everything else from this point on is handled in an event form and requires a user initiating it. The
  1453. // events are as follows.
  1454. //
  1455. // Post event
  1456. // A user makes a new post. A payload of data containing the post and lecture id is sent to the server.
  1457. // The server recieves the data, assembles a new post object for the database and then fills it with
  1458. // the appropriate data. If a user selected for the post to be anonymous, the userName and userAffil are
  1459. // replaced. If the user chose for the post to be private, then public will be set to false and it
  1460. // will be filtered from being sent to users not logged into and not having access to the school. Once
  1461. // the post has been created and saved into the database, it is sent to all connected users to that
  1462. // particular lecture, unless it is private, than only logged in users will get it.
  1463. //
  1464. // Vote event
  1465. // A user votes for a post. A payload of data containing the post id and lecture id are sent along with
  1466. // the user id. A new vote is created by first fetching the parent post, then adding the user id to the
  1467. // votes array, and then the post is subsequently saved back to the database and sent to all connected
  1468. // users unless the post is private, which then it will be only sent to logged in users.
  1469. //
  1470. // Report event
  1471. // Similar to the vote event, reports are sent as a payload of a post id, lecture id, and user id, which
  1472. // are then used to fetch the parent post, add the user id to the reports array, and then saved to the db.
  1473. // Then the report is sent out to all connected users unless it is a private post, which will be only sent
  1474. // to logged in users. On the client, once a post has more two (2) or more reports, it will be moved to the
  1475. // bottom of the interface.
  1476. //
  1477. // Comment event
  1478. // A user posts a comment to a post. A payload of data containing the post id, lecture id, comment body,
  1479. // user name, and user affiliation are sent to the server, which are then used to find the parent post
  1480. // and then a new comment object is assembled. When new comments are made, it updates the posts date
  1481. // which allows the post to be sorted by date and the posts with the freshest comments would be pushed
  1482. // to the top of the interface. The comment can be anonymous, which then will have the user
  1483. // name and affiliation stripped before saving to the database. The comment then will be sent out to all
  1484. // connected users unless the post is private, then only logged in users will recieve the comment.
  1485. var io = require( 'socket.io' ).listen( app );
  1486. var Post = mongoose.model( 'Post' );
  1487. io.set('authorization', function ( handshake, next ) {
  1488. var rawCookie = handshake.headers.cookie;
  1489. if (rawCookie) {
  1490. handshake.cookie = parseCookie(rawCookie);
  1491. handshake.sid = handshake.cookie['connect.sid'];
  1492. if ( handshake.sid ) {
  1493. app.set( 'sessionStore' ).get( handshake.sid, function( err, session ) {
  1494. if( err ) {
  1495. handshake.user = false;
  1496. return next(null, true);
  1497. } else {
  1498. // bake a new session object for full r/w
  1499. handshake.session = new Session( handshake, session );
  1500. User.findOne( { session : handshake.sid }, function( err, user ) {
  1501. if( user ) {
  1502. handshake.user = user;
  1503. return next(null, true);
  1504. } else {
  1505. handshake.user = false;
  1506. return next(null, true);
  1507. }
  1508. });
  1509. }
  1510. })
  1511. }
  1512. } else {
  1513. data.user = false;
  1514. return next(null, true);
  1515. }
  1516. });
  1517. var backchannel = new Backchannel(app, io.of('/backchannel'), {
  1518. subscribe: function(lecture, send) {
  1519. Post.find({'lecture': lecture}, function(err, posts) {
  1520. send(posts);
  1521. });
  1522. },
  1523. post: function(fillPost) {
  1524. var post = new Post;
  1525. fillPost(post, function(send) {
  1526. post.save(function(err) {
  1527. send();
  1528. });
  1529. });
  1530. },
  1531. items: function(postId, addItem) {
  1532. Post.findById(postId, function( err, post ) {
  1533. addItem(post, function(send) {
  1534. post.save(function(err) {
  1535. send();
  1536. });
  1537. })
  1538. })
  1539. }
  1540. });
  1541. var counters = {};
  1542. var counts = io
  1543. .of( '/counts' )
  1544. .on( 'connection', function( socket ) {
  1545. // pull out user/session information etc.
  1546. var handshake = socket.handshake;
  1547. var userID = handshake.user._id;
  1548. var watched = [];
  1549. var noteID = null;
  1550. var timer = null;
  1551. socket.on( 'join', function( note ) {
  1552. if (handshake.user === false) {
  1553. noteID = note;
  1554. // XXX: replace by addToSet (once it's implemented in mongoose)
  1555. Note.findById( noteID, function( err, note ) {
  1556. if( note ) {
  1557. if( note.collaborators.indexOf( userID ) == -1 ) {
  1558. note.collaborators.push( userID );
  1559. note.save();
  1560. }
  1561. }
  1562. });
  1563. }
  1564. });
  1565. socket.on( 'watch', function( l ) {
  1566. var sendCounts = function() {
  1567. var send = {};
  1568. Note.find( { '_id' : { '$in' : watched } }, function( err, notes ) {
  1569. async.forEach(
  1570. notes,
  1571. function( note, callback ) {
  1572. var id = note._id;
  1573. var count = note.collaborators.length;
  1574. send[ id ] = count;
  1575. callback();
  1576. }, function() {
  1577. socket.emit( 'counts', send );
  1578. timer = setTimeout( sendCounts, 5000 );
  1579. }
  1580. );
  1581. });
  1582. }
  1583. Note.find( { 'lecture' : l }, [ '_id' ], function( err, notes ) {
  1584. notes.forEach( function( note ) {
  1585. watched.push( note._id );
  1586. });
  1587. });
  1588. sendCounts();
  1589. });
  1590. socket.on( 'disconnect', function() {
  1591. clearTimeout( timer );
  1592. if (handshake.user === false) {
  1593. // XXX: replace with $pull once it's available
  1594. if( noteID ) {
  1595. Note.findById( noteID, function( err, note ) {
  1596. if( note ) {
  1597. var index = note.collaborators.indexOf( userID );
  1598. if( index != -1 ) {
  1599. note.collaborators.splice( index, 1 );
  1600. }
  1601. note.save();
  1602. }
  1603. });
  1604. }
  1605. }
  1606. });
  1607. });
  1608. // Exception Catch-All
  1609. process.on('uncaughtException', function (e) {
  1610. console.log("!!!!!! UNCAUGHT EXCEPTION\n" + e.stack);
  1611. });
  1612. // Launch
  1613. mongoose.connect( app.set( 'dbUri' ) );
  1614. mongoose.connection.db.serverConfig.connection.autoReconnect = true
  1615. var mailer = new Mailer( app.set('awsAccessKey'), app.set('awsSecretKey') );
  1616. app.listen( serverPort, function() {
  1617. console.log( "Express server listening on port %d in %s mode", app.address().port, app.settings.env );
  1618. // if run as root, downgrade to the owner of this file
  1619. if (process.getuid() === 0) {
  1620. require('fs').stat(__filename, function(err, stats) {
  1621. if (err) { return console.log(err); }
  1622. process.setuid(stats.uid);
  1623. });
  1624. }
  1625. });
  1626. function isValidEmail(email) {
  1627. 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])?/;
  1628. return email.match(re);
  1629. }