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

Merge branch 'feat/notification' into feat/77832-get-subscription-on-client-side

Shun Miyazawa 4 лет назад
Родитель
Сommit
52608fc6a4

+ 2 - 38
packages/app/src/server/service/activity.ts

@@ -24,44 +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 {Comment} comment
-   * @return {Promise}
-   */
-  removeByPageCommentDelete = async function(comment) {
-    const parameters = await {
-      user: comment.creator,
-      targetModel: ActivityDefine.MODEL_PAGE,
-      target: comment.page,
-      eventModel: ActivityDefine.MODEL_COMMENT,
-      event: comment._id,
-      action: ActivityDefine.ACTION_COMMENT,
-    };
-
-    const Activity = getModelSafely('Activity') || require('../models/activity')(this.crowi);
-    await Activity.removeByParameters(parameters);
-    return;
-  };
 
   /**
    * @param {Page} page
@@ -76,8 +40,8 @@ class ActivityService {
       action: ActivityDefine.ACTION_UPDATE,
     };
     const Activity = getModelSafely('Activity') || require('../models/activity')(this.crowi);
-    await Activity.createByParameters(parameters);
-    return;
+    const savedActivity = await Activity.createByParameters(parameters);
+    return savedActivity;
   };
 
 

+ 15 - 14
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,19 +11,25 @@ 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();
+    this.initCommentEventListeners();
   }
 
 
-  initCommentEvent(): void {
+  initCommentEventListeners(): void {
     // create
     this.commentEvent.on('create', async(savedComment) => {
 
@@ -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);
@@ -52,8 +63,6 @@ class CommentService {
     this.commentEvent.on('remove', async(comment) => {
       this.commentEvent.onRemove();
 
-      const { activityService } = this.crowi;
-
       try {
         const Page = getModelSafely('Page') || require('../models/page')(this.crowi);
         await Page.updateCommentCount(comment.page);
@@ -61,14 +70,6 @@ class CommentService {
       catch (err) {
         logger.error('Error occurred while updating the comment count:\n', err);
       }
-
-      try {
-        // TODO: Able to remove child activities of comment by GW-7510
-        await activityService.removeByPageCommentDelete(comment);
-      }
-      catch (err) {
-        logger.error(err);
-      }
     });
   }
 

+ 6 - 2
packages/app/src/server/service/page.js

@@ -31,12 +31,16 @@ class PageService {
 
     // update
     this.pageEvent.on('update', async(page, user) => {
-      const { activityService } = this.crowi;
+      const { activityService, inAppNotificationService } = this.crowi;
 
       this.pageEvent.onUpdate();
 
       try {
-        await activityService.createByPageUpdate(page, user);
+        const savedActivity = await activityService.createByPageUpdate(page, user);
+        let targetUsers = [];
+        targetUsers = await savedActivity.getNotificationTargetUsers();
+
+        await inAppNotificationService.upsertByActivity(targetUsers, savedActivity);
       }
       catch (err) {
         logger.error(err);