I changed a word just to screw around with github.
[oweals/finalsclub.git] / models.js
index 779ed8aae986f283ce308a086ad481512b99930c..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 ) {
@@ -132,6 +144,17 @@ var SchoolSchema = new Schema( {
        users                           : Array
 });
 
+SchoolSchema.virtual( 'sanitized' ).get(function() {
+  var school = {
+    _id: this._id,
+    name: this.name,
+    description: this.description,
+    url: this.url
+  }
+
+  return school;
+})
+
 SchoolSchema.method( 'authorize', function( user, cb ) {
        return cb(user.admin || ( this.users.indexOf( user._id ) !== -1 ));
 });
@@ -153,11 +176,25 @@ var CourseSchema = new Schema( {
        // XXX: room for additional resources
   created     : { type : Date, default : Date.now },
   creator     : ObjectId,
+  deleted     : Boolean,
 
        // many users may subscribe to a course
        users                           : Array
 });
 
+CourseSchema.virtual( 'sanitized' ).get(function() {
+  var course = {
+    _id: this._id,
+    name: this.name,
+    number: this.number || 'None',
+    description: this.description || 'None',
+    subject: this.subject || 'None',
+    department: this.department || 'None'
+  }
+
+  return course;
+});
+
 CourseSchema.virtual( 'displayName' )
        .get( function() {
                if( this.number ) {
@@ -203,7 +240,14 @@ CourseSchema.method( 'delete', function( callback ) {
   var id = this._id;
 
   Course.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
-    callback( err );
+    if (callback) callback( err );
+    Lecture.find( { course: id }, function( err, lectures) {
+      if (lectures.length > 0) {
+        lectures.forEach(function(lecture) {
+          lecture.delete();
+        })
+      }
+    })
   })
 });
 
@@ -216,10 +260,22 @@ var LectureSchema = new Schema( {
        date                                    : { type : Date, default: Date.now },
        live                                    : Boolean,
   creator       : ObjectId,
+  deleted       : Boolean,
 
        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) {
@@ -232,6 +288,24 @@ LectureSchema.method( 'authorize', function( user, cb ) {
        });
 });
 
+LectureSchema.method( 'delete', function( callback ) {
+  var id = this._id;
+
+  Lecture.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
+    if (callback) callback( err );
+    Note.find( { lecture : id }, function(err, notes) {
+      notes.forEach(function(note) {
+        note.delete();
+      })
+    })
+    Post.find( { lecture : id }, function(err, posts) {
+      posts.forEach(function(post) {
+        post.delete();
+      })
+    })
+  })
+});
+
 var Lecture = mongoose.model( 'Lecture', LectureSchema );
 
 // notes
@@ -244,12 +318,26 @@ var NoteSchema = new Schema( {
        visits                          : Number,
   created         : { type : Date, default : Date.now },
   creator       : ObjectId,
+  deleted       : Boolean,
 
        lecture                         : ObjectId,
 
        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) {
@@ -268,6 +356,14 @@ NoteSchema.method( 'addVisit', function() {
        Note.collection.update( { '_id' : id }, { '$inc' : { 'visits' : 1 } } );
 });
 
+NoteSchema.method( 'delete', function( callback ) {
+  var id = this._id;
+
+  Note.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
+    if (callback) callback( err );
+  })
+});
+
 var Note = mongoose.model( 'Note', NoteSchema );
 
 // comments
@@ -285,81 +381,19 @@ var PostSchema = new Schema({
 
   comments   : Array,
 
-  lecture   : String // ObjectId
+  lecture   : String, // ObjectId
+  deleted   : Boolean
 })
 
-mongoose.model( 'Post', PostSchema );
-
-
-// Deleted documents
-
-var DeletedCourse = new Schema( {
-       name                            : { type : String, required : true },
-       number                  : String,
-       description     : String,
-  instructor  : ObjectId,
-       // courses are tied to one school
-       school                  : ObjectId,
-
-       // XXX: room for additional resources
-  created     : { type : Date, default : Date.now },
-  creator     : ObjectId,
-
-       // many users may subscribe to a course
-       users                           : Array
-});
-
-
-mongoose.model( 'DeletedCourse', DeletedCourse )
-
-var DeletedLecture = new Schema( {
-       name                                    : { type : String, required : true },
-       date                                    : { type : Date, default: Date.now },
-       live                                    : Boolean,
-  creator       : ObjectId,
-
-       course                          : ObjectId
-});
-
-
-mongoose.model( 'DeletedLecture', DeletedLecture )
-
-var DeletedNote = new Schema( {
-       name                                    : { type : String, required : true },
-       path                                    : String,
-  public        : Boolean,
-  roID          : String,
-       visits                          : Number,
-  created         : { type : Date, default : Date.now },
-  creator       : ObjectId,
-
-       lecture                         : ObjectId,
+PostSchema.method( 'delete', function( callback ) {
+  var id = this._id;
 
-       collaborators : [String]
+  Post.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
+    if (callback) callback( err );
+  })
 });
 
-
-mongoose.model( 'DeletedNote', DeletedNote )
-
-var DeletedPost = new Schema({
-  date      : { type : Date, default : Date.now },
-  body      : String,
-  votes     : [String],
-  reports   : [String],
-  public    : Boolean,
-
-  userid    : String, // ObjectId,
-  userName  : String,
-  userAffil : String,
-
-  comments   : Array,
-
-  lecture   : String // ObjectId
-})
-
-
-mongoose.model( 'DeletedPost', DeletedPost )
-
+mongoose.model( 'Post', PostSchema );
 
 var ArchivedCourse = new Schema({
   id: Number,
@@ -378,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({