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

Merge pull request #3425 from weseek/feat/add-try-catch-in-growi-search

Feat/add try catch in growi search
Sizma yosimaz 5 лет назад
Родитель
Сommit
9f76ee52e4
1 измененных файлов с 95 добавлено и 55 удалено
  1. 95 55
      src/server/service/bolt.js

+ 95 - 55
src/server/service/bolt.js

@@ -8,6 +8,12 @@ class BoltReciever {
 
 
   async requestHandler(req, res) {
   async requestHandler(req, res) {
     let ackCalled = false;
     let ackCalled = false;
+
+    // for verification request URL on Event Subscriptions
+    if (req.body.challenge && req.body.type) {
+      return res.send(req.body);
+    }
+
     const event = {
     const event = {
       body: req.body,
       body: req.body,
       ack: (response) => {
       ack: (response) => {
@@ -30,9 +36,6 @@ class BoltReciever {
     };
     };
 
 
     await this.bolt.processEvent(event);
     await this.bolt.processEvent(event);
-
-    // for verification request URL on Event Subscriptions
-    res.send(req.body);
   }
   }
 
 
 }
 }
@@ -79,63 +82,100 @@ class BoltService {
       await say(`${command.text}`);
       await say(`${command.text}`);
     });
     });
 
 
-    // TODO check if firstArg is the supported command(like "search")
     this.bolt.command('/growi', async({ command, ack }) => {
     this.bolt.command('/growi', async({ command, ack }) => {
       await ack();
       await ack();
-      const inputSlack = command.text.split(' ');
-      const firstArg = inputSlack[0];
-      const secondArg = inputSlack[1];
-
-      let resultPaths;
-      if (firstArg === 'search') {
-        const { searchService } = this.crowi;
-        const option = { limit: 10 };
-        const searchResults = await searchService.searchKeyword(secondArg, null, {}, option);
-        resultPaths = searchResults.data.map((data) => {
-          return data._source.path;
-        });
-      }
+      const args = command.text.split(' ');
+      const firstArg = args[0];
 
 
-      // TODO impl try-catch
-      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')}`,
-              },
-            },
-            {
-              type: 'actions',
-              elements: [
-                {
-                  type: 'button',
-                  text: {
-                    type: 'plain_text',
-                    text: '検索結果をこのチャンネルに共有する',
-                  },
-                  style: 'primary',
-                },
-              ],
-            },
-          ],
-        });
-      }
-      catch {
-        console.log('This is error');
+      switch (firstArg) {
+        case 'search':
+          this.searchResults(command, args);
+          break;
+
+        default:
+          this.notCommand(command);
+          break;
       }
       }
     });
     });
+
+  }
+
+  notCommand(command) {
+    logger.error('Input first arguments');
+    return this.client.chat.postEphemeral({
+      channel: command.channel_id,
+      user: command.user_id,
+      blocks: [
+        this.generateMarkdownSectionBlock('*コマンドが存在しません。*\n Hint\n `/growi [command] [keyword]`'),
+      ],
+    });
+
+  }
+
+  async searchResults(command, args) {
+    const firstKeyword = args[1];
+    if (firstKeyword == null) {
+      return this.client.chat.postEphemeral({
+        channel: command.channel_id,
+        user: command.user_id,
+        blocks: [
+          this.generateMarkdownSectionBlock('*キーワードを入力してください。*\n Hint\n `/growi search [keyword]`'),
+        ],
+      });
+    }
+
+    // remove leading 'search'.
+    args.shift();
+    const keywords = args.join(' ');
+    const { searchService } = this.crowi;
+    const option = { limit: 10 };
+    const results = await searchService.searchKeyword(keywords, null, {}, option);
+
+    // no search results
+    if (results.data.length === 0) {
+      return this.client.chat.postEphemeral({
+        channel: command.channel_id,
+        user: command.user_id,
+        blocks: [
+          this.generateMarkdownSectionBlock('*キーワードに該当するページは存在しません。*'),
+        ],
+      });
+    }
+
+    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: [
+          this.generateMarkdownSectionBlock('検索結果 10 件'),
+          this.generateMarkdownSectionBlock(`${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: [
+          this.generateMarkdownSectionBlock('*検索に失敗しました。*\n Hint\n `/growi search [keyword]`'),
+        ],
+      });
+    }
+  }
+
+  generateMarkdownSectionBlock(blocks) {
+    return {
+      type: 'section',
+      text: {
+        type: 'mrkdwn',
+        text: blocks,
+      },
+    };
   }
   }
 
 
 }
 }