Răsfoiți Sursa

Simplify get comment and username by username list

https://youtrack.weseek.co.jp/issue/GW-7812
- Remove  findCommentById and findUserByUsernames method
- Update function to get username from comment and remove duplicate username
I Komang Mudana 3 ani în urmă
părinte
comite
9b6e3f2243

+ 0 - 3
packages/app/src/server/models/comment.js

@@ -94,9 +94,6 @@ module.exports = function(crowi) {
     return commentData;
   };
 
-  commentSchema.statics.findCommentById = async function(commentId) {
-    return this.findOne({ _id: commentId });
-  };
 
   /**
    * post remove hook

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

@@ -719,10 +719,6 @@ 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() {

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

@@ -114,16 +114,18 @@ class CommentService {
     const User = getModelSafely('User') || require('../models/user')(this.crowi);
 
     // Get comment by comment ID
-    const commentData = await Comment.findCommentById(commentId);
+    const commentData = await Comment.findOne({ _id: commentId });
     const { comment } = commentData;
 
-    // Get username from comment
-    const mentionedUsernames = comment.match(USERNAME_PATTERN)?.map((username) => {
+    const usernamesFromComment = comment.match(USERNAME_PATTERN);
+
+    // Get username from comment and remove duplicate username
+    const mentionedUsernames = [...new Set(usernamesFromComment?.map((username) => {
       return username.slice(1);
-    });
+    }))];
 
     // Get mentioned users ID
-    const mentionedUserIDs = await User.findUserByUsernames(mentionedUsernames);
+    const mentionedUserIDs = await User.find({ username: { $in: mentionedUsernames } });
     return mentionedUserIDs?.map((user) => {
       return user._id;
     });