Procházet zdrojové kódy

GW-6871,72,82 Show message & Cancel action

hakumizuki před 4 roky
rodič
revize
2d703bbb7f

+ 3 - 3
src/server/routes/apiv3/slack-integration.js

@@ -157,12 +157,12 @@ module.exports = (crowi) => {
         await togetterHandler.handleCommand(client, body, args, newLimit);
         await togetterHandler.handleCommand(client, body, args, newLimit);
         break;
         break;
       }
       }
-      case 'togetterCreatePage': {
+      case 'togetter:createPage': {
         await crowi.slackBotService.togetterCreatePageInGrowi(client, payload);
         await crowi.slackBotService.togetterCreatePageInGrowi(client, payload);
         break;
         break;
       }
       }
-      case 'togetterCancelPageCreation': {
-        console.log('Cancel here');
+      case 'togetter:cancel': {
+        await crowi.slackBotService.togetterCancel(client, payload);
         break;
         break;
       }
       }
       default:
       default:

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

@@ -1,9 +1,9 @@
 const {
 const {
-  inputBlock, actionsBlock, buttonElement, checkboxesElementOption,
+  inputBlock, actionsBlock, buttonElement, markdownSectionBlock,
 } = require('@growi/slack');
 } = require('@growi/slack');
-const { fromUnixTime, format } = require('date-fns');
+const { format } = require('date-fns');
 
 
-module.exports = () => {
+module.exports = (crowi) => {
   const BaseSlackCommandHandler = require('./slack-command-handler');
   const BaseSlackCommandHandler = require('./slack-command-handler');
   const handler = new BaseSlackCommandHandler();
   const handler = new BaseSlackCommandHandler();
 
 
@@ -25,34 +25,17 @@ module.exports = () => {
 
 
   handler.togetterMessageBlocks = function(messages, body, args, limit) {
   handler.togetterMessageBlocks = function(messages, body, args, limit) {
     return [
     return [
-      inputBlock(this.togetterCheckboxesElement(messages), 'selected_messages', 'Select massages to use.'),
-      actionsBlock(buttonElement({ text: 'Show more', actionId: 'togetterShowMore', value: JSON.stringify({ body, args, limit }) })),
+      markdownSectionBlock('Select the oldest and latest datetime of the messages to use'),
+      inputBlock(this.plainTextInputElementWithInitialTime('togetter:oldestDateTime'), 'oldest_time', 'Oldest datetime'),
+      inputBlock(this.plainTextInputElementWithInitialTime('togetter:latestDateTime'), 'latest_time', 'Latest datetime'),
       inputBlock(this.togetterInputBlockElement('page_path', '/'), 'page_path', 'Page path'),
       inputBlock(this.togetterInputBlockElement('page_path', '/'), 'page_path', 'Page path'),
       actionsBlock(
       actionsBlock(
-        buttonElement({ text: 'Cancel', actionId: 'togetterCancelPageCreation' }),
-        buttonElement({ text: 'Create page', actionId: 'togetterCreatePage', color: 'primary' }),
+        buttonElement({ text: 'Cancel', actionId: 'togetter:cancel' }),
+        buttonElement({ text: 'Create page', actionId: 'togetter:createPage', style: 'primary' }),
       ),
       ),
     ];
     ];
   };
   };
 
 
-  handler.togetterCheckboxesElement = function(messages) {
-    return {
-      type: 'checkboxes',
-      options: this.togetterCheckboxesElementOptions(messages),
-      action_id: 'checkboxes_changed',
-    };
-  };
-
-  handler.togetterCheckboxesElementOptions = function(messages) {
-    const options = messages
-      .sort((a, b) => { return a.ts - b.ts })
-      .map((message, index) => {
-        const date = fromUnixTime(message.ts);
-        return checkboxesElementOption(`*${message.user}*  ${format(new Date(date), 'yyyy/MM/dd HH:mm:ss')}`, message.text, `selected-${index}`);
-      });
-    return options;
-  };
-
   /**
   /**
    * Plain-text input element
    * Plain-text input element
    * https://api.slack.com/reference/block-kit/block-elements#input
    * https://api.slack.com/reference/block-kit/block-elements#input
@@ -68,5 +51,16 @@ module.exports = () => {
     };
     };
   };
   };
 
 
+  handler.plainTextInputElementWithInitialTime = function(actionId) {
+    const tzDateSec = new Date().getTime();
+    const grwTzoffset = crowi.appService.getTzoffset() * 60 * 1000;
+    const initialDateTime = format(new Date(tzDateSec - grwTzoffset), 'yyyy/MM/dd-HH:mm');
+    return {
+      type: 'plain_text_input',
+      action_id: actionId,
+      initial_value: initialDateTime,
+    };
+  };
+
   return handler;
   return handler;
 };
 };

+ 8 - 1
src/server/service/slackbot.js

@@ -204,7 +204,7 @@ class SlackBotService extends S2sMessageHandlable {
     });
     });
     let path = '';
     let path = '';
     let channelId = '';
     let channelId = '';
-    if (payload.type === 'block_actions' && payload.actions[0].action_id === 'togetterCreatePage') {
+    if (payload.type === 'block_actions' && payload.actions[0].action_id === 'togetter:createPage') {
       path = payload.state.values.page_path.page_path.value;
       path = payload.state.values.page_path.page_path.value;
       channelId = payload.channel.id;
       channelId = payload.channel.id;
     }
     }
@@ -216,6 +216,13 @@ class SlackBotService extends S2sMessageHandlable {
     await this.createPage(client, payload, path, channelId, contentsBody);
     await this.createPage(client, payload, path, channelId, contentsBody);
   }
   }
 
 
+  async togetterCancel(client, payload) {
+    const { response_url: responseUrl } = payload;
+    axios.post(responseUrl, {
+      delete_original: true,
+    });
+  }
+
 }
 }
 
 
 module.exports = SlackBotService;
 module.exports = SlackBotService;

+ 4 - 20
yarn.lock

@@ -2762,26 +2762,10 @@
     p-queue "^6.6.1"
     p-queue "^6.6.1"
     p-retry "^4.0.0"
     p-retry "^4.0.0"
 
 
-"@slack/web-api@^6.1.0":
-  version "6.1.0"
-  resolved "https://registry.yarnpkg.com/@slack/web-api/-/web-api-6.1.0.tgz#27a17f61eb72100d6722ff17f581349c41d19b5f"
-  integrity sha512-9MVHw+rDBaFvkvzm8lDNH/nlkvJCDKRIjFGMdpbyZlVLsm4rcht4qyiL71bqdyLATHXJnWknb/sl0FQGLLobIA==
-  dependencies:
-    "@slack/logger" ">=1.0.0 <3.0.0"
-    "@slack/types" "^1.7.0"
-    "@types/is-stream" "^1.1.0"
-    "@types/node" ">=12.0.0"
-    axios "^0.21.1"
-    eventemitter3 "^3.1.0"
-    form-data "^2.5.0"
-    is-stream "^1.1.0"
-    p-queue "^6.6.1"
-    p-retry "^4.0.0"
-
-"@slack/web-api@^6.2.3":
-  version "6.2.3"
-  resolved "https://registry.yarnpkg.com/@slack/web-api/-/web-api-6.2.3.tgz#063ca32987587d1879520badab485796be621e77"
-  integrity sha512-Qt0JUSuT1RKFYRpMfbP0xKkqWoXGzmEa4N+7H5oB9eH228ABn1sM3LDNW7ZQiLs5jpDuzHAp7dSxAZpb+ZfO/w==
+"@slack/web-api@^6.1.0", "@slack/web-api@^6.2.3":
+  version "6.3.0"
+  resolved "https://registry.yarnpkg.com/@slack/web-api/-/web-api-6.3.0.tgz#a6e592d2bed49666ac9c31cdc6ab0c04443e935e"
+  integrity sha512-EYJ5PuYtSzbrnFCoVE2+JzdM5NTPBlgJYpPGbiVyAved7if4tnbgZpeQT/Vg6E18w5RrFOZScbQaEU78GWmVDA==
   dependencies:
   dependencies:
     "@slack/logger" "^3.0.0"
     "@slack/logger" "^3.0.0"
     "@slack/types" "^2.0.0"
     "@slack/types" "^2.0.0"