Просмотр исходного кода

FB: move initCommentEvent to comment.js

kaori 4 лет назад
Родитель
Сommit
51ea342669

+ 9 - 0
packages/app/src/server/crowi/index.js

@@ -65,6 +65,7 @@ function Crowi() {
   this.slackIntegrationService = null;
   this.inAppNotificationService = null;
   this.ActivityService = null;
+  this.CommentService = null;
   this.xss = new Xss();
 
   this.tokens = null;
@@ -124,6 +125,7 @@ Crowi.prototype.init = async function() {
     this.setupPageService(),
     this.setupInAppNotificationService(),
     this.setupActivityService(),
+    this.setupCommentService(),
     this.setupSyncPageStatusService(),
   ]);
 
@@ -658,6 +660,13 @@ Crowi.prototype.setupActivityService = async function() {
   }
 };
 
+Crowi.prototype.setupCommentService = async function() {
+  const CommentService = require('../service/comment');
+  if (this.commentService == null) {
+    this.commentService = new CommentService(this);
+  }
+};
+
 Crowi.prototype.setupSyncPageStatusService = async function() {
   const SyncPageStatusService = require('../service/system-events/sync-page-status');
   if (this.syncPageStatusService == null) {

+ 42 - 42
packages/app/src/server/service/activity.ts

@@ -1,10 +1,10 @@
 import loggerFactory from '../../utils/logger';
 
-import { Activity } from '~/server/models/activity';
+import { Activity } from '../models/activity';
 
 import ActivityDefine from '../util/activityDefine';
 
-const InAppNotificationService = require('./in-app-notification');
+// const InAppNotificationService = require('./in-app-notification');
 
 
 const logger = loggerFactory('growi:service:ActivityService');
@@ -15,53 +15,53 @@ class ActivityService {
 
   inAppNotificationService: any;
 
-  commentEvent!: any;
+  // commentEvent!: any;
 
   constructor(crowi) {
     this.crowi = crowi;
     this.inAppNotificationService = crowi.inAppNotificationService;
-    this.commentEvent = crowi.event('comment');
+    // this.commentEvent = crowi.event('comment');
 
-    // init
-    this.initCommentEvent();
+    // // init
+    // this.initCommentEvent();
   }
 
-  initCommentEvent(): void {
-    // create
-    this.commentEvent.on('create', async(savedComment) => {
-      this.commentEvent.onCreate();
-
-      try {
-        const activityLog = await Activity.createByPageComment(savedComment);
-        logger.info('Activity created', activityLog);
-      }
-      catch (err) {
-        throw err;
-      }
-
-    });
-
-    // update
-    this.commentEvent.on('update', (user) => {
-      this.commentEvent.onUpdate();
-      const inAppNotificationService = new InAppNotificationService(this.crowi);
-
-      inAppNotificationService.emitSocketIo(user);
-    });
-
-    // remove
-    this.commentEvent.on('remove', async(comment) => {
-      this.commentEvent.onRemove();
-
-      try {
-        // TODO: Able to remove child activities of comment by GW-7510
-        await this.removeByPageCommentDelete(comment);
-      }
-      catch (err) {
-        logger.error(err);
-      }
-    });
-  }
+  // initCommentEvent(): void {
+  //   // create
+  //   this.commentEvent.on('create', async(savedComment) => {
+  //     this.commentEvent.onCreate();
+
+  //     try {
+  //       const activityLog = await Activity.createByPageComment(savedComment);
+  //       logger.info('Activity created', activityLog);
+  //     }
+  //     catch (err) {
+  //       throw err;
+  //     }
+
+  //   });
+
+  //   // update
+  //   this.commentEvent.on('update', (user) => {
+  //     this.commentEvent.onUpdate();
+  //     const inAppNotificationService = new InAppNotificationService(this.crowi);
+
+  //     inAppNotificationService.emitSocketIo(user);
+  //   });
+
+  //   // remove
+  //   this.commentEvent.on('remove', async(comment) => {
+  //     this.commentEvent.onRemove();
+
+  //     try {
+  //       // TODO: Able to remove child activities of comment by GW-7510
+  //       await this.removeByPageCommentDelete(comment);
+  //     }
+  //     catch (err) {
+  //       logger.error(err);
+  //     }
+  //   });
+  // }
 
   /**
    * @param {Comment} comment

+ 54 - 0
packages/app/src/server/service/comment.ts

@@ -1,5 +1,10 @@
 import loggerFactory from '../../utils/logger';
 
+import { Activity } from '../models/activity';
+
+const InAppNotificationService = require('./in-app-notification');
+const ActivityService = require('./activity');
+
 const logger = loggerFactory('growi:service:CommentService');
 
 
@@ -7,8 +12,57 @@ class CommentService {
 
   crowi!: any;
 
+  commentEvent!: any;
+
+  activityService: any;
+
   constructor(crowi: any) {
     this.crowi = crowi;
+    this.activityService = crowi.activityService;
+    this.commentEvent = crowi.event('comment');
+
+    // init
+    this.initCommentEvent();
+  }
+
+
+  initCommentEvent(): void {
+    // create
+    this.commentEvent.on('create', async(savedComment) => {
+      this.commentEvent.onCreate();
+
+      try {
+        const activityLog = await Activity.createByPageComment(savedComment);
+        logger.info('Activity created', activityLog);
+      }
+      catch (err) {
+        throw err;
+      }
+
+    });
+
+    // update
+    this.commentEvent.on('update', (user) => {
+      this.commentEvent.onUpdate();
+      const inAppNotificationService = new InAppNotificationService(this.crowi);
+
+      inAppNotificationService.emitSocketIo(user);
+    });
+
+    // remove
+    this.commentEvent.on('remove', async(comment) => {
+      this.commentEvent.onRemove();
+
+      const activityService = new ActivityService(this.crowi);
+
+      try {
+        // TODO: Able to remove child activities of comment by GW-7510
+        await activityService.removeByPageCommentDelete(comment);
+      }
+      catch (err) {
+        logger.error(err);
+      }
+    });
   }
 
 }