Taichi Masuyama 4 лет назад
Родитель
Сommit
4394bd828c
2 измененных файлов с 17 добавлено и 42 удалено
  1. 1 35
      packages/app/src/server/models/comment.js
  2. 16 7
      packages/app/src/server/service/comment.ts

+ 1 - 35
packages/app/src/server/models/comment.js

@@ -4,6 +4,7 @@ module.exports = function(crowi) {
   const debug = require('debug')('growi:models:comment');
   const mongoose = require('mongoose');
   const ObjectId = mongoose.Schema.Types.ObjectId;
+  const commentEvent = crowi.event('comment');
 
   const commentSchema = new mongoose.Schema({
     page: { type: ObjectId, ref: 'Page', index: true },
@@ -66,7 +67,6 @@ module.exports = function(crowi) {
 
   commentSchema.statics.updateCommentsByPageId = async function(comment, isMarkdown, commentId) {
     const Comment = this;
-    const commentEvent = crowi.event('comment');
 
     const commentData = await Comment.findOneAndUpdate(
       { _id: commentId },
@@ -83,24 +83,11 @@ module.exports = function(crowi) {
    * post remove hook
    */
   commentSchema.post('reomove', async(savedComment) => {
-    const Page = crowi.model('Page');
-    const commentEvent = crowi.event('comment');
-
-    try {
-      // TODO: move Page.updateCommentCount to commentService by GW7532
-      const page = await Page.updateCommentCount(savedComment.page);
-      debug('CommentCount Updated', page);
-    }
-    catch (err) {
-      throw err;
-    }
-
     await commentEvent.emit('remove', savedComment);
   });
 
   commentSchema.methods.removeWithReplies = async function(comment) {
     const Comment = crowi.model('Comment');
-    const commentEvent = crowi.event('comment');
 
     await Comment.remove({
       $or: (
@@ -119,27 +106,6 @@ module.exports = function(crowi) {
    * post save hook
    */
   commentSchema.post('save', async(savedComment) => {
-    const Page = crowi.model('Page');
-    const commentEvent = crowi.event('comment');
-    await commentEvent.emit('create', savedComment.creator);
-
-    try {
-      // TODO: move Page.updateCommentCount to commentService by GW7532
-      const page = await Page.updateCommentCount(savedComment.page);
-      debug('CommentCount Updated', page);
-    }
-    catch (err) {
-      throw err;
-    }
-
-    try {
-      const Activity = getModelSafely('Activity') || require('../models/activity')(this.crowi);
-      const activityLog = await Activity.createByPageComment(savedComment);
-      debug('Activity created', activityLog);
-    }
-    catch (err) {
-      throw err;
-    }
     await commentEvent.emit('create', savedComment);
   });
 

+ 16 - 7
packages/app/src/server/service/comment.ts

@@ -1,19 +1,17 @@
 import loggerFactory from '../../utils/logger';
 import { getModelSafely } from '../util/mongoose-utils';
-
-const InAppNotificationService = require('./in-app-notification');
-const ActivityService = require('./activity');
+import Crowi from '../crowi';
 
 const logger = loggerFactory('growi:service:CommentService');
 
 
 class CommentService {
 
-  crowi!: any;
+  crowi!: Crowi;
 
   commentEvent!: any;
 
-  constructor(crowi: any) {
+  constructor(crowi: Crowi) {
     this.crowi = crowi;
 
     this.commentEvent = crowi.event('comment');
@@ -26,15 +24,18 @@ class CommentService {
   initCommentEvent(): void {
     // create
     this.commentEvent.on('create', async(savedComment) => {
-      this.commentEvent.onCreate();
 
       try {
+        const Page = getModelSafely('Page') || require('../models/page')(this.crowi);
+        await Page.updateCommentCount(savedComment.page);
+
         const Activity = getModelSafely('Activity') || require('../models/activity')(this.crowi);
         const activityLog = await Activity.createByPageComment(savedComment);
+
         logger.info('Activity created', activityLog);
       }
       catch (err) {
-        throw err;
+        logger.error('Error occurred while handling the comment create event:\n', err);
       }
 
     });
@@ -53,6 +54,14 @@ class CommentService {
 
       const { activityService } = this.crowi;
 
+      try {
+        const Page = getModelSafely('Page') || require('../models/page')(this.crowi);
+        await Page.updateCommentCount(comment.page);
+      }
+      catch (err) {
+        throw err;
+      }
+
       try {
         // TODO: Able to remove child activities of comment by GW-7510
         await activityService.removeByPageCommentDelete(comment);