Преглед изворни кода

Merge pull request #6450 from weseek/feat/descendant-notifications-renaming

feat: Created the activity inside service/page.ts and gathered descendants users.
Yuki Takei пре 3 година
родитељ
комит
310c1f460c

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

@@ -6,6 +6,7 @@ import {
 } from '~/interfaces/activity';
 } from '~/interfaces/activity';
 import { Ref } from '~/interfaces/common';
 import { Ref } from '~/interfaces/common';
 import { IPage } from '~/interfaces/page';
 import { IPage } from '~/interfaces/page';
+import { IUser } from '~/interfaces/user';
 import Activity from '~/server/models/activity';
 import Activity from '~/server/models/activity';
 
 
 import loggerFactory from '../../utils/logger';
 import loggerFactory from '../../utils/logger';
@@ -41,7 +42,7 @@ class ActivityService {
   }
   }
 
 
   initActivityEventListeners(): void {
   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;
       let activity: IActivity;
       const shoudUpdate = this.shoudUpdateActivity(parameters.action);
       const shoudUpdate = this.shoudUpdateActivity(parameters.action);
 
 
@@ -54,7 +55,7 @@ class ActivityService {
           return;
           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 {
   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 {
       try {
         const shouldNotification = activity != null && target != null && (AllEssentialActions as ReadonlyArray<string>).includes(activity.action);
         const shouldNotification = activity != null && target != null && (AllEssentialActions as ReadonlyArray<string>).includes(activity.action);
         if (shouldNotification) {
         if (shouldNotification) {
-          await this.createInAppNotification(activity, target, descendantPages);
+          await this.createInAppNotification(activity, target, descendantsSubscribedUsers);
         }
         }
       }
       }
       catch (err) {
       catch (err) {
@@ -201,7 +201,7 @@ export default class InAppNotificationService {
     return;
     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 shouldNotification = activity != null && target != null && (AllEssentialActions as ReadonlyArray<string>).includes(activity.action);
     const snapshot = stringifySnapshot(target);
     const snapshot = stringifySnapshot(target);
     if (shouldNotification) {
     if (shouldNotification) {
@@ -211,10 +211,9 @@ export default class InAppNotificationService {
       }
       }
       const notificationTargetUsers = await activity?.getNotificationTargetUsers();
       const notificationTargetUsers = await activity?.getNotificationTargetUsers();
       let notificationDescendantsUsers = [];
       let notificationDescendantsUsers = [];
-      if (descendantPages != null && descendantPages.length > 0) {
+      if (descendantsSubscribedUsers != null) {
         const User = this.crowi.model('User');
         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({
         notificationDescendantsUsers = await User.find({
           _id: { $in: descendantsUsers },
           _id: { $in: descendantsUsers },
           status: User.STATUS_ACTIVE,
           status: User.STATUS_ACTIVE,

+ 10 - 8
packages/app/src/server/service/page.ts

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