Yuki Takei 4 лет назад
Родитель
Сommit
276dbb1d98

+ 31 - 5
packages/app/src/server/service/slack-integration.ts

@@ -1,6 +1,7 @@
 import mongoose from 'mongoose';
 
-import { WebClient } from '@slack/web-api';
+import { IncomingWebhookSendArguments } from '@slack/webhook';
+import { ChatPostMessageArguments, ChatPostMessageResponse, WebClient } from '@slack/web-api';
 import { generateWebClient, markdownSectionBlock } from '@growi/slack';
 
 
@@ -79,13 +80,20 @@ export class SlackIntegrationService implements S2sMessageHandlable {
   }
 
   get isSlackConfigured(): boolean {
+    return this.isSlackbotConfigured || this.isSlackLegacyConfigured;
+  }
+
+  get isSlackbotConfigured(): boolean {
+    const hasSlackbotType = !!this.configManager.getConfig('crowi', 'slackbot:currentBotType');
+    return hasSlackbotType;
+  }
+
+  get isSlackLegacyConfigured(): boolean {
     // for legacy util
     const hasSlackToken = !!this.configManager.getConfig('notification', 'slack:token');
     const hasSlackIwhUrl = !!this.configManager.getConfig('notification', 'slack:incomingWebhookUrl');
-    // for slackbot
-    const hasSlackbotType = !!this.configManager.getConfig('crowi', 'slackbot:currentBotType');
 
-    return hasSlackToken || hasSlackIwhUrl || hasSlackbotType;
+    return hasSlackToken || hasSlackIwhUrl;
   }
 
   /**
@@ -148,18 +156,36 @@ export class SlackIntegrationService implements S2sMessageHandlable {
   }
 
   async postMessage(messageArgs: ChatPostMessageArguments): Promise<void> {
+    // use legacy slack configuration
+    if (this.isSlackLegacyConfigured && !this.isSlackbotConfigured) {
+      return this.postMessageWithLegacyUtil(messageArgs);
+    }
+
     // TODO: determine target slack workspace
     const tokenPtoG = '...';
     const client = await this.generateClient(tokenPtoG);
 
     try {
       await client.chat.postMessage(messageArgs);
-  }
+    }
     catch (error) {
       logger.debug('Post error', error);
       logger.debug('Sent data to slack is:', messageArgs);
       throw error;
+    }
   }
+
+  private async postMessageWithLegacyUtil(messageArgs: ChatPostMessageArguments | IncomingWebhookSendArguments): Promise<void> {
+    const slackLegacyUtil = require('../util/slack-legacy')(this.crowi);
+
+    try {
+      await slackLegacyUtil.postMessage(messageArgs);
+    }
+    catch (error) {
+      logger.debug('Post error', error);
+      logger.debug('Sent data to slack is:', messageArgs);
+      throw error;
+    }
   }
 
   /**

+ 15 - 43
packages/app/src/server/util/slack-legacy.js

@@ -1,20 +1,15 @@
-const debug = require('debug')('growi:util:slack');
-// const slack = require('./slack');
+import { IncomingWebhook } from '@slack/webhook';
+import { WebClient } from '@slack/web-api';
 
-/**
- * slack
- */
+import loggerFactory from '~/utils/logger';
 
-/* eslint-disable no-use-before-define */
+const logger = loggerFactory('growi:util:slack-legacy');
 
 module.exports = function(crowi) {
-  const { IncomingWebhook } = require('@slack/webhook');
-  const { WebClient } = require('@slack/web-api');
 
   const { configManager } = crowi;
-  const slack = crowi.getSlack();
 
-  const slackLegacy = {};
+  const slackUtilLegacy = {};
 
   const postWithIwh = async(messageObj) => {
     const webhook = new IncomingWebhook(configManager.getConfig('notification', 'slack:incomingWebhookUrl'));
@@ -22,71 +17,48 @@ module.exports = function(crowi) {
       await webhook.send(messageObj);
     }
     catch (error) {
-      debug('Post error', error);
-      debug('Sent data to slack is:', messageObj);
+      logger.debug('Post error', error);
+      logger.debug('Sent data to slack is:', messageObj);
       throw error;
     }
   };
 
   const postWithWebApi = async(messageObj) => {
     const client = new WebClient(configManager.getConfig('notification', 'slack:token'));
-    // stringify attachments
-    if (messageObj.attachments != null) {
-      messageObj.attachments = JSON.stringify(messageObj.attachments);
-    }
     try {
       await client.chat.postMessage(messageObj);
     }
     catch (error) {
-      debug('Post error', error);
-      debug('Sent data to slack is:', messageObj);
+      logger.debug('Post error', error);
+      logger.debug('Sent data to slack is:', messageObj);
       throw error;
     }
   };
 
-  // slackLegacy.post = function (channel, message, opts) {
-  slackLegacy.postPage = (page, user, channel, updateType, previousRevision) => {
-    const messageObj = slack.prepareSlackMessageForPage(page, user, channel, updateType, previousRevision);
-
-    return slackPost(messageObj);
-  };
-
-  slackLegacy.postComment = (comment, user, channel, path) => {
-    const messageObj = slack.prepareSlackMessageForComment(comment, user, channel, path);
-
-    return slackPost(messageObj);
-  };
-
-  slackLegacy.sendGlobalNotification = async(messageBody, attachmentBody, slackChannel) => {
-    const messageObj = await slack.prepareSlackMessageForGlobalNotification(messageBody, attachmentBody, slackChannel);
-
-    return slackPost(messageObj);
-  };
-
-  const slackPost = (messageObj) => {
+  slackUtilLegacy.postMessage = async(messageObj) => {
     // when incoming Webhooks is prioritized
     if (configManager.getConfig('notification', 'slack:isIncomingWebhookPrioritized')) {
       if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
-        debug('posting message with IncomingWebhook');
+        logger.debug('posting message with IncomingWebhook');
         return postWithIwh(messageObj);
       }
       if (configManager.getConfig('notification', 'slack:token')) {
-        debug('posting message with Web API');
+        logger.debug('posting message with Web API');
         return postWithWebApi(messageObj);
       }
     }
     // else
     else {
       if (configManager.getConfig('notification', 'slack:token')) {
-        debug('posting message with Web API');
+        logger.debug('posting message with Web API');
         return postWithWebApi(messageObj);
       }
       if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
-        debug('posting message with IncomingWebhook');
+        logger.debug('posting message with IncomingWebhook');
         return postWithIwh(messageObj);
       }
     }
   };
 
-  return slackLegacy;
+  return slackUtilLegacy;
 };