WNomunomu 2 лет назад
Родитель
Сommit
ab569e9879

+ 30 - 0
apps/app/src/server/service/in-app-notification/in-app-notification-delegator.ts

@@ -0,0 +1,30 @@
+import type { IUser, IPage, Ref } from '@growi/core';
+
+import { SupportedTargetModel } from '~/interfaces/activity';
+import { ActivityDocument } from '~/server/models/activity';
+
+import { PageNotificationDelegator } from './page-notification';
+import { UserNotificationDelegator } from './user-notification';
+
+const isPageNotification = (targetModel: string, target: IUser | IPage): target is IPage => {
+  return targetModel === SupportedTargetModel.MODEL_PAGE;
+};
+
+const isUserNotification = (targetModel: string, target: IUser | IPage): target is IUser => {
+  return targetModel === SupportedTargetModel.MODEL_PAGE;
+};
+
+export const getDelegator = (
+    targetModel: string, target: IUser | IPage,
+): PageNotificationDelegator | UserNotificationDelegator => {
+  let delegator;
+
+  if (isPageNotification(targetModel, target)) {
+    delegator = new PageNotificationDelegator(target);
+  }
+  else if (isUserNotification(targetModel, target)) {
+    delegator = new UserNotificationDelegator();
+  }
+
+  return delegator;
+};

+ 42 - 0
apps/app/src/server/service/in-app-notification/page-notification.ts

@@ -0,0 +1,42 @@
+import type {
+  Ref, IUser, IPage,
+} from '@growi/core';
+
+import { AllEssentialActions, SupportedAction } from '~/interfaces/activity';
+import * as pageSerializers from '~/models/serializers/in-app-notification-snapshot/page';
+import { ActivityDocument } from '~/server/models/activity';
+
+import { generateInitialPreNotifyProps, type PreNotify, type PreNotifyProps } from '../preNotify';
+
+import { upsertByActivity, emitSocketIo } from './in-app-notification-utils';
+
+
+export class PageNotificationDelegator {
+
+  target: IPage;
+
+  constructor(target: IPage) {
+    this.target = target;
+  }
+
+  createInAppNotification = async(activity: ActivityDocument, target, preNotify, socketIoService, commentService): 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();
+
+      await preNotify(props);
+
+      await this.upsertByActivity(props.notificationTargetUsers, activity, snapshot);
+      await this.emitSocketIo(props.notificationTargetUsers);
+    }
+    else {
+      throw Error('No activity to notify');
+    }
+    return;
+  };
+
+}

+ 20 - 0
apps/app/src/server/service/in-app-notification/user-notification.ts

@@ -0,0 +1,20 @@
+import type {
+  Ref, IUser,
+} from '@growi/core';
+
+import * as userSerializers from '~/models/serializers/in-app-notification-snapshot/user';
+import { ActivityDocument } from '~/server/models/activity';
+
+import { upsertByActivity, emitSocketIo } from './in-app-notification-utils';
+
+
+export class UserNotificationDelegator {
+
+  createInAppNotification = async(activity: ActivityDocument, target, users: Ref<IUser>[], socketIoService): Promise<void> => {
+    const snapshot = userSerializers.stringifySnapshot(target);
+    await upsertByActivity(users, activity, snapshot);
+    await emitSocketIo(users, socketIoService);
+    return;
+  };
+
+}