Jelajahi Sumber

move logic of prenotify to service

WNomunomu 2 tahun lalu
induk
melakukan
cac2d6fab2

+ 6 - 20
apps/app/src/server/routes/comment.js

@@ -5,6 +5,8 @@ import Subscription from '~/server/models/subscription';
 import { getModelSafely } from '~/server/util/mongoose-utils';
 import loggerFactory from '~/utils/logger';
 
+import { generatePreNotify } from '../service/preNotify';
+
 /**
  * @swagger
  *  tags:
@@ -269,29 +271,13 @@ module.exports = function(crowi, app) {
       action: SupportedAction.ACTION_COMMENT_CREATE,
     };
 
-    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.push(...activeNotificationUsers, ...mentionedUsers);
-
-      };
+    const getAditionalTargetUsers = async(activity) => {
+      const mentionedUsers = await crowi.commentService.getMentionedUsers(activity.event);
 
-      return preNotify;
+      return mentionedUsers;
     };
 
-    activityEvent.emit('update', res.locals.activity._id, parameters, page, generatePreNotify);
+    activityEvent.emit('update', res.locals.activity._id, parameters, page, generatePreNotify, getAditionalTargetUsers);
 
     res.json(ApiResponse.success({ comment: createdComment }));
 

+ 6 - 4
apps/app/src/server/service/activity.ts

@@ -1,4 +1,4 @@
-import type { Ref, IPage, IUser } from '@growi/core';
+import type { IPage } from '@growi/core';
 import mongoose from 'mongoose';
 
 import {
@@ -11,7 +11,7 @@ import loggerFactory from '../../utils/logger';
 import Crowi from '../crowi';
 
 
-import type { PreNotify } from './preNotify';
+import type { GeneratePreNotify, GetAditionalTargetUsers } from './preNotify';
 
 const logger = loggerFactory('growi:service:ActivityService');
 
@@ -41,7 +41,9 @@ class ActivityService {
   }
 
   initActivityEventListeners(): void {
-    this.activityEvent.on('update', async(activityId: string, parameters, target: IPage, getPreNotify: (activity: ActivityDocument) => PreNotify) => {
+    this.activityEvent.on('update', async(
+        activityId: string, parameters, target: IPage, generatePreNotify: GeneratePreNotify, getAditionalTargetUsers?: GetAditionalTargetUsers,
+    ) => {
       let activity: ActivityDocument;
       const shoudUpdate = this.shoudUpdateActivity(parameters.action);
 
@@ -54,7 +56,7 @@ class ActivityService {
           return;
         }
 
-        const preNotify = getPreNotify(activity);
+        const preNotify = generatePreNotify(activity, getAditionalTargetUsers);
 
         this.activityEvent.emit('updated', activity, target, preNotify);
       }