app.js 61 KB

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