Jelajahi Sumber

create "updateByParmeters"

Shun Miyazawa 3 tahun lalu
induk
melakukan
383ddf2bdc

+ 14 - 0
packages/app/src/interfaces/activity.ts

@@ -39,9 +39,23 @@ export const SUPPORTED_ACTION_TYPE = {
   ACTION_COMMENT_UPDATE,
 } as const;
 
+export const SUPPORTED_ACTION_TO_NOTIFIED_TYPE = {
+  ACTION_PAGE_LIKE,
+  ACTION_PAGE_BOOKMARK,
+  ACTION_PAGE_UPDATE,
+  ACTION_PAGE_RENAME,
+  ACTION_PAGE_DUPLICATE,
+  ACTION_PAGE_DELETE,
+  ACTION_PAGE_DELETE_COMPLETELY,
+  ACTION_PAGE_REVERT,
+  ACTION_COMMENT_CREATE,
+  ACTION_COMMENT_UPDATE,
+} as const;
+
 
 export const AllSupportedTargetModelType = Object.values(SUPPORTED_TARGET_MODEL_TYPE);
 export const AllSupportedActionType = Object.values(SUPPORTED_ACTION_TYPE);
+export const AllSupportedActionToNotifiedType = Object.values(SUPPORTED_ACTION_TO_NOTIFIED_TYPE);
 
 /*
  * For AuditLogManagement.tsx

+ 4 - 2
packages/app/src/server/models/activity.ts

@@ -4,7 +4,9 @@ import {
 } from 'mongoose';
 import mongoosePaginate from 'mongoose-paginate-v2';
 
-import { AllSupportedTargetModelType, AllSupportedActionType, ISnapshot } from '~/interfaces/activity';
+import {
+  AllSupportedTargetModelType, AllSupportedActionType, SupportedActionType, ISnapshot,
+} from '~/interfaces/activity';
 
 import loggerFactory from '../../utils/logger';
 import activityEvent from '../events/activity';
@@ -20,7 +22,7 @@ export interface ActivityDocument extends Document {
   path: string
   targetModel: string
   target: Types.ObjectId
-  action: string
+  action: SupportedActionType
   snapshot: ISnapshot
 
   getNotificationTargetUsers(): Promise<any[]>

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

@@ -1,7 +1,15 @@
 import { getModelSafely } from '@growi/core';
 
+import { IActivity, AllSupportedActionToNotifiedType } from '~/interfaces/activity';
+import { IPage } from '~/interfaces/page';
+import { stringifySnapshot } from '~/models/serializers/in-app-notification-snapshot/page';
+import Activity from '~/server/models/activity';
+
 import Crowi from '../crowi';
 
+
+type ParameterType = Omit<IActivity, 'ip' | 'path' | 'createdAt'>
+
 class ActivityService {
 
   crowi!: Crowi;
@@ -24,6 +32,17 @@ class ActivityService {
     return Activity.create(parameters);
   };
 
+  updateByParmeters = async function(activityId: string, parameters: ParameterType, target?: IPage): Promise<void> {
+    const activity = await Activity.findOneAndUpdate({ _id: activityId }, parameters, { new: true });
+
+    const shouldNotification = activity != null && target != null && (AllSupportedActionToNotifiedType as ReadonlyArray<string>).includes(activity.action);
+    if (shouldNotification) {
+      const notificationTargetUsers = await activity?.getNotificationTargetUsers();
+      const snapshotForInAppNotification = stringifySnapshot(target);
+      await this.inAppNotificationService.upsertByActivity(notificationTargetUsers, activity, snapshotForInAppNotification);
+      await this.inAppNotificationService.emitSocketIo(notificationTargetUsers);
+    }
+  };
 
   /**
    * @param {User} user