Explorar el Código

Merge pull request #4429 from weseek/imprv/#78067-create-inAppNotification-with-activity-in-commentService

Imprv/#78067 create inAppNotification in commentService
Yuki Takei hace 4 años
padre
commit
0df5769bcc

+ 0 - 17
packages/app/src/server/service/activity.ts

@@ -24,25 +24,8 @@ class ActivityService {
     this.crowi = crowi;
     this.inAppNotificationService = crowi.inAppNotificationService;
     this.activityEvent = crowi.event('activity');
-
-    this.setUpEventListeners();
-  }
-
-  setUpEventListeners() {
-    this.initActivityEventListeners();
   }
 
-  initActivityEventListeners() {
-    this.activityEvent.on('create', async(targetUsers: Types.ObjectId[], activity: ActivityDocument) => {
-      try {
-        await this.inAppNotificationService.upsertByActivity(targetUsers, activity);
-      }
-      catch (err) {
-        logger.error('Error occurred while saving InAppNotification');
-        throw err;
-      }
-    });
-  }
 
   /**
    * @param {Page} page

+ 13 - 2
packages/app/src/server/service/comment.ts

@@ -1,5 +1,7 @@
+import { Types } from 'mongoose';
 import loggerFactory from '../../utils/logger';
 import { getModelSafely } from '../util/mongoose-utils';
+import { ActivityDocument } from '../models/activity';
 import Crowi from '../crowi';
 
 const logger = loggerFactory('growi:service:CommentService');
@@ -9,12 +11,18 @@ class CommentService {
 
   crowi!: Crowi;
 
+  inAppNotificationService!: any;
+
   commentEvent!: any;
 
+  activityEvent!: any;
+
   constructor(crowi: Crowi) {
     this.crowi = crowi;
+    this.inAppNotificationService = crowi.inAppNotificationService;
 
     this.commentEvent = crowi.event('comment');
+    this.activityEvent = crowi.event('activity');
 
     // init
     this.initCommentEvent();
@@ -30,9 +38,12 @@ class CommentService {
         await Page.updateCommentCount(savedComment.page);
 
         const Activity = getModelSafely('Activity') || require('../models/activity')(this.crowi);
-        const activityLog = await Activity.createByPageComment(savedComment);
+        const savedActivity = await Activity.createByPageComment(savedComment);
+
+        let targetUsers: Types.ObjectId[] = [];
+        targetUsers = await savedActivity.getNotificationTargetUsers();
 
-        logger.info('Activity created', activityLog);
+        await this.inAppNotificationService.upsertByActivity(targetUsers, savedActivity);
       }
       catch (err) {
         logger.error('Error occurred while handling the comment create event:\n', err);