Просмотр исходного кода

Handling users/list API results

https://youtrack.weseek.co.jp/issue/GW-7773
- Add limit options to users/list API route
- Implement limit options in API request
- Remove empty array checking block
- Display custom hint list when API return empty array
- Add translation for custom hint list
I Komang Mudana 3 лет назад
Родитель
Сommit
6a339ac616

+ 2 - 1
packages/app/resource/locales/en_US/translation.json

@@ -390,7 +390,8 @@
     }
   },
   "page_comment": {
-    "display_the_page_when_posting_this_comment": "Display the page when posting this comment"
+    "display_the_page_when_posting_this_comment": "Display the page when posting this comment",
+    "no_user_found": "No user found"
   },
   "page_api_error": {
     "notfound_or_forbidden": "Original page is not found or forbidden.",

+ 2 - 1
packages/app/resource/locales/ja_JP/translation.json

@@ -390,7 +390,8 @@
     }
   },
   "page_comment": {
-    "display_the_page_when_posting_this_comment": "投稿時のページを表示する"
+    "display_the_page_when_posting_this_comment": "投稿時のページを表示する",
+    "no_user_found": "ユーザー名が見つかりません"
   },
   "page_api_error": {
     "notfound_or_forbidden": "元のページが見つからないか、アクセス権がありません。",

+ 2 - 1
packages/app/resource/locales/zh_CN/translation.json

@@ -369,7 +369,8 @@
 		}
   },
   "page_comment": {
-    "display_the_page_when_posting_this_comment": "Display the page when posting this comment"
+    "display_the_page_when_posting_this_comment": "Display the page when posting this comment",
+    "no_user_found": "未找到用户名"
   },
 	"page_api_error": {
 		"notfound_or_forbidden": "未找到或禁止原始页。",

+ 4 - 2
packages/app/src/components/PageEditor/CommentMentionHelper.ts

@@ -1,3 +1,4 @@
+import i18n from 'i18next';
 import { debounce } from 'throttle-debounce';
 
 import { apiv3Get } from '~/client/util/apiv3-client';
@@ -38,7 +39,7 @@ export default class CommentMentionHelper {
         if (mention.length > 0) {
           const users = await this.getUsersList(mention);
           return {
-            list: users,
+            list: users.length > 0 ? users : [{ text: '', displayText: i18n.t('page_comment.no_user_found') }],
             from: searchFrom,
             to: searchTo,
           };
@@ -48,7 +49,8 @@ export default class CommentMentionHelper {
   }
 
   getUsersList = async(username) => {
-    const { data } = await apiv3Get('/users/list', { username });
+    const limit = 20;
+    const { data } = await apiv3Get('/users/list', { username, limit });
     return data.users.map(user => ({
       text: `@${user.username} `,
       displayText: user.username,

+ 3 - 6
packages/app/src/server/models/user.js

@@ -715,13 +715,10 @@ module.exports = function(crowi) {
     return users;
   };
 
-  userSchema.statics.findUserByUsernameRegex = async function(username) {
-    const users = this.find({ username: { $regex: username, $options: 'i' } });
-    if (users.length === 0) {
-      throw new Error('No User found');
-    }
-    return users;
+  userSchema.statics.findUserByUsernameRegex = async function(username, limit) {
+    return this.find({ username: { $regex: username, $options: 'i' } }).limit(limit);
   };
+
   class UserUpperLimitException {
 
     constructor() {

+ 2 - 1
packages/app/src/server/routes/apiv3/users.js

@@ -900,13 +900,14 @@ module.exports = (crowi) => {
   router.get('/list', accessTokenParser, loginRequired, async(req, res) => {
     const userIds = req.query.userIds || null;
     const username = req.query.username || null;
+    const limit = req.query.limit || 20;
 
     let userFetcher;
     if (userIds !== null && userIds.split(',').length > 0) {
       userFetcher = User.findUsersByIds(userIds.split(','));
     }
     else if (username !== null) {
-      userFetcher = User.findUserByUsernameRegex(username);
+      userFetcher = User.findUserByUsernameRegex(username, limit);
     }
     else {
       userFetcher = User.findAllUsers();