|
| 1 | +module.exports = function(crowi) { |
| 2 | + var debug = require('debug')('crowi:models:comment') |
| 3 | + , mongoose = require('mongoose') |
| 4 | + , ObjectId = mongoose.Schema.Types.ObjectId |
| 5 | + , USER_PUBLIC_FIELDS = '_id fbId image googleId name username email status createdAt' // TODO: どこか別の場所へ... |
| 6 | + , commentSchema |
| 7 | + ; |
| 8 | + |
| 9 | + commentSchema = new mongoose.Schema({ |
| 10 | + page: { type: ObjectId, ref: 'Page', index: true }, |
| 11 | + creator: { type: ObjectId, ref: 'User', index: true }, |
| 12 | + revision: { type: ObjectId, ref: 'Revision', index: true }, |
| 13 | + comment: { type: String, required: true }, |
| 14 | + commentPosition: { type: Number, default: -1 }, |
| 15 | + createdAt: { type: Date, default: Date.now } |
| 16 | + }); |
| 17 | + |
| 18 | + commentSchema.statics.create = function(pageId, creatorId, revisionId, comment, position) { |
| 19 | + var Comment = this, |
| 20 | + commentPosition = position || -1; |
| 21 | + |
| 22 | + |
| 23 | + return new Promise(function(resolve, reject) { |
| 24 | + var newComment = new Comment(); |
| 25 | + |
| 26 | + newComment.page = pageId; |
| 27 | + newComment.creator = creatorId; |
| 28 | + newComment.revision = revisionId; |
| 29 | + newComment.comment = comment; |
| 30 | + newComment.commentPosition = position; |
| 31 | + |
| 32 | + newComment.save(function(err, data) { |
| 33 | + if (err) { |
| 34 | + debug('Error on saving comment.', err); |
| 35 | + return reject(err); |
| 36 | + } |
| 37 | + debug('Comment saved.', data); |
| 38 | + return resolve(data); |
| 39 | + }); |
| 40 | + }); |
| 41 | + }; |
| 42 | + |
| 43 | + commentSchema.statics.getCommentsByPageId = function(id) { |
| 44 | + var self = this; |
| 45 | + |
| 46 | + return new Promise(function(resolve, reject) { |
| 47 | + self |
| 48 | + .find({page: id}) |
| 49 | + .sort({'createdAt': -1}) |
| 50 | + .populate('creator', USER_PUBLIC_FIELDS) |
| 51 | + .exec(function(err, data) { |
| 52 | + if (err) { |
| 53 | + return reject(err); |
| 54 | + } |
| 55 | + |
| 56 | + if (data.length < 1) { |
| 57 | + return resolve([]); |
| 58 | + } |
| 59 | + |
| 60 | + debug('Comment loaded', data); |
| 61 | + return resolve(data); |
| 62 | + }); |
| 63 | + }); |
| 64 | + }; |
| 65 | + |
| 66 | + commentSchema.statics.getCommentsByRevisionId = function(id) { |
| 67 | + var self = this; |
| 68 | + |
| 69 | + return new Promise(function(resolve, reject) { |
| 70 | + self |
| 71 | + .find({revision: id}) |
| 72 | + .sort({'createdAt': -1}) |
| 73 | + .populate('creator', USER_PUBLIC_FIELDS) |
| 74 | + .exec(function(err, data) { |
| 75 | + if (err) { |
| 76 | + return reject(err); |
| 77 | + } |
| 78 | + |
| 79 | + if (data.length < 1) { |
| 80 | + return resolve([]); |
| 81 | + } |
| 82 | + |
| 83 | + debug('Comment loaded', data); |
| 84 | + return resolve(data); |
| 85 | + }); |
| 86 | + }); |
| 87 | + }; |
| 88 | + |
| 89 | + commentSchema.statics.countCommentByPageId = function(page) { |
| 90 | + var self = this; |
| 91 | + |
| 92 | + return new Promise(function(resolve, reject) { |
| 93 | + self.count({page: page}, function(err, data) { |
| 94 | + if (err) { |
| 95 | + return reject(err); |
| 96 | + } |
| 97 | + |
| 98 | + return resolve(data); |
| 99 | + }); |
| 100 | + }); |
| 101 | + }; |
| 102 | + |
| 103 | + /** |
| 104 | + * post save hook |
| 105 | + */ |
| 106 | + commentSchema.post('save', function(savedComment) { |
| 107 | + var Page = crowi.model('Page') |
| 108 | + , Comment = crowi.model('Comment') |
| 109 | + ; |
| 110 | + |
| 111 | + Comment.countCommentByPageId(savedComment.page) |
| 112 | + .then(function(count) { |
| 113 | + return Page.updateCommentCount(savedComment.page, count); |
| 114 | + }).then(function(page) { |
| 115 | + debug('CommentCount Updated', page); |
| 116 | + }).catch(function() { |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + return mongoose.model('Comment', commentSchema); |
| 121 | +}; |
0 commit comments