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

Created the activity inside service/page.ts and gathered descendants subscribed users.

Shunm634-source 3 лет назад
Родитель
Сommit
0ec9a87c91

+ 3 - 2
packages/app/src/server/service/activity.ts

@@ -6,6 +6,7 @@ import {
 } from '~/interfaces/activity';
 import { Ref } from '~/interfaces/common';
 import { IPage } from '~/interfaces/page';
+import { IUser } from '~/interfaces/user';
 import Activity from '~/server/models/activity';
 
 import loggerFactory from '../../utils/logger';
@@ -41,7 +42,7 @@ class ActivityService {
   }
 
   initActivityEventListeners(): void {
-    this.activityEvent.on('update', async(activityId: string, parameters, target?: IPage, descendantPages?: Ref<IPage>[]) => {
+    this.activityEvent.on('update', async(activityId: string, parameters, target?: IPage, descendantsSubscribedUsers?: Ref<IUser>[]) => {
       let activity: IActivity;
       const shoudUpdate = this.shoudUpdateActivity(parameters.action);
 
@@ -54,7 +55,7 @@ class ActivityService {
           return;
         }
 
-        this.activityEvent.emit('updated', activity, target, descendantPages);
+        this.activityEvent.emit('updated', activity, target, descendantsSubscribedUsers);
       }
     });
   }

+ 5 - 6
packages/app/src/server/service/in-app-notification.ts

@@ -53,11 +53,11 @@ export default class InAppNotificationService {
   }
 
   initActivityEventListeners(): void {
-    this.activityEvent.on('updated', async(activity: ActivityDocument, target: IPage, descendantPages?: Ref<IPage>[]) => {
+    this.activityEvent.on('updated', async(activity: ActivityDocument, target: IPage, descendantsSubscribedUsers?: Ref<IUser>[]) => {
       try {
         const shouldNotification = activity != null && target != null && (AllEssentialActions as ReadonlyArray<string>).includes(activity.action);
         if (shouldNotification) {
-          await this.createInAppNotification(activity, target, descendantPages);
+          await this.createInAppNotification(activity, target, descendantsSubscribedUsers);
         }
       }
       catch (err) {
@@ -201,7 +201,7 @@ export default class InAppNotificationService {
     return;
   };
 
-  createInAppNotification = async function(activity: ActivityDocument, target: IPage, descendantPages?: Ref<IPage>[]): Promise<void> {
+  createInAppNotification = async function(activity: ActivityDocument, target: IPage, descendantsSubscribedUsers?: Ref<IUser>[]): Promise<void> {
     const shouldNotification = activity != null && target != null && (AllEssentialActions as ReadonlyArray<string>).includes(activity.action);
     const snapshot = stringifySnapshot(target);
     if (shouldNotification) {
@@ -211,10 +211,9 @@ export default class InAppNotificationService {
       }
       const notificationTargetUsers = await activity?.getNotificationTargetUsers();
       let notificationDescendantsUsers = [];
-      if (descendantPages != null && descendantPages.length > 0) {
+      if (descendantsSubscribedUsers != null) {
         const User = this.crowi.model('User');
-        const targetDescendantsUsers = await Subscription.getSubscriptions(descendantPages);
-        const descendantsUsers = targetDescendantsUsers.filter(item => (item.toString() !== activity.user._id.toString()));
+        const descendantsUsers = descendantsSubscribedUsers.filter(item => (item.toString() !== activity.user._id.toString()));
         notificationDescendantsUsers = await User.find({
           _id: { $in: descendantsUsers },
           status: User.STATUS_ACTIVE,

+ 12 - 7
packages/app/src/server/service/page.ts

@@ -19,7 +19,7 @@ import {
 import {
   IPageOperationProcessInfo, IPageOperationProcessData, PageActionStage, PageActionType,
 } from '~/interfaces/page-operation';
-import { IUserHasId } from '~/interfaces/user';
+import { IUser, IUserHasId } from '~/interfaces/user';
 import { PageMigrationErrorData, SocketEventName, UpdateDescCountRawData } from '~/interfaces/websocket';
 import {
   CreateMethod, PageCreateOptions, PageModel, PageDocument, pushRevision, PageQueryBuilder,
@@ -369,7 +369,6 @@ class PageService {
     };
 
     const activity = await this.crowi.activityService.createActivity(parameters);
-    console.log('What is the activity\n', activity);
 
     const isExist = await Page.exists({ path: newPagePath });
     if (isExist) {
@@ -536,8 +535,8 @@ class PageService {
     const timerObj = this.crowi.pageOperationService.autoUpdateExpiryDate(pageOpId);
     try {
     // update descendants first
-      const descendantPages = await this.renameDescendantsWithStream(page, newPagePath, user, options, false);
-      this.activityEvent.emit('updated', activity, page, descendantPages);
+      const descendantsSubscribedUsers = await this.renameDescendantsWithStream(page, newPagePath, user, options, false);
+      this.activityEvent.emit('updated', activity, page, descendantsSubscribedUsers);
     }
     catch (err) {
       logger.warn(err);
@@ -840,7 +839,7 @@ class PageService {
     const renameDescendants = this.renameDescendants.bind(this);
     const pageEvent = this.pageEvent;
     let count = 0;
-    let descendantPages = [];
+    const descendantsSubscribedUsers = [];
     const writeStream = new Writable({
       objectMode: true,
       async write(batch, encoding, callback) {
@@ -849,7 +848,12 @@ class PageService {
           await renameDescendants(
             batch, user, options, pathRegExp, newPagePathPrefix, shouldUseV4Process,
           );
-          descendantPages = descendantPages.concat(batch);
+          const subscribedUsers = await Subscription.getSubscriptions(batch);
+          subscribedUsers.forEach((eachUser) => {
+            if (!descendantsSubscribedUsers.includes(eachUser as never)) {
+              descendantsSubscribedUsers.push(eachUser as never);
+            }
+          });
           logger.debug(`Renaming pages progressing: (count=${count})`);
         }
         catch (err) {
@@ -874,7 +878,8 @@ class PageService {
       .pipe(writeStream);
 
     await streamToPromise(writeStream);
-    return descendantPages;
+    console.log('Who are the subscribed users\n', descendantsSubscribedUsers);
+    return descendantsSubscribedUsers;
   }
 
   private async renameDescendantsWithStreamV4(targetPage, newPagePath, user, options = {}) {