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

Merge pull request #3446 from weseek/feat/create-new-page-at-GROWI-side

Feat/create new page at growi side
Sizma yosimaz 5 лет назад
Родитель
Сommit
6a18b732fe
1 измененных файлов с 58 добавлено и 9 удалено
  1. 58 9
      src/server/service/bolt.js

+ 58 - 9
src/server/service/bolt.js

@@ -114,6 +114,11 @@ class BoltService {
       }
     });
 
+    this.bolt.view('createPage', async({ ack, view }) => {
+      await ack();
+      return this.createPageInGrowi(view);
+    });
+
     this.bolt.action('button_click', async({ body, ack, say }) => {
       await ack();
       await say('clicked the button');
@@ -127,7 +132,7 @@ class BoltService {
       channel: command.channel_id,
       user: command.user_id,
       blocks: [
-        this.generateMarkdownSectionBlock('*コマンドが存在しません。*\n Hint\n `/growi [command] [keyword]`'),
+        this.generateMarkdownSectionBlock('*No command.*\n Hint\n `/growi [command] [keyword]`'),
       ],
     });
 
@@ -140,7 +145,7 @@ class BoltService {
         channel: command.channel_id,
         user: command.user_id,
         blocks: [
-          this.generateMarkdownSectionBlock('*キーワードを入力してください。*\n Hint\n `/growi search [keyword]`'),
+          this.generateMarkdownSectionBlock('*Input keywords.*\n Hint\n `/growi search [keyword]`'),
         ],
       });
     }
@@ -158,7 +163,7 @@ class BoltService {
         channel: command.channel_id,
         user: command.user_id,
         blocks: [
-          this.generateMarkdownSectionBlock('*キーワードに該当するページは存在しません。*'),
+          this.generateMarkdownSectionBlock('*No page that match your keywords.*'),
         ],
       });
     }
@@ -172,7 +177,7 @@ class BoltService {
         channel: command.channel_id,
         user: command.user_id,
         blocks: [
-          this.generateMarkdownSectionBlock('検索結果 10 件'),
+          this.generateMarkdownSectionBlock('10 results.'),
           this.generateMarkdownSectionBlock(`${resultPaths.join('\n')}`),
           {
             type: 'actions',
@@ -181,7 +186,7 @@ class BoltService {
                 type: 'button',
                 text: {
                   type: 'plain_text',
-                  text: '検索結果をこのチャンネルに共有する',
+                  text: 'Share the results in this channel.',
                 },
                 style: 'primary',
                 action_id: 'button_click',
@@ -197,14 +202,23 @@ class BoltService {
         channel: command.channel_id,
         user: command.user_id,
         blocks: [
-          this.generateMarkdownSectionBlock('*検索に失敗しました。*\n Hint\n `/growi search [keyword]`'),
+          this.generateMarkdownSectionBlock('*Failed to search.*\n Hint\n `/growi search [keyword]`'),
         ],
       });
     }
   }
 
   async createModal(command, client, body) {
+    const User = this.crowi.model('User');
+
     try {
+      const slackUser = await User.findUserByUsername('slackUser');
+
+      // if "slackUser" is null, don't show create Modal
+      if (slackUser == null) {
+        throw new Error('userNull');
+      }
+
       await client.views.open({
         trigger_id: body.trigger_id,
 
@@ -224,25 +238,60 @@ class BoltService {
             text: 'Cancel',
           },
           blocks: [
-            this.generateMarkdownSectionBlock('ページを作成します'),
+            this.generateMarkdownSectionBlock('Create new page.'),
             this.generateInputSectionBlock('path', 'Path', 'path_input', false, '/path'),
             this.generateInputSectionBlock('contents', 'Contents', 'contents_input', true, 'Input with Markdown...'),
           ],
         },
       });
     }
-    catch {
+    catch (e) {
+      if (e instanceof Error) {
+        return this.client.chat.postEphemeral({
+          channel: command.channel_id,
+          user: command.user_id,
+          blocks: [
+            this.generateMarkdownSectionBlock('*slackUser does not exist.*'),
+          ],
+        });
+      }
+
       logger.error('Failed to create page.');
       await this.client.chat.postEphemeral({
         channel: command.channel_id,
         user: command.user_id,
         blocks: [
-          this.generateMarkdownSectionBlock('*ページ作成に失敗しました。*\n Hint\n `/growi create`'),
+          this.generateMarkdownSectionBlock('*Failed to create new page.*\n Hint\n `/growi create`'),
         ],
       });
     }
   }
 
+  // Submit action in create Modal
+  async createPageInGrowi(view) {
+    const User = this.crowi.model('User');
+    const Page = this.crowi.model('Page');
+    const pathUtils = require('growi-commons').pathUtils;
+
+    try {
+      // search "slackUser" to create page in slack
+      const slackUser = await User.findUserByUsername('slackUser');
+
+      let path = view.state.values.path.path_input.value;
+      const body = view.state.values.contents.contents_input.value;
+
+      // sanitize path
+      path = this.crowi.xss.process(path);
+      path = pathUtils.normalizePath(path);
+
+      const user = slackUser._id;
+      return Page.create(path, body, user, {});
+    }
+    catch {
+      logger.error('Failed to create page in GROWI.');
+    }
+  }
+
   generateMarkdownSectionBlock(blocks) {
     return {
       type: 'section',