Răsfoiți Sursa

typescriptize

Yuki Takei 2 ani în urmă
părinte
comite
30b706a003

+ 2 - 1
apps/app/src/server/service/slack-integration.ts

@@ -15,6 +15,7 @@ import loggerFactory from '~/utils/logger';
 import { EventActionsPermission } from '../interfaces/slack-integration/events';
 import S2sMessage from '../models/vo/s2s-message';
 import { SlackCommandHandlerError } from '../models/vo/slack-command-handler-error';
+import { slackLegacyUtilFactory } from '../util/slack-legacy';
 
 import ConfigManager from './config-manager';
 import { S2sMessagingService } from './s2s-messaging/base';
@@ -230,7 +231,7 @@ export class SlackIntegrationService implements S2sMessageHandlable {
   }
 
   private async postMessageWithLegacyUtil(messageArgs: ChatPostMessageArguments | IncomingWebhookSendArguments): Promise<void> {
-    const slackLegacyUtil = require('../util/slack-legacy')(this.crowi);
+    const slackLegacyUtil = slackLegacyUtilFactory(this.configManager);
 
     try {
       await slackLegacyUtil.postMessage(messageArgs);

+ 32 - 29
apps/app/src/server/util/slack-legacy.ts

@@ -1,17 +1,20 @@
-import { IncomingWebhook } from '@slack/webhook';
+import type { ChatPostMessageArguments } from '@slack/web-api';
 import { WebClient } from '@slack/web-api';
+import { IncomingWebhook, type IncomingWebhookSendArguments } from '@slack/webhook';
 
 import loggerFactory from '~/utils/logger';
 
 const logger = loggerFactory('growi:util:slack-legacy');
 
-module.exports = function(crowi) {
 
-  const { configManager } = crowi;
+interface SlackLegacyUtil {
+  postMessage(messageObj: IncomingWebhookSendArguments | ChatPostMessageArguments): Promise<void>,
+}
 
-  const slackUtilLegacy = {};
+// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
+export const slackLegacyUtilFactory = (configManager: any): SlackLegacyUtil => {
 
-  const postWithIwh = async(messageObj) => {
+  const postWithIwh = async(messageObj: IncomingWebhookSendArguments) => {
     const webhook = new IncomingWebhook(configManager.getConfig('notification', 'slack:incomingWebhookUrl'));
     try {
       await webhook.send(messageObj);
@@ -23,7 +26,7 @@ module.exports = function(crowi) {
     }
   };
 
-  const postWithWebApi = async(messageObj) => {
+  const postWithWebApi = async(messageObj?: ChatPostMessageArguments) => {
     const client = new WebClient(configManager.getConfig('notification', 'slack:token'));
     try {
       await client.chat.postMessage(messageObj);
@@ -35,30 +38,30 @@ module.exports = function(crowi) {
     }
   };
 
-  slackUtilLegacy.postMessage = async(messageObj) => {
-    // when incoming Webhooks is prioritized
-    if (configManager.getConfig('notification', 'slack:isIncomingWebhookPrioritized')) {
-      if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
-        logger.debug('posting message with IncomingWebhook');
-        return postWithIwh(messageObj);
+  return {
+    postMessage: async(messageObj) => {
+      // when incoming Webhooks is prioritized
+      if (configManager.getConfig('notification', 'slack:isIncomingWebhookPrioritized')) {
+        if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
+          logger.debug('posting message with IncomingWebhook');
+          return postWithIwh(messageObj as IncomingWebhookSendArguments);
+        }
+        if (configManager.getConfig('notification', 'slack:token')) {
+          logger.debug('posting message with Web API');
+          return postWithWebApi(messageObj as ChatPostMessageArguments);
+        }
       }
-      if (configManager.getConfig('notification', 'slack:token')) {
-        logger.debug('posting message with Web API');
-        return postWithWebApi(messageObj);
+      // else
+      else {
+        if (configManager.getConfig('notification', 'slack:token')) {
+          logger.debug('posting message with Web API');
+          return postWithWebApi(messageObj as ChatPostMessageArguments);
+        }
+        if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
+          logger.debug('posting message with IncomingWebhook');
+          return postWithIwh(messageObj as IncomingWebhookSendArguments);
+        }
       }
-    }
-    // else
-    else {
-      if (configManager.getConfig('notification', 'slack:token')) {
-        logger.debug('posting message with Web API');
-        return postWithWebApi(messageObj);
-      }
-      if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
-        logger.debug('posting message with IncomingWebhook');
-        return postWithIwh(messageObj);
-      }
-    }
+    },
   };
-
-  return slackUtilLegacy;
 };