Explorar o código

Merge branch 'feat/6450' into feat/6450-6845-check-expire

itizawa %!s(int64=4) %!d(string=hai) anos
pai
achega
25666c2b1d

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

@@ -1,5 +1,5 @@
 import {
 import {
-  Controller, Get, Post, Inject, Req, Res, UseBefore, PathParams,
+  Controller, Get, Post, Inject, Req, Res, UseBefore, PathParams, Put,
 } from '@tsed/common';
 } from '@tsed/common';
 import axios from 'axios';
 import axios from 'axios';
 import createError from 'http-errors';
 import createError from 'http-errors';
@@ -92,6 +92,23 @@ export class GrowiToSlackCtrl {
     return res.send({ connectionStatuses });
     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];
+    const relation = await this.relationRepository.update({ tokenGtoP }, { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse });
+
+    return res.send({ relation });
+  }
+
   @Post('/relation-test')
   @Post('/relation-test')
   @UseBefore(verifyGrowiToSlackRequest)
   @UseBefore(verifyGrowiToSlackRequest)
   async postRelation(@Req() req: GrowiReq, @Res() res: Res): Promise<void|string|Res|WebAPICallResult> {
   async postRelation(@Req() req: GrowiReq, @Res() res: Res): Promise<void|string|Res|WebAPICallResult> {

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

@@ -1,6 +1,6 @@
 const mongoose = require('mongoose');
 const mongoose = require('mongoose');
 const express = require('express');
 const express = require('express');
-const { body, query } = require('express-validator');
+const { body, query, param } = require('express-validator');
 const axios = require('axios');
 const axios = require('axios');
 const urljoin = require('url-join');
 const urljoin = require('url-join');
 const loggerFactory = require('@alias/logger');
 const loggerFactory = require('@alias/logger');
@@ -59,6 +59,11 @@ module.exports = (crowi) => {
       body('proxyUri').if(value => value !== '').trim().matches(/^(https?:\/\/)/)
       body('proxyUri').if(value => value !== '').trim().matches(/^(https?:\/\/)/)
         .isURL({ require_tld: false }),
         .isURL({ require_tld: false }),
     ],
     ],
+    updateSupportedCommands: [
+      body('supportedCommandsForSingleUse').toArray(),
+      body('supportedCommandsForBroadcastUse').toArray(),
+      param('id').isMongoId().withMessage('id is required'),
+    ],
     RelationTest: [
     RelationTest: [
       body('slackAppIntegrationId').isMongoId(),
       body('slackAppIntegrationId').isMongoId(),
       body('channel').trim().isString(),
       body('channel').trim().isString(),
@@ -105,7 +110,7 @@ module.exports = (crowi) => {
     return result.data;
     return result.data;
   }
   }
 
 
-  async function postRelationTest(token, supportedCommandsForBroadcastUse, supportedCommandsForSingleUse) {
+  async function requestToProxyServer(token, method, endpoint, body) {
     const proxyUri = crowi.configManager.getConfig('crowi', 'slackbot:proxyServerUri');
     const proxyUri = crowi.configManager.getConfig('crowi', 'slackbot:proxyServerUri');
     if (proxyUri == null) {
     if (proxyUri == null) {
       throw new Error('Proxy URL is not registered');
       throw new Error('Proxy URL is not registered');
@@ -115,7 +120,7 @@ module.exports = (crowi) => {
       'x-growi-gtop-tokens': token,
       'x-growi-gtop-tokens': token,
     };
     };
 
 
-    const result = await axios.post(urljoin(proxyUri, '/g2s/relation-test'), { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse }, { headers });
+    const result = await axios[method](urljoin(proxyUri, endpoint), body, { headers });
 
 
     return result.data;
     return result.data;
   }
   }
@@ -491,6 +496,49 @@ module.exports = (crowi) => {
 
 
   });
   });
 
 
+  /**
+   * @swagger
+   *
+   *    /slack-integration-settings/:id/supported-commands:
+   *      put:
+   *        tags: [SlackIntegration]
+   *        operationId: putSupportedCommands
+   *        summary: /slack-integration-settings/:id/supported-commands
+   *        description: update supported commands
+   *        responses:
+   *          200:
+   *            description: Succeeded to update supported commands
+   */
+  router.put('/:id/supported-commands', loginRequiredStrictly, adminRequired, csrf, validator.updateSupportedCommands, apiV3FormValidator, async(req, res) => {
+    const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = req.body;
+    const { id } = req.params;
+
+    try {
+      const slackAppIntegration = await SlackAppIntegration.findByIdAndUpdate(
+        id,
+        { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse },
+        { new: true },
+      );
+
+      await requestToProxyServer(
+        slackAppIntegration.tokenGtoP,
+        'put',
+        '/g2s/supported-commands',
+        {
+          supportedCommandsForBroadcastUse: slackAppIntegration.supportedCommandsForBroadcastUse,
+          supportedCommandsForSingleUse: slackAppIntegration.supportedCommandsForSingleUse,
+        },
+      );
+
+      return res.apiv3({ slackAppIntegration });
+    }
+    catch (error) {
+      const msg = 'Error occured in updating Custom bot setting';
+      logger.error('Error', error);
+      return res.apiv3Err(new ErrorV3(msg, 'update-CustomBotSetting-failed'), 500);
+    }
+  });
+
   /**
   /**
    * @swagger
    * @swagger
    *
    *
@@ -526,9 +574,17 @@ module.exports = (crowi) => {
         const msg = 'Could not find SlackAppIntegration by id';
         const msg = 'Could not find SlackAppIntegration by id';
         return res.apiv3Err(new ErrorV3(msg, 'find-slackAppIntegration-failed'), 400);
         return res.apiv3Err(new ErrorV3(msg, 'find-slackAppIntegration-failed'), 400);
       }
       }
-      const result = await postRelationTest(
-        slackAppIntegration.tokenGtoP, slackAppIntegration.supportedCommandsForBroadcastUse, slackAppIntegration.supportedCommandsForSingleUse,
+
+      const result = await requestToProxyServer(
+        slackAppIntegration.tokenGtoP,
+        'post',
+        '/g2s/relation-test',
+        {
+          supportedCommandsForBroadcastUse: slackAppIntegration.supportedCommandsForBroadcastUse,
+          supportedCommandsForSingleUse: slackAppIntegration.supportedCommandsForSingleUse,
+        },
       );
       );
+
       slackBotToken = result.slackBotToken;
       slackBotToken = result.slackBotToken;
       if (slackBotToken == null) {
       if (slackBotToken == null) {
         const msg = 'Could not find slackBotToken by relation';
         const msg = 'Could not find slackBotToken by relation';