itizawa 4 лет назад
Родитель
Сommit
ae03517c14

+ 6 - 0
packages/slackbot-proxy/src/controllers/slack.ts

@@ -221,6 +221,12 @@ export class SlackCtrl {
       return;
     }
 
+    // forward to GROWI server
+    if (callBackId === 'select_growi') {
+      await this.selectRequestService.forwardRequest(this.relationRepository, installation, payload);
+      return;
+    }
+
     /*
     * forward to GROWI server
     */

+ 39 - 2
packages/slackbot-proxy/src/services/SelectRequestService.ts

@@ -1,8 +1,13 @@
 import { Service } from '@tsed/di';
+import axios from 'axios';
+
 import { WebClient, LogLevel } from '@slack/web-api';
 import { GrowiCommand } from '@growi/slack';
 import { AuthorizeResult } from '@slack/oauth';
+
 import { GrowiCommandProcessor } from '~/interfaces/slack-to-growi/growi-command-processor';
+import { RelationRepository } from '~/repositories/relation';
+import { Installation } from '~/entities/installation';
 
 
 const isProduction = process.env.NODE_ENV === 'production';
@@ -18,7 +23,7 @@ export class SelectRequestService implements GrowiCommandProcessor {
       trigger_id: body.trigger_id,
       view: {
         type: 'modal',
-        callback_id: 'register',
+        callback_id: 'select_growi',
         title: {
           type: 'plain_text',
           text: 'Slect Growi url',
@@ -31,7 +36,7 @@ export class SelectRequestService implements GrowiCommandProcessor {
           type: 'plain_text',
           text: 'Close',
         },
-        private_metadata: JSON.stringify({ channel: body.channel_name }),
+        private_metadata: JSON.stringify({ channel: body.channel_name, growiCommand }),
 
         blocks: [
           {
@@ -60,4 +65,36 @@ export class SelectRequestService implements GrowiCommandProcessor {
     });
   }
 
+  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
+  async forwardRequest(relationRepository:RelationRepository, installation:Installation | undefined, payload:any):Promise<void> {
+    const { state, private_metadata: privateMetadata } = payload?.view;
+    const { value: growiUri } = state?.values?.select_growi?.growi_app?.selected_option;
+
+    const parsedPrivateMetadata = JSON.parse(privateMetadata);
+    const { growiCommand } = parsedPrivateMetadata;
+
+    if (growiUri == null || growiCommand == null) {
+      throw new Error('growiUri and growiCommand are required.');
+    }
+
+    const relation = await relationRepository.findOne({ installation, growiUri });
+
+    if (relation == null) {
+      throw new Error('No relation found.');
+    }
+
+    /*
+     * forward to GROWI server
+     */
+    // generate API URL
+    const url = new URL('/_api/v3/slack-integration/proxied/commands', relation.growiUri);
+    return axios.post(url.toString(), {
+      growiCommand,
+    }, {
+      headers: {
+        'x-growi-ptog-tokens': relation.tokenPtoG,
+      },
+    });
+  }
+
 }