I changed a word just to screw around with github.
[oweals/finalsclub.git] / models.js
index f9bea517884dc5cd846eaf2457a6e85a1dadbea9..15b812521b5b662a9a3dce6143b83408d7483629 100644 (file)
--- a/models.js
+++ b/models.js
@@ -36,6 +36,18 @@ var UserSchema = new Schema( {
        admin                                           : { 'type' : Boolean, 'default' : false }
 });
 
+UserSchema.virtual( 'sanitized' ).get(function() {
+  var user = {
+    email: this.email,
+    name: this.name,
+    affil: this.affil,
+    showName: this.showName,
+    admin: this.admin
+  }
+
+  return user;
+});
+
 UserSchema.virtual( 'displayName' )
        .get( function() {
                if( this.showName ) {
@@ -174,10 +186,10 @@ CourseSchema.virtual( 'sanitized' ).get(function() {
   var course = {
     _id: this._id,
     name: this.name,
-    number: this.number,
-    description: this.description,
-    subject: this.subject,
-    department: this.department
+    number: this.number || 'None',
+    description: this.description || 'None',
+    subject: this.subject || 'None',
+    department: this.department || 'None'
   }
 
   return course;
@@ -253,6 +265,17 @@ var LectureSchema  = new Schema( {
        course                          : ObjectId
 });
 
+LectureSchema.virtual( 'sanitized' ).get(function() {
+  var lecture = {
+    _id: this._id,
+    name: this.name,
+    date: this.date,
+    live: this.live
+  }
+
+  return lecture;
+})
+
 LectureSchema.method( 'authorize', function( user, cb ) {
        Course.findById( this.course, function( err, course ) {
                if (course) {
@@ -302,6 +325,19 @@ var NoteSchema = new Schema( {
        collaborators : [String]
 });
 
+NoteSchema.virtual( 'sanitized').get(function() {
+  var note = {
+    _id: this._id,
+    name: this.name,
+    path: this.path,
+    public: this.public,
+    roID: this.roID,
+    visits: this.visits
+  }
+
+  return note;
+});
+
 NoteSchema.method( 'authorize', function( user, cb ) {
        Lecture.findById( this.lecture, function( err, lecture ) {
                if (lecture) {
@@ -376,6 +412,14 @@ var ArchivedNote = new Schema({
   text: String
 })
 
+ArchivedNote.virtual( 'sanitized' ).get(function() {
+  var note = {
+    _id: this._id,
+    topic: this.topic === '' ? (this.text.replace(/(<(.|\n)*?>)|[\r\n\t]*/g, '')).substr(0, 15) + '...' : this.topic
+  }
+  return note;
+})
+
 mongoose.model( 'ArchivedNote', ArchivedNote )
 
 var ArchivedSubject = new Schema({