zahmis пре 5 година
родитељ
комит
ef8b95f309
1 измењених фајлова са 85 додато и 67 уклоњено
  1. 85 67
      src/server/service/bolt.js

+ 85 - 67
src/server/service/bolt.js

@@ -36,7 +36,6 @@ class BoltReciever {
     };
 
     await this.bolt.processEvent(event);
-
   }
 
 }
@@ -90,9 +89,8 @@ class BoltService {
       const firstArg = inputSlack[0];
       const secondArg = inputSlack[1];
 
-      let resultPaths;
-
-      if (firstArg === 'search' && secondArg == null) {
+      if (!firstArg) {
+        logger.error('Input first arguments');
         return this.client.chat.postEphemeral({
           channel: command.channel_id,
           user: command.user_id,
@@ -101,81 +99,101 @@ class BoltService {
               type: 'section',
               text: {
                 type: 'mrkdwn',
-                text: '*キーワードを入力してください。*\n Hint\n `/growi search [keyword]`',
+                text: '*コマンドを入力してください。*\n Hint\n `/growi search [keyword]`',
               },
             },
           ],
         });
       }
 
-      if (firstArg === 'search' && secondArg != null) {
-        const { searchService } = this.crowi;
-        const option = { limit: 10 };
-        const searchResults = await searchService.searchKeyword(secondArg, null, {}, option);
-
-        // no search results
-        if (searchResults.data.length === 0) {
-          return this.client.chat.postEphemeral({
-            channel: command.channel_id,
-            user: command.user_id,
-            blocks: [
-              {
-                type: 'section',
-                text: {
-                  type: 'mrkdwn',
-                  text: '*キーワードに該当するページは存在しません。*',
-                },
-              },
-            ],
-          });
-        }
-
-        resultPaths = searchResults.data.map((data) => {
-          return data._source.path;
-        });
+      if (firstArg === 'search') {
+        return this.searchResults(command, secondArg);
       }
+    });
 
-      try {
-        await this.client.chat.postEphemeral({
-          channel: command.channel_id,
-          user: command.user_id,
-          blocks: [
-            {
-              type: 'section',
-              text: {
-                type: 'mrkdwn',
-                text: '*検索結果 10 件*',
-              },
-            },
-            {
-              type: 'section',
-              text: {
-                type: 'mrkdwn',
-                text: `${resultPaths.join('\n')}`,
-              },
+
+  }
+
+  async searchResults(command, secondArg) {
+
+    if (secondArg == null) {
+      return this.client.chat.postEphemeral({
+        channel: command.channel_id,
+        user: command.user_id,
+        blocks: [
+          {
+            type: 'section',
+            text: {
+              type: 'mrkdwn',
+              text: '*キーワードを入力してください。*\n Hint\n `/growi search [keyword]`',
             },
-          ],
-        });
-      }
-      catch {
-        logger.error('Failed to get search results.');
-        await this.client.chat.postEphemeral({
-          channel: command.channel_id,
-          user: command.user_id,
-          blocks: [
-            {
-              type: 'section',
-              text: {
-                type: 'mrkdwn',
-                text: '*検索に失敗しました。*\n Hint\n `/growi search [keyword]`',
-              },
+          },
+        ],
+      });
+    }
+
+    const { searchService } = this.crowi;
+    const option = { limit: 10 };
+    const results = await searchService.searchKeyword(secondArg, null, {}, option);
+    // no search results
+    if (results.data.length === 0) {
+      return this.client.chat.postEphemeral({
+        channel: command.channel_id,
+        user: command.user_id,
+        blocks: [
+          {
+            type: 'section',
+            text: {
+              type: 'mrkdwn',
+              text: '*キーワードに該当するページは存在しません。*',
             },
-          ],
-        });
-      }
-    });
+          },
+        ],
+      });
+    }
 
+    const resultPaths = results.data.map((data) => {
+      return data._source.path;
+    });
 
+    try {
+      await this.client.chat.postEphemeral({
+        channel: command.channel_id,
+        user: command.user_id,
+        blocks: [
+          {
+            type: 'section',
+            text: {
+              type: 'mrkdwn',
+              text: '*検索結果 10 件*',
+            },
+          },
+          {
+            type: 'section',
+            text: {
+              type: 'mrkdwn',
+              text: `${resultPaths.join('\n')}`,
+            },
+          },
+        ],
+      });
+    }
+    catch {
+      logger.error('Failed to get search results.');
+      await this.client.chat.postEphemeral({
+        channel: command.channel_id,
+        user: command.user_id,
+        blocks: [
+          {
+            type: 'section',
+            text: {
+              type: 'mrkdwn',
+              text: '*検索に失敗しました。*\n Hint\n `/growi search [keyword]`',
+            },
+          },
+        ],
+      });
+    }
   }
 
 }