Selaa lähdekoodia

create "createInAppNotification"

Shun Miyazawa 3 vuotta sitten
vanhempi
sitoutus
05c865ed20
1 muutettua tiedostoa jossa 19 lisäystä ja 0 poistoa
  1. 19 0
      packages/app/src/server/service/in-app-notification.ts

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

@@ -1,10 +1,13 @@
 import { subDays } from 'date-fns';
 import { Types } from 'mongoose';
 
+import { AllSupportedActionToNotifiedType } from '~/interfaces/activity';
 import { HasObjectId } from '~/interfaces/has-object-id';
 import { InAppNotificationStatuses, PaginateResult } from '~/interfaces/in-app-notification';
+import { IPage } from '~/interfaces/page';
 import { SubscriptionStatusType } from '~/interfaces/subscription';
 import { IUser } from '~/interfaces/user';
+import { stringifySnapshot } from '~/models/serializers/in-app-notification-snapshot/page';
 import { ActivityDocument } from '~/server/models/activity';
 import {
   InAppNotification,
@@ -36,6 +39,8 @@ export default class InAppNotificationService {
     this.crowi = crowi;
     this.socketIoService = crowi.socketIoService;
 
+    this.emitSocketIo = this.emitSocketIo.bind(this);
+    this.upsertByActivity = this.upsertByActivity.bind(this);
     this.getUnreadCountByUser = this.getUnreadCountByUser.bind(this);
   }
 
@@ -175,6 +180,20 @@ export default class InAppNotificationService {
     return;
   };
 
+  createInAppNotification = async function(activity: ActivityDocument, target: IPage): Promise<void> {
+    const shouldNotification = activity != null && target != null && (AllSupportedActionToNotifiedType as ReadonlyArray<string>).includes(activity.action);
+    if (shouldNotification) {
+      const snapshot = stringifySnapshot(target as IPage);
+      const notificationTargetUsers = await activity?.getNotificationTargetUsers();
+      await this.upsertByActivity(notificationTargetUsers, activity, snapshot);
+      await this.emitSocketIo(notificationTargetUsers);
+    }
+    else {
+      throw Error('No activity to notify');
+    }
+    return;
+  };
+
 }
 
 module.exports = InAppNotificationService;