Yuki Takei 5 лет назад
Родитель
Сommit
16bfe014d5

+ 3 - 0
packages/slackbot-proxy/src/Server.ts

@@ -26,6 +26,9 @@ const connectionOptions: ConnectionOptions = {
       `${rootDir}/middlewares/*.ts`,
       `${rootDir}/middlewares/*.ts`,
     ],
     ],
   },
   },
+  componentsScan: [
+    `${rootDir}/services/*.ts`,
+  ],
   typeorm: [
   typeorm: [
     {
     {
       ...connectionOptions,
       ...connectionOptions,

+ 5 - 22
packages/slackbot-proxy/src/controllers/slack.ts

@@ -2,35 +2,18 @@ import {
   BodyParams, Controller, Get, Post, Req, Res,
   BodyParams, Controller, Get, Post, Req, Res,
 } from '@tsed/common';
 } from '@tsed/common';
 
 
-import { InstallProvider } from '@slack/oauth';
+import { InstallerService } from '~/services/InstallerService';
 
 
 @Controller('/slack')
 @Controller('/slack')
 export class SlackCtrl {
 export class SlackCtrl {
 
 
-  installer: InstallProvider;
-
-  constructor() {
-    const clientId = process.env.SLACK_CLIENT_ID;
-    const clientSecret = process.env.SLACK_CLIENT_SECRET;
-    const stateSecret = process.env.SLACK_INSTALLPROVIDER_STATE_SECRET;
-
-    if (clientId === undefined) {
-      throw new Error('The environment variable \'SLACK_CLIENT_ID\' must be defined.');
-    }
-    if (clientSecret === undefined) {
-      throw new Error('The environment variable \'SLACK_CLIENT_SECRET\' must be defined.');
-    }
-
-    this.installer = new InstallProvider({
-      clientId,
-      clientSecret,
-      stateSecret,
-    });
+  // eslint-disable-next-line no-useless-constructor
+  constructor(private readonly installerService: InstallerService) {
   }
   }
 
 
   @Get('/install')
   @Get('/install')
   async install(): Promise<string> {
   async install(): Promise<string> {
-    const url = await this.installer.generateInstallUrl({
+    const url = await this.installerService.installer.generateInstallUrl({
       // Add the scopes your app needs
       // Add the scopes your app needs
       scopes: [
       scopes: [
         'channels:history',
         'channels:history',
@@ -67,7 +50,7 @@ export class SlackCtrl {
       throw new Error('illegal state');
       throw new Error('illegal state');
     }
     }
 
 
-    this.installer.handleCallback(req, res);
+    this.installerService.installer.handleCallback(req, res);
 
 
     // TODO: https://youtrack.weseek.co.jp/issue/GW-5543
     // TODO: https://youtrack.weseek.co.jp/issue/GW-5543
     // this.installer.handleCallback(req, res, {
     // this.installer.handleCallback(req, res, {

+ 49 - 0
packages/slackbot-proxy/src/services/InstallerService.ts

@@ -0,0 +1,49 @@
+import {
+  Installation, InstallationQuery, InstallationStore, InstallProvider,
+} from '@slack/oauth';
+import { Service } from '@tsed/di';
+
+
+const installationStore: InstallationStore = {
+  storeInstallation: async(installation: Installation<'v1' | 'v2', boolean>) => {
+  },
+  fetchInstallation: async(installQuery: InstallationQuery<boolean>) => {
+    const installation: Installation<'v1' | 'v2', boolean> = {
+      team: undefined,
+      enterprise: undefined,
+      user: {
+        id: '',
+        token: undefined,
+        scopes: undefined,
+      },
+    };
+    return installation;
+  },
+};
+
+@Service()
+export class InstallerService {
+
+  installer: InstallProvider;
+
+  $onInit(): Promise<any> | void {
+    const clientId = process.env.SLACK_CLIENT_ID;
+    const clientSecret = process.env.SLACK_CLIENT_SECRET;
+    const stateSecret = process.env.SLACK_INSTALLPROVIDER_STATE_SECRET;
+
+    if (clientId === undefined) {
+      throw new Error('The environment variable \'SLACK_CLIENT_ID\' must be defined.');
+    }
+    if (clientSecret === undefined) {
+      throw new Error('The environment variable \'SLACK_CLIENT_SECRET\' must be defined.');
+    }
+
+    this.installer = new InstallProvider({
+      clientId,
+      clientSecret,
+      stateSecret,
+      // installationStore,
+    });
+  }
+
+}