2
0
Эх сурвалжийг харах

Merge branch 'feat/GW-6710-slackbot-togetter-command' into feat/GW-6710-slackbot-togetter-command-choose-growi

hakumizuki 4 жил өмнө
parent
commit
d3c1c40864

+ 1 - 5
src/server/routes/apiv3/slack-integration.js

@@ -148,12 +148,8 @@ module.exports = (crowi) => {
         await crowi.slackBotService.showEphemeralSearchResults(client, body, args, newOffset);
         break;
       }
-      case 'togetterShowMore': {
-        console.log('Show more here');
-        break;
-      }
       case 'togetterCreatePage': {
-        console.log('Create page and delete the original message here');
+        await crowi.slackBotService.togetterCreatePageInGrowi(client, payload);
         break;
       }
       case 'togetterCancelPageCreation': {

+ 2 - 2
src/server/service/slack-command-handler/togetter.js

@@ -26,7 +26,7 @@ module.exports = () => {
   handler.togetterMessageBlocks = function() {
     return [
       inputBlock(this.togetterCheckboxesElement(), 'selected_messages', 'Select massages to use.'),
-      actionsBlock(buttonElement('Show more', 'togetterShowMore')),
+      actionsBlock(buttonElement('Show more', 'showMoreTogetterResults')),
       inputBlock(this.togetterInputBlockElement('page_path', '/'), 'page_path', 'Page path'),
       actionsBlock(buttonElement('Cancel', 'togetterCancelPageCreation'), buttonElement('Create page', 'togetterCreatePage', 'primary')),
     ];
@@ -45,7 +45,7 @@ module.exports = () => {
     const options = [];
     // temporary code
     for (let i = 0; i < 10; i++) {
-      const option = checkboxesElementOption('*username*  12:00PM', 'sample slack messages ... :star:', `selected-${i}`);
+      const option = checkboxesElementOption('username  12:00 PM', 'sample slack messages ... :star:\nsample slack messages ... :star:\n', `selected-${i}`);
       options.push(option);
     }
     return options;

+ 36 - 10
src/server/service/slackbot.js

@@ -375,41 +375,67 @@ class SlackBotService extends S2sMessageHandlable {
   }
 
   // Submit action in create Modal
-  async createPageInGrowi(client, payload) {
+  async createPage(client, payload, path, channelId, contentsBody) {
     const Page = this.crowi.model('Page');
     const pathUtils = require('growi-commons').pathUtils;
-    const contentsBody = reshapeContentsBody(payload.view.state.values.contents.contents_input.value);
-
+    const reshapedContentsBody = reshapeContentsBody(contentsBody);
     try {
-      let path = payload.view.state.values.path.path_input.value;
       // 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
       const dummyObjectIdOfUser = new mongoose.Types.ObjectId();
-      const page = await Page.create(path, contentsBody, dummyObjectIdOfUser, {});
+      const page = await Page.create(normalizedPath, reshapedContentsBody, dummyObjectIdOfUser, {});
 
       // Send a message when page creation is complete
       const growiUri = this.crowi.appService.getSiteUrl();
-      const channelId = JSON.parse(payload.view.private_metadata).channelId;
       await client.chat.postEphemeral({
         channel: channelId,
         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) {
       client.chat.postMessage({
         channel: payload.user.id,
         blocks: [
-          markdownSectionBlock(`Cannot create new page to existed path\n *Contents* :memo:\n ${contentsBody}`)],
+          markdownSectionBlock(`Cannot create new page to existed path\n *Contents* :memo:\n ${reshapedContentsBody}`)],
       });
       logger.error('Failed to create page in GROWI.');
       throw err;
     }
   }
 
+  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;
+    await this.createPage(client, payload, path, channelId, contentsBody);
+  }
+
+  async togetterCreatePageInGrowi(client, payload) {
+    const { response_url: responseUrl } = payload;
+    const selectedOptions = payload.state.values.selected_messages.checkboxes_changed.selected_options;
+    const messages = selectedOptions.map((option) => {
+      const header = option.text.text.concat('\n');
+      const body = option.description.text.concat('\n');
+      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('');
+    // dismiss
+    axios.post(responseUrl, {
+      delete_original: true,
+    });
+    await this.createPage(client, payload, path, channelId, contentsBody);
+  }
+
 }
 
 module.exports = SlackBotService;