hakumizuki 4 лет назад
Родитель
Сommit
12e7caa5d3
1 измененных файлов с 15 добавлено и 17 удалено
  1. 15 17
      src/server/service/slackbot.js

+ 15 - 17
src/server/service/slackbot.js

@@ -155,35 +155,25 @@ class SlackBotService extends S2sMessageHandlable {
   }
   }
 
 
   // Submit action in create Modal
   // Submit action in create Modal
-  async createPage(client, payload, contentsBody) {
+  async createPage(client, payload, path, channelId, contentsBody) {
     const Page = this.crowi.model('Page');
     const Page = this.crowi.model('Page');
     const pathUtils = require('growi-commons').pathUtils;
     const pathUtils = require('growi-commons').pathUtils;
     const reshapedContentsBody = reshapeContentsBody(contentsBody);
     const reshapedContentsBody = reshapeContentsBody(contentsBody);
-    let path = '';
-    let channelId = '';
     try {
     try {
-      if (payload.type === 'block_actions' && payload.actions[0].action_id === 'togetterCreatePage') {
-        path = payload.state.values.page_path.page_path.value;
-        channelId = payload.channel.id;
-      }
-      else {
-        path = payload.view.state.values.path.path_input.value;
-        channelId = JSON.parse(payload.view.private_metadata).channelId;
-      }
       // sanitize path
       // sanitize path
-      path = this.crowi.xss.process(path);
-      path = pathUtils.normalizePath(path);
+      const sanitizedPath = this.crowi.xss.process(path);
+      const normalizedPath = pathUtils.normalizePath(sanitizedPath);
 
 
       // generate a dummy id because Operation to create a page needs ObjectId
       // generate a dummy id because Operation to create a page needs ObjectId
       const dummyObjectIdOfUser = new mongoose.Types.ObjectId();
       const dummyObjectIdOfUser = new mongoose.Types.ObjectId();
-      const page = await Page.create(path, reshapedContentsBody, dummyObjectIdOfUser, {});
+      const page = await Page.create(normalizedPath, reshapedContentsBody, dummyObjectIdOfUser, {});
 
 
       // Send a message when page creation is complete
       // Send a message when page creation is complete
       const growiUri = this.crowi.appService.getSiteUrl();
       const growiUri = this.crowi.appService.getSiteUrl();
       await client.chat.postEphemeral({
       await client.chat.postEphemeral({
         channel: channelId,
         channel: channelId,
         user: payload.user.id,
         user: payload.user.id,
-        text: `The page <${decodeURI(`${growiUri}/${page._id} | ${decodeURI(growiUri + path)}`)}> has been created.`,
+        text: `The page <${decodeURI(`${growiUri}/${page._id} | ${decodeURI(growiUri + normalizedPath)}`)}> has been created.`,
       });
       });
     }
     }
     catch (err) {
     catch (err) {
@@ -198,8 +188,10 @@ class SlackBotService extends S2sMessageHandlable {
   }
   }
 
 
   async createPageInGrowi(client, payload) {
   async createPageInGrowi(client, payload) {
+    const path = payload.view.state.values.path.path_input.value;
+    const channelId = JSON.parse(payload.view.private_metadata).channelId;
     const contentsBody = payload.view.state.values.contents.contents_input.value;
     const contentsBody = payload.view.state.values.contents.contents_input.value;
-    await this.createPage(client, payload, contentsBody);
+    await this.createPage(client, payload, path, channelId, contentsBody);
   }
   }
 
 
   async togetterCreatePageInGrowi(client, payload) {
   async togetterCreatePageInGrowi(client, payload) {
@@ -210,12 +202,18 @@ class SlackBotService extends S2sMessageHandlable {
       const body = option.description.text.concat('\n');
       const body = option.description.text.concat('\n');
       return header.concat(body);
       return header.concat(body);
     });
     });
+    let path = '';
+    let channelId = '';
+    if (payload.type === 'block_actions' && payload.actions[0].action_id === 'togetterCreatePage') {
+      path = payload.state.values.page_path.page_path.value;
+      channelId = payload.channel.id;
+    }
     const contentsBody = messages.join('');
     const contentsBody = messages.join('');
     // dismiss
     // dismiss
     axios.post(responseUrl, {
     axios.post(responseUrl, {
       delete_original: true,
       delete_original: true,
     });
     });
-    await this.createPage(client, payload, contentsBody);
+    await this.createPage(client, payload, path, channelId, contentsBody);
   }
   }
 
 
 }
 }