Kaynağa Gözat

create generateSnapshot function

WNomunomu 2 yıl önce
ebeveyn
işleme
d261c0bc65

+ 10 - 6
apps/app/src/server/service/in-app-notification.ts

@@ -5,7 +5,7 @@ import { SubscriptionStatusType } from '@growi/core';
 import { subDays } from 'date-fns';
 import { Types, FilterQuery, UpdateQuery } from 'mongoose';
 
-import { AllEssentialActions, SupportedAction } from '~/interfaces/activity';
+import { AllEssentialActions, SupportedTargetModel } from '~/interfaces/activity';
 import { InAppNotificationStatuses, PaginateResult } from '~/interfaces/in-app-notification';
 import * as pageSerializers from '~/models/serializers/in-app-notification-snapshot/page';
 import * as userSerializers from '~/models/serializers/in-app-notification-snapshot/user';
@@ -16,11 +16,13 @@ import {
 } from '~/server/models/in-app-notification';
 import InAppNotificationSettings from '~/server/models/in-app-notification-settings';
 import Subscription from '~/server/models/subscription';
+import { getDelegator } from '~/server/service/in-app-notification/in-app-notification-delegator';
 import loggerFactory from '~/utils/logger';
 
 import Crowi from '../crowi';
 import { RoomPrefix, getRoomNameWithId } from '../util/socket-io-helpers';
 
+import { generateSnapshot } from './in-app-notification/in-app-notification-utils';
 import { generateInitialPreNotifyProps, type PreNotify, type PreNotifyProps } from './preNotify';
 
 
@@ -28,7 +30,6 @@ const { STATUS_UNREAD, STATUS_UNOPENED, STATUS_OPENED } = InAppNotificationStatu
 
 const logger = loggerFactory('growi:service:inAppNotification');
 
-
 export default class InAppNotificationService {
 
   crowi!: Crowi;
@@ -206,10 +207,13 @@ export default class InAppNotificationService {
   createInAppNotification = async function(activity: ActivityDocument, target: IUser | IPage, preNotify: PreNotify): Promise<void> {
 
     const shouldNotification = activity != null && target != null && (AllEssentialActions as ReadonlyArray<string>).includes(activity.action);
-    const snapshot = pageSerializers.stringifySnapshot(target);
-    if (shouldNotification) {
 
-      const props: PreNotifyProps = generateInitialPreNotifyProps();
+    const targetModel = activity.targetModel;
+
+    const snapshot = generateSnapshot(targetModel, target);
+
+    if (shouldNotification) {
+      const props = generateInitialPreNotifyProps();
 
       await preNotify(props);
 
@@ -217,7 +221,7 @@ export default class InAppNotificationService {
       await this.emitSocketIo(props.notificationTargetUsers);
     }
     else {
-      throw Error('No activity to notify');
+      throw Error('no activity to notify');
     }
     return;
   };

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

@@ -0,0 +1,19 @@
+import type { IUser, IPage } from '@growi/core';
+
+import { SupportedTargetModel } from '~/interfaces/activity';
+import * as pageSerializers from '~/models/serializers/in-app-notification-snapshot/page';
+
+export const generateSnapshot = (targetModel: string, target: IUser | IPage) => {
+
+  let snapshot;
+
+  switch (targetModel) {
+    case SupportedTargetModel.MODEL_PAGE:
+      snapshot = pageSerializers.stringifySnapshot(target as IPage);
+      break;
+    default:
+      break;
+  }
+
+  return snapshot;
+};