Jelajahi Sumber

get mentioned username in the comment

https://youtrack.weseek.co.jp/issue/GW-7812
- Add method findUserByUsernames in User model
- Implement method to get comment by comment ID
- Get username list in comment with regex pattern
- Implement findUserByUsernames to get mentioned user Ids
- Add mentioned user Ids to targetUsers and send notification
I Komang Mudana 3 tahun lalu
induk
melakukan
42c1a254d3

+ 1 - 1
packages/app/src/server/models/comment.js

@@ -95,7 +95,7 @@ module.exports = function(crowi) {
   };
 
   commentSchema.statics.findCommentById = async function(commentId) {
-    return this.findOne({ commentId });
+    return this.findOne({ _id: commentId });
   };
 
   /**

+ 4 - 0
packages/app/src/server/models/user.js

@@ -719,6 +719,10 @@ module.exports = function(crowi) {
     return this.find({ username: { $regex: username, $options: 'i' } }).limit(limit);
   };
 
+  userSchema.statics.findUserByUsernames = async function(usernames) {
+    return this.find({ username: { $in: usernames } });
+  };
+
   class UserUpperLimitException {
 
     constructor() {

+ 22 - 7
packages/app/src/server/service/comment.ts

@@ -7,6 +7,7 @@ import { stringifySnapshot } from '~/models/serializers/in-app-notification-snap
 import loggerFactory from '../../utils/logger';
 import Crowi from '../crowi';
 
+const USERNAME_PATTERN = new RegExp(/@[\w@.-]+/g);
 
 const logger = loggerFactory('growi:service:CommentService');
 
@@ -99,18 +100,32 @@ class CommentService {
     let targetUsers: Types.ObjectId[] = [];
     targetUsers = await activity.getNotificationTargetUsers();
 
-    // TODO get mentioned users from comment
-    // const mentionedUsers = await this.getMentionedUsers(page.event);
-    // targetUsers = targetUsers.concat(mentionedUsers);
-    // Create and send notifications
+    // Add mentioned users to targetUsers
+    const mentionedUsers = await this.getMentionedUsers(activity.event);
+    targetUsers = targetUsers.concat(mentionedUsers);
+
     await this.inAppNotificationService.upsertByActivity(targetUsers, activity, snapshot);
     await this.inAppNotificationService.emitSocketIo(targetUsers);
   };
 
-  getMentionedUsers = async(commentId: Types.ObjectId) => {
+  getMentionedUsers = async(commentId: Types.ObjectId): Promise<Types.ObjectId[]> => {
     const Comment = getModelSafely('Comment') || require('../models/comment')(this.crowi);
-    const comment = await Comment.findCommentById(commentId);
-    // TODO  get users from comment
+    const User = getModelSafely('User') || require('../models/user')(this.crowi);
+
+    // Get comment by comment ID
+    const commentData = await Comment.findCommentById(commentId);
+    const { comment } = commentData;
+
+    // Get username from comment
+    const mentionedUsernames = comment.match(USERNAME_PATTERN)?.map((username) => {
+      return username.slice(1);
+    });
+
+    // Get mentioned users ID
+    const mentionedUserIDs = await User.findUserByUsernames(mentionedUsernames);
+    return mentionedUserIDs?.map((user) => {
+      return user._id;
+    });
   }
 
 }