Explorar o código

implement update commands

itizawa %!s(int64=4) %!d(string=hai) anos
pai
achega
54c2456f0e

+ 20 - 1
packages/slackbot-proxy/src/controllers/growi-to-slack.ts

@@ -1,5 +1,5 @@
 import {
-  Controller, Get, Post, Inject, Req, Res, UseBefore, PathParams,
+  Controller, Get, Post, Inject, Req, Res, UseBefore, PathParams, Put,
 } from '@tsed/common';
 import axios from 'axios';
 import createError from 'http-errors';
@@ -92,6 +92,25 @@ export class GrowiToSlackCtrl {
     return res.send({ connectionStatuses });
   }
 
+  @Put('/supported-commands')
+  @UseBefore(verifyGrowiToSlackRequest)
+  async putSupportedCommands(@Req() req: GrowiReq, @Res() res: Res): Promise<void|string|Res|WebAPICallResult> {
+    // asserted (tokenGtoPs.length > 0) by verifyGrowiToSlackRequest
+    const { tokenGtoPs } = req;
+    const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = req.body;
+
+    if (tokenGtoPs.length !== 1) {
+      throw createError(400, 'installation is invalid');
+    }
+
+    const tokenGtoP = tokenGtoPs[0];
+
+    // retrieve Relation with Installation
+    const relation = await this.relationRepository.update({ tokenGtoP }, { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse });
+
+    return res.send({ relation });
+  }
+
   @Post('/relation-test')
   @UseBefore(verifyGrowiToSlackRequest)
   async postRelation(@Req() req: GrowiReq, @Res() res: Res): Promise<void|string|Res|WebAPICallResult> {

+ 24 - 0
src/server/routes/apiv3/slack-integration-settings.js

@@ -110,6 +110,25 @@ module.exports = (crowi) => {
     return result.data;
   }
 
+  async function putSupportedCommands(token, supportedCommandsForBroadcastUse, supportedCommandsForSingleUse) {
+    const proxyUri = crowi.configManager.getConfig('crowi', 'slackbot:proxyServerUri');
+    if (proxyUri == null) {
+      throw new Error('Proxy URL is not registered');
+    }
+
+    const headers = {
+      'x-growi-gtop-tokens': token,
+    };
+
+    const result = await axios.put(
+      urljoin(proxyUri, '/g2s/supported-commands'),
+      { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse },
+      { headers },
+    );
+
+    return result.data;
+  }
+
   async function postRelationTest(token, supportedCommandsForBroadcastUse, supportedCommandsForSingleUse) {
     const proxyUri = crowi.configManager.getConfig('crowi', 'slackbot:proxyServerUri');
     if (proxyUri == null) {
@@ -515,6 +534,11 @@ module.exports = (crowi) => {
 
     try {
       const slackAppIntegration = await SlackAppIntegration.findByIdAndUpdate(id, { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse });
+
+      await putSupportedCommands(
+        slackAppIntegration.tokenGtoP, slackAppIntegration.supportedCommandsForBroadcastUse, slackAppIntegration.supportedCommandsForSingleUse,
+      );
+
       return res.apiv3({ slackAppIntegration });
     }
     catch (error) {