Shun Miyazawa 3 лет назад
Родитель
Сommit
269c2476e3

+ 0 - 11
packages/app/src/server/events/activity.ts

@@ -1,5 +1,3 @@
-import { IPage } from '~/interfaces/page';
-import { ActivityDocument } from '~/server/models/activity';
 import loggerFactory from '~/utils/logger';
 
 import Crowi from '../crowi';
@@ -16,13 +14,4 @@ function ActivityEvent(crowi: Crowi) {
 }
 util.inherits(ActivityEvent, events.EventEmitter);
 
-ActivityEvent.prototype.onUpdate = async function(activity: ActivityDocument, target: IPage) {
-  try {
-    await this.crowi.inAppNotificationService.createInAppNotification(activity, target);
-  }
-  catch (err) {
-    logger.error('Create InAppNotification failed', err);
-  }
-};
-
 module.exports = ActivityEvent;

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

@@ -43,7 +43,7 @@ class ActivityService {
       // create inAppNotification
       const shouldNotification = (AllSupportedActionToNotifiedType as ReadonlyArray<string>).includes(activity.action);
       if (shouldNotification) {
-        this.activityEvent.onUpdate(activity, target);
+        this.activityEvent.emit('createInAppNotification', activity, target);
       }
     });
   }

+ 14 - 0
packages/app/src/server/service/in-app-notification.ts

@@ -32,18 +32,32 @@ export default class InAppNotificationService {
 
   socketIoService!: any;
 
+  activityEvent!: any;
+
   commentEvent!: any;
 
 
   constructor(crowi: Crowi) {
     this.crowi = crowi;
+    this.activityEvent = crowi.event('activity');
     this.socketIoService = crowi.socketIoService;
 
     this.emitSocketIo = this.emitSocketIo.bind(this);
     this.upsertByActivity = this.upsertByActivity.bind(this);
     this.getUnreadCountByUser = this.getUnreadCountByUser.bind(this);
+    this.createInAppNotification = this.createInAppNotification.bind(this);
   }
 
+  initActivityEventListeners(): void {
+    this.activityEvent.on('createInAppNotification', async(activity: ActivityDocument, target: IPage) => {
+      try {
+        await this.createInAppNotification(activity, target);
+      }
+      catch (err) {
+        logger.error('Create InAppNotification failed', err);
+      }
+    });
+  }
 
   emitSocketIo = async(targetUsers) => {
     if (this.socketIoService.isInitialized) {