All files / models comment.js

43.66% Statements 31/71
11.11% Branches 2/18
44% Functions 11/25
43.66% Lines 31/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 15415x 15x 15x 15x     15x                 15x                                               15x                                             15x                                             15x 3x   3x 3x 3x       3x         15x                           15x 9x   9x 9x 9x       9x               15x 3x 3x 3x   3x   3x     3x       3x   3x         15x    
module.exports = function(crowi) {
  var debug = require('debug')('crowi:models:comment')
  var mongoose = require('mongoose')
  var ObjectId = mongoose.Schema.Types.ObjectId
  var commentSchema
 
  commentSchema = new mongoose.Schema({
    page: { type: ObjectId, ref: 'Page', index: true },
    creator: { type: ObjectId, ref: 'User', index: true },
    revision: { type: ObjectId, ref: 'Revision', index: true },
    comment: { type: String, required: true },
    commentPosition: { type: Number, default: -1 },
    createdAt: { type: Date, default: Date.now },
  })
 
  commentSchema.statics.create = function(pageId, creatorId, revisionId, comment, position) {
    var Comment = this
    var commentPosition = position || -1
 
    return new Promise(function(resolve, reject) {
      var newComment = new Comment()
 
      newComment.page = pageId
      newComment.creator = creatorId
      newComment.revision = revisionId
      newComment.comment = comment
      newComment.commentPosition = commentPosition
 
      newComment.save(function(err, data) {
        if (err) {
          debug('Error on saving comment.', err)
          return reject(err)
        }
        debug('Comment saved.', data)
        return resolve(data)
      })
    })
  }
 
  commentSchema.statics.getCommentsByPageId = function(id) {
    var self = this
 
    return new Promise(function(resolve, reject) {
      self
        .find({ page: id })
        .sort({ createdAt: -1 })
        .populate('creator')
        .exec(function(err, data) {
          if (err) {
            return reject(err)
          }
 
          if (data.length < 1) {
            return resolve([])
          }
 
          // debug('Comment loaded', data);
          return resolve(data)
        })
    })
  }
 
  commentSchema.statics.getCommentsByRevisionId = function(id) {
    var self = this
 
    return new Promise(function(resolve, reject) {
      self
        .find({ revision: id })
        .sort({ createdAt: -1 })
        .populate('creator')
        .exec(function(err, data) {
          if (err) {
            return reject(err)
          }
 
          if (data.length < 1) {
            return resolve([])
          }
 
          debug('Comment loaded', data)
          return resolve(data)
        })
    })
  }
 
  commentSchema.statics.countCommentByPageId = function(page) {
    var self = this
 
    return new Promise(function(resolve, reject) {
      self.count({ page: page }, function(err, data) {
        Iif (err) {
          return reject(err)
        }
 
        return resolve(data)
      })
    })
  }
 
  commentSchema.statics.removeCommentsByPageId = function(pageId) {
    var Comment = this
 
    return new Promise(function(resolve, reject) {
      Comment.remove({ page: pageId }, function(err, done) {
        if (err) {
          return reject(err)
        }
 
        resolve(done)
      })
    })
  }
 
  commentSchema.statics.findCreatorsByPage = function(page) {
    var Comment = this
 
    return new Promise(function(resolve, reject) {
      Comment.distinct('creator', { page: page }).exec(function(err, creaters) {
        Iif (err) {
          reject(err)
        }
 
        resolve(creaters)
      })
    })
  }
 
  /**
   * post save hook
   */
  commentSchema.post('save', function(savedComment) {
    var Page = crowi.model('Page')
    var Comment = crowi.model('Comment')
    var Activity = crowi.model('Activity')
 
    Comment.countCommentByPageId(savedComment.page)
      .then(function(count) {
        return Page.updateCommentCount(savedComment.page, count)
      })
      .then(function(page) {
        debug('CommentCount Updated', page)
      })
      .catch(function() {})
 
    Activity.createByPageComment(savedComment)
      .then(function(activityLog) {
        debug('Activity created', activityLog)
      })
      .catch(function(err) {})
  })
 
  return mongoose.model('Comment', commentSchema)
}