Просмотр исходного кода

add user info to page bulk export activity

Futa Arai 1 год назад
Родитель
Сommit
3f5e87bf80

+ 9 - 8
apps/app/src/features/page-bulk-export/server/service/page-bulk-export.ts

@@ -99,20 +99,17 @@ class PageBulkExportService {
 
     // Cannot directly pipe from pagesWritable to zipArchiver due to how the 'append' method works.
     // Hence, execution of two pipelines is required.
-    pipeline(pagesReadable, pagesWritable, err => this.handleExportError(err, activityParameters, pageBulkExportJob, multipartUploader));
+    pipeline(pagesReadable, pagesWritable, err => this.handleExportErrorInStream(err, activityParameters, pageBulkExportJob, multipartUploader));
     pipeline(zipArchiver, bufferToPartSizeTransform, multipartUploadWritable,
-      err => this.handleExportError(err, activityParameters, pageBulkExportJob, multipartUploader));
+      err => this.handleExportErrorInStream(err, activityParameters, pageBulkExportJob, multipartUploader));
   }
 
-  private async handleExportError(
-      err: Error | null, activityParameters: ActivityParameters, pageBulkExportJob: PageBulkExportJobDocument, multipartUploader?: IAwsMultipartUploader,
+  private async handleExportErrorInStream(
+      err: Error | null, activityParameters: ActivityParameters, pageBulkExportJob: PageBulkExportJobDocument, multipartUploader: IAwsMultipartUploader,
   ): Promise<void> {
     if (err != null) {
       logger.error(err);
-      if (multipartUploader != null) {
-        await multipartUploader.abortUpload();
-      }
-
+      await multipartUploader.abortUpload();
       await this.notifyExportResult(activityParameters, pageBulkExportJob, SupportedAction.ACTION_PAGE_BULK_EXPORT_FAILED);
     }
   }
@@ -239,6 +236,10 @@ class PageBulkExportService {
       action,
       targetModel: SupportedTargetModel.MODEL_PAGE_BULK_EXPORT_JOB,
       target: pageBulkExportJob,
+      user: pageBulkExportJob.user,
+      snapshot: {
+        username: isPopulated(pageBulkExportJob.user) ? pageBulkExportJob.user.username : '',
+      },
     });
     const preNotify = preNotifyService.generatePreNotify(activity);
     this.activityEvent.emit('updated', activity, pageBulkExportJob, preNotify);

+ 2 - 2
apps/app/src/server/models/activity.ts

@@ -25,7 +25,7 @@ const logger = loggerFactory('growi:models:activity');
 
 export interface ActivityDocument extends Document {
   _id: Types.ObjectId
-  user?: Types.ObjectId
+  user: Types.ObjectId
   ip: string
   endpoint: string
   targetModel: SupportedTargetModelType
@@ -33,7 +33,7 @@ export interface ActivityDocument extends Document {
   eventModel: SupportedEventModelType
   event: Types.ObjectId
   action: SupportedActionType
-  snapshot?: ISnapshot
+  snapshot: ISnapshot
 
   getNotificationTargetUsers(): Promise<any[]>
 }

+ 5 - 1
apps/app/src/server/service/pre-notify.ts

@@ -2,6 +2,8 @@ import type {
   IPage, IUser, Ref,
 } from '@growi/core';
 
+import { SupportedTargetModel } from '~/interfaces/activity';
+
 import type { ActivityDocument } from '../models/activity';
 import Subscription from '../models/subscription';
 import { getModelSafely } from '../util/mongoose-utils';
@@ -38,7 +40,9 @@ class PreNotifyService implements IPreNotifyService {
       const actionUser = activity.user;
       const target = activity.target;
       const subscribedUsers = await Subscription.getSubscription(target as unknown as Ref<IPage>);
-      const notificationUsers = subscribedUsers.filter(item => (item.toString() !== actionUser?._id.toString()));
+      // If target model is PageBulkExportJob, notify the user who started the job. Otherwise, exclude the activity user from the notification.
+      const notificationUsers = activity.targetModel === SupportedTargetModel.MODEL_PAGE_BULK_EXPORT_JOB ? subscribedUsers
+        : subscribedUsers.filter(item => (item.toString() !== actionUser._id.toString()));
       const activeNotificationUsers = await User.find({
         _id: { $in: notificationUsers },
         status: User.STATUS_ACTIVE,