Browse Source

changed to use generatePreNotify

WNomunomu 2 years ago
parent
commit
b2d6a3a47a
2 changed files with 31 additions and 2 deletions
  1. 5 1
      apps/app/src/server/routes/apiv3/page.js
  2. 26 1
      apps/app/src/server/routes/comment.js

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

@@ -7,12 +7,14 @@ import { ErrorV3 } from '@growi/core/dist/models';
 import { convertToNewAffiliationPath } from '@growi/core/dist/utils/page-path-utils';
 import { convertToNewAffiliationPath } from '@growi/core/dist/utils/page-path-utils';
 import sanitize from 'sanitize-filename';
 import sanitize from 'sanitize-filename';
 
 
+
 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 { apiV3FormValidator } from '~/server/middlewares/apiv3-form-validator';
 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 { generateDefaultPreNotify } from '~/server/service/preNotify';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 
 
@@ -358,7 +360,9 @@ module.exports = (crowi) => {
       target: page,
       target: page,
       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);
+
+    activityEvent.emit('update', res.locals.activity._id, parameters, page, generateDefaultPreNotify);
+
 
 
     res.apiv3({ result });
     res.apiv3({ result });
 
 

+ 26 - 1
apps/app/src/server/routes/comment.js

@@ -1,6 +1,8 @@
 
 
 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';
 
 
 /**
 /**
@@ -266,7 +268,30 @@ module.exports = function(crowi, app) {
       event: createdComment,
       event: createdComment,
       action: SupportedAction.ACTION_COMMENT_CREATE,
       action: SupportedAction.ACTION_COMMENT_CREATE,
     };
     };
-    activityEvent.emit('update', res.locals.activity._id, parameters, page);
+
+    const generatePreNotify = (activity) => {
+
+      const preNotify = async(props) => {
+        const User = getModelSafely('User') || require('~/server/models/user')();
+        const actionUser = activity.user;
+        const target = activity.target;
+        const subscribedUsers = await Subscription.getSubscription(target);
+        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 mentionedUsers = await crowi.commentService.getMentionedUsers(activity.event);
+
+        props.concat(activeNotificationUsers, mentionedUsers);
+
+      };
+
+      return preNotify;
+    };
+
+    activityEvent.emit('update', res.locals.activity._id, parameters, page, generatePreNotify);
 
 
     res.json(ApiResponse.success({ comment: createdComment }));
     res.json(ApiResponse.success({ comment: createdComment }));