zahmis 4 лет назад
Родитель
Сommit
5a44f4cd5f

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

@@ -330,24 +330,15 @@ export class SlackCtrl {
       });
     }
 
-
-    const allowedRelations:Relation[] = [];
-    const disallowedGrowiUrls: Set<string> = new Set();
-    let notAllowedCommandName!:string;
     const actionId:string = payload?.actions?.[0].action_id;
 
     await Promise.all(relations.map(async(relation) => {
-      const permission = await this.relationsService.checkPermissionForInteractions(relation, channelName, callbackId, actionId);
-      const { isPermittedForInteractions, commandName } = permission;
-
-      if (!isPermittedForInteractions) {
-        disallowedGrowiUrls.add(relation.growiUri);
-        notAllowedCommandName = commandName;
-      }
-
-      allowedRelations.push(relation);
+      await this.relationsService.checkPermissionForInteractions(relation, channelName, callbackId, actionId);
     }));
 
+    const disallowedGrowiUrls = this.relationsService.getDisallowedGrowiUrls();
+    const notAllowedCommandName = this.relationsService.getNotAllowedCommandName();
+
     if (relations.length === disallowedGrowiUrls.size) {
       // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
       const client = generateWebClient(authorizeResult.botToken!);
@@ -377,6 +368,7 @@ export class SlackCtrl {
      * forward to GROWI server
      */
 
+    const allowedRelations = this.relationsService.getAllowedRelations();
     allowedRelations.map(async(relation) => {
       try {
         // generate API URL

+ 30 - 13
packages/slackbot-proxy/src/services/RelationsService.ts

@@ -107,18 +107,37 @@ export class RelationsService {
     return permission;
   }
 
+  allowedRelations:Relation[];
+
+  disallowedGrowiUrls: Set<string>;
+
+  notAllowedCommandName:string
+
+  getAllowedRelations():Relation[] {
+    return this.allowedRelations;
+  }
+
+  getDisallowedGrowiUrls():Set<string> {
+    return this.disallowedGrowiUrls;
+  }
+
+  getNotAllowedCommandName():string {
+    return this.notAllowedCommandName;
+  }
 
   async checkPermissionForInteractions(
       // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
       relation:Relation, channelName:string, callbackId:string, actionId:string,
-  ):Promise<{isPermittedForInteractions:boolean, commandName:string}> {
+  ):Promise<void> {
 
-    let isPermittedForInteractions = false;
-    let permissionForInteractions:boolean|string[];
+    // initialize params
+    this.allowedRelations = [];
+    this.disallowedGrowiUrls = new Set();
+    this.notAllowedCommandName = '';
 
+    let permissionForInteractions:boolean|string[];
     const singleUse = Object.keys(relation.permissionsForSingleUseCommands);
     const broadCastUse = Object.keys(relation.permissionsForBroadcastUseCommands);
-    let commandName!:string;
 
     [...singleUse, ...broadCastUse].forEach(async(tempCommandName) => {
 
@@ -129,27 +148,25 @@ export class RelationsService {
         return;
       }
 
-      commandName = tempCommandName;
-
       // case: singleUse
       permissionForInteractions = relation.permissionsForSingleUseCommands[tempCommandName];
       // case: broadcastUse
       if (permissionForInteractions == null) {
         permissionForInteractions = relation.permissionsForBroadcastUseCommands[tempCommandName];
       }
+
       if (permissionForInteractions === true) {
-        isPermittedForInteractions = true;
-        return;
+        return this.allowedRelations.push(relation);
       }
+
       // check permission at channel level
       if (Array.isArray(permissionForInteractions) && permissionForInteractions.includes(channelName)) {
-        isPermittedForInteractions = true;
-        return;
+        return this.allowedRelations.push(relation);
       }
-    });
 
-
-    return { isPermittedForInteractions, commandName };
+      this.disallowedGrowiUrls.add(relation.growiUri);
+      this.notAllowedCommandName = tempCommandName;
+    });
   }
 
 }