Przeglądaj źródła

grouping methods in a file as a singleton class

WNomunomu 2 lat temu
rodzic
commit
89a566fa12

+ 2 - 2
apps/app/src/server/routes/apiv3/bookmarks.js

@@ -1,7 +1,7 @@
 import { SupportedAction, SupportedTargetModel } from '~/interfaces/activity';
 import { SupportedAction, SupportedTargetModel } from '~/interfaces/activity';
 import { generateAddActivityMiddleware } from '~/server/middlewares/add-activity';
 import { generateAddActivityMiddleware } from '~/server/middlewares/add-activity';
 import { serializeBookmarkSecurely } from '~/server/models/serializers/bookmark-serializer';
 import { serializeBookmarkSecurely } from '~/server/models/serializers/bookmark-serializer';
-import { generatePreNotify } from '~/server/service/preNotify';
+import { preNotifyService } from '~/server/service/preNotify';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import { apiV3FormValidator } from '../../middlewares/apiv3-form-validator';
 import { apiV3FormValidator } from '../../middlewares/apiv3-form-validator';
@@ -303,7 +303,7 @@ module.exports = (crowi) => {
       action: bool ? SupportedAction.ACTION_PAGE_BOOKMARK : SupportedAction.ACTION_PAGE_UNBOOKMARK,
       action: bool ? SupportedAction.ACTION_PAGE_BOOKMARK : SupportedAction.ACTION_PAGE_UNBOOKMARK,
     };
     };
 
 
-    activityEvent.emit('update', res.locals.activity._id, parameters, page, generatePreNotify);
+    activityEvent.emit('update', res.locals.activity._id, parameters, page, preNotifyService.generatePreNotify);
 
 
     return res.apiv3({ bookmark });
     return res.apiv3({ bookmark });
   });
   });

+ 2 - 2
apps/app/src/server/routes/apiv3/page.js

@@ -14,7 +14,7 @@ import { apiV3FormValidator } from '~/server/middlewares/apiv3-form-validator';
 import { excludeReadOnlyUser } from '~/server/middlewares/exclude-read-only-user';
 import { excludeReadOnlyUser } from '~/server/middlewares/exclude-read-only-user';
 import Subscription from '~/server/models/subscription';
 import Subscription from '~/server/models/subscription';
 import UserGroup from '~/server/models/user-group';
 import UserGroup from '~/server/models/user-group';
-import { generatePreNotify } from '~/server/service/preNotify';
+import { preNotifyService } from '~/server/service/preNotify';
 import { divideByType } from '~/server/util/granted-group';
 import { divideByType } from '~/server/util/granted-group';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
@@ -364,7 +364,7 @@ module.exports = (crowi) => {
       action: isLiked ? SupportedAction.ACTION_PAGE_LIKE : SupportedAction.ACTION_PAGE_UNLIKE,
       action: isLiked ? SupportedAction.ACTION_PAGE_LIKE : SupportedAction.ACTION_PAGE_UNLIKE,
     };
     };
 
 
-    activityEvent.emit('update', res.locals.activity._id, parameters, page, generatePreNotify);
+    activityEvent.emit('update', res.locals.activity._id, parameters, page, preNotifyService.generatePreNotify);
 
 
 
 
     res.apiv3({ result });
     res.apiv3({ result });

+ 2 - 2
apps/app/src/server/routes/apiv3/pages.js

@@ -6,7 +6,7 @@ import { normalizePath, addHeadingSlash, attachTitleHeader } from '@growi/core/d
 
 
 import { SupportedTargetModel, SupportedAction } from '~/interfaces/activity';
 import { SupportedTargetModel, SupportedAction } from '~/interfaces/activity';
 import { subscribeRuleNames } from '~/interfaces/in-app-notification';
 import { subscribeRuleNames } from '~/interfaces/in-app-notification';
-import { generatePreNotify } from '~/server/service/preNotify';
+import { preNotifyService } from '~/server/service/preNotify';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import { generateAddActivityMiddleware } from '../../middlewares/add-activity';
 import { generateAddActivityMiddleware } from '../../middlewares/add-activity';
@@ -851,7 +851,7 @@ module.exports = (crowi) => {
         action: SupportedAction.ACTION_PAGE_DUPLICATE,
         action: SupportedAction.ACTION_PAGE_DUPLICATE,
       };
       };
 
 
-      activityEvent.emit('update', res.locals.activity._id, parameters, page, generatePreNotify);
+      activityEvent.emit('update', res.locals.activity._id, parameters, page, preNotifyService.generatePreNotify);
 
 
       return res.apiv3(result);
       return res.apiv3(result);
     });
     });

+ 2 - 4
apps/app/src/server/routes/comment.js

@@ -1,11 +1,9 @@
 
 
 import { Comment, CommentEvent, commentEvent } from '~/features/comment/server';
 import { Comment, CommentEvent, commentEvent } from '~/features/comment/server';
 import { SupportedAction, SupportedTargetModel, SupportedEventModel } from '~/interfaces/activity';
 import { SupportedAction, SupportedTargetModel, SupportedEventModel } from '~/interfaces/activity';
-import Subscription from '~/server/models/subscription';
-import { getModelSafely } from '~/server/util/mongoose-utils';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
-import { generatePreNotify } from '../service/preNotify';
+import { preNotifyService } from '../service/preNotify';
 
 
 /**
 /**
  * @swagger
  * @swagger
@@ -277,7 +275,7 @@ module.exports = function(crowi, app) {
       return mentionedUsers;
       return mentionedUsers;
     };
     };
 
 
-    activityEvent.emit('update', res.locals.activity._id, parameters, page, generatePreNotify, getAditionalTargetUsers);
+    activityEvent.emit('update', res.locals.activity._id, parameters, page, preNotifyService.generatePreNotify, getAditionalTargetUsers);
 
 
     res.json(ApiResponse.success({ comment: createdComment }));
     res.json(ApiResponse.success({ comment: createdComment }));
 
 

+ 4 - 2
apps/app/src/server/routes/page.js

@@ -7,7 +7,7 @@ import loggerFactory from '~/utils/logger';
 
 
 import { PathAlreadyExistsError } from '../models/errors';
 import { PathAlreadyExistsError } from '../models/errors';
 import UpdatePost from '../models/update-post';
 import UpdatePost from '../models/update-post';
-import { generatePreNotify } from '../service/preNotify';
+import { preNotifyService } from '../service/preNotify';
 
 
 const { serializePageSecurely } = require('../models/serializers/page-serializer');
 const { serializePageSecurely } = require('../models/serializers/page-serializer');
 const { serializeRevisionSecurely } = require('../models/serializers/revision-serializer');
 const { serializeRevisionSecurely } = require('../models/serializers/revision-serializer');
@@ -524,7 +524,9 @@ module.exports = function(crowi, app) {
       action: SupportedAction.ACTION_PAGE_UPDATE,
       action: SupportedAction.ACTION_PAGE_UPDATE,
     };
     };
 
 
-    activityEvent.emit('update', res.locals.activity._id, parameters, { path: page.path, creator: page.creator._id.toString() }, generatePreNotify);
+    activityEvent.emit(
+      'update', res.locals.activity._id, parameters, { path: page.path, creator: page.creator._id.toString() }, preNotifyService.generatePreNotify,
+    );
   };
   };
 
 
   /**
   /**

+ 10 - 10
apps/app/src/server/service/page.ts

@@ -46,7 +46,7 @@ import UserGroupRelation from '../models/user-group-relation';
 import { V5ConversionError } from '../models/vo/v5-conversion-error';
 import { V5ConversionError } from '../models/vo/v5-conversion-error';
 import { divideByType } from '../util/granted-group';
 import { divideByType } from '../util/granted-group';
 
 
-import { generatePreNotify } from './preNotify';
+import { preNotifyService } from './preNotify';
 
 
 const debug = require('debug')('growi:services:page');
 const debug = require('debug')('growi:services:page');
 
 
@@ -446,7 +446,7 @@ class PageService {
       throw err;
       throw err;
     }
     }
     if (page.descendantCount < 1) {
     if (page.descendantCount < 1) {
-      const preNotify = generatePreNotify(activity);
+      const preNotify = preNotifyService.generatePreNotify(activity);
 
 
       this.activityEvent.emit('updated', activity, page, preNotify);
       this.activityEvent.emit('updated', activity, page, preNotify);
     }
     }
@@ -555,7 +555,7 @@ class PageService {
       await this.renameDescendantsWithStream(page, newPagePath, user, options, false, descendantsSubscribedSets);
       await this.renameDescendantsWithStream(page, newPagePath, user, options, false, descendantsSubscribedSets);
       const descendantsSubscribedUsers = Array.from(descendantsSubscribedSets) as Ref<IUser>[];
       const descendantsSubscribedUsers = Array.from(descendantsSubscribedSets) as Ref<IUser>[];
 
 
-      const preNotify = generatePreNotify(activity, () => { return descendantsSubscribedUsers });
+      const preNotify = preNotifyService.generatePreNotify(activity, () => { return descendantsSubscribedUsers });
 
 
       this.activityEvent.emit('updated', activity, page, preNotify);
       this.activityEvent.emit('updated', activity, page, preNotify);
     }
     }
@@ -1489,7 +1489,7 @@ class PageService {
       })();
       })();
     }
     }
     else {
     else {
-      const preNotify = generatePreNotify(activity);
+      const preNotify = preNotifyService.generatePreNotify(activity);
 
 
       this.activityEvent.emit('updated', activity, page, preNotify);
       this.activityEvent.emit('updated', activity, page, preNotify);
     }
     }
@@ -1529,7 +1529,7 @@ class PageService {
 
 
     const descendantsSubscribedUsers = Array.from(descendantsSubscribedSets) as Ref<IUser>[];
     const descendantsSubscribedUsers = Array.from(descendantsSubscribedSets) as Ref<IUser>[];
 
 
-    const preNotify = generatePreNotify(activity, () => { return descendantsSubscribedUsers });
+    const preNotify = preNotifyService.generatePreNotify(activity, () => { return descendantsSubscribedUsers });
 
 
     this.activityEvent.emit('updated', activity, page, preNotify);
     this.activityEvent.emit('updated', activity, page, preNotify);
 
 
@@ -1842,7 +1842,7 @@ class PageService {
       })();
       })();
     }
     }
     else {
     else {
-      const preNotify = generatePreNotify(activity);
+      const preNotify = preNotifyService.generatePreNotify(activity);
 
 
       this.activityEvent.emit('updated', activity, page, preNotify);
       this.activityEvent.emit('updated', activity, page, preNotify);
     }
     }
@@ -1855,7 +1855,7 @@ class PageService {
     await this.deleteCompletelyDescendantsWithStream(page, user, options, false, descendantsSubscribedSets);
     await this.deleteCompletelyDescendantsWithStream(page, user, options, false, descendantsSubscribedSets);
     const descendantsSubscribedUsers = Array.from(descendantsSubscribedSets) as Ref<IUser>[];
     const descendantsSubscribedUsers = Array.from(descendantsSubscribedSets) as Ref<IUser>[];
 
 
-    const preNotify = generatePreNotify(activity, () => { return descendantsSubscribedUsers });
+    const preNotify = preNotifyService.generatePreNotify(activity, () => { return descendantsSubscribedUsers });
 
 
     this.activityEvent.emit('updated', activity, page, preNotify);
     this.activityEvent.emit('updated', activity, page, preNotify);
 
 
@@ -1902,7 +1902,7 @@ class PageService {
     const pages = await this.deleteCompletelyDescendantsWithStream(page, user, options, true, descendantsSubscribedSets);
     const pages = await this.deleteCompletelyDescendantsWithStream(page, user, options, true, descendantsSubscribedSets);
     const descendantsSubscribedUsers = Array.from(descendantsSubscribedSets) as Ref<IUser>[];
     const descendantsSubscribedUsers = Array.from(descendantsSubscribedSets) as Ref<IUser>[];
 
 
-    const preNotify = generatePreNotify(activity, () => { return descendantsSubscribedUsers });
+    const preNotify = preNotifyService.generatePreNotify(activity, () => { return descendantsSubscribedUsers });
 
 
     this.activityEvent.emit('updated', activity, page, preNotify);
     this.activityEvent.emit('updated', activity, page, preNotify);
 
 
@@ -2182,7 +2182,7 @@ class PageService {
     if (!isRecursively) {
     if (!isRecursively) {
       await this.updateDescendantCountOfAncestors(parent._id, 1, true);
       await this.updateDescendantCountOfAncestors(parent._id, 1, true);
 
 
-      const preNotify = generatePreNotify(activity);
+      const preNotify = preNotifyService.generatePreNotify(activity);
 
 
       this.activityEvent.emit('updated', activity, page, preNotify);
       this.activityEvent.emit('updated', activity, page, preNotify);
     }
     }
@@ -2232,7 +2232,7 @@ class PageService {
     await this.revertDeletedDescendantsWithStream(page, user, options, false, descendantsSubscribedSets);
     await this.revertDeletedDescendantsWithStream(page, user, options, false, descendantsSubscribedSets);
     const descendantsSubscribedUsers = Array.from(descendantsSubscribedSets) as Ref<IUser>[];
     const descendantsSubscribedUsers = Array.from(descendantsSubscribedSets) as Ref<IUser>[];
 
 
-    const preNotify = generatePreNotify(activity, () => { return descendantsSubscribedUsers });
+    const preNotify = preNotifyService.generatePreNotify(activity, () => { return descendantsSubscribedUsers });
 
 
     this.activityEvent.emit('updated', activity, page, preNotify);
     this.activityEvent.emit('updated', activity, page, preNotify);
 
 

+ 31 - 71
apps/app/src/server/service/preNotify.ts

@@ -15,87 +15,47 @@ export type GeneratePreNotify = (activity: ActivityDocument, getAditionalTargetU
 
 
 export type GetAditionalTargetUsers = (activity: ActivityDocument) => Ref<IUser>[];
 export type GetAditionalTargetUsers = (activity: ActivityDocument) => Ref<IUser>[];
 
 
-export const generateInitialPreNotifyProps = (): PreNotifyProps => {
+class PreNotifyService {
 
 
-  const initialPreNotifyProps: Ref<IUser>[] = [];
+  generateInitialPreNotifyProps = (): PreNotifyProps => {
 
 
-  return { notificationTargetUsers: initialPreNotifyProps };
-};
-
-export const generatePreNotify = (activity: ActivityDocument, getAditionalTargetUsers?: GetAditionalTargetUsers): PreNotify => {
-
-  const preNotify = async(props: PreNotifyProps) => {
-    const { notificationTargetUsers } = props;
-
-    const User = getModelSafely('User') || require('~/server/models/user')();
-    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()));
-    const activeNotificationUsers = await User.find({
-      _id: { $in: notificationUsers },
-      status: User.STATUS_ACTIVE,
-    }).distinct('_id');
-
-    if (getAditionalTargetUsers == null) {
-      notificationTargetUsers?.push(...activeNotificationUsers);
-    }
-    else {
-      const aditionalTargetUsers = getAditionalTargetUsers(activity);
-
-      notificationTargetUsers?.push(
-        ...activeNotificationUsers,
-        ...aditionalTargetUsers,
-      );
-    }
+    const initialPreNotifyProps: Ref<IUser>[] = [];
 
 
+    return { notificationTargetUsers: initialPreNotifyProps };
   };
   };
 
 
-  return preNotify;
-};
+  generatePreNotify = (activity: ActivityDocument, getAditionalTargetUsers?: GetAditionalTargetUsers): PreNotify => {
 
 
-// export const generateDefaultPreNotify = (activity: ActivityDocument): PreNotify => {
+    const preNotify = async(props: PreNotifyProps) => {
+      const { notificationTargetUsers } = props;
 
 
-//   const preNotify = async(props: PreNotifyProps) => {
-//     const { notificationTargetUsers } = props;
+      const User = getModelSafely('User') || require('~/server/models/user')();
+      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()));
+      const activeNotificationUsers = await User.find({
+        _id: { $in: notificationUsers },
+        status: User.STATUS_ACTIVE,
+      }).distinct('_id');
 
 
-//     const User = getModelSafely('User') || require('~/server/models/user')();
-//     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()));
-//     const activeNotificationUsers = await User.find({
-//       _id: { $in: notificationUsers },
-//       status: User.STATUS_ACTIVE,
-//     }).distinct('_id');
+      if (getAditionalTargetUsers == null) {
+        notificationTargetUsers?.push(...activeNotificationUsers);
+      }
+      else {
+        const aditionalTargetUsers = getAditionalTargetUsers(activity);
 
 
-//     notificationTargetUsers?.push(...activeNotificationUsers);
-//   };
+        notificationTargetUsers?.push(
+          ...activeNotificationUsers,
+          ...aditionalTargetUsers,
+        );
+      }
 
 
-//   return preNotify;
-// };
+    };
 
 
-// export const generatePreNotifyAlsoDescendants = (activity: ActivityDocument, descendantsSubscribedUsers: Ref<IUser>[]): PreNotify => {
-
-//   const preNotify = async(props: PreNotifyProps) => {
-//     const { notificationTargetUsers } = props;
-
-//     const User = getModelSafely('User') || require('~/server/models/user')();
-//     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()));
-//     const activeNotificationUsers = await User.find({
-//       _id: { $in: notificationUsers },
-//       status: User.STATUS_ACTIVE,
-//     }).distinct('_id');
-
-//     notificationTargetUsers?.push(
-//       ...activeNotificationUsers,
-//       ...descendantsSubscribedUsers,
-//     );
+    return preNotify;
+  };
 
 
-//   };
+}
 
 
-//   return preNotify;
-// };
+export const preNotifyService = new PreNotifyService();