zahmis 4 年 前
コミット
c398e9daa1

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

@@ -4,10 +4,10 @@ import {
 
 import axios from 'axios';
 
-import { WebAPICallResult, ChatPostEphemeralResponse } from '@slack/web-api';
+import { WebAPICallResult } from '@slack/web-api';
 
 import {
-  markdownSectionBlock, GrowiCommand, parseSlashCommand, postEphemeralErrors, verifySlackRequest, generateWebClient,
+  markdownSectionBlock, GrowiCommand, parseSlashCommand, postEphemeralErrors, verifySlackRequest,
 } from '@growi/slack';
 
 // import { Relation } from '~/entities/relation';
@@ -30,22 +30,6 @@ import loggerFactory from '~/utils/logger';
 
 
 const logger = loggerFactory('slackbot-proxy:controllers:slack');
-
-
-const postNotPermissionMessage = (relations:RelationMock[], commandName:string, body:any):Promise<ChatPostEphemeralResponse> => {
-  const botToken = relations[0].installation?.data.bot?.token;
-  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-  const client = generateWebClient(botToken!);
-
-  return client.chat.postEphemeral({
-    text: 'Error occured.',
-    channel: body.channel_id,
-    user: body.user_id,
-    blocks: [
-      markdownSectionBlock(`It is not allowed to run *'${commandName}'* command to this GROWI.`),
-    ],
-  });
-};
 @Controller('/slack')
 export class SlackCtrl {
 
@@ -209,7 +193,7 @@ export class SlackCtrl {
         return relationsForBroadcastUse.push(relation);
       }
 
-      postNotPermissionMessage(relations, growiCommand.growiCommandType, body);
+      this.relationsService.postNotPermissionMessage(relations, growiCommand.growiCommandType, body);
     }));
 
     /*
@@ -299,43 +283,10 @@ export class SlackCtrl {
       });
     }
 
-    let isPermitted = false;
-    let permission:boolean|string[];
-    await Promise.all(relations.map(async(relation) => {
-      const singleUse = Object.keys(relation.supportedCommandsForSingleUse);
-      const broadCastUse = Object.keys(relation.supportedCommandsForBroadcastUse);
-
-      [...singleUse, ...broadCastUse].forEach(async(commandName) => {
-        permission = relation.supportedCommandsForSingleUse[commandName];
-
-        if (permission == null) {
-          permission = relation.supportedCommandsForBroadcastUse[commandName];
-        }
 
-        // permission check
-        if (permission === true) {
-          isPermitted = true;
-          return;
-        }
-
-        if (Array.isArray(permission)) {
-          isPermitted = permission.includes(channelName);
-          return;
-        }
-
-        // ex. search OR search:handlerName
-        const commandRegExp = new RegExp(`(^${commandName}$)|(^${commandName}:\\w+)`);
-
-        // skip this forEach loop if the requested command is not in permissionsForBroadcastUseCommands and permissionsForSingleUseCommandskey
-        if (!commandRegExp.test(actionId) && !commandRegExp.test(callbackId)) {
-          return;
-        }
-
-        if (!isPermitted) {
-          postNotPermissionMessage(relations, commandName, body);
-        }
-      });
+    await Promise.all(relations.map(async(relation) => {
 
+      await this.relationsService.checkPermissionForInteractions(relation, channelName, callbackId, actionId, body, relations);
 
       /*
        * forward to GROWI server

+ 69 - 0
packages/slackbot-proxy/src/services/RelationsService.ts

@@ -3,6 +3,8 @@ import axios from 'axios';
 import { addHours } from 'date-fns';
 
 // import { Relation } from '~/entities/relation';
+import { markdownSectionBlock, generateWebClient } from '@growi/slack';
+import { ChatPostEphemeralResponse } from '@slack/web-api';
 import { RelationMock } from '~/entities/relation-mock';
 // import { RelationRepository } from '~/repositories/relation';
 import { RelationMockRepository } from '~/repositories/relation-mock';
@@ -118,4 +120,71 @@ export class RelationsService {
     return permission;
   }
 
+  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
+  postNotPermissionMessage(relations:RelationMock[], commandName:string, body:any):Promise<ChatPostEphemeralResponse> {
+    const botToken = relations[0].installation?.data.bot?.token;
+    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+    const client = generateWebClient(botToken!);
+
+    return client.chat.postEphemeral({
+      text: 'Error occured.',
+      channel: body.channel_id,
+      user: body.user_id,
+      blocks: [
+        markdownSectionBlock(`It is not allowed to run *'${commandName}'* command to this GROWI.`),
+      ],
+    });
+  }
+
+  isPermitted = false;
+
+  permission:boolean|string[];
+
+  async checkPermissionForInteractions(
+      // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
+      relation:RelationMock, channelName:string, callbackId:string, actionId:string, body:any, relations:RelationMock[],
+  ):Promise<void> {
+
+    const baseDate = new Date();
+    const syncedRelation = await this.syncRelation(relation, baseDate);
+
+    if (syncedRelation == null) {
+      this.isPermitted = false;
+      return;
+    }
+    const singleUse = Object.keys(relation.supportedCommandsForSingleUse);
+    const broadCastUse = Object.keys(relation.supportedCommandsForBroadcastUse);
+
+    [...singleUse, ...broadCastUse].forEach(async(commandName) => {
+      this.permission = relation.supportedCommandsForSingleUse[commandName];
+
+      if (this.permission == null) {
+        this.permission = relation.supportedCommandsForBroadcastUse[commandName];
+      }
+
+      // permission check
+      if (this.permission === true) {
+        this.isPermitted = true;
+        return;
+      }
+
+      if (Array.isArray(this.permission)) {
+        this.isPermitted = this.permission.includes(channelName);
+        return;
+      }
+
+      // ex. search OR search:handlerName
+      const commandRegExp = new RegExp(`(^${commandName}$)|(^${commandName}:\\w+)`);
+
+      // skip this forEach loop if the requested command is not in permissionsForBroadcastUseCommands and permissionsForSingleUseCommandskey
+      if (!commandRegExp.test(actionId) && !commandRegExp.test(callbackId)) {
+        return;
+      }
+
+      if (!this.isPermitted) {
+        this.postNotPermissionMessage(relations, commandName, body);
+      }
+    });
+  }
+
 }