styling fixes, login, removed donate temp
[oweals/finalsclub.git] / models.js
1 /* vim: set ts=2: */
2
3 // DEPENDENCIES
4
5 var crypto = require( 'crypto' );
6
7 var mongoose    = require( 'mongoose' );
8
9 var Schema              = mongoose.Schema;
10 var ObjectId    = mongoose.SchemaTypes.ObjectId;
11
12 // SUPPORT FUNCTIONS
13
14 function salt() {
15         return Math.round( ( new Date().valueOf() * Math.random() ) ).toString();
16 }
17
18 // MODELS
19
20 // user
21
22 var UserSchema = new Schema( {
23         email                                           : { type : String, require: true, index : { unique : true } },
24         school                                  : String,
25         name                                            : String,
26         affil                                           : String,
27   created         : { type : Date, default : Date.now },
28         hashed                                  : String,
29         activated                               : Boolean,
30         activateCode            : String,
31         resetPassCode           : String,
32         resetPassDate           : Date,
33         salt                                            : String,
34         session                                 : String,
35         showName                                : { 'type' : Boolean, 'default' : true },
36         admin                                           : { 'type' : Boolean, 'default' : false }
37 });
38
39 UserSchema.virtual( 'sanitized' ).get(function() {
40   var user = {
41     email: this.email,
42     name: this.name,
43     affil: this.affil,
44     showName: this.showName,
45     admin: this.admin
46   }
47
48   return user;
49 });
50
51 UserSchema.virtual( 'displayName' )
52         .get( function() {
53                 if( this.showName ) {
54                         return this.name;
55                 } else {
56                         return this.email;
57                 }
58         });
59
60 UserSchema.virtual( 'password' )
61         .set( function( password ) {
62                 this.salt                               = salt();
63                 this.hashed                     = this.encrypt( password );
64         });
65
66 UserSchema.virtual( 'isComplete' )
67         .get( function() {
68                 // build on this as the schema develops
69
70                 return ( this.name && this.affil && this.hashed );
71         });
72
73 UserSchema.method( 'encrypt', function( password ) {
74         var hmac = crypto.createHmac( 'sha1', this.salt );
75
76         return hmac.update( password ).digest( 'hex' );
77 });
78
79 UserSchema.method( 'authenticate', function( plaintext ) {
80         return ( this.encrypt( plaintext ) === this.hashed );
81 });
82
83 UserSchema.method('genRandomPassword', function () {
84         // this function generates the random password, it does not keep or save it.
85         var plaintext = '';
86
87         var len = 8;
88         var charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
89         for (var i = 0; i < len; i++) {
90                 var randomPoz = Math.floor(Math.random() * charSet.length);
91                 plaintext += charSet.substring(randomPoz, randomPoz + 1);
92         }
93
94         return plaintext;
95 });
96
97 UserSchema.method( 'setResetPassCode', function ( code ) {
98         this.resetPassCode = code;
99         this.resetPassDate = new Date();
100         return this.resetPassCode;
101 });
102
103 UserSchema.method( 'canResetPassword', function ( code ) {
104         // ensure the passCode is valid, matches and the date has not yet expired, lets say 2 weeks for good measure.
105         var value = false;
106
107         var expDate = new Date();
108         expDate.setDate(expDate.getDate() - 14);
109
110         // we have a valid code and date
111         if (this.resetPassCode != null && this.resetPassDate != null && this.resetPassDate >= expDate && this.resetPassCode == code) {
112                 value = true;
113         }
114
115         return value;
116 });
117
118 UserSchema.method( 'resetPassword', function ( code, newPass1,  newPass2) {
119         // ensure the date has not expired, lets say 2 weeks for good measure.
120
121         var success = false;
122         if (this.canResetPassword(code) && newPass1 != null && newPass1.length > 0 && newPass1 == newPass2) {
123                 this.password = newPass1;
124                 this.resetPassCode = null;
125                 this.resetPassDate = null;
126                 success = true;
127         }
128
129         return success;
130 });
131
132 var User = mongoose.model( 'User', UserSchema );
133
134 // schools
135
136 var SchoolSchema = new Schema( {
137         name                            : { type : String, required : true },
138         description     : String,
139         url                                     : String,
140
141   created     : { type : Date, default : Date.now },
142         hostnames               : Array,
143
144         users                           : Array
145 });
146
147 SchoolSchema.virtual( 'sanitized' ).get(function() {
148   var school = {
149     _id: this._id,
150     name: this.name,
151     description: this.description,
152     url: this.url
153   }
154
155   return school;
156 })
157
158 SchoolSchema.method( 'authorize', function( user, cb ) {
159         return cb(user.admin || ( this.users.indexOf( user._id ) !== -1 ));
160 });
161
162 var School = mongoose.model( 'School', SchoolSchema );
163
164 // courses
165
166 var CourseSchema = new Schema( {
167         name                            : { type : String, required : true },
168         number                  : String,
169         description     : String,
170   instructor  : ObjectId,
171   subject     : String,
172   department  : String,
173         // courses are tied to one school
174         school                  : ObjectId,
175
176         // XXX: room for additional resources
177   created     : { type : Date, default : Date.now },
178   creator     : ObjectId,
179   deleted     : Boolean,
180
181         // many users may subscribe to a course
182         users                           : Array
183 });
184
185 CourseSchema.virtual( 'sanitized' ).get(function() {
186   var course = {
187     _id: this._id,
188     name: this.name,
189     number: this.number || 'None',
190     description: this.description || 'None',
191     subject: this.subject || 'None',
192     department: this.department || 'None'
193   }
194
195   return course;
196 });
197
198 CourseSchema.virtual( 'displayName' )
199         .get( function() {
200                 if( this.number ) {
201                         return this.number + ': ' + this.name;
202                 } else {
203                         return this.name;
204                 }
205         });
206
207 CourseSchema.method( 'authorize', function( user, cb ) {
208         School.findById( this.school, function( err, school ) {
209                 if ( school ) {
210                         school.authorize( user, function( result ) {
211                                                         return cb( result );
212                         })
213                 }
214         });
215 });
216
217 CourseSchema.method( 'subscribed', function( user ) {
218         return ( this.users.indexOf( user ) > -1 ) ;
219 });
220
221 CourseSchema.method( 'subscribe', function( user, callback ) {
222         var id = this._id;
223
224         // mongoose issue #404
225         Course.collection.update( { '_id' : id }, { '$addToSet' : { 'users' : user } }, function( err ) {
226                 callback( err );
227         });
228 });
229
230 CourseSchema.method( 'unsubscribe', function( user, callback ) {
231         var id = this._id;
232
233         // mongoose issue #404
234         Course.collection.update( { '_id' : id }, { '$pull' : { 'users' : user } }, function( err ) {
235                 callback( err );
236         });
237 });
238
239 CourseSchema.method( 'delete', function( callback ) {
240   var id = this._id;
241
242   Course.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
243     if (callback) callback( err );
244     Lecture.find( { course: id }, function( err, lectures) {
245       if (lectures.length > 0) {
246         lectures.forEach(function(lecture) {
247           lecture.delete();
248         })
249       }
250     })
251   })
252 });
253
254 var Course = mongoose.model( 'Course', CourseSchema );
255
256 // lectures
257
258 var LectureSchema       = new Schema( {
259         name                                    : { type : String, required : true },
260         date                                    : { type : Date, default: Date.now },
261         live                                    : Boolean,
262   creator       : ObjectId,
263   deleted       : Boolean,
264
265         course                          : ObjectId
266 });
267
268 LectureSchema.virtual( 'sanitized' ).get(function() {
269   var lecture = {
270     _id: this._id,
271     name: this.name,
272     date: this.date,
273     live: this.live
274   }
275
276   return lecture;
277 })
278
279 LectureSchema.method( 'authorize', function( user, cb ) {
280         Course.findById( this.course, function( err, course ) {
281                 if (course) {
282                         course.authorize( user, function( res ) {
283                                 return cb( res );
284                         })
285                 } else {
286                  return cb( false );
287                 }
288         });
289 });
290
291 LectureSchema.method( 'delete', function( callback ) {
292   var id = this._id;
293
294   Lecture.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
295     if (callback) callback( err );
296     Note.find( { lecture : id }, function(err, notes) {
297       notes.forEach(function(note) {
298         note.delete();
299       })
300     })
301     Post.find( { lecture : id }, function(err, posts) {
302       posts.forEach(function(post) {
303         post.delete();
304       })
305     })
306   })
307 });
308
309 var Lecture = mongoose.model( 'Lecture', LectureSchema );
310
311 // notes
312
313 var NoteSchema = new Schema( {
314         name                                    : { type : String, required : true },
315         path                                    : String,
316   public        : Boolean,
317   roID          : String,
318         visits                          : Number,
319   created         : { type : Date, default : Date.now },
320   creator       : ObjectId,
321   deleted       : Boolean,
322
323         lecture                         : ObjectId,
324
325         collaborators : [String]
326 });
327
328 NoteSchema.virtual( 'sanitized').get(function() {
329   var note = {
330     _id: this._id,
331     name: this.name,
332     path: this.path,
333     public: this.public,
334     roID: this.roID,
335     visits: this.visits
336   }
337
338   return note;
339 });
340
341 NoteSchema.method( 'authorize', function( user, cb ) {
342         Lecture.findById( this.lecture, function( err, lecture ) {
343                 if (lecture) {
344                         lecture.authorize( user, function( res ) {
345                                 return cb( res );
346                         })
347                 } else {
348                         return cb( false );
349                 }
350         });
351 });
352
353 NoteSchema.method( 'addVisit', function() {
354         var id = this._id;
355
356         Note.collection.update( { '_id' : id }, { '$inc' : { 'visits' : 1 } } );
357 });
358
359 NoteSchema.method( 'delete', function( callback ) {
360   var id = this._id;
361
362   Note.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
363     if (callback) callback( err );
364   })
365 });
366
367 var Note = mongoose.model( 'Note', NoteSchema );
368
369 // comments
370
371 var PostSchema = new Schema({
372   date      : { type : Date, default : Date.now },
373   body      : String,
374   votes     : [String],
375   reports   : [String],
376   public    : Boolean,
377
378   userid    : String, // ObjectId,
379   userName  : String,
380   userAffil : String,
381
382   comments   : Array,
383
384   lecture   : String, // ObjectId
385   deleted   : Boolean
386 })
387
388 PostSchema.method( 'delete', function( callback ) {
389   var id = this._id;
390
391   Post.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
392     if (callback) callback( err );
393   })
394 });
395
396 mongoose.model( 'Post', PostSchema );
397
398 var ArchivedCourse = new Schema({
399   id: Number,
400   instructor: String,
401   section: String,
402   name: String,
403   description: String,
404   subject_id: Number
405 })
406
407 mongoose.model( 'ArchivedCourse', ArchivedCourse )
408
409 var ArchivedNote = new Schema({
410   course_id: Number,
411   topic: String,
412   text: String
413 })
414
415 ArchivedNote.virtual( 'sanitized' ).get(function() {
416   var note = {
417     _id: this._id,
418     topic: this.topic === '' ? (this.text.replace(/(<(.|\n)*?>)|[\r\n\t]*/g, '')).substr(0, 15) + '...' : this.topic
419   }
420   return note;
421 })
422
423 mongoose.model( 'ArchivedNote', ArchivedNote )
424
425 var ArchivedSubject = new Schema({
426   id: Number,
427   name: String
428 })
429
430 mongoose.model( 'ArchivedSubject', ArchivedSubject )
431
432 module.exports.mongoose = mongoose;